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

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

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

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <tina/sys.h>
  4 #include <tina/sysfuncs.h>
  5 #include <string.h>
  6 
  7 #define SUBSTRUCT_ALIGNMENT 4
  8 #define ALIGN_VARPTR(start,here,size) \
  9 { \
 10    int tab=(int)((here).char_v-(char*)(start))%(size);\
 11    (here).char_v+=tab?(size)-tab:0;\
 12    }
 13 
 14 static List *serialise_parse(List * lg, void *data, Varptr dp, char **ss);
 15 
 16 static List *serialise_parse(List * lg, void *data, Varptr dp, char **ss)
 17 {
 18     char   *s = *ss;
 19     int     repeats = 0, exiting = 0;
 20     static Varptr d, nulld;
 21 
 22     nulld.char_v = NULL;
 23     if (dp.char_v)
 24         d = dp;                 /* if dp is null use static version d */
 25     /* else use new value */
 26 
 27     while (*s && !exiting)
 28     {
 29         switch (*s++)
 30         {
 31         case '':
 32             repeats *= 10;
 33             break;
 34         case '1':
 35         case '2':
 36         case '3':
 37         case '4':
 38         case '5':
 39         case '6':
 40         case '7':
 41         case '8':
 42         case '9':
 43             repeats *= 10;
 44             repeats += *(s - 1) - 48;
 45             break;
 46         case '[':
 47         case '(':               /* start of a repetition */
 48             {
 49                 char   *temp_s = s;
 50 
 51                 while (repeats-- > 0)
 52                 {
 53                     temp_s = s;
 54                     lg = serialise_parse(lg, data, nulld, &temp_s);
 55                 }
 56                 s = temp_s;     /* the rest of the swap string */
 57                 repeats = 0;
 58             }
 59             break;
 60         case ')':
 61         case ']':
 62             exiting = 1;
 63             break;
 64         case '/':
 65             {
 66                 char   *end = strchr(s, '/');
 67 
 68                 s = end + 1;
 69             }
 70             break;
 71         default:
 72             {
 73                 do
 74                 {
 75                     switch (*(s - 1))
 76                     {
 77                     case 'b':
 78                     case 'c':
 79                         d.char_v++;
 80                         break;
 81                     case 'f':
 82                     case 'l':
 83                     case 'i':
 84                     case 'o':
 85                     case 'u':
 86                     case ',':
 87                     case '.':
 88                     case 'w':
 89                         ALIGN_VARPTR(data, d, 4);
 90                         d.int_v++;
 91                         break;
 92                     case 'p':
 93                     case 's':
 94                         ALIGN_VARPTR(data, d, 4);
 95                         break;
 96                     case 'h':
 97                     case 'k':
 98                         ALIGN_VARPTR(data, d, 2);
 99                         d.short_v++;
100                         break;
101                     case 'g':
102                     case 'e':
103                         ALIGN_VARPTR(data, d, 8);
104                         d.double_v++;
105                         break;
106                     case 't':
107                         ALIGN_VARPTR(data, d, SUBSTRUCT_ALIGNMENT);
108                         break;
109                     }
110                     switch (*(s - 1))
111                     {
112                     case 'p':   /* pointer types - recurse (later) */
113                         if (*d.ptr_v)
114                         {
115                             Varptr  td = d;     /* keep d pointer */
116 
117                             lg = ts_serialise_gen(lg, *d.ptr_v);
118                             d = td;
119                         }
120                         d.ptr_v++;
121                         break;
122                     case 's':   /* string */
123                         if (*d.ptr_v)
124                             serialise_item(&lg, *d.ptr_v, strlen(*d.ptr_v));
125                         d.ptr_v++;
126                         break;
127                     case 't':   /* tagged struct */
128                         serialise_tagged_sub_item(&lg, d.int_v);
129                         break;
130                     }
131                 } while (--repeats > 0);
132                 repeats = 0;
133             }
134             break;
135         }
136     }
137 
138 
139     *ss = s;
140     return lg;
141 }
142 
143 /********* GLOBAL Variable count - maintains current item count***********/
144 static int count = 0;
145 
146 /********* GLOBAL Variable count ***********/
147 
148 int     ts_serialise_end(void)
149 {
150     int     c = count;
151 
152     count = 0;
153     return c;
154 }
155 
156 int     serialise_item(List ** lgp, void *d, size_t s)
157 {
158     List   *l=NULL, *lg = *lgp;
159     int     c, rcode = 0;
160 
161     if (s == 0)
162     {
163         return rcode;
164     } else
165     {
166         if(repeatp_checking_get())
167           for (c = -1, l = lg; l; c--, l = l->next)     /* have we already had
168                                                          * this pointer? */
169             if (l->to == d)
170               break;
171         /* else l defaults to new pointer */
172         if (l)                  /* if we have */
173         {
174             /* mprintf("Repeat Item 0x%x %s\n",l->to,
175              * tina_data_info_array[*(int*)l->to - ts_base_id].name); */
176             lg = ref_addtostart(lg, l->to, c);  /* store an offset to
177                                                  * the prev ref */
178             rcode = 0;          /* return code to say don't process
179                                  * this item further */
180         } else
181         {
182             /* mprintf("New Item 0x%x %s\n",d,
183              * tina_data_info_array[*(int*)d - ts_base_id].name); */
184             lg = ref_addtostart(lg, d, s);      /* store the new one and
185                                                  * the size */
186             rcode = 1;          /* ok to process this items contents */
187         }
188         count++;
189     }
190     *lgp = lg;
191     return rcode;
192 }
193 
194 int     serialise_tagged_item(List ** lgp, void *data, size_t s)
195 {
196     int     rcode;
197 
198     if ((rcode = serialise_item(lgp, data, s)) == 1)
199     {
200         char   *ss = tina_data_info_array[*(int *) data - ts_base_id].swap_string;
201         Varptr  d;
202 
203         d.char_v = data;
204 
205         d.int_v++;              /* skip over tag */
206         *lgp = serialise_parse(*lgp, data, d, &ss);     /* serialise contents */
207     }
208     return rcode;
209 }
210 int     serialise_tagged_sub_item(List ** lgp, void *data)
211 {
212     char   *ss = tina_data_info_array[*(int *) data - ts_base_id].swap_string;
213     Varptr  d;
214 
215     d.char_v = data;
216 
217     d.int_v++;                  /* skip over tag */
218     *lgp = serialise_parse(*lgp, data, d, &ss); /* serialise contents */
219     return 1;
220 }
221 
222 List   *ts_serialise(List * lg, void *data)
223 {
224     size_t  s = tina_data_info_array[*(int *) data - ts_base_id].size;
225 
226     /* mprintf("Serialising a %s StructP <0x%x> with %s\n",
227      * tina_data_info_array[*(int*)data - ts_base_id].name, data,
228      * tina_data_info_array[*(int*)data - ts_base_id].swap_string); */
229     serialise_tagged_item(&lg, data, s);
230     return lg;
231 }
232 
233 List   *ts_serialise_gen(List * lg, void *d)
234 {
235     Sfunc s_func;
236     /* mprintf("ts_serialise_gen(0x%x,0x%x) count %d \n",lg,d,count); */
237     if (d)
238     {
239         s_func = (Sfunc)(tina_data_info_array[*(int *) d - ts_base_id].serialise);
240         lg = s_func( lg, d);
241 /*  code rewritten for MVP compatability
242         lg = tina_data_info_array[*(int *) d - ts_base_id].serialise(
243         (struct list *)lg,
244         (void *) d);
245 */
246     }
247     return lg;
248 }
249 

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