gecko-dev/rdf/opendir/gs.c

249 lines
6.2 KiB
C
Raw Normal View History

/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
1998-12-14 20:51:53 +00:00
*
* The contents of this file are subject to the Netscape Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
1998-12-14 20:51:53 +00:00
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
1998-12-14 20:51:53 +00:00
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the NPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the NPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
1998-12-14 20:51:53 +00:00
// 39204897
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
1998-12-16 19:20:49 +00:00
#include <ctype.h>
1998-12-14 20:51:53 +00:00
#include "rdf-int.h"
#include "gs.h"
typedef struct _TrieNodeStruct {
char c;
1998-12-15 00:11:09 +00:00
struct _TrieNodeStruct* child;
1998-12-14 20:51:53 +00:00
struct _TrieNodeStruct* next;
struct _TrieTargetStruct* targets;
} TrieNodeStruct;
typedef TrieNodeStruct* TNS;
typedef struct _TrieTargetStruct {
RDF_Resource label;
RDF_Resource target;
struct _TrieTargetStruct* next;
RDFT db;
} TrieTargetStruct;
typedef TrieTargetStruct* TTS;
static TNS gRootNode = 0;
1998-12-20 00:16:45 +00:00
int
addTarget (RDFT db, TNS node, RDF_Resource label, RDF_Resource targetNode) {
TTS target ;
int n = 0;
/* for (target = node->targets; target != null; target = target->next) {
if (target->target == targetNode) return 0;
n++;
} */
target = (TTS) fgetMem(sizeof(TrieTargetStruct));
target->next = node->targets;
node->targets = target;
target->label = label;
target->target = targetNode;
target->db = db;
return n;
1998-12-14 20:51:53 +00:00
}
1998-12-15 00:11:09 +00:00
TNS
findChildOfChar (TNS node, char c) {
TNS ch = node->child;
char c1 = tolower(c);
1999-01-25 23:16:04 +00:00
int n = 0;
1998-12-15 00:11:09 +00:00
while (ch) {
if (c1 == ch->c) return ch;
ch = ch->next;
1999-01-25 23:16:04 +00:00
n++;
1998-12-15 00:11:09 +00:00
}
return 0;
}
TNS
findChildOfString (TNS node, char* str) {
size_t size = strlen(str);
size_t n = 0;
while (n < size) {
char c = str[n++];
node = findChildOfChar(node, c);
1998-12-20 00:16:45 +00:00
if (!node) return 0;
1998-12-15 00:11:09 +00:00
}
return node;
}
1998-12-14 20:51:53 +00:00
void RDFGS_AddSearchIndex (RDFT db, char* string, RDF_Resource label, RDF_Resource target) {
size_t size = strlen(string);
size_t n = 0;
1998-12-20 00:16:45 +00:00
char* stk = 0;
1998-12-14 20:51:53 +00:00
TNS prev, next;
if (!gRootNode) gRootNode = (TNS) getMem(sizeof(TrieNodeStruct));
prev = gRootNode;
next = 0;
while (n < size) {
1998-12-15 00:11:09 +00:00
char c = string[n++];
if (!wsCharp(c) && (c != '/')) {
1998-12-20 00:16:45 +00:00
if (!stk) stk = &string[n-1];
1998-12-15 00:11:09 +00:00
next = (TNS) findChildOfChar(prev, c);
if (!next) {
next = (TNS)fgetMem(sizeof(TrieNodeStruct));
next->next = prev->child;
prev->child = next;
next->c = tolower(c);
}
prev = next;
1998-12-14 20:51:53 +00:00
} else if (next) {
1998-12-20 00:16:45 +00:00
int n = addTarget(db, next, label, target);
stk = 0;
1998-12-15 00:11:09 +00:00
prev = gRootNode;
1998-12-15 00:47:13 +00:00
next = 0;
1998-12-14 20:51:53 +00:00
}
}
1998-12-15 00:47:13 +00:00
if (next) {
addTarget(db, next, label, target);
prev = gRootNode;
next = 0;
}
1998-12-14 20:51:53 +00:00
}
1998-12-15 00:11:09 +00:00
void
1998-12-20 00:16:45 +00:00
countChildren (TNS node, size_t *n, size_t *m) {
1998-12-15 00:11:09 +00:00
TNS ch;
1998-12-20 00:16:45 +00:00
TTS tg ;
1998-12-15 00:47:13 +00:00
if (node->targets) (*n)++;
1998-12-20 00:16:45 +00:00
for (tg = node->targets; tg; tg = tg->next) (*m)++;
1998-12-15 00:11:09 +00:00
ch = node->child;
while (ch) {
1998-12-20 00:16:45 +00:00
countChildren(ch, n, m);
1998-12-15 00:11:09 +00:00
ch = ch->next;
}
}
void
fillUpChildren (RDF_Cursor c, TNS node) {
TNS ch;
1998-12-16 01:25:37 +00:00
if (node->targets) *((TTS*)c->pdata1 + c->count++) = node->targets;
1998-12-15 00:11:09 +00:00
ch = node->child;
while (ch) {
fillUpChildren(c, ch);
ch = ch->next;
}
}
1998-12-14 20:51:53 +00:00
RDF_Cursor RDFGS_Search (RDFT db, char* searchString, RDF_Resource label) {
RDF_Cursor c = (RDF_Cursor) getMem(sizeof(RDF_CursorStruct));
1998-12-15 00:11:09 +00:00
size_t n = 0;
1998-12-20 00:16:45 +00:00
size_t m = 0;
1998-12-14 20:51:53 +00:00
c->searchString = searchString;
c->s = label;
c->db = db;
1998-12-15 00:11:09 +00:00
c->pdata = findChildOfString(gRootNode, searchString);
1998-12-20 00:16:45 +00:00
if (!c->pdata) return c;
countChildren((TNS)c->pdata, &n, &m);
c->pdata2 = (RDF_Resource*) getMem(sizeof(RDF_Resource) * (m+1));
c->off1 = m;
1998-12-15 00:11:09 +00:00
if (n > 0) {
c->count = 0;
1998-12-20 00:16:45 +00:00
c->pdata1 = getMem(sizeof(TTS) * (n+1));
1998-12-15 00:11:09 +00:00
fillUpChildren(c, (TNS)c->pdata);
1998-12-16 01:25:37 +00:00
c->count = 1;
1998-12-15 00:11:09 +00:00
}
1998-12-20 00:16:45 +00:00
if (c->pdata) c->pdata = ((TNS)c->pdata)->targets;
1998-12-14 20:51:53 +00:00
return c;
}
void RDFGS_DisposeCursor (RDF_Cursor c) {
1998-12-20 00:16:45 +00:00
if (c->pdata1) freeMem(c->pdata1);
if (c->pdata2) freeMem(c->pdata2);
freeMem(c);
}
int
alreadyAdded(RDF_Resource node, RDF_Cursor c) {
int n =0;
while (c->pdata2[n] && (n < c->off)) {
if (c->pdata2[n] == node) return 1;
n++;
}
return 0;
1998-12-14 20:51:53 +00:00
}
1998-12-16 01:25:37 +00:00
RDF_Resource RDFGS_NextValue (RDF_Cursor c) {
1998-12-20 00:16:45 +00:00
if (!c->pdata) {
1998-12-15 00:11:09 +00:00
return 0;
} else {
1998-12-20 00:16:45 +00:00
TTS currentTTS = (TTS) c->pdata;
1998-12-15 00:11:09 +00:00
while (currentTTS) {
1998-12-20 00:16:45 +00:00
if (((!c->s) || (c->s == currentTTS->label)) &&
(!alreadyAdded(currentTTS->target, c))) {
RDF_Resource ans = currentTTS->target;
1998-12-16 19:20:49 +00:00
c->pdata = currentTTS = currentTTS->next;
1998-12-20 00:16:45 +00:00
if (!currentTTS && (c->pdata1)) {
c->pdata = ((TTS*)c->pdata1)[c->count++];
}
if (c->off < c->off1) c->pdata2[c->off++] = ans;
1998-12-15 00:11:09 +00:00
return ans;
1998-12-20 00:16:45 +00:00
}
1998-12-15 00:11:09 +00:00
c->pdata = currentTTS = currentTTS->next;
1998-12-20 00:16:45 +00:00
if (!currentTTS && (c->pdata1)) {
c->pdata = currentTTS = ((TTS*)c->pdata1)[c->count++];
}
1998-12-14 20:51:53 +00:00
}
1998-12-15 00:11:09 +00:00
}
1998-12-16 01:25:37 +00:00
return 0;
1998-12-14 20:51:53 +00:00
}