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 size_t len = strlen(uri) + 1;
253 char *quri = malloc (sizeof(char) * len * 2);
254 PQescapeString(quri,uri,len);
255 len = strlen(name) + 1;
256 char *qname = malloc (sizeof(char) * len * 2);
257 PQescapeString(qname,name,len);
258 printf("INSERT INTO objectName VALUES ('%s', '%s');\n",quri,qname);
263 /****************************************************************/
264 /* 5. Definitions of functions local to the module. */
265 /****************************************************************/
267 struct int_list *add(l,m)
271 struct int_list *curr;
272 /* scan the list looking for m */
274 while ((curr != NULL) && (m != (curr->val)))
277 /* m is not in the list */
279 curr = (struct int_list *)malloc(sizeof(struct int_list));
289 void print_mainhyp(about,uri,l)
294 struct int_list *curr;
296 if (!strcmp(uri,"Rel"))
301 size_t len = strlen(about) + 1;
302 char *qabout = malloc (sizeof(char) * len * 2);
303 PQescapeString(qabout,about,len);
304 printf("INSERT INTO refRel VALUES ('%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#MainHypothesis', %d);\n",qabout,curr->val);
309 else if ((!strcmp(uri,"Prop")) || (!strcmp(uri,"Type")) ||
310 (!strcmp(uri,"Set")))
315 size_t len = strlen(about) + 1;
316 char *qabout = malloc (sizeof(char) * len * 2);
317 PQescapeString(qabout,about,len);
318 printf("INSERT INTO refSort VALUES ('%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#MainHypothesis', %d, '%s');\n",qabout,curr->val,uri);
328 size_t len = strlen(about) + 1;
329 char *qabout = malloc (sizeof(char) * len * 2);
331 PQescapeString(qabout,about,len);
332 len = strlen(uri) + 1;
333 quri = malloc (sizeof(char) * len * 2);
334 PQescapeString(quri,uri,len);
335 printf("INSERT INTO refObj VALUES ('%s', '%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#MainHypothesis', %d);\n",qabout,quri,curr->val);
343 void print_mainconcl(about,uri,depth)
349 /* fprintf(stderr,"about = %s\n",about); */
350 if (!strcmp(uri,"Rel"))
352 size_t len = strlen(about) + 1;
353 char *qabout = malloc (sizeof(char) * len * 2);
354 PQescapeString(qabout,about,len);
355 printf("INSERT INTO refRel VALUES ('%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#MainConclusion', %d);\n",qabout,depth);
358 else if ((!strcmp(uri,"Prop")) || (!strcmp(uri,"Type")) ||
359 (!strcmp(uri,"Set")))
361 size_t len = strlen(about) + 1;
362 char *qabout = malloc (sizeof(char) * len * 2);
363 PQescapeString(qabout,about,len);
364 printf("INSERT INTO refSort VALUES ('%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#MainConclusion', %d, '%s');\n",qabout,depth,uri);
369 size_t len = strlen(about) + 1;
370 char *qabout = malloc (sizeof(char) * len * 2);
372 PQescapeString(qabout,about,len);
373 len = strlen(uri) + 1;
374 quri = malloc (sizeof(char) * len * 2);
375 PQescapeString(quri,uri,len);
376 printf("INSERT INTO refObj VALUES ('%s', '%s','http://www.cs.unibo.it/helm/schemas/schema-helm#MainConclusion', %d);\n",qabout,quri,depth);
382 // dome: cambiata per usare il modello con position
383 void print_one(about,uri,pos)
388 char *position = (char *)malloc((sizeof('a')*20));
389 size_t len = strlen(about) + 1;
390 char *qabout = malloc (sizeof(char) * len * 2);
392 PQescapeString(qabout,about,len);
393 len = strlen(uri) + 1;
394 quri = malloc (sizeof(char) * len * 2);
395 PQescapeString(quri,uri,len);
398 else if (pos == MAINHYP)
399 position="MainHypothesis"; /* This should never happen */
400 else if (pos == INHYP)
401 position="InHypothesis";
402 else if (pos == INCONCL)
403 position="InConclusion";
404 else if (pos == MAINCONCL)
405 position="MainConclusion"; /* This should never happen */
406 printf("INSERT INTO refObj VALUES ('%s', '%s', \
407 'http://www.cs.unibo.it/helm/schemas/schema-helm#%s', NULL);\n",qabout,quri,position);
412 /* The following function allocates a bucket for an identifier. */
413 void allocate_bucket(st, id, where)
416 /* pointer to the bucket to be */
424 *st = (struct st_bucket *)malloc(sizeof(struct st_bucket));
425 (*st)->id = (char *)malloc(sizeof('a')*(strlen(id) + 1));
426 strcpy((*st)->id,id);
427 (*st)->main_depth = 0;
428 (*st)->depths = NULL;
429 (*st)->next_st_bucket = NULL;
430 (*st)->all_next = all;
432 for (i = 0; i < 5; ++i)
434 (*st)->pos[where] = 1;
437 /* The following function moves a bucket to the head of the */
438 /* list in which it lies. */
439 void move_bucket(st, dict_index)
442 /* pointer to the bucket to */
445 /* index corresponding to */
446 /* the list in which the */
449 st->next_st_bucket = dictionary[dict_index];
450 dictionary[dict_index] = st;
453 /* The following function implements Weinberger's hash function. */
457 /* identifier to be hashed */
462 for (h = 0; *id != EOS; id++)
464 h = (h << HASH1) + (*id);
466 h = h ^ (g >> HASH3) ^ g;
468 return(h % DICTSIZE);