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

Linux Cross Reference
Tina6/tina-libs/tina/file/fileTs_save_load.c

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

  1 /**********
  2  * 
  3  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  4  * University of Manchester, UK.  All rights reserved.
  5  * 
  6  * Redistribution and use in source and binary forms, with or without modification, 
  7  * are permitted provided that the following conditions are met:
  8  * 
  9  *   . Redistributions of source code must retain the above copyright notice, 
 10  *     this list of conditions and the following disclaimer.
 11  *    
 12  *   . Redistributions in binary form must reproduce the above copyright notice,
 13  *     this list of conditions and the following disclaimer in the documentation 
 14  *     and/or other materials provided with the distribution.
 15  * 
 16  *   . Neither the name of the University of Manchester nor the names of its
 17  *     contributors may be used to endorse or promote products derived from this 
 18  *     software without specific prior written permission.
 19  * 
 20  * 
 21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  * POSSIBILITY OF SUCH DAMAGE.
 32  *
 33  **********
 34  * 
 35  * Program :    TINA
 36  * File    :  $Source: /home/tina/cvs/tina-libs/tina/file/fileTs_save_load.c,v $
 37  * Date    :  $Date: 2005/01/09 17:49:25 $
 38  * Version :  $Revision: 1.4 $
 39  * CVS Id  :  $Id: fileTs_save_load.c,v 1.4 2005/01/09 17:49:25 paul Exp $
 40  *
 41  * Author  :   Stuart Cornell 
 42  *
 43  * Notes :
 44  *
 45  *********
 46 */
 47 
 48 #include "fileTs_save_load.h"
 49 
 50 #if HAVE_CONFIG_H
 51   #include <config.h>
 52 #endif
 53 
 54 #include <stdlib.h>
 55 #include <time.h>
 56 #include <string.h>
 57 #include <tina/sys/sysDef.h>
 58 #include <tina/sys/sysPro.h>
 59 #include <tina/file/file_TsDef.h>
 60 #include <tina/file/fileTs_io.h>
 61 #include <tina/file/fileTs_deserialise.h>
 62 #include <tina/file/fileTs_serialise.h>
 63 
 64 
 65 static char tdi_version[25] = "1.1";    /* The current tsid version  */         /* static data! */
 66 
 67 /* Return the current version string for the Ts_id array */
 68 char *ts_version_get(void)
 69 {
 70         return tdi_version;
 71 }
 72 
 73 /* Write the Tina data tree at 'data' to a FILE stream*/
 74 int ts_fsave(void *data, FILE * fp)
 75 {
 76         List *s = NULL;
 77         int count;
 78 
 79         if (fp)
 80         {
 81                 fprintf(fp, "Tina Structure Data. Version %s\n", ts_version_get());
 82                 s = ts_serialise_gen(s, data);  /* produce the serial list */
 83                 count = ts_serialise_end();     /* get the item count */
 84                 fwrite_ts(fp, s, count);                /* write to file */
 85                 return 1;
 86         }
 87         return 0;
 88 }
 89 
 90 /* Write the Tina data tree at 'data' to a filename constructed from  */
 91 /* 'path' + 'name' + an appropriate extension. */
 92 int ts_save(void *data, char *path, char *name)
 93 {
 94         char fname[256];
 95         FILE *fp;
 96 
 97         sprintf(fname, "%s/%s.tsd", path, name);
 98         fp = fopen(fname, "wb");
 99         if (fp)
100         {
101                 int r = ts_fsave(data, fp);
102                 fclose(fp);
103                 return r;
104         } else
105                 format("ERROR: Could not open file %s\n", fname);
106         return 0;
107 }
108 
109 /* Read the Tina data tree into pointer 'dp' from a FILE stream */
110 int ts_fload(void **dp, FILE * fp)
111 {
112         List *s = NULL;
113         int count;
114         char version[25];
115 
116         if (fp)
117         {
118                 int toks = fscanf(fp, "Tina Structure Data. Version %s\n", version);
119                 if (toks != 1)                                                  /* file reading error */
120                 {
121                         *(dp) = NULL;
122                         return toks;
123                 }
124                 s = fread_ts(fp, &count);               /* Read the serial list */
125                 if (strcmp(version, ts_version_get()))  /* if old version */
126                         ts_upgrade_data(s, version);    /* upgrade ids to current Ver */
127                 s = ts_deserialise_gen(s, dp);  /* produce tree from serial list */
128                 return 1;
129         }
130         return 0;
131 }
132 
133 /* Read the Tina data tree into pointer 'dp' from a filename constructed from  */
134 /* 'path' + 'name' + an appropriate extension. */
135 int ts_load(void **dp, char *path, char *name)
136 {
137         char fname[256];
138         FILE *fp;
139 
140         sprintf(fname, "%s/%s.tsd", path, name);
141         fp = fopen(fname, "rb");
142         if (fp)
143         {
144                 int r = ts_fload(dp, fp);
145                 fclose(fp);
146                 return r;
147         } else
148                 format("ERROR: Could not open file %s\n", fname);
149         return 0;
150 }
151 
152 /* Save a Tina_data_info array into a filename constructed from 'path'*/
153 int ts_table_save(char *path, Tina_data_info * tdi, int size)
154 {
155         char fname[256];
156         FILE *fp;
157         int id;
158 
159         sprintf(fname, "%s/tstable.%s", path, ts_version_get());
160         fp = fopen(fname, "wt");
161         if (fp)
162         {
163                 time_t tm = time(NULL);
164                 fprintf(fp, "Tina Structure Data Table. Version %s\n%s",
165                                                 ts_version_get(), asctime(gmtime(&tm)));
166                 fprintf(fp, "size=%i\n", size); /* write the header */
167                 for (id = 0; id < size; id++)   /* for each id in table */
168                         fprintf(fp, "%i\t%s\t%s\n",     /* write out the */
169                                                         id,                                                             /* relative id no */
170                                                         tdi[id].name,                   /* the structure name */
171                                                         tdi[id].swap_string);   /* and the swapstring */
172                 fclose(fp);
173                 return 1;
174         } else
175                 format("ERROR: Could not open file %s\n", fname);
176         return 0;
177 }
178 
179 /* load in a TDI table from a filename made from path + version */
180 /* Return the newly ralloced table and set the size */
181 Tina_data_info *ts_table_load(char *path, char *version, int *size)
182 {
183         char fname[256], version_read[25], date_read[256], lstore[256];
184         FILE *fp;
185         Tina_data_info *tdi;
186 
187         sprintf(fname, "%s/tstable.%s", path, version);
188         fp = fopen(fname, "rt");
189         if (fp)
190         {
191                 int id, ids;
192 
193                 fscanf(fp, "Tina Structure Data Table. Version %s\n", version_read);
194                 fgets(date_read, 256, fp);
195                 fscanf(fp, "size=%i\n", &ids);
196 #ifdef DEBUG
197                 format("Reading table Version %s %s Ts ids %i\n",
198                                          version_read, date_read, ids);
199 #endif
200                 *size = ids;                                                            /*Set the table size for caller */
201                 tdi = (Tina_data_info *) ralloc(sizeof(Tina_data_info) * ids);
202                 while (ids-- && !feof(fp))      /* for all ids + while in file */
203                 {
204                         char *name2, *ss2;                              /* the new name and ss */
205                         char *line = lstore;
206                         char *delim;                                                    /* delimiter pointer ->'\t' */
207 
208                         fgets(line, 256, fp);                   /* read a line */
209                         delim = strchr(line, '\t');     /* find the first tab */
210                         *delim = 0;                                                             /* terminate the string at it */
211                         id = atoi(line);                                        /* convert the string to id no */
212 
213                         line = delim + 1;                                       /* move to next token */
214                         delim = strchr(line, '\t');     /* find the next tab */
215                         *delim++ = 0;                                                   /* terminate the string at it */
216                         name2 = ralloc(delim - line);   /* alocate the string space */
217                         strcpy(name2, line);                    /* copy the structure name */
218 
219                         line = delim;                                                   /* move to last token */
220                         delim = strchr(line, '\n');     /* find the end of line */
221                         *delim++ = 0;                                                   /* terminate string at it */
222                         ss2 = ralloc(delim - line);     /* copy the swap string */
223                         strcpy(ss2, line);
224 
225 #ifdef DEBUG
226                         format("ID %i %s <%s>\n", id, name2, ss2);
227 #endif
228                         tdi[id].name = name2;                   /* set the fields if the tdi */
229                         tdi[id].swap_string = ss2;      /* array with name and ss */
230                 }
231                 fclose(fp);
232                 return tdi;
233         } else
234                 fprintf(stderr, "ERROR: Cannot open file %s\n", fname);
235         return NULL;
236 }
237 
238 
239 /* Build a cross reference table to look up then new ids from the old */
240 /* o_size is the size of the old table and n_size the new */
241 /* Returns an array of Ts_id's which when indexed by old id */
242 /* gives the appropriate new id or -1 if non-exists */
243 Ts_id *tdi_xref(Tina_data_info * tdi_old, int o_size,
244                                                                 Tina_data_info * tdi_new, int n_size)
245 {
246         Ts_id *reft = ((Ts_id *) ralloc(sizeof(Ts_id) * o_size));
247         int co, cn, upper;
248 
249         upper = MAX(o_size, n_size);
250         for (co = 0; co < o_size; co++)
251                 if (co > upper)
252                         reft[co] = -1;                                          /* error if outside new table */
253         /* else is the structure name diff */
254                 else if (strcmp(tdi_old[co].name, tdi_new[co].name))
255                 {
256                         mprintf("WARNING: Looking for <%s> in new tables\n",
257                                                         tdi_old[co].name);
258                         for (cn = 0; cn < n_size; cn++) /* find name in new table */
259                                 if (!strcmp(tdi_old[co].name, tdi_new[cn].name))
260                                 {
261                                         reft[co] = cn + ts_base_id;     /* set xreference */
262                                         break;                                                          /* drop out of search */
263                                 }
264                         if (cn == n_size)                                       /* didn't find a match */
265                         {
266                                 reft[co] = 0;                                           /* set to flag error */
267                                 mprintf
268                                                 ("ERROR: Couln't find a ts match for <%s> in current version\n",
269                                                  tdi_old[co].name);
270                         }
271                 }
272         /* else is the ss different */
273                 else if (strcmp(tdi_old[co].swap_string, tdi_new[co].swap_string))
274                 {
275                         reft[co] = 0;                                                   /* then flag error */
276                         mprintf
277                                         ("WARNING: Swap string is different for <%s> OLD<%s>!=NEW<%s>\n",
278                                          tdi_old[co].name, tdi_old[co].swap_string,
279                                          tdi_new[co].swap_string);
280                 }
281         return reft;
282 }
283 
284 /* Translate the data in the serial list from tdi_old type to tdi_new*/
285 /* o_size is the size of the old table and n_size the new */
286 /* Returns 0 if ok !0 if not */
287 int ts_version_translate(List * lg,
288                                                                                                  Tina_data_info * tdi_old, int o_size,
289                                                                                                  Tina_data_info * tdi_new, int n_size)
290 {
291         List *l;
292         int err = 0;
293         Ts_id *reft;
294 
295         reft = tdi_xref(tdi_old, o_size, tdi_new, n_size);      /* make xref table */
296         reft -= ts_base_id;
297         for (l = lg; l; l = l->next)    /* for serial list members */
298         {                                                                                                                       /* pickup id and pointer */
299                 Ts_id id = *(Ts_id *) lg->to, *idp = (Ts_id *) lg->to;
300                 if (lg->type > 0 && reft[id])   /* if new pointer and no error */
301                         *idp = reft[id];                                        /* set new id from xref table */
302                 else
303                         err++;                                                                          /* else flag an error */
304         }
305         rfree(reft + ts_base_id);
306         return err == 0;
307 }
308 
309 /* Translate the data in the serial list from version 'version' to the */
310 /* current Ts_id system . Return an error code !0 = error if unsuccesful*/
311 int ts_upgrade_data(List * l, char *version)
312 {
313         char *dir = getenv("TS_VERSION_DIR");   /* find the version data dir */
314         int o_size;
315         Tina_data_info *tdi_old;                        /* a tdi table to match data */
316 
317         format("WARNING: Upgrading data from version %s\n", version);
318 
319         if (!dir)
320         {
321                 format("WARNING: You have not set $TS_VERSION_DIR. \n"
322                                          "WARNING: Trying current directory\n");
323                 dir = ".";                                                                      /* no env so use current dir */
324         }
325         tdi_old = ts_table_load(dir, version, &o_size); /* read the old version table */
326         /* translate the ids in the  */
327         /* data to match the current */
328         /* ids in this version */
329         if (tdi_old)
330                 return ts_version_translate(l, tdi_old, o_size,
331                                                                                                                                 tina_data_info_array,
332                                                                                                                                 ts_last_id - ts_base_id);
333         /* and return the error flag */
334         else
335                 return -1;
336 }
337 

~ [ 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.