Add opendir files to the repository

This commit is contained in:
guha%netscape.com 1998-12-09 23:08:59 +00:00
Родитель a469d9466b
Коммит bb564c7158
12 изменённых файлов: 1866 добавлений и 0 удалений

96
rdf/opendir/hash.c Normal file
Просмотреть файл

@ -0,0 +1,96 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
/* This file holds a temporary implementation of hash tables. It will
be replaced with STL or NSPR
*/
#include "rdf-int.h"
typedef struct _HashEntryStruct {
struct _HashEntryStruct* next; /* hash chain linkage */
char* key;
void* value;
} HashEntryStruct;
typedef HashEntryStruct* HashEntry;
typedef struct _HashTableStruct {
int size;
HashEntry* buckets; /* vector of hash buckets */
} HashTableStruct;
typedef HashTableStruct* HashTable;
int
hashKey (HashTable ht, char* key) {
size_t len = strlen(key);
int sum = 0;
size_t n = 0;
for (n = 0; n < len; n++) sum = sum + (int)key[n];
return sum & ht->size;
}
HashTable
NewHashTable(int size) {
HashTable ht = (HashTable)getMem(sizeof(HashTableStruct));
ht->size = size;
ht->buckets = (HashEntry*)getMem(sizeof(HashEntry) * size);
return ht;
}
void*
HashLookup(HashTable ht, char* key) {
int offset = hashKey(ht, key);
HashEntry he = ht->buckets[offset];
while (he) {
if (strcmp(he->key, key) == 0) return he->value;
he = he->next;
}
return NULL;
}
void
HashAdd (HashTable ht, char* key, void* value) {
int offset = hashKey(ht, key);
HashEntry he = ht->buckets[offset];
HashEntry prev = he;
while (he) {
if (strcmp(he->key, key) == 0) {
if (value == he->value) {
return;
} else {
he->value = value;
return;
}
}
prev = he;
he = he->next;
}
he = (HashEntry) getMem(sizeof(HashEntryStruct));
he->value = value;
he->key = key;
if (prev) {
prev->next = he;
} else {
ht->buckets[offset] = he;
}
}

26
rdf/opendir/makefile.win Normal file
Просмотреть файл

@ -0,0 +1,26 @@
#
# Microsoft Visual C makefile for WAI sample
INCLUDES = -I..\..\..\include -I..\..\include
NSLIB = ..\..\..\lib
WAILIB= c:\netscape\suitespot\wai\lib
LIBS = $(WAILIB)\ONEiiop10.lib WSOCK32.lib
CPPFLAGS=$(INCLUDES) -DXP_WIN32 -DWIN32 -DDEBUG /MD
OPTIMIZER=-Zi -Od -DDEBUG
OBJS = opendir.o \
rdf.o \
rdfparse.o \
remstore.o \
hash.o \
$(NULL)
all: opendir
opendir: opendir.c rdf.c rdfparse.c remstore.c hash.c
$(CC) $(CPPFLAGS) $(LIBS) $? -o $@
clean:
del *.obj

112
rdf/opendir/opendir.c Normal file
Просмотреть файл

@ -0,0 +1,112 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include <sys/types.h>
#include "ONEiiop.h"
#include "rdf.h"
#if !defined(WIN32) && !defined(HPUX) && !defined(OSF1)
extern "C" int gethostname(char *name, int namelen);
#endif
long
Run(ServerSession_t obj)
{
char *buffer = "Hello World!!!\n";
char* query = malloc(300);
size_t bufflen = strlen(buffer);
WAIgetRequestInfo(obj, "QUERY", &query);
WAIsetResponseContentLength(obj, bufflen);
WAIStartResponse(obj);
WAIWriteClient(obj, (const unsigned char *)buffer, bufflen);
WAIWriteClient(obj, (const unsigned char *)query, strlen(query));
return 0;
}
//
// Two different ways to build here.
// WinMain if you want a windows app.
// main for Unix and windows console app.
#if defined(WIN32)
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // pointer to command line
int nCmdShow // show state of window
)
{
int argc = 0;
char **argv = NULL;
#else
int main(int argc, char **argv)
{
#endif
char localhost[256];
char *host;
IIOPWebAppService_t obj;
//
// Normally we expect to see a hostname:port as
// our one and only argument. If we are started by
// the OAD we will see an extra argument which should
// be ignored.
// The recommended registration with the OAD should
// specify the hostname:port as an argument to the
// object provider.
//
// So try to ferrit out the hostname:port
#ifndef WIN32
// Easier if we just have a normal main.
if (argc > 1){
if (argc == 1)
host = argv[1];
else if (*argv[1] != '-')
host = argv[1];
#else
// Windows command line drops the program name
// that appears as argv[0].
// GetCommandLine can be used to retrieve the
// whole thing.
if (lpCmdLine && *lpCmdLine){
if (*lpCmdLine != '-'){
char *sep = strchr(lpCmdLine, ' ');
if (sep)
*sep = '\0';
host = lpCmdLine;
}
#endif
}else{
gethostname(localhost, sizeof(localhost));
host = localhost;
}
//
// Pass in argc/argv if we have 'em.
#ifndef WIN32
obj = WAIcreateWebAppService("OpenDir", Run, argc, argv);
#else
obj = WAIcreateWebAppService("OpenDir", Run, 0, 0);
#endif
WAIregisterService(obj, host);
WAIimplIsReady();
RDF_Initialize();
RDF_ReadFile("opendir.rdf");
return 0;
}

126
rdf/opendir/query.c Normal file
Просмотреть файл

@ -0,0 +1,126 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include "rdf-int.h"
#define SEP '|'
typedef struct _QuerySegmentStruct {
RDF_Resource s;
int num;
int inversep;
} QuerySegmentStruct;
typedef QuerySegmentStruct* QuerySegment;
QuerySegment*
parseQuery (char* query, RDF_Resource *root, int *numSegments) {
int len = strlen(query);
int count ;
char* rootURL = getMem(100);
QuerySegment* ans;
*numSegments = 0;
for (count = 0; count < len; count++) {
if (query[count] != SEP) {
if (!*numSegments) rootURL[count] = query[count];
} else
(*numSegments)++;
}
*root = getResource(rootURL, 1);
freeMem(rootURL);
ans = (QuerySegment*)getMem(*numSegments * (sizeof(QuerySegment)));
query = strchr(query, SEP)+1;
for (count = 0; count < *numSegments; count++) {
char *surl = getMem(100);
int num = -1;
char* nquery = strchr(query, SEP) ;
QuerySegment nq = (QuerySegment)getMem(sizeof(QuerySegmentStruct));
if (query[0] == '!') {
memcpy(surl, query+1, (nquery ? nquery-query-1 : strlen(query)-1));
nq->inversep = 1;
} else {
memcpy(surl, query, (nquery ? nquery-query : strlen(query)));
}
if (strchr(surl, '[') && strchr(surl, ']')) {
int n = strchr(surl, '[') - surl;
surl[n] = '\0';
sscanf(&surl[n+1], "%i]", &num);
if (num == -1) num = 4096;
nq->s = getResource(surl, 1);
nq->num = num;
} else {
nq->s = getResource(surl, 1);
nq->num = 4096;
}
freeMem(surl);
ans[count] = nq;
if (nquery) {
query = nquery+1;
} else break;
}
return ans;
}
char**
processRDFQuery (char* query) {
RDF_Resource root;
int numSegments;
int count = 0;
char** ans = NULL;
QuerySegment* querySegments = parseQuery(query, &root, &numSegments);
int n = 0;
QuerySegment current = NULL;
QuerySegment prev = NULL;
char** currentValArray = (char**)getMem(sizeof(RDF_Resource)*2);
char** newValArray = (char**)getMem(4096 * sizeof(RDF_Resource));
currentValArray[0] = (char*) root;
for (n = 0; n < numSegments; n++) {
QuerySegment q = querySegments[n];
int nc = 0;
int ansType = ((n == numSegments-1) ? RDF_STRING_TYPE : RDF_RESOURCE_TYPE);
count = 0;
for (nc = 0; currentValArray[nc] != NULL; nc++) {
RDF_Cursor c = getSlotValues(NULL, (RDF_Resource)currentValArray[nc], q->s,
ansType, q->inversep, 1);
RDF_Resource ans;
int lc = 0;
while (c && (lc++ < q->num) && (ans = nextValue(c))) {
newValArray[count++] = (char*) ans;
}
if (c) disposeCursor(c);
}
freeMem(currentValArray);
currentValArray = newValArray;
newValArray = (char**) getMem(4096 * sizeof(RDF_Resource));
}
freeMem(newValArray);
if (count > 0) {
ans = (char**)getMem((count+1) * sizeof(char*));
memcpy(ans, currentValArray, (count * sizeof(char*)));
}
freeMem(currentValArray);
for (n = 0; n < numSegments; n++) freeMem(querySegments[n]);
return ans;
}

154
rdf/opendir/rdf-int.h Normal file
Просмотреть файл

@ -0,0 +1,154 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "rdf.h"
#define true 1
#define false 0
#define null NULL
#define nullp(x) (((void*)x) == ((void*)0))
#define noRDFErr 0
#define noMoreValuesErr 1
#define LookupResource(x) ((RDF_Resource)PL_HashTableLookup(resourceHash, x));
#define stringEquals(x, y) (strcmp(x, y) ==0)
#define wsCharp(c) ((c == '\r') || (c == '\t') || (c == ' ') || (c == '\n'))
#define RDF_BUF_SIZE (4096 * 16)
#define MAX_ATTRIBUTES 256
#define EXPECTING_OBJECT 1
#define EXPECTING_PROPERTY 2
#define GROW_LIST_INCR 100
typedef struct _RDF_ResourceStruct {
char* url;
struct _RDF_AssertionStruct* rarg1;
struct _RDF_AssertionStruct* rarg2;
} RDF_ResourceStruct ;
typedef RDF_ResourceStruct* RDF_Resource;
typedef enum {
GET_SLOT_VALUES,
ARC_LABELS_IN,
ARC_LABELS_OUT
} QueryType;
typedef struct _RDF_FileStruct {
char* url;
char* storeAway;
char* line;
int status;
char* holdOver;
RDF_Resource stack[16];
RDF_Resource lastItem;
int depth ;
char* tagStack[16];
int tagDepth;
int assertionListCount;
int assertionListSize;
struct _RDF_AssertionStruct** assertionList;
} RDF_FileStruct;
typedef RDF_FileStruct* RDFT;
typedef struct _RDF_CursorStruct {
RDF_Resource u;
RDF_Resource s;
void *value;
struct _RDF_AssertionStruct* pdata;
int inversep;
RDF_ValueType type;
int count;
RDFT db;
QueryType queryType;
} RDF_CursorStruct;
typedef RDF_CursorStruct* RDF_Cursor;
typedef struct _RDF_AssertionStruct {
RDF_Resource u;
RDF_Resource s;
void* value;
RDF_ValueType type;
struct _RDF_AssertionStruct* next;
struct _RDF_AssertionStruct* invNext;
RDFT db;
} RDF_AssertionStruct;
typedef RDF_AssertionStruct* Assertion;
typedef struct _HashTableStruct* HashTable;
HashTable NewHashTable(int size) ;
void* HashLookup(HashTable ht, char* key) ;
void HashAdd (HashTable ht, char* key, void* value) ;
RDF_Resource getResource(char* url, int createp);
char* getMem(size_t n);
void freeMem(void* item);
RDFT initFileStruct (char* url) ;
void rdf_init();
int startsWith (const char* pattern, const char* uuid);
char decodeEntityRef (char* string, int* stringIndexPtr, int len);
char * copyStringIgnoreWhiteSpace(char* string);
char * getHref(char** attlist);
int parseNextRDFXMLBlobInt(RDFT f, char* blob, int size) ;
char * getAttributeValue (char** attlist, char* elName);
int tagEquals (RDFT f, char* tag1, char* tag2);
void addElementProps (char** attlist, char* elementName, RDFT f, RDF_Resource obj);
int knownObjectElement (char* eln);
char *possiblyMakeAbsolute (RDFT f, char* url);
int containerTagp (RDFT f, char* elementName);
RDF_Resource ResourceFromElementName (RDFT f, char* elementName);
int parseNextRDFToken (RDFT f, char* token);
int tokenizeElement (char* attr, char** attlist, char** elementName);
void addSlotValue (RDFT f, RDF_Resource u, RDF_Resource s, void* v,
RDF_ValueType type, char* op);
char* copyString(char* str) ;
int asEqual(RDFT r, Assertion as, RDF_Resource u, RDF_Resource s, void* v,
RDF_ValueType type);
Assertion makeNewAssertion (RDFT r, RDF_Resource u, RDF_Resource s, void* v,
RDF_ValueType type, int tv);
void freeAssertion (Assertion as);
Assertion remoteStoreAdd (RDFT mcf, RDF_Resource u, RDF_Resource s, void* v,
RDF_ValueType type, int tv);
Assertion remoteStoreRemove (RDFT mcf, RDF_Resource u, RDF_Resource s, void* v,
RDF_ValueType type);
int remoteStoreHasAssertion (RDFT mcf, RDF_Resource u, RDF_Resource s, void* v,
RDF_ValueType type, int tv);
void * getSlotValue (RDFT mcf, RDF_Resource u, RDF_Resource s, RDF_ValueType type,
int inversep, int tv);
RDF_Cursor getSlotValues (RDFT mcf, RDF_Resource u, RDF_Resource s, RDF_ValueType type,
int inversep, int tv);
void* nextValue (RDF_Cursor c) ;
void disposeCursor (RDF_Cursor c);
void unloadRDFT (RDFT f);
int rdf_DigestNewStuff (char* fileName, char* data, int len) ;
RDFT getRDFT(char* url, int createp);
char** processRDFQuery (char* query);

86
rdf/opendir/rdf.c Normal file
Просмотреть файл

@ -0,0 +1,86 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
/* This file contains the RDF APIs for Brooklyn. These are just
wrappers around functions implemented in other files */
#include "rdf-int.h"
void RDF_Initialize () {
rdf_init();
}
void RDF_ReadFile (char* fileName) {
readRDFFile(fileName);
}
int RDF_Consume (char* fileName, char* data, int len) {
return rdf_DigestNewStuff (fileName, data, len) ;
}
void RDF_Unload (RDFT f) {
unloadRDFT (f);
}
RDFT RDF_GetRDFT (char* url, int createp) {
return getRDFT (url, createp) ;
}
RDF_Resource RDF_GetResource (char* url, int createp) {
return getResource (url, createp) ;
}
int RDF_Assert(RDFT db, RDF_Resource u, RDF_Resource s, void* v, RDF_ValueType type) {
return (NULL != remoteStoreAdd (db, u, s, v, type, 1));
}
int RDF_Unassert (RDFT db, RDF_Resource u, RDF_Resource s, void* v, RDF_ValueType type) {
return (NULL != remoteStoreRemove (db, u, s, v, type));
}
int RDF_HasAssertion (RDFT db, RDF_Resource u, RDF_Resource s,
void* v, RDF_ValueType type) {
return remoteStoreHasAssertion (db, u, s, v, type, 1);
}
void* RDF_OnePropValue (RDFT db, RDF_Resource u, RDF_Resource s, RDF_ValueType type) {
return getSlotValue (db, u, s, type, 0, 1);
}
RDF_Cursor RDF_GetTargets (RDFT db, RDF_Resource u, RDF_Resource s, RDF_ValueType type) {
return getSlotValues (db, u, s, type, 0, 1);
}
RDF_Cursor RDF_GetSourcess (RDFT db, RDF_Resource u, RDF_Resource s) {
return getSlotValues (db, u, s, RDF_RESOURCE_TYPE, 1, 1);
}
void* RDF_NextValue (RDF_Cursor c) {
return nextValue (c) ;
}
void RDF_DisposeCursor (RDF_Cursor c) {
disposeCursor(c);
}
char** RDF_processPathQuery(char* query) {
// return processRDFQuery (query) ;
}

52
rdf/opendir/rdf.h Normal file
Просмотреть файл

@ -0,0 +1,52 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
/* This is the header file for the functions exported from RDF */
typedef struct _RDF_ResourceStruct* RDF_Resource;
typedef struct _RDF_FileStruct* RDFT;
typedef struct _RDF_CursorStruct* RDF_Cursor;
typedef enum {
RDF_RESOURCE_TYPE,
RDF_INT_TYPE,
RDF_STRING_TYPE
} RDF_ValueType;
void RDF_Initialize () ;
int RDF_Consume (char* fileName, char* data, int len) ;
void RDF_ReadFile (char* fileName) ;
void RDF_Unload (RDFT f);
RDFT RDF_GetRDFT (char* url, int createp);
RDF_Resource RDF_GetResource (char* url, int createp);
int RDF_Assert(RDFT db, RDF_Resource u, RDF_Resource s, void* v, RDF_ValueType type);
int RDF_Unassert (RDFT db, RDF_Resource u, RDF_Resource s, void* v, RDF_ValueType type);
int RDF_HasAssertion (RDFT db, RDF_Resource u, RDF_Resource s, void* v, RDF_ValueType type);
void* RDF_OnePropValue (RDFT db, RDF_Resource u, RDF_Resource s, RDF_ValueType type);
RDF_Cursor RDF_GetTargets (RDFT db, RDF_Resource u, RDF_Resource s, RDF_ValueType type);
RDF_Cursor RDF_GetSourcess (RDFT db, RDF_Resource u, RDF_Resource s);
void* RDF_NextValue (RDF_Cursor c) ;
void RDF_DisposeCursor (RDF_Cursor c);
char** RDF_processPathQuery(char* query);

441
rdf/opendir/rdfparse.c Normal file
Просмотреть файл

@ -0,0 +1,441 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include "rdf-int.h"
char* error_string = NULL;
int lineNumber = 0;
static HashTable resourceHash = NULL;
RDF_Resource
getResource (char* key, int createp) {
RDF_Resource existing = HashLookup(resourceHash, key);
if (existing) {
return existing;
} else if (createp){
existing = (RDF_Resource)getMem(sizeof(RDF_ResourceStruct));
existing->url = copyString(key);
HashAdd(resourceHash, existing->url, existing);
return existing;
} else return NULL;
}
void readRDFFile (char* file) {
FILE* f = fopen(file, "r");
if (f) {
char* buff = malloc(100 * 1024);
int len ;
memset(buff, '\0', (100 * 1024));
len = fread(buff, 1, (100 * 1024) -1, f);
buff[len] = '\0';
if (!RDF_Consume(file, buff, len)) {
printf("Error in RDF File\n");
} else {
printf("Finished reading %s\n", file);
}
free(buff);
}
}
static HashTable rdftHash = NULL;
RDFT
getRDFT (char* key, int createp) {
RDFT existing = HashLookup(rdftHash, key);
if (existing) {
return existing;
} else if (createp){
existing = (RDFT)getMem(sizeof(RDF_FileStruct));
existing->url = copyString(key);
HashAdd(rdftHash, existing->url, existing);
return existing;
} else return NULL;
}
void
rdf_init () {
error_string = getMem(1000);
resourceHash = NewHashTable((int)0x00000FFF);
rdftHash = NewHashTable((int)0x00000FFF);
}
int
rdf_DigestNewStuff (char* url, char* data, int len) {
RDFT rf = (RDFT)getRDFT(url, 1) ;
int ok = 1;
unloadRDFT(rf);
memset(rf, '\0', sizeof(RDF_FileStruct));
rf->line = (char*)getMem(RDF_BUF_SIZE);
rf->holdOver = (char*)getMem(RDF_BUF_SIZE);
rf->depth = 1;
rf->lastItem = rf->stack[0] ;
ok = parseNextRDFXMLBlobInt(rf, data, len);
if (!ok) unloadRDFT(rf);
freeMem(rf->line);
rf->line = NULL;
freeMem(rf->holdOver);
rf->holdOver = NULL;
return ok;
}
int
startsWith (const char* pattern, const char* uuid) {
int l1 = strlen(pattern);
int l2 = strlen(uuid);
int n;
if (l2 < l1) return 0;
for (n = 0; n < l1; n++) {
if (pattern[n] != uuid[n]) return 0;
}
return 1;
}
char*
getMem (size_t n) {
char* ans = (char*) malloc(n);
if (ans) memset(ans, '\0', n);
return ans;
}
void
freeMem(void* item) {
free(item);
}
char
decodeEntityRef (char* string, int* stringIndexPtr, int len) {
if (startsWith("lt;", string)) {
*stringIndexPtr = *stringIndexPtr + 3;
return '<';
} else if (startsWith("gt;", string)) {
*stringIndexPtr = *stringIndexPtr + 3;
return '>';
} else if (startsWith("amp;", string)) {
*stringIndexPtr = *stringIndexPtr + 4;
return '&';
} else return '&';
}
char *
copyStringIgnoreWhiteSpace(char* string)
{
int len = strlen(string);
char* buf = (char*)getMem(len + 1);
int inWhiteSpace = 1;
int buffIndex = 0;
int stringIndex = 0;
while (stringIndex < len) {
char nextChar = *(string + stringIndex);
int wsp = wsCharp(nextChar);
if (!wsp) {
if (nextChar == '&') {
*(buf + buffIndex++) = decodeEntityRef(&string[stringIndex+1],
&stringIndex, len-stringIndex);
} else {
*(buf + buffIndex++) = nextChar;
}
inWhiteSpace = 0;
} else if (!inWhiteSpace) {
*(buf + buffIndex++) = ' ';
inWhiteSpace = 1;
} else {
inWhiteSpace = 1;
}
stringIndex++;
}
return buf;
}
char *
getHref(char** attlist)
{
char* ans = getAttributeValue(attlist, "resource");
if (!ans) ans = getAttributeValue(attlist, "rdf:resource");
return ans;
}
char *
getID(char** attlist)
{
char* ans = getAttributeValue(attlist, "id");
if (!ans) ans = getAttributeValue(attlist, "about");
if (!ans) ans = getAttributeValue(attlist, "rdf:about");
return ans;
}
int
parseNextRDFXMLBlobInt(RDFT f, char* blob, int size) {
int n, last, m;
int somethingseenp = 0;
n = last = 0;
while (n < size) {
char c = blob[n];
if ((c == '\n') || (c == '\r')) lineNumber++;
m = 0;
somethingseenp = 0;
memset(f->line, '\0', RDF_BUF_SIZE-1);
if (f->holdOver[0] != '\0') {
memcpy(f->line, f->holdOver, strlen(f->holdOver));
m = strlen(f->holdOver);
somethingseenp = 1;
memset(f->holdOver, '\0', RDF_BUF_SIZE-1);
}
while ((n < size) && (wsCharp(c))) {
c = blob[++n];
if ((c == '\n') || (c == '\r')) lineNumber++;
}
while ((m < RDF_BUF_SIZE) && (c != '<') && (c != '>')) {
f->line[m] = c;
m++;
somethingseenp = (somethingseenp || (!(wsCharp(c))));
n++;
if (n < size) c = blob[n];
else break;
if ((c == '\n') || (c == '\r')) lineNumber++;
}
if (c == '>') f->line[m] = c;
n++;
if (m > 0) {
if ((c == '<') || (c == '>')) {
last = n;
if (c == '<') f->holdOver[0] = '<';
if (somethingseenp == 1) {
int ok = parseNextRDFToken(f, f->line);
if (!ok)
return 0;
}
} else if (size > last) {
memcpy(f->holdOver, f->line, m);
}
} else if (c == '<') f->holdOver[0] = '<';
}
return(1);
}
char *
getAttributeValue (char** attlist, char* elName)
{
size_t n = 0;
if (!attlist) return NULL;
while ((n < 2*MAX_ATTRIBUTES) && (*(attlist + n) != NULL)) {
if (strcmp(*(attlist + n), elName) == 0) return *(attlist + n + 1);
n = n + 2;
}
return NULL;
}
char*
copyString (char* str) {
char* ans = getMem(strlen(str)+1);
if (ans) {
memcpy(ans, str, strlen(str));
return ans;
} else return NULL;
}
void
addElementProps (char** attlist, char* elementName, RDFT f, RDF_Resource obj)
{
int count = 0;
while (count < 2*MAX_ATTRIBUTES) {
char* attName = attlist[count++];
char* attValue = attlist[count++];
if ((attName == NULL) || (attValue == NULL)) break;
if (!stringEquals(attName, "resource") &&
!stringEquals(attName, "rdf:resource") &&
!stringEquals(attName, "about") &&
!stringEquals(attName, "rdf:about") &&
!stringEquals(attName, "tv") &&
!stringEquals(attName, "id")) {
remoteStoreAdd(f, obj, getResource(attName, 1),
copyStringIgnoreWhiteSpace(attValue),
RDF_STRING_TYPE, 1);
}
}
}
int
parseNextRDFToken (RDFT f, char* token)
{
char* attlist[2*MAX_ATTRIBUTES+1];
char* elementName;
if (token[0] == '<') {
size_t len = strlen(token);
if (token[len-2] != '/') {
if (token[1] == '/') {
char* tok = getMem(len);
memcpy(tok, &token[2], len-3);
if (!stringEquals(tok, f->tagStack[f->tagDepth-1])) {
sprintf(error_string, "Unbalanced tags : Expecting </%s>, found %s",
f->tagStack[f->tagDepth-1], token);
return 0;
} else {
f->tagDepth--;
freeMem(tok);
}
}
}
}
if (token[0] != '<') {
if ((f->status == EXPECTING_OBJECT) && (f->depth > 1)) {
RDF_Resource u = f->stack[f->depth-2];
RDF_Resource s = f->stack[f->depth-1];
char* val = copyStringIgnoreWhiteSpace(token);
remoteStoreAdd(f, u, s, val , RDF_STRING_TYPE, 1);
} else {
sprintf(error_string, "Did not expect \n\"%s\".\n Was expecting a tag.", token);
return 0;
}
} else if (startsWith("<!--", token)) {
return 1;
} else if (token[1] == '?') {
return 1;
} else if (token[1] == '/') {
if ((f->status != EXPECTING_OBJECT) && (f->status != EXPECTING_PROPERTY)) {
sprintf(error_string, "Did not expect %s. Something pretty screwed up", token);
return 0;
}
if (f->depth > 0) f->depth--;
f->status = (f->status == EXPECTING_OBJECT ? EXPECTING_PROPERTY : EXPECTING_OBJECT);
return 1;
} else if ((f->status == 0) && (startsWith("<RDF:RDF", token) ||
startsWith("<RDF>", token))) {
f->tagStack[f->tagDepth++] = copyString("RDF");
f->status = EXPECTING_OBJECT;
return 1;
} else {
int emptyElementp = (token[strlen(token)-2] == '/');
if ((f->status != EXPECTING_OBJECT) && (f->status != EXPECTING_PROPERTY)) return 1;
if (!tokenizeElement(token, attlist, &elementName)) return 0;
if (!emptyElementp)
f->tagStack[f->tagDepth++] = copyString(elementName);
if (f->status == EXPECTING_OBJECT) {
char* url = NULL;
RDF_Resource obj;
int count = 0;
url = getID(attlist);
if (!url) {
if (f->tagDepth > 2) {
sprintf(error_string, "Unbalanced tags : Expecting </%s>, found %s",
f->tagStack[f->tagDepth-2], token);
} else {
sprintf(error_string, "Require a \"about\" attribute on %s", token);
}
return 0;
}
obj = getResource(url, 1);
addElementProps (attlist, elementName, f, obj) ;
if (!stringEquals(elementName, "RDF:Description")) {
RDF_Resource eln = getResource(elementName, 1);
remoteStoreAdd(f, obj, getResource("instanceOf", 1),
eln, RDF_RESOURCE_TYPE,
1);
}
if (f->depth > 1) {
remoteStoreAdd(f, f->stack[f->depth-2], f->stack[f->depth-1], obj,
RDF_RESOURCE_TYPE, 1);
}
if (!emptyElementp) {
f->stack[f->depth++] = obj;
f->status = EXPECTING_PROPERTY;
}
} else if (f->status == EXPECTING_PROPERTY) {
char* url;
RDF_Resource obj;
int count = 0;
url = getHref(attlist) ;
if (url) {
RDF_Resource eln = getResource(elementName, 1);
obj = getResource(url, 1);
freeMem(url);
addElementProps (attlist, elementName, f, obj) ;
remoteStoreAdd(f, f->stack[f->depth-1], eln, obj, RDF_RESOURCE_TYPE, 1);
}
if (!emptyElementp) {
f->stack[f->depth++] = getResource(elementName, 1);
f->status = EXPECTING_OBJECT;
}
}
}
}
int
tokenizeElement (char* attr, char** attlist, char** elementName)
{
size_t n = 1;
size_t s = strlen(attr);
char c ;
size_t m = 0;
size_t atc = 0;
int emptyTagp = (attr[s-2] == '/');
int inAttrNamep = 1;
c = attr[n++];
while (wsCharp(c)) {
c = attr[n++];
}
*elementName = &attr[n-1];
while (n < s) {
if (wsCharp(c)) break;
c = attr[n++];
}
attr[n-1] = '\0';
while (atc < 2*MAX_ATTRIBUTES+1) {*(attlist + atc++) = NULL;}
atc = 0;
s = (emptyTagp ? s-2 : s-1);
while (n < s) {
int attributeOpenStringSeenp = 0;
m = 0;
c = attr[n++];
while ((n <= s) && (atc < 2*MAX_ATTRIBUTES)) {
if (inAttrNamep && (m > 0) && (wsCharp(c) || (c == '='))) {
attr[n-1] = '\0';
*(attlist + atc++) = &attr[n-m-1];
break;
}
if (!inAttrNamep && attributeOpenStringSeenp && (c == '"')) {
attr[n-1] = '\0';
*(attlist + atc++) = &attr[n-m-1];
break;
}
if (inAttrNamep) {
if ((m > 0) || (!wsCharp(c))) m++;
} else {
if (c == '"') {
attributeOpenStringSeenp = 1;
} else {
if ((m > 0) || (!(wsCharp(c)))) m++;
}
}
c = attr[n++];
}
inAttrNamep = (inAttrNamep ? 0 : 1);
}
return 1;
}

237
rdf/opendir/remstore.c Normal file
Просмотреть файл

@ -0,0 +1,237 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include "rdf-int.h"
int
asEqual(RDFT r, Assertion as, RDF_Resource u, RDF_Resource s, void* v,
RDF_ValueType type)
{
return (((r == NULL) || (as->db == r)) &&
(as->u == u) &&
(as->s == s) &&
(as->type == type) &&
((as->value == v) ||
((type == RDF_STRING_TYPE) && (strcmp(v, as->value) == 0))));
}
Assertion
makeNewAssertion (RDFT r, RDF_Resource u, RDF_Resource s, void* v,
RDF_ValueType type, int tv)
{
Assertion newAs = (Assertion) getMem(sizeof(RDF_AssertionStruct));
newAs->u = u;
newAs->s = s;
newAs->value = v;
newAs->type = type;
newAs->db = r;
return newAs;
}
void
addToAssertionList (RDFT f, Assertion as)
{
if (f->assertionListCount >= f->assertionListSize) {
f->assertionList = realloc(f->assertionList,
(sizeof(Assertion*) *
(f->assertionListSize =
f->assertionListSize + GROW_LIST_INCR)));
/* Assertion* old = f->assertionList;
f->assertionListSize = f->assertionListSize + GROW_LIST_INCR;
f->assertionList = (Assertion*)getMem(f->assertionListSize);
if (old) memcpy(f->assertionList, old,
(f->assertionListSize - GROW_LIST_INCR) * sizeof(Assertion*));
freeMem(old); */
}
*(f->assertionList + f->assertionListCount++) = as;
}
void
freeAssertion (Assertion as)
{
if (as->type == RDF_STRING_TYPE) {
freeMem(as->value);
}
freeMem(as);
}
Assertion
remoteStoreAdd (RDFT mcf, RDF_Resource u, RDF_Resource s, void* v,
RDF_ValueType type, int tv)
{
Assertion nextAs, prevAs, newAs;
nextAs = prevAs = u->rarg1;
while (nextAs != null) {
if (asEqual(mcf, nextAs, u, s, v, type)) return null;
prevAs = nextAs;
nextAs = nextAs->next;
}
newAs = makeNewAssertion(mcf, u, s, v, type, tv);
if (prevAs == null) {
u->rarg1 = newAs;
} else {
prevAs->next = newAs;
}
if (type == RDF_RESOURCE_TYPE) {
nextAs = prevAs = ((RDF_Resource)v)->rarg2;
while (nextAs != null) {
prevAs = nextAs;
nextAs = nextAs->invNext;
}
if (prevAs == null) {
((RDF_Resource)v)->rarg2 = newAs;
} else {
prevAs->invNext = newAs;
}
}
addToAssertionList(mcf, newAs);
return newAs;
}
Assertion
remoteStoreRemove (RDFT mcf, RDF_Resource u, RDF_Resource s,
void* v, RDF_ValueType type)
{
Assertion nextAs, prevAs, ans;
int found = false;
nextAs = prevAs = u->rarg1;
while (nextAs != null) {
if (asEqual(mcf, nextAs, u, s, v, type)) {
if (prevAs == nextAs) {
u->rarg1 = nextAs->next;
} else {
prevAs->next = nextAs->next;
}
found = true;
ans = nextAs;
break;
}
prevAs = nextAs;
nextAs = nextAs->next;
}
if (found == false) return null;
if (type == RDF_RESOURCE_TYPE) {
nextAs = prevAs = ((RDF_Resource)v)->rarg2;
while (nextAs != null) {
if (nextAs == ans) {
if (prevAs == nextAs) {
((RDF_Resource)v)->rarg2 = nextAs->invNext;
} else {
prevAs->invNext = nextAs->invNext;
}
}
prevAs = nextAs;
nextAs = nextAs->invNext;
}
}
return ans;
}
int
remoteStoreHasAssertion (RDFT mcf, RDF_Resource u, RDF_Resource s, void* v, RDF_ValueType type, int tv)
{
Assertion nextAs;
nextAs = u->rarg1;
while (nextAs != null) {
if (asEqual(mcf, nextAs, u, s, v, type)) return true;
nextAs = nextAs->next;
}
return false;
}
void *
getSlotValue (RDFT mcf, RDF_Resource u, RDF_Resource s, RDF_ValueType type, int inversep, int tv)
{
Assertion nextAs;
nextAs = (inversep ? u->rarg2 : u->rarg1);
while (nextAs != null) {
if (((nextAs->db == mcf) || (!mcf)) && (nextAs->s == s)
&& (nextAs->type == type)) {
return (inversep ? nextAs->u : nextAs->value);
}
nextAs = (inversep ? nextAs->invNext : nextAs->next);
}
return null;
}
RDF_Cursor
getSlotValues (RDFT mcf, RDF_Resource u, RDF_Resource s, RDF_ValueType type, int inversep, int tv)
{
Assertion as = (inversep ? u->rarg2 : u->rarg1);
RDF_Cursor c;
if (!as) return NULL;
c = (RDF_Cursor)getMem(sizeof(RDF_CursorStruct));
c->u = u;
c->s = s;
c->type = type;
c->inversep = inversep;
c->count = 0;
c->db = mcf;
c->pdata = as;
c->queryType = GET_SLOT_VALUES;
return c;
}
void *
nextValue (RDF_Cursor c) {
while (c->pdata != null) {
Assertion as = (Assertion) c->pdata;
if (((as->db == c->db) || (!c->db)) && (as->s == c->s) && (c->type == as->type)) {
c->value = (c->inversep ? as->u : as->value);
c->pdata = (c->inversep ? as->invNext : as->next);
return c->value;
}
c->pdata = (c->inversep ? as->invNext : as->next);
}
return null;
}
void
disposeCursor (RDF_Cursor c)
{
freeMem(c);
}
void
unloadRDFT (RDFT f)
{
int n = 0;
while (n < f->assertionListCount) {
Assertion as = *(f->assertionList + n);
remoteStoreRemove(f, as->u, as->s, as->value, as->type);
freeAssertion(as);
*(f->assertionList + n) = NULL;
n++;
}
f->assertionListCount = 0;
f->assertionListSize = 0;
freeMem(f->assertionList);
f->assertionList = NULL;
}

39
rdf/opendir/rl.c Normal file
Просмотреть файл

@ -0,0 +1,39 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include "rl.h"
#include "plstr.h"
PR_IMPLEMENT(void)
RLInit(void)
{
}
PR_IMPLEMENT(void)
RLTrace(const char* fmtstr, ...)
{
char* str;
va_list ap;
va_start(ap, fmtstr);
str = PR_vsmprintf(fmtstr, ap);
if (str) {
PR_Write(PR_STDERR, str, PL_strlen(str));
PR_smprintf_free(str);
}
}

431
rdf/opendir/spf2ldiff.c Normal file
Просмотреть файл

@ -0,0 +1,431 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include "rdf-int.h"
char* error_string = NULL;
int lineNumber = 0;
void
main (int argc, char* argv[]) {
RDFFile rf = initFileStruct();
int ok = 1;
error_string = getMem(1000);
if (argc == 2) {
FILE* f = fopen(argv[1], "r");
if (f != NULL) {
char* buff = getMem(RDF_BUF_SIZE);
int len;
while (ok && ((len = fread(buff, 1, RDF_BUF_SIZE-1, f)) > 0)) {
buff[len] = '\0';
ok = parseNextRDFXMLBlobInt(rf, buff, strlen(buff));
memset(buff, '\0', RDF_BUF_SIZE);
}
}
if (ok) {
while (1) {
char* nextQuery = getMem(200);
int n;
nextQuery = gets(nextQuery);
if (nextQuery) {
char** ans = processRDFQuery(nextQuery);
if (ans) {
for (n = 0; ans[n] != NULL; n++) {
printf("%s\n", ans[n]);
}
}
} else {
break;
}
}
}
}
}
void
RDF_Init () {
error_string = getMem(1000);
}
int
RDF_DigestNewStuff (char* fileName, char* data, int len) {
RDFFile rf = initFileStruct();
int ok = 1;
ok = parseNextRDFXMLBlobInt(rf, data, len);
freeMem(rf->line);
freeMem(rf->holdOver);
return ok;
}
RDFFile
initFileStruct (void) {
RDFFile ans = (RDFFile)getMem(sizeof(RDF_FileStruct));
ans->line = (char*)getMem(RDF_BUF_SIZE);
ans->holdOver = (char*)getMem(RDF_BUF_SIZE);
ans->depth = 1;
ans->lastItem = ans->stack[0] ;
return ans;
}
PRBool
startsWith (const char* pattern, const char* uuid)
{
int l1 = strlen(pattern);
int l2 = strlen(uuid);
int n;
if (l2 < l1) return 0;
for (n = 0; n < l1; n++) {
if (pattern[n] != uuid[n]) return 0;
}
return 1;
}
char*
getMem (size_t n) {
char* ans = (char*) malloc(n);
if (ans) memset(ans, '\0', n);
return ans;
}
void
freeMem(void* item) {
free(item);
}
char
decodeEntityRef (char* string, int* stringIndexPtr, int len) {
if (startsWith("lt;", string)) {
*stringIndexPtr = *stringIndexPtr + 3;
return '<';
} else if (startsWith("gt;", string)) {
*stringIndexPtr = *stringIndexPtr + 3;
return '>';
} else if (startsWith("amp;", string)) {
*stringIndexPtr = *stringIndexPtr + 4;
return '&';
} else return '&';
}
char *
copyStringIgnoreWhiteSpace(char* string)
{
int len = strlen(string);
char* buf = (char*)getMem(len + 1);
PRBool inWhiteSpace = 1;
int buffIndex = 0;
int stringIndex = 0;
while (stringIndex < len) {
char nextChar = *(string + stringIndex);
PRBool wsp = wsCharp(nextChar);
if (!wsp) {
if (nextChar == '&') {
*(buf + buffIndex++) = decodeEntityRef(&string[stringIndex+1],
&stringIndex, len-stringIndex);
} else {
*(buf + buffIndex++) = nextChar;
}
inWhiteSpace = 0;
} else if (!inWhiteSpace) {
*(buf + buffIndex++) = ' ';
inWhiteSpace = 1;
} else {
inWhiteSpace = 1;
}
stringIndex++;
}
return buf;
}
char *
getHref(char** attlist)
{
char* ans = getAttributeValue(attlist, "resource");
if (!ans) ans = getAttributeValue(attlist, "rdf:resource");
return ans;
}
char *
getID(char** attlist)
{
char* ans = getAttributeValue(attlist, "id");
if (!ans) ans = getAttributeValue(attlist, "about");
if (!ans) ans = getAttributeValue(attlist, "rdf:about");
return ans;
}
int
parseNextRDFXMLBlobInt(RDFFile f, char* blob, int size) {
int n, last, m;
PRBool somethingseenp = 0;
n = last = 0;
while (n < size) {
char c = blob[n];
if ((c == '\n') || (c == '\r')) lineNumber++;
m = 0;
somethingseenp = 0;
memset(f->line, '\0', RDF_BUF_SIZE-1);
if (f->holdOver[0] != '\0') {
memcpy(f->line, f->holdOver, strlen(f->holdOver));
m = strlen(f->holdOver);
somethingseenp = 1;
memset(f->holdOver, '\0', RDF_BUF_SIZE-1);
}
while ((n < size) && (wsCharp(c))) {
c = blob[++n];
if ((c == '\n') || (c == '\r')) lineNumber++;
}
while ((m < RDF_BUF_SIZE) && (c != '<') && (c != '>')) {
f->line[m] = c;
m++;
somethingseenp = (somethingseenp || (!(wsCharp(c))));
n++;
if (n < size) c = blob[n];
else break;
if ((c == '\n') || (c == '\r')) lineNumber++;
}
if (c == '>') f->line[m] = c;
n++;
if (m > 0) {
if ((c == '<') || (c == '>')) {
last = n;
if (c == '<') f->holdOver[0] = '<';
if (somethingseenp == 1) {
int ok = parseNextRDFToken(f, f->line);
if (!ok)
return 0;
}
} else if (size > last) {
memcpy(f->holdOver, f->line, m);
}
} else if (c == '<') f->holdOver[0] = '<';
}
return(1);
}
char *
getAttributeValue (char** attlist, char* elName)
{
size_t n = 0;
if (!attlist) return NULL;
while ((n < 2*MAX_ATTRIBUTES) && (*(attlist + n) != NULL)) {
if (strcmp(*(attlist + n), elName) == 0) return *(attlist + n + 1);
n = n + 2;
}
return NULL;
}
char*
copyString (char* str) {
char* ans = getMem(strlen(str)+1);
if (ans) {
memcpy(ans, str, strlen(str));
return ans;
} else return NULL;
}
void
addElementProps (char** attlist, char* elementName, RDFFile f, RDF_Resource obj)
{
int count = 0;
while (count < 2*MAX_ATTRIBUTES) {
char* attName = attlist[count++];
char* attValue = attlist[count++];
if ((attName == NULL) || (attValue == NULL)) break;
if (!stringEquals(attName, "resource") &&
!stringEquals(attName, "rdf:resource") &&
!stringEquals(attName, "about") &&
!stringEquals(attName, "rdf:about") &&
!stringEquals(attName, "tv") &&
!stringEquals(attName, "id")) {
remoteStoreAdd(f, obj, getResource(f, attName),
copyStringIgnoreWhiteSpace(attValue),
RDF_STRING_TYPE, 1);
}
}
}
int
parseNextRDFToken (RDFFile f, char* token)
{
char* attlist[2*MAX_ATTRIBUTES+1];
char* elementName;
if (token[0] == '<') {
size_t len = strlen(token);
if (token[len-2] != '/') {
if (token[1] == '/') {
char* tok = getMem(len);
memcpy(tok, &token[2], len-3);
if (!stringEquals(tok, f->tagStack[f->tagDepth-1])) {
sprintf(error_string, "Unbalanced tags : Expecting </%s>, found %s",
f->tagStack[f->tagDepth-1], token);
return 0;
} else {
f->tagDepth--;
freeMem(tok);
}
}
}
}
if (token[0] != '<') {
if ((f->status == EXPECTING_OBJECT) && (f->depth > 1)) {
RDF_Resource u = f->stack[f->depth-2];
RDF_Resource s = f->stack[f->depth-1];
char* val = copyStringIgnoreWhiteSpace(token);
remoteStoreAdd(f, u, s, val , RDF_STRING_TYPE, 1);
} else {
sprintf(error_string, "Did not expect \n\"%s\".\n Was expecting a tag.", token);
return 0;
}
} else if (startsWith("<!--", token)) {
return 1;
} else if (token[1] == '?') {
return 1;
} else if (token[1] == '/') {
if ((f->status != EXPECTING_OBJECT) && (f->status != EXPECTING_PROPERTY)) {
sprintf(error_string, "Did not expect %s. Something pretty screwed up", token);
return 0;
}
if (f->depth > 0) f->depth--;
f->status = (f->status == EXPECTING_OBJECT ? EXPECTING_PROPERTY : EXPECTING_OBJECT);
return 1;
} else if ((f->status == 0) && (startsWith("<RDF:RDF", token) ||
startsWith("<RDF>", token))) {
f->tagStack[f->tagDepth++] = copyString("RDF");
f->status = EXPECTING_OBJECT;
return 1;
} else {
PRBool emptyElementp = (token[strlen(token)-2] == '/');
if ((f->status != EXPECTING_OBJECT) && (f->status != EXPECTING_PROPERTY)) return 1;
if (!tokenizeElement(token, attlist, &elementName)) return 0;
if (!emptyElementp)
f->tagStack[f->tagDepth++] = copyString(elementName);
if (f->status == EXPECTING_OBJECT) {
char* url = NULL;
RDF_Resource obj;
int count = 0;
url = getID(attlist);
if (!url) {
if (f->tagDepth > 2) {
sprintf(error_string, "Unbalanced tags : Expecting </%s>, found %s",
f->tagStack[f->tagDepth-2], token);
} else {
sprintf(error_string, "Require a \"about\" attribute on %s", token);
}
return 0;
}
obj = getResource(f, url);
addElementProps (attlist, elementName, f, obj) ;
if (!stringEquals(elementName, "RDF:Description")) {
RDF_Resource eln = getResource(f, elementName);
remoteStoreAdd(f, obj, getResource(NULL, "instanceOf"),
eln, RDF_RESOURCE_TYPE,
1);
}
if (f->depth > 1) {
remoteStoreAdd(f, f->stack[f->depth-2], f->stack[f->depth-1], obj,
RDF_RESOURCE_TYPE, 1);
}
if (!emptyElementp) {
f->stack[f->depth++] = obj;
f->status = EXPECTING_PROPERTY;
}
} else if (f->status == EXPECTING_PROPERTY) {
char* url;
RDF_Resource obj;
int count = 0;
url = getHref(attlist) ;
if (url) {
RDF_Resource eln = getResource(f, elementName);
char* tvAtt = getAttributeValue(attlist, "tv");
obj = getResource(f, url);
freeMem(url);
addElementProps (attlist, elementName, f, obj) ;
remoteStoreAdd(f, f->stack[f->depth-1], eln, obj, RDF_RESOURCE_TYPE, 1);
}
if (!emptyElementp) {
f->stack[f->depth++] = getResource(f, elementName);
f->status = EXPECTING_OBJECT;
}
}
}
}
int
tokenizeElement (char* attr, char** attlist, char** elementName)
{
size_t n = 1;
size_t s = strlen(attr);
char c ;
size_t m = 0;
size_t atc = 0;
PRBool emptyTagp = (attr[s-2] == '/');
PRBool inAttrNamep = 1;
c = attr[n++];
while (wsCharp(c)) {
c = attr[n++];
}
*elementName = &attr[n-1];
while (n < s) {
if (wsCharp(c)) break;
c = attr[n++];
}
attr[n-1] = '\0';
while (atc < 2*MAX_ATTRIBUTES+1) {*(attlist + atc++) = NULL;}
atc = 0;
s = (emptyTagp ? s-2 : s-1);
while (n < s) {
PRBool attributeOpenStringSeenp = 0;
m = 0;
c = attr[n++];
while ((n <= s) && (atc < 2*MAX_ATTRIBUTES)) {
if (inAttrNamep && (m > 0) && (wsCharp(c) || (c == '='))) {
attr[n-1] = '\0';
*(attlist + atc++) = &attr[n-m-1];
break;
}
if (!inAttrNamep && attributeOpenStringSeenp && (c == '"')) {
attr[n-1] = '\0';
*(attlist + atc++) = &attr[n-m-1];
break;
}
if (inAttrNamep) {
if ((m > 0) || (!wsCharp(c))) m++;
} else {
if (c == '"') {
attributeOpenStringSeenp = 1;
} else {
if ((m > 0) || (!(wsCharp(c)))) m++;
}
}
c = attr[n++];
}
inAttrNamep = (inAttrNamep ? 0 : 1);
}
return 1;
}

66
rdf/opendir/test.c Normal file
Просмотреть файл

@ -0,0 +1,66 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include "rdf.h"
#include <stdlib.h>
#include <stdio.h>
void main () {
char* nextQuery = malloc(200);
memset(nextQuery, '\0', 200);
RDF_Initialize();
while (1) {
int n;
int startsWith(const char* pattern, const char* item);
nextQuery = gets(nextQuery);
if (nextQuery) {
if (startsWith("read", nextQuery)) {
FILE* f = fopen(&nextQuery[5], "r");
if (f) {
char* buff = malloc(100 * 1024);
int len ;
memset(buff, '\0', (100 * 1024));
len = fread(buff, 1, (100 * 1024) -1, f);
buff[len] = '\0';
if (!RDF_Consume(&nextQuery[5], buff, len)) {
printf("Error in RDF File\n");
} else {
printf("Finished reading %s\n", &nextQuery[5]);
}
free(buff);
} else {
printf("could not open %s\n", &nextQuery[5]);
}
} else if (startsWith("rdf", nextQuery)) {
char** ans = RDF_processPathQuery(&nextQuery[4]);
printf("\n");
if (ans) {
for (n = 0; ans[n] != 0; n++) {
printf("(%i) %s\n",n, ans[n]);
}
} else {
printf("No answers\n");
}
free(ans);
} else break;
}
}
}