~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina4/src/sys/gen/ts_save_load.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* ts_io.c 7/3/94 Stuart Cornell */
  2 /* The functions in the file are for saving and loading tina data structure */
  3 /* trees generically. Also for comparing and updating id format versions */
  4 
  5 #include <tina/sys.h>
  6 #include <tina/sysfuncs.h>
  7 #include <time.h>
  8 #include <string.h>
  9 
 10 static char tdi_version[25]="1.1"; /* The current tsid version  */
 11 
 12 /* Return the current version string for the Ts_id array */
 13 char *ts_version_get(void)
 14 {
 15   return tdi_version;
 16 }
 17 
 18 /* Write the Tina data tree at 'data' to a FILE stream*/
 19 int ts_fsave(void *data, FILE *fp)
 20 {
 21   List *s=NULL;
 22   int count;
 23   
 24   if(fp)
 25   {
 26       fprintf(fp,"Tina Structure Data. Version %s\n",ts_version_get());
 27       s=ts_serialise_gen(s,data);            /* produce the serial list */
 28       count=ts_serialise_end();              /* get the item count */
 29       fwrite_ts(fp,s,count);         /* write to file */
 30       return 1;
 31   }
 32   return 0;
 33 }
 34 /* Write the Tina data tree at 'data' to a filename constructed from  */
 35 /* 'path' + 'name' + an appropriate extension. */
 36 int ts_save(void *data, char *path, char *name)
 37 {
 38   char fname[256];
 39   FILE *fp;
 40   List *s=NULL;
 41 
 42   sprintf(fname,"%s/%s.tsd",path,name);
 43   fp=fopen(fname,"wb");
 44   if(fp)
 45   {
 46       int r=ts_fsave(data,fp);
 47       fclose(fp);
 48       return r;
 49   }
 50   else format("ERROR: Could not open file %s\n",fname);
 51   return 0;
 52 }
 53 
 54 /* Read the Tina data tree into pointer 'dp' from a FILE stream */
 55 int ts_fload(void **dp,FILE *fp )
 56 {
 57   List *s=NULL;
 58   int count;
 59   char version[25];
 60 
 61   if(fp)
 62     {
 63       int toks=fscanf(fp,"Tina Structure Data. Version %s\n",version);
 64       if(toks!=1)               /* file reading error */
 65         {
 66           *(dp)=NULL;
 67           return toks;
 68         }
 69       s=fread_ts(fp,&count);         /* Read the serial list */
 70       if(strcmp(version,ts_version_get()))   /* if old version */
 71         ts_upgrade_data(s,version);          /* upgrade ids to current Ver */
 72       s=ts_deserialise_gen(s,dp);            /* produce tree from serial list */
 73       return 1;
 74     }
 75   return 0;
 76 }
 77 /* Read the Tina data tree into pointer 'dp' from a filename constructed from  */
 78 /* 'path' + 'name' + an appropriate extension. */
 79 int ts_load(void **dp, char *path, char *name)
 80 {
 81   char fname[256];
 82   FILE *fp;
 83   List *s=NULL;
 84 
 85   sprintf(fname,"%s/%s.tsd",path,name);
 86   fp=fopen(fname,"rb");
 87   if(fp)
 88     {
 89       int r=ts_fload(dp,fp);
 90       fclose(fp);
 91       return r;
 92     }
 93   else format("ERROR: Could not open file %s\n",fname);
 94   return 0;
 95 }
 96 
 97 /* Save a Tina_data_info array into a filename constructed from 'path'*/
 98 int ts_table_save(char *path, Tina_data_info *tdi, int size)
 99 {
100   char fname[256];
101   FILE *fp;
102   int id;
103 
104   sprintf(fname,"%s/tstable.%s",path,ts_version_get());
105   fp=fopen(fname,"wt");
106   if(fp)
107     {
108       time_t tm=time(NULL);
109       fprintf(fp,"Tina Structure Data Table. Version %s\n%s",
110               ts_version_get(),asctime(gmtime(&tm)));
111       fprintf(fp,"size=%i\n",size);          /* write the header */
112       for(id=0; id<size; id++)               /* for each id in table */
113         fprintf(fp,"%i\t%s\t%s\n",           /* write out the */
114                 id,                          /* relative id no */
115                 tdi[id].name,                /* the structure name */
116                 tdi[id].swap_string);        /* and the swapstring */
117       fclose(fp);
118       return 1;
119     }
120   else format("ERROR: Could not open file %s\n",fname);
121   return 0;
122 }
123 
124 /* load in a TDI table from a filename made from path + version */
125 /* Return the newly ralloced table and set the size */
126 Tina_data_info *ts_table_load(char *path,char *version, int *size)
127 {
128   char fname[256],version_read[25],date_read[256],lstore[256];
129   FILE *fp;
130   Tina_data_info *tdi;
131   
132   sprintf(fname,"%s/tstable.%s",path,version);
133   fp=fopen(fname,"rt");
134   if(fp)
135     {
136       int id,ids;
137 
138       fscanf(fp,"Tina Structure Data Table. Version %s\n",version_read);
139       fgets(date_read,256,fp);
140       fscanf(fp,"size=%i\n",&ids);
141 #ifdef DEBUG
142       format("Reading table Version %s %s Ts ids %i\n",
143              version_read,date_read,ids);
144 #endif
145       *size=ids;                             /*Set the table size for caller */
146       tdi=(Tina_data_info*)ralloc(sizeof(Tina_data_info)*ids);
147       while(ids-- && !feof(fp))              /* for all ids + while in file */
148         {
149           char *name2,*ss2;                  /* the new name and ss */
150           char *line=lstore;
151           char *delim;                       /* delimiter pointer ->'\t' */
152 
153           fgets(line,256,fp);                /* read a line */
154           delim=strchr(line,'\t');           /* find the first tab */
155           *delim=0;                          /* terminate the string at it */
156           id=atoi(line);                     /* convert the string to id no */
157 
158           line=delim+1;                      /* move to next token */
159           delim=strchr(line,'\t');           /* find the next tab */
160           *delim++=0;                        /* terminate the string at it */
161           name2=ralloc(delim-line);          /* alocate the string space */
162           strcpy(name2,line);                /* copy the structure name */
163 
164           line=delim;                        /* move to last token */
165           delim=strchr(line,'\n');           /* find the end of line */
166           *delim++=0;                        /* terminate string at it */
167           ss2=ralloc(delim-line);            /* copy the swap string */
168           strcpy(ss2,line);
169 
170 #ifdef DEBUG
171           format("ID %i %s <%s>\n",id,name2,ss2);
172 #endif
173           tdi[id].name=name2;                /* set the fields if the tdi */
174           tdi[id].swap_string=ss2;           /* array with name and ss */
175         }
176       fclose(fp);
177       return tdi;
178     }
179   else
180     fprintf(stderr,"ERROR: Cannot open file %s\n",fname);
181   return NULL;
182 }
183 
184 /* Build a cross reference table to look up then new ids from the old */
185 /* o_size is the size of the old table and n_size the new */
186 /* Returns an array of Ts_id's which when indexed by old id */
187 /* gives the appropriate new id or -1 if non-exists */
188 Ts_id *tdi_xref(Tina_data_info *tdi_old, int o_size,
189                 Tina_data_info *tdi_new, int n_size)
190 {
191     Ts_id *reft=((Ts_id*)ralloc(sizeof(Ts_id)*o_size));
192     int co,cn,upper;
193     
194     upper= MAX(o_size,n_size);
195     for(co=0; co<o_size; co++)
196     if(co > upper) reft[co]=-1;              /* error if outside new table */
197                                              /* else is the structure name diff */
198     else if( strcmp( tdi_old[co].name, tdi_new[co].name ))
199     {           
200         mprintf("WARNING: Looking for <%s> in new tables\n",tdi_old[co].name);
201         for(cn=0; cn<n_size; cn++)           /* find name in new table */
202         if(!strcmp( tdi_old[co].name, tdi_new[cn].name ))
203         {
204             reft[co]=cn+ts_base_id;          /* set xreference */
205             break;                           /* drop out of search */
206         }
207         if(cn==n_size)                       /* didn't find a match */
208         {
209             reft[co]=0;                      /* set to flag error */
210             mprintf("ERROR: Couln't find a ts match for <%s> in current version\n",
211                     tdi_old[co].name);
212         }
213     }
214     /* else is the ss different */
215     else if( strcmp( tdi_old[co].swap_string, tdi_new[co].swap_string ))
216     {
217         reft[co]=0;                          /* then flag error */
218         mprintf("WARNING: Swap string is different for <%s> OLD<%s>!=NEW<%s>\n",
219                 tdi_old[co].name,tdi_old[co].swap_string, tdi_new[co].swap_string );
220     }
221     return reft;
222 }
223 
224 /* Translate the data in the serial list from tdi_old type to tdi_new*/
225 /* o_size is the size of the old table and n_size the new */
226 /* Returns 0 if ok !0 if not */
227 int ts_version_translate(List *lg, 
228                           Tina_data_info *tdi_old, int o_size,
229                           Tina_data_info *tdi_new, int n_size)
230 {
231   List *l;
232   int err=0;
233   Ts_id *reft;
234 
235   reft=tdi_xref(tdi_old,o_size,tdi_new,n_size); /* make xref table */
236   reft-=ts_base_id;
237   for(l=lg; l; l=l->next)                    /* for serial list members */
238     {                                        /* pickup id and pointer */
239       Ts_id id=*(Ts_id*)lg->to, *idp=(Ts_id*)lg->to;
240       if(lg->type>0 && reft[id])             /* if new pointer and no error */
241         *idp=reft[id];                       /* set new id from xref table */
242       else err++;                            /* else flag an error */
243   }
244   rfree(reft+ts_base_id);
245   return err==0;
246 }
247 
248 /* Translate the data in the serial list from version 'version' to the */
249 /* current Ts_id system . Return an error code !0 = error if unsuccesful*/
250 int ts_upgrade_data(List *l, char *version)
251 {
252   char *dir=getenv("TS_VERSION_DIR");        /* find the version data dir */
253   int o_size;
254   Tina_data_info *tdi_old;                   /* a tdi table to match data */
255 
256   format("WARNING: Upgrading data from version %s\n",version);
257 
258   if(!dir) 
259   {
260       format("WARNING: You have not set $TS_VERSION_DIR. \n"
261              "WARNING: Trying current directory\n");
262       dir=".";                       /* no env so use current dir */
263   }
264   tdi_old=ts_table_load(dir,version,&o_size); /* read the old version table */
265                                              /* translate the ids in the  */
266                                              /* data to match the current */
267                                              /* ids in this version */
268   if(tdi_old)
269   return ts_version_translate(l, tdi_old, o_size, 
270                               tina_data_info_array, ts_last_id-ts_base_id);
271                                 /* and return the error flag */
272   else return -1;
273 }
274 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.