Subversion Repositories public

Rev

Rev 246 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 246 Rev 314
Line 13... Line 13...
13
  garmin_data * d = malloc(sizeof(garmin_data));
13
  garmin_data * d = malloc(sizeof(garmin_data));
14
 
14
 
15
  if (!d)
15
  if (!d)
16
     return NULL;
16
     return NULL;
17
 
17
 
-
 
18
  memset(d, 0, sizeof(garmin_data));
18
  d->type = type;
19
  d->type = type;
19
 
20
 
20
#define CASE_DATA(x) \
21
#define CASE_DATA(x) \
21
  case data_D##x: d->data = calloc(1,sizeof(D##x)); break
22
  case data_D##x: d->data = calloc(1,sizeof(D##x)); break
22
 
23
 
Line 91... Line 92...
91
  garmin_list * l;
92
  garmin_list * l;
92
 
93
 
93
  if ((l = calloc(1,sizeof(garmin_list))) == NULL)
94
  if ((l = calloc(1,sizeof(garmin_list))) == NULL)
94
     return NULL;
95
     return NULL;
95
 
96
 
-
 
97
  memset(l, 0, sizeof(garmin_list));
96
  l->id = ++gListId;
98
  l->id = ++gListId;
97
 
99
 
98
  return l;
100
  return l;
99
}
101
}
100
 
102
 
101
 
103
 
102
garmin_list *
104
garmin_list *
103
garmin_list_append ( garmin_list * list, garmin_data * data )
105
garmin_list_append ( garmin_list * list, garmin_data * data )
104
{
106
{
105
garmin_list *      l = list;
107
    garmin_list *      l = list;
106
garmin_list_node * n;
108
    garmin_list_node * n;
107
 
109
 
108
	if ( data != NULL )
110
    if ( data != NULL )
109
	{
111
    {
110
	   if ( l == NULL )
112
        if ( l == NULL )
111
	   {
113
        {
112
	      if ((l = garmin_alloc_list()) == NULL)
114
            if ( ( l = garmin_alloc_list() ) == NULL )
113
		 return NULL;
115
                return NULL;
114
	   }
116
        }
115
 
117
 
116
	   if ((n = malloc(sizeof(garmin_list_node))) == NULL)
118
        if ( ( n = malloc ( sizeof ( garmin_list_node ) ) ) == NULL )
117
	      return NULL;
119
            return NULL;
118
 
120
 
-
 
121
		memset(n, 0, sizeof(garmin_list_node));
119
	   n->data = data;
122
        n->data = data;
120
	   n->next = NULL;
123
        n->next = NULL;
121
 
124
 
122
	   if ( l->head == NULL )
125
        if ( l->head == NULL )
123
	      l->head = n;
126
            l->head = n;
124
 
127
 
125
	   if ( l->tail != NULL )
128
        if ( l->tail != NULL )
126
	      l->tail->next = n;
129
            l->tail->next = n;
127
 
130
 
128
	   l->tail = n;
131
        l->tail = n;
129
	   l->elements++;
132
        l->elements++;
130
	}
133
    }
131
 
134
 
132
	return l;
135
    return l;
133
}
136
}
134
 
137
 
135
 
138
 
136
garmin_data *
-
 
137
garmin_list_data ( garmin_data * data, uint32 which )
139
garmin_data *garmin_list_data (garmin_data *data, uint32 which)
138
{
140
{
139
  garmin_data *       ret = NULL;
141
garmin_data *       ret = NULL;
140
  garmin_list *       list;
142
garmin_list *       list;
141
  garmin_list_node *  n;
143
garmin_list_node *  n;
142
  unsigned int        i;
144
unsigned int        i;
143
 
145
 
144
  if ( data                 != NULL       && 
-
 
145
       data->type           == data_Dlist && 
-
 
146
       (list = data->data)  != NULL ) {
146
	if (data != NULL && data->type == data_Dlist && (list = data->data) != NULL)
-
 
147
	{
147
    for ( i = 0, n = list->head; i < which && n != NULL; i++, n = n->next );
148
		for ( i = 0, n = list->head; i < which && n != NULL; i++, n = n->next );
148
    if ( n != NULL ) ret = n->data;
-
 
149
  }
-
 
150
 
149
 
-
 
150
		if ( n != NULL )
-
 
151
			ret = n->data;
-
 
152
	}
-
 
153
 
151
  return ret;
154
	return ret;
152
}
155
}
153
 
156
 
154
 
157
 
155
void
158
void
156
garmin_free_list ( garmin_list * l )
159
garmin_free_list (garmin_list *l)
157
{
160
{
158
  garmin_list_node * n;
161
garmin_list_node *n;
159
  garmin_list_node * x;
162
garmin_list_node *x;
-
 
163
 
-
 
164
    if ( l != NULL )
-
 
165
    {
-
 
166
        for ( n = l->head; n != NULL; n = x )
-
 
167
        {
-
 
168
            x = n->next;
-
 
169
            garmin_free_data (n->data);
-
 
170
            free (n);
-
 
171
        }
160
 
172
 
161
  if ( l != NULL ) {
-
 
162
    for ( n = l->head; n != NULL; n = x ) {
-
 
163
      x = n->next;
-
 
164
      garmin_free_data(n->data);
-
 
165
      free(n);
173
        free (l);
166
    }
174
    }
167
    free(l);
-
 
168
  }
-
 
169
}
175
}
170
 
176
 
171
 
177
 
172
void
178
void
173
garmin_free_list_only ( garmin_list * l )
179
garmin_free_list_only ( garmin_list * l )
174
{
180
{
175
  garmin_list_node * n;
181
    garmin_list_node * n;
176
  garmin_list_node * x;
182
    garmin_list_node * x;
177
 
183
 
178
  if ( l != NULL ) {
184
    if ( l != NULL )
-
 
185
    {
179
    for ( n = l->head; n != NULL; n = x ) {
186
        for ( n = l->head; n != NULL; n = x )
-
 
187
        {
180
      x = n->next;
188
            x = n->next;
-
 
189
            free ( n );
-
 
190
        }
181
      free(n);
191
        free ( l );
182
    }
192
    }
183
    free(l);
-
 
184
  }
-
 
185
}
193
}
186
 
194
 
187
 
195
 
188
#define TRYFREE(x) if ( x != NULL ) free(x)
196
#define TRYFREE(x) if ( x != NULL ) free(x)
189
 
197
 
190
 
198
 
191
void
199
void
192
garmin_free_data ( garmin_data * d )
200
garmin_free_data ( garmin_data * d )
193
{
201
{
194
  D105 *   d105;
202
    D105 *   d105;
195
  D106 *   d106;
203
    D106 *   d106;
196
  D108 *   d108;
204
    D108 *   d108;
197
  D109 *   d109;
205
    D109 *   d109;
198
  D110 *   d110;
206
    D110 *   d110;
199
  D202 *   d202;
207
    D202 *   d202;
200
  D210 *   d210;
208
    D210 *   d210;
201
  D310 *   d310;
209
    D310 *   d310;
202
  D312 *   d312;
210
    D312 *   d312;
203
  D650 *   d650;
211
    D650 *   d650;
204
 
212
 
205
  if ( d != NULL ) {
213
    if ( d != NULL )
-
 
214
    {
206
    if ( d->data != NULL ) {
215
        if ( d->data != NULL )
-
 
216
        {
207
      if ( d->type == data_Dlist ) {
217
            if ( d->type == data_Dlist )
-
 
218
            {
208
	garmin_free_list((garmin_list *)d->data);
219
                garmin_free_list ( ( garmin_list * ) d->data );
-
 
220
            }
-
 
221
            else
209
      } else {
222
            {
210
	switch ( d->type ) {
223
                switch ( d->type )
-
 
224
                {
211
	case data_D105:
225
                case data_D105:
212
	  d105 = d->data;
226
                    d105 = d->data;
213
	  TRYFREE(d105->wpt_ident);
227
                    TRYFREE ( d105->wpt_ident );
214
	  break;
228
                    break;
215
	case data_D106:
229
                case data_D106:
216
	  d106 = d->data;
230
                    d106 = d->data;
217
	  TRYFREE(d106->wpt_ident);
231
                    TRYFREE ( d106->wpt_ident );
218
	  TRYFREE(d106->lnk_ident);
232
                    TRYFREE ( d106->lnk_ident );
219
	  break;
233
                    break;
220
	case data_D108:
234
                case data_D108:
221
	  d108 = d->data;
235
                    d108 = d->data;
222
	  TRYFREE(d108->ident);
236
                    TRYFREE ( d108->ident );
223
	  TRYFREE(d108->comment);
237
                    TRYFREE ( d108->comment );
224
	  TRYFREE(d108->facility);
238
                    TRYFREE ( d108->facility );
225
	  TRYFREE(d108->city);
239
                    TRYFREE ( d108->city );
226
	  TRYFREE(d108->addr);
240
                    TRYFREE ( d108->addr );
227
	  TRYFREE(d108->cross_road);
241
                    TRYFREE ( d108->cross_road );
228
	  break;
242
                    break;
229
	case data_D109:
243
                case data_D109:
230
	  d109 = d->data;
244
                    d109 = d->data;
231
	  TRYFREE(d109->ident);
245
                    TRYFREE ( d109->ident );
232
	  TRYFREE(d109->comment);
246
                    TRYFREE ( d109->comment );
233
	  TRYFREE(d109->facility);
247
                    TRYFREE ( d109->facility );
234
	  TRYFREE(d109->city);
248
                    TRYFREE ( d109->city );
235
	  TRYFREE(d109->addr);
249
                    TRYFREE ( d109->addr );
236
	  TRYFREE(d109->cross_road);
250
                    TRYFREE ( d109->cross_road );
237
	  break;
251
                    break;
238
	case data_D110:
252
                case data_D110:
239
	  d110 = d->data;
253
                    d110 = d->data;
240
	  TRYFREE(d110->ident);
254
                    TRYFREE ( d110->ident );
241
	  TRYFREE(d110->comment);
255
                    TRYFREE ( d110->comment );
242
	  TRYFREE(d110->facility);
256
                    TRYFREE ( d110->facility );
243
	  TRYFREE(d110->city);
257
                    TRYFREE ( d110->city );
244
	  TRYFREE(d110->addr);
258
                    TRYFREE ( d110->addr );
245
	  TRYFREE(d110->cross_road);
259
                    TRYFREE ( d110->cross_road );
246
	  break;
260
                    break;
247
	case data_D202:
261
                case data_D202:
248
	  d202 = d->data;
262
                    d202 = d->data;
249
	  TRYFREE(d202->rte_ident);
263
                    TRYFREE ( d202->rte_ident );
250
	  break;
264
                    break;
251
	case data_D210:
265
                case data_D210:
252
	  d210 = d->data;
266
                    d210 = d->data;
253
	  TRYFREE(d210->ident);
267
                    TRYFREE ( d210->ident );
254
	  break;
268
                    break;
255
	case data_D310:
269
                case data_D310:
256
	  d310 = d->data;
270
                    d310 = d->data;
257
	  TRYFREE(d310->trk_ident);
271
                    TRYFREE ( d310->trk_ident );
258
	  break;
272
                    break;
259
	case data_D312:
273
                case data_D312:
260
	  d312 = d->data;
274
                    d312 = d->data;
261
	  TRYFREE(d312->trk_ident);
275
                    TRYFREE ( d312->trk_ident );
262
	  break;
276
                    break;
263
	case data_D650:
277
                case data_D650:
264
	  d650 = d->data;
278
                    d650 = d->data;
265
	  TRYFREE(d650->departure_name);
279
                    TRYFREE ( d650->departure_name );
266
	  TRYFREE(d650->departure_ident);
280
                    TRYFREE ( d650->departure_ident );
267
	  TRYFREE(d650->arrival_name);
281
                    TRYFREE ( d650->arrival_name );
268
	  TRYFREE(d650->arrival_ident);
282
                    TRYFREE ( d650->arrival_ident );
269
	  TRYFREE(d650->ac_id);
283
                    TRYFREE ( d650->ac_id );
270
	  break;
284
                    break;
271
	default:
285
                default:
272
	  break;
286
                    break;
273
	}
287
                }
274
	free(d->data);
288
                free ( d->data );
-
 
289
            }
275
      }
290
        }
-
 
291
        free ( d );
276
    }
292
    }
277
    free(d);
-
 
278
  }
-
 
279
}
293
}
280
 
294
 
281
 
295
 
282
/* 
296
/*
283
   Returns the number of bytes needed in order to serialize the data.  Note
297
   Returns the number of bytes needed in order to serialize the data.  Note
284
   that this is an upper bound!  Some of the Garmin data structures do not
298
   that this is an upper bound!  Some of the Garmin data structures do not
285
   align to word boundaries.  Their memory representation will take up more
299
   align to word boundaries.  Their memory representation will take up more
286
   bytes than the serialized representation will.
300
   bytes than the serialized representation will.
287
*/
301
*/
Line 292... Line 306...
292
  garmin_list *       list;
306
  garmin_list *       list;
293
  garmin_list_node *  node;
307
  garmin_list_node *  node;
294
  uint32              bytes = 0;
308
  uint32              bytes = 0;
295
  char                hv0[256];
309
  char                hv0[256];
296
 
310
 
297
  /* 
311
  /*
298
     The number of bytes needed in order to serialize a Garmin data structure
312
     The number of bytes needed in order to serialize a Garmin data structure
299
     is almost equal to the size of its data structure - but not quite.  If
313
     is almost equal to the size of its data structure - but not quite.  If
300
     we have variable length strings, we need to add their string lengths
314
     we have variable length strings, we need to add their string lengths
301
     (including space for the terminating '\0') and subtract the size of the 
315
     (including space for the terminating '\0') and subtract the size of the
302
     char * placeholders.  We also need 4 bytes for the data type, and 4
316
     char * placeholders.  We also need 4 bytes for the data type, and 4
303
     additional bytes in which we store the number of bytes that we should
317
     additional bytes in which we store the number of bytes that we should
304
     seek forward in order to skip this record.  This allows us to handle
318
     seek forward in order to skip this record.  This allows us to handle
305
     files that include new, unrecognized data types but still conform to
319
     files that include new, unrecognized data types but still conform to
306
     the file format rules (i.e. we can skip data records that we don't
320
     the file format rules (i.e. we can skip data records that we don't