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 /****************************************************************/
45 #include <postgresql/libpq-fe.h>
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);
98 /****************************************************************/
99 /* 5. Definitions of functions to be exported. */
100 /****************************************************************/
102 struct st_bucket *all;
104 /* The following function initializes the symbol table to NULL */
105 void init_symbol_table()
107 struct st_bucket *st;
110 /* initialize the dictionary */
111 for (i = 0; i < DICTSIZE; i++)
112 dictionary[i] = NULL;
116 /* The following function searches the symbol table for an identifier */
117 /* and inserts it if it is not present.
118 /* The bucket associated with the given identifier */
119 /* becomes the first one in its list. */
121 search_bucket(id, where, depth)
128 /* value returned by the */
134 struct st_bucket *st;
136 /* apply the hash function */
137 dict_index = hash_pjw(id);
138 /* fprintf(stderr,"%d\n", dict_index); */
140 /* scan the bucket list indicated by the hash function */
141 prev = curr = dictionary[dict_index];
142 while ((curr != NULL) && (strcmp(id, curr->id)))
145 curr = curr->next_st_bucket;
148 /* the identifier is not in the list */
150 allocate_bucket(&st,id,where);
151 if (where == MAINCONCL)
152 st->main_depth = depth;
153 else if (where == MAINHYP)
154 st->depths = add(st->depths,depth);
155 move_bucket(st,dict_index);
160 printf("uno=%s\n", id);
161 printf("st=%s\n", curr->id); fflush(stdout) */
163 /* the identifier is already in the list */
166 curr->pos[where] = 1;
168 curr->pos[INBODY] = 0; /* it will never be set again to 1 */
169 if (where == MAINHYP)
170 curr->depths=add(curr->depths,depth);
171 else if (where == MAINCONCL)
172 curr->main_depth = depth;
174 /* the identifier is not in the first position */
176 prev->next_st_bucket = curr->next_st_bucket;
177 move_bucket(curr,dict_index);
183 print_all(about,conn)
189 struct st_bucket *curr;
193 for (i = 0; i < 5; ++i)
194 if ((curr->pos[i]) == 1)
197 print_mainhyp(about,conn,curr->id,curr->depths);
198 else if (i == MAINCONCL)
199 print_mainconcl(about,conn,curr->id,curr->main_depth);
201 print_one(conn,about,curr->id,i);
203 curr = curr->all_next;
208 /****************************************************************/
209 /* 5. Definitions of functions local to the module. */
210 /****************************************************************/
212 void exit_nicely(PGconn *conn)
218 struct int_list *add(l,m)
222 struct int_list *curr;
223 /* scan the list looking for m */
225 while ((curr != NULL) && (m != (curr->val)))
228 /* m is not in the list */
230 curr = (struct int_list *)malloc(sizeof(struct int_list));
240 print_mainhyp(about,conn,uri,l)
247 char *command = (char *)malloc((sizeof('a')*200));
248 struct int_list *curr;
250 if (!strcmp(uri,"Rel"))
255 size_t len = strlen(about) + 1;
256 char *qabout = malloc (sizeof(char) * len * 2);
257 PQescapeString(qabout,about,len);
258 sprintf(command,"INSERT INTO refRel values ('%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#MainHypothesis', %d)",qabout,curr->val);
259 /* fprintf(stderr, "%s\n", command); */
260 res = PQexec(conn, command);
261 if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
263 fprintf(stderr, "BEGIN command failed\n");
270 else if ((!strcmp(uri,"Prop")) || (!strcmp(uri,"Type")) ||
271 (!strcmp(uri,"Set")))
276 size_t len = strlen(about) + 1;
277 char *qabout = malloc (sizeof(char) * len * 2);
278 PQescapeString(qabout,about,len);
279 sprintf(command,"INSERT INTO refSort values ('%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#MainHypothesis', %d, '%s')",qabout,curr->val,uri);
280 /* fprintf(stderr, "%s\n", command); */
281 res = PQexec(conn, command);
282 if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
284 fprintf(stderr, "BEGIN command failed\n");
296 size_t len = strlen(about) + 1;
297 char *qabout = malloc (sizeof(char) * len * 2);
299 PQescapeString(qabout,about,len);
300 len = strlen(uri) + 1;
301 quri = malloc (sizeof(char) * len * 2);
302 PQescapeString(quri,uri,len);
303 sprintf(command,"INSERT INTO refObj values ('%s', '%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#MainHypothesis', %d)",qabout,quri,curr->val);
304 /* fprintf(stderr, "%s\n", command); */
305 res = PQexec(conn, command);
306 if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
308 fprintf(stderr, "BEGIN command failed\n");
317 print_mainconcl(about,conn,uri,depth)
325 char *command = (char *)malloc((sizeof('a')*200));
326 /* fprintf(stderr,"about = %s\n",about); */
327 if (!strcmp(uri,"Rel"))
329 size_t len = strlen(about) + 1;
330 char *qabout = malloc (sizeof(char) * len * 2);
331 PQescapeString(qabout,about,len);
332 sprintf(command,"INSERT INTO refRel values ('%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#MainConclusion', %d)",qabout,depth);
333 /* fprintf(stderr, "%s\n", command); */
334 res = PQexec(conn, command);
336 else if ((!strcmp(uri,"Prop")) || (!strcmp(uri,"Type")) ||
337 (!strcmp(uri,"Set")))
339 size_t len = strlen(about) + 1;
340 char *qabout = malloc (sizeof(char) * len * 2);
341 PQescapeString(qabout,about,len);
342 sprintf(command,"INSERT INTO refSort values ('%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#MainConclusion', %d, '%s')",qabout,depth,uri);
343 /* fprintf(stderr, "%s\n", command); */
344 res = PQexec(conn, command);
348 size_t len = strlen(about) + 1;
349 char *qabout = malloc (sizeof(char) * len * 2);
351 PQescapeString(qabout,about,len);
352 len = strlen(uri) + 1;
353 quri = malloc (sizeof(char) * len * 2);
354 PQescapeString(quri,uri,len);
355 sprintf(command,"INSERT INTO refObj values ('%s', '%s','http://www.cs.unibo.it/helm/schemas/schema-helm#MainConclusion', %d)",qabout,quri,depth);
356 /* fprintf(stderr, "%s\n", command); */
357 res = PQexec(conn, command);
359 if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
361 fprintf(stderr, "BEGIN command failed\n");
365 /* fprintf(stderr,"FINITO\n"); */
368 // dome: cambiata per usare il modello con position
369 print_one(conn,about,uri,pos)
376 char *command = (char *)malloc((sizeof('a')*200));
377 char *position = (char *)malloc((sizeof('a')*20));
378 size_t len = strlen(about) + 1;
379 char *qabout = malloc (sizeof(char) * len * 2);
381 PQescapeString(qabout,about,len);
382 len = strlen(uri) + 1;
383 quri = malloc (sizeof(char) * len * 2);
384 PQescapeString(quri,uri,len);
387 else if (pos == MAINHYP)
388 position="MainHypothesis"; /* This should never happen */
389 else if (pos == INHYP)
390 position="InHypothesis";
391 else if (pos == INCONCL)
392 position="InConclusion";
393 else if (pos == MAINCONCL)
394 position="MainConclusion"; /* This should never happen */
395 sprintf(command,"INSERT INTO refObj values ('%s', '%s', 'http://www.cs.unibo.it/helm/schemas/schema-helm#%s')",qabout,quri,position);
396 /* fprintf(stderr, "%s\n", command); */
397 res = PQexec(conn, command);
398 if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
400 fprintf(stderr, "BEGIN command failed\n");
406 /* The following function allocates a bucket for an identifier. */
407 allocate_bucket(st, id, where)
410 /* pointer to the bucket to be */
418 *st = (struct st_bucket *)malloc(sizeof(struct st_bucket));
419 (*st)->id = (char *)malloc(sizeof('a')*(strlen(id) + 1));
420 strcpy((*st)->id,id);
421 (*st)->main_depth = 0;
422 (*st)->depths = NULL;
423 (*st)->next_st_bucket = NULL;
424 (*st)->all_next = all;
426 for (i = 0; i < 5; ++i)
428 (*st)->pos[where] = 1;
431 /* The following function moves a bucket to the head of the */
432 /* list in which it lies. */
433 move_bucket(st, dict_index)
436 /* pointer to the bucket to */
439 /* index corresponding to */
440 /* the list in which the */
443 st->next_st_bucket = dictionary[dict_index];
444 dictionary[dict_index] = st;
447 /* The following function implements Weinberger's hash function. */
451 /* identifier to be hashed */
456 for (h = 0; *id != EOS; id++)
458 h = (h << HASH1) + (*id);
460 h = h ^ (g >> HASH3) ^ g;
462 return(h % DICTSIZE);