1 /*********************************************************************/
2 /* Copyright (C) 2000, HELM Team */
4 /* This file is part of HELM, an Hypertextual, Electronic */
5 /* Library of Mathematics, developed at the Computer Science */
6 /* Department, University of Bologna, Italy. */
8 /* HELM is free software; you can redistribute it and/or */
9 /* modify it under the terms of the GNU General Public License */
10 /* as published by the Free Software Foundation; either version 2 */
11 /* of the License, or (at your option) any later version. */
13 /* HELM is distributed in the hope that it will be useful, */
14 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
15 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
16 /* GNU General Public License for more details. */
18 /* You should have received a copy of the GNU General Public License */
19 /* along with HELM; if not, write to the Free Software */
20 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, */
21 /* MA 02111-1307, USA. */
23 /* For details, see the HELM World-Wide-Web page, */
24 /* http://cs.unibo.it/helm/. */
25 /*********************************************************************/
27 /****************************************************************/
29 /****************************************************************/
30 /* This module supplies routines for symbol table handling. */
31 /* - init_symbol_table(): it initializes the symbol table */
33 /* - search_bucket(): it searches the symbol table for the */
34 /* bucket containing a given identifier, and */
35 /* inserts it if it is not present; */
36 /****************************************************************/
37 /* First draft 11/12/2001, by Andrea Asperti */
38 /****************************************************************/
40 /****************************************************************/
41 /* 1. Inclusion of header files. */
42 /****************************************************************/
48 /****************************************************************/
50 /****************************************************************/
55 #define HASH2 0xf0000000
67 /****************************************************************/
69 /****************************************************************/
73 struct int_list *next;
80 struct int_list *depths;
81 struct st_bucket *next_st_bucket;
82 /* next bucket in the list */
83 struct st_bucket *all_next;
84 /* all buckets in symbol
85 table are linked together */
90 struct st_bucket *dictionary[DICTSIZE];
91 /* pointers to bucket lists */
93 /****************************************************************/
94 /* 4. Local functions. */
95 /****************************************************************/
96 struct int_list *add(struct int_list *,int);
97 void allocate_bucket(struct st_bucket **st, char *id, int where);
98 void print_mainhyp(char *about, char *uri,struct int_list *l);
99 void print_mainconcl(char *about, char *uri, int depth);
100 void move_bucket(struct st_bucket *st, int dict_index);
101 void print_one(char *about, char *uri, int pos);
102 int hash_pjw(char *id);
104 /* This function is copied from the file fe-exec.c of PostgreSQL. */
105 /* Copyright (c) 1996-2003, PostgreSQL Global Development Group */
106 /* Copyright (c) 1994, Regents of the University of California */
108 PQescapeString(char *to, const char *from, size_t length)
110 const char *source = from;
112 size_t remaining = length;
114 while (remaining > 0 && *source != '\0')
136 /* Write the terminating NUL character. */
143 /****************************************************************/
144 /* 5. Definitions of functions to be exported. */
145 /****************************************************************/
147 struct st_bucket *all;
149 /* The following function initializes the symbol table to NULL */
150 void init_symbol_table()
154 /* initialize the dictionary */
155 for (i = 0; i < DICTSIZE; i++)
156 dictionary[i] = NULL;
160 /* The following function searches the symbol table for an identifier */
161 /* and inserts it if it is not present. */
162 /* The bucket associated with the given identifier */
163 /* becomes the first one in its list. */
165 int search_bucket(id, where, depth)
172 /* value returned by the */
178 struct st_bucket *st;
180 /* apply the hash function */
181 dict_index = hash_pjw(id);
182 /* fprintf(stderr,"%d\n", dict_index); */
184 /* scan the bucket list indicated by the hash function */
185 prev = curr = dictionary[dict_index];
186 while ((curr != NULL) && (strcmp(id, curr->id)))
189 curr = curr->next_st_bucket;
192 /* the identifier is not in the list */
194 allocate_bucket(&st,id,where);
195 if (where == MAINCONCL)
196 st->main_depth = depth;
197 else if (where == MAINHYP)
198 st->depths = add(st->depths,depth);
199 move_bucket(st,dict_index);
204 printf("uno=%s\n", id);
205 printf("st=%s\n", curr->id); fflush(stdout) */
207 /* the identifier is already in the list */
210 curr->pos[where] = 1;
212 curr->pos[INBODY] = 0; /* it will never be set again to 1 */
213 if (where == MAINHYP)
214 curr->depths=add(curr->depths,depth);
215 else if (where == MAINCONCL)
216 curr->main_depth = depth;
218 /* the identifier is not in the first position */
220 prev->next_st_bucket = curr->next_st_bucket;
221 move_bucket(curr,dict_index);
227 void print_all(about,conn)
232 struct st_bucket *curr;
236 for (i = 0; i < 5; ++i)
237 if ((curr->pos[i]) == 1)
240 print_mainhyp(about,curr->id,curr->depths);
241 else if (i == MAINCONCL)
242 print_mainconcl(about,curr->id,curr->main_depth);
244 print_one(about,curr->id,i);
246 curr = curr->all_next;
250 void print_name(char *name, char *uri)
252 printf("INSERT INTO objectName VALUES ('%s', '%s');\n",uri,name);
255 /****************************************************************/
256 /* 5. Definitions of functions local to the module. */
257 /****************************************************************/
259 struct int_list *add(l,m)
263 struct int_list *curr;
264 /* scan the list looking for m */
266 while ((curr != NULL) && (m != (curr->val)))
269 /* m is not in the list */
271 curr = (struct int_list *)malloc(sizeof(struct int_list));
281 void print_mainhyp(about,uri,l)
286 struct int_list *curr;
288 if (!strcmp(uri,"Rel"))
293 size_t len = strlen(about) + 1;
294 char *qabout = malloc (sizeof(char) * len * 2);
295 PQescapeString(qabout,about,len);
296 printf("INSERT INTO refRel VALUES ('%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#MainHypothesis', %d);\n",qabout,curr->val);
300 else if ((!strcmp(uri,"Prop")) || (!strcmp(uri,"Type")) ||
301 (!strcmp(uri,"Set")))
306 size_t len = strlen(about) + 1;
307 char *qabout = malloc (sizeof(char) * len * 2);
308 PQescapeString(qabout,about,len);
309 printf("INSERT INTO refSort VALUES ('%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#MainHypothesis', %d, '%s');\n",qabout,curr->val,uri);
318 size_t len = strlen(about) + 1;
319 char *qabout = malloc (sizeof(char) * len * 2);
321 PQescapeString(qabout,about,len);
322 len = strlen(uri) + 1;
323 quri = malloc (sizeof(char) * len * 2);
324 PQescapeString(quri,uri,len);
325 printf("INSERT INTO refObj VALUES ('%s', '%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#MainHypothesis', %d);\n",qabout,quri,curr->val);
331 void print_mainconcl(about,uri,depth)
337 /* fprintf(stderr,"about = %s\n",about); */
338 if (!strcmp(uri,"Rel"))
340 size_t len = strlen(about) + 1;
341 char *qabout = malloc (sizeof(char) * len * 2);
342 PQescapeString(qabout,about,len);
343 printf("INSERT INTO refRel VALUES ('%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#MainConclusion', %d);\n",qabout,depth);
345 else if ((!strcmp(uri,"Prop")) || (!strcmp(uri,"Type")) ||
346 (!strcmp(uri,"Set")))
348 size_t len = strlen(about) + 1;
349 char *qabout = malloc (sizeof(char) * len * 2);
350 PQescapeString(qabout,about,len);
351 printf("INSERT INTO refSort VALUES ('%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#MainConclusion', %d, '%s');\n",qabout,depth,uri);
355 size_t len = strlen(about) + 1;
356 char *qabout = malloc (sizeof(char) * len * 2);
358 PQescapeString(qabout,about,len);
359 len = strlen(uri) + 1;
360 quri = malloc (sizeof(char) * len * 2);
361 PQescapeString(quri,uri,len);
362 printf("INSERT INTO refObj VALUES ('%s', '%s','http://www.cs.unibo.it/helm/schemas/schema-helm#MainConclusion', %d);\n",qabout,quri,depth);
366 // dome: cambiata per usare il modello con position
367 void print_one(about,uri,pos)
372 char *position = (char *)malloc((sizeof('a')*20));
373 size_t len = strlen(about) + 1;
374 char *qabout = malloc (sizeof(char) * len * 2);
376 PQescapeString(qabout,about,len);
377 len = strlen(uri) + 1;
378 quri = malloc (sizeof(char) * len * 2);
379 PQescapeString(quri,uri,len);
382 else if (pos == MAINHYP)
383 position="MainHypothesis"; /* This should never happen */
384 else if (pos == INHYP)
385 position="InHypothesis";
386 else if (pos == INCONCL)
387 position="InConclusion";
388 else if (pos == MAINCONCL)
389 position="MainConclusion"; /* This should never happen */
390 printf("INSERT INTO refObj VALUES ('%s', '%s', \
391 'http://www.cs.unibo.it/helm/schemas/schema-helm#%s', NULL);\n",qabout,quri,position);
394 /* The following function allocates a bucket for an identifier. */
395 void allocate_bucket(st, id, where)
398 /* pointer to the bucket to be */
406 *st = (struct st_bucket *)malloc(sizeof(struct st_bucket));
407 (*st)->id = (char *)malloc(sizeof('a')*(strlen(id) + 1));
408 strcpy((*st)->id,id);
409 (*st)->main_depth = 0;
410 (*st)->depths = NULL;
411 (*st)->next_st_bucket = NULL;
412 (*st)->all_next = all;
414 for (i = 0; i < 5; ++i)
416 (*st)->pos[where] = 1;
419 /* The following function moves a bucket to the head of the */
420 /* list in which it lies. */
421 void move_bucket(st, dict_index)
424 /* pointer to the bucket to */
427 /* index corresponding to */
428 /* the list in which the */
431 st->next_st_bucket = dictionary[dict_index];
432 dictionary[dict_index] = st;
435 /* The following function implements Weinberger's hash function. */
439 /* identifier to be hashed */
444 for (h = 0; *id != EOS; id++)
446 h = (h << HASH1) + (*id);
448 h = h ^ (g >> HASH3) ^ g;
450 return(h % DICTSIZE);