Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages   Examples  

dts.c

00001 #include "config.h"
00002 #include "dts.h"
00003 #include "parser.h"    /* just for N_I */
00004 
00005 objref* objref_obtain_local(char* r) {
00006     NOT_IMPLEMENTED;
00007 }
00008 
00009 void objref_release(objref* r) {
00010     NOT_IMPLEMENTED;
00011 }
00012 
00013 
00014 #if 0
00015 
00016     NEEDS TO BE UPDATES WITH ALL THE NAME CHANGES.
00017 
00018 /*
00019   $Id: dts_8c-source.html,v 1.10 2001/10/10 20:40:59 sandro Exp $
00020 */
00021 
00022 #include <assert.h>
00023 #include <stdio.h>
00024 #include <malloc.h>
00025 #include <string.h>
00026 #include <search.h>            /* obviously a hack! */
00027 
00028 
00029 #include "rdf.h"
00030 
00031 
00032 typedef struct add_s {
00033   Symbol subject, predicate, object;
00034   struct add_s *next;
00035 } Add;
00036 
00037 typedef struct include_s {
00038   Symbol inner;
00039   struct include_s *next;
00040 } Include;
00041     
00042 struct _object {
00043   int refcount;
00044   char *swid;
00045   char *litval;          /* fold this into swid with data:? */
00046   char *localname;       /* fold this into swid with genid */
00047   Add *adds;
00048   Include *includes;
00049   int mark;
00050 };
00051 
00052 
00053 void fprint_sym(FILE *f, Symbol s) {
00054     assert(0);
00055 }
00056 
00057 void add(Symbol set, Symbol subject, Symbol predicate, Symbol object) {
00058   Add *previous_adds = set->adds;
00059 
00060   fprintf(stderr, "add: ");
00061   fprint_sym(stderr, set);
00062   fprint_sym(stderr, subject);
00063   fprint_sym(stderr, predicate);
00064   fprint_sym(stderr, object);
00065   fprintf(stderr, "\n");
00066 
00067   set->adds = (Add *) malloc(sizeof(struct add_s));
00068   keepsym(subject);
00069   keepsym(predicate);
00070   keepsym(object);
00071   set->adds->subject = subject;
00072   set->adds->subject = predicate;
00073   set->adds->subject = object;
00074   set->adds->next = previous_adds;
00075 }
00076 
00077 void include(Symbol outer, Symbol inner) {
00078   Include *previous_includes = outer->includes;
00079 
00080   fprintf(stderr, "include:");
00081   fprint_sym(stderr, outer);
00082   fprint_sym(stderr, inner);
00083   fprintf(stderr, "\n");
00084 
00085   outer->includes = (Include *) malloc(sizeof(struct include_s));
00086   outer->includes->inner = inner;
00087   keepsym(inner);
00088   outer->includes->next = previous_includes;
00089 }
00090 
00091 void keepsym(Symbol s) {
00092   assert(s->refcount > 0);
00093   s->refcount++;
00094 }
00095 
00096 void releasesym(Symbol s) {
00097 
00098   fprintf(stderr, "release:");
00099   fprint_sym(stderr, s);
00100   fprintf(stderr, "\n");
00101 
00102   assert(s->refcount > 0);
00103   s->refcount--;
00104   if (s->refcount) return;
00105 
00106   fprintf(stderr, "--- refcount reached zero, freeing\n");
00107 
00108   free(s->swid);
00109   free(s->litval);
00110 
00111   {
00112     Add *this;
00113     Add *next = s->adds;
00114     while (next) {
00115       this = next;
00116       releasesym(this->subject);
00117       releasesym(this->predicate);
00118       releasesym(this->object);
00119       next = this->next;
00120       free(this);
00121     }
00122   }
00123 
00124   {
00125     Include *this;
00126     Include *next = s->includes;
00127     while (next) {
00128       this = next;
00129       releasesym(this->inner);
00130       next = this->next;
00131       free(this);
00132     }
00133   }
00134 
00135   free(s);
00136   fprintf(stderr, "--- done with releasesym");
00137   fprint_sym(stderr, s);
00138   fprintf(stderr, "\n");
00139 
00140 }
00141 
00142 void reify_statement_sets(Symbol outer_set, Symbol flat_set);
00143 void datafy(Symbol s);
00144 
00145 static int genid_count = 0;
00146 void genify_m(Symbol s) {
00147   if (marked(s)) return;
00148   mark(s);
00149   
00150   {
00151     Add *this;
00152     for (this = s->adds; this; this = this->next) {
00153       genify_m(this->subject);
00154       genify_m(this->predicate);
00155       genify_m(this->object);
00156     }
00157   }
00158 
00159   {
00160     Include *this;
00161     for (this = s->includes; this; this = this->next) {
00162       /* it's odd to reach the symbol via inclusion, but
00163      I guess it works.   The included set symbol itself 
00164      would not be genified if we had just used copy. */
00165       genify_m(this->inner);
00166     }
00167   }  
00168 
00169 
00170   /* alternate version 
00171   {
00172     Add *add;
00173     Include *include;
00174     for (add = use_top->adds; add; add = add->next) {
00175       genify_m(add->subject);
00176       genify_m(add->predicate);
00177       genify_m(add->object);
00178     }
00179     for (include = use_top->includes; include; include = include->next) {
00180       genify_m(include->inner);
00181     }
00182   }  
00183   */
00184 
00185   if (!s->swid) {
00186     static char buf[80];
00187     sprintf(buf, "genid:%d", genid_count++);
00188     s->swid = (char *) strdup(buf);
00189   }
00190 
00191 }
00192 
00193 void genify(Symbol s) {
00194   mark_begin();
00195   genify_m(s);
00196   mark_end();
00197 }
00198 
00199 void write_n3(FILE* out, Symbol top);
00200 void write_canonical(FILE* out, Symbol top);
00201 void write_prolog_3(FILE* out, Symbol top, char* predicate);
00202 
00203 void write_prolog_symbol(FILE* out, Symbol s) {
00204   if (s->swid) {
00205     fprintf(out, "'%s'", s->swid);
00206     return;
00207   }
00208   if (s->litval) {
00209     fprintf(out, "\"%s\"", s->litval);
00210     return;
00211   }
00212   assert(0);  /* there shouldn't be any anons at this point */
00213 }
00214 
00215 void write_prolog_4_masq(FILE* out, Symbol show_top, Symbol use_top, 
00216              char* predicate) {
00217   
00218   /* we use marks here to prevent repeatedly describing the same
00219      mentioned set.  it also avoids problems from mention-loops. */
00220   if (marked(use_top)) return;
00221   mark(use_top);
00222 
00223   {
00224     Add *this;
00225     for (this = use_top->adds; this; this = this->next) {
00226       fputs(predicate, out);
00227       fputs("(", out);
00228       write_prolog_symbol(out, show_top);
00229       fputs(", ", out);
00230       write_prolog_symbol(out, this->subject);
00231       fputs(", ", out);
00232       write_prolog_symbol(out, this->predicate);
00233       fputs(", ", out);
00234       write_prolog_symbol(out, this->object);
00235       fputs(")\n", out);
00236 
00237       write_prolog_4_masq(out, this->subject, this->subject, predicate);
00238       write_prolog_4_masq(out, this->predicate, this->predicate, predicate);
00239       write_prolog_4_masq(out, this->object, this->object, predicate);
00240     }
00241 
00242   }
00243 
00244   {
00245     Include *this;
00246     for (this = use_top->includes; this; this = this->next) {
00247       write_prolog_4_masq(out, show_top, this->inner, predicate);
00248     }
00249   }  
00250 
00251 }
00252 
00253 void write_prolog_4(FILE* out, Symbol top, char* predicate) {
00254 
00255   mark_begin();
00256   write_prolog_4_masq(out, top, top, predicate);
00257   mark_end();
00258 }
00259 
00260 static int current_mark=0;
00261 static int marking = 0;
00262 
00263 void mark_begin() {
00264   assert(!marking);
00265   marking = 1;
00266   current_mark++;
00267 }
00268 
00269 int marked(Symbol s) {
00270   return s->mark == current_mark;
00271 }
00272 
00273 void mark(Symbol s) {
00274   s->mark = current_mark;
00275 }
00276 
00277 void mark_end() {
00278   assert(marking);
00279   marking = 0;
00280 }
00281 
00282 
00283 
00284 int count_m(Symbol s) {
00285   int result = 1;
00286   if (marked(s)) return 0;
00287   mark(s);
00288   
00289   {
00290     Add *this;
00291     for (this = s->adds; this; this = this->next) {
00292       result += count_m(this->subject);
00293       result += count_m(this->predicate);
00294       result += count_m(this->object);
00295     }
00296   }
00297 
00298   {
00299     Include *this;
00300     for (this = s->includes; this; this = this->next) {
00301       /* it's odd to reach the symbol via inclusion, but
00302      I guess it works.   The included set symbol itself 
00303      would not be genified if we had just used copy. */
00304       result += count_m(this->inner);
00305     }
00306   }  
00307 
00308   return result;
00309 }
00310 
00311 int count(Symbol s) {
00312   int result;
00313   mark_begin();
00314   result = count_m(s);
00315   mark_end();
00316   return result;
00317 }
00318 
00319 /****************************************************************
00320 
00321     hack temporary implementation, using hsearch
00322 
00323 ****************************************************************/
00324 
00325 #include <search.h>
00326 
00327 #define BUFLEN 16384
00328 static char buf[BUFLEN];
00329 static int h_created = 0;
00330 
00331 static Symbol makesym() {
00332   Symbol s = (Symbol) malloc(sizeof(struct _object));
00333   s->refcount = 1;
00334   s->swid = 0;
00335   s->litval = 0;
00336   s->adds = 0;
00337   s->includes = 0;
00338   return s;
00339 }
00340 
00341 Symbol obtain_anon() {
00342   return makesym();
00343 }
00344 
00345 static Symbol obtain(char type, char *name, int *created_p) {
00346   ENTRY e, *ep;
00347   if (snprintf(buf, BUFLEN, "%c %s", type, name) == -1) {
00348     fprintf(stderr, "string too long\n"); abort(0);
00349   }
00350   if (!h_created) { hcreate(100);  h_created++; }
00351   e.key = buf;
00352   ep = hsearch(e, FIND);
00353   if (ep) { *created_p = 0; return (Symbol) ep->data; }
00354   e.key = strdup(buf);
00355   e.data = (char*) obtain_anon();
00356   *created_p = 1;
00357   hsearch(e, ENTER);
00358   return (Symbol) e.data;
00359 }
00360 
00361 Symbol obtain_global(char *name) {
00362   int created;
00363   Symbol result = obtain('g', name, &created);
00364   if (created) {
00365     result->swid = strdup(name);
00366   }
00367   return result;
00368 }
00369 
00370 Symbol obtain_local(char *name) {
00371   int created;
00372   Symbol result = obtain('g', name, &created);
00373   if (created) {
00374     result->localname = strdup(name);
00375   }
00376   return result;
00377 }
00378 
00379 Symbol obtain_literal_utf8(char *text) {
00380   int created;
00381   Symbol result = obtain('g', text, &created);
00382   if (created) {
00383     result->litval = strdup(text);
00384   }
00385   return result;
00386 }
00387 
00388 #endif

Home to blindfold. This page generated via doxygen 1.2.11.1 Wed Oct 10 16:40:33 2001.