@@ -109,7 +109,7 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
109
109
double * outer_arr , outer_arr0 ;
110
110
long * outer_pts ;
111
111
FILE * inpFile ;
112
- long file_length , ilen , index , i , j , nx , maxcol ;
112
+ long file_length , ilen , index , i , j , k , nx , maxcol ;
113
113
long npts_ , ncols , icol , nheader , nwords , ndict ;
114
114
long ignored_headerline , iret , ipt , nouter , iouter ;
115
115
int is_newline , fnlen , mode , valid , stat ;
@@ -191,8 +191,16 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
191
191
firstline = textlines [0 ]; firstline ++ ;
192
192
firstline [strcspn (firstline , CRLF )] = '\0' ;
193
193
nwords = make_words (firstline , cwords , 2 );
194
- if (nwords < 1 ) { return ERR_NOTXDI ; }
194
+ if (nwords < 1 ) {
195
+ for (j = 0 ; j <=ilen ; j ++ ) {
196
+ free (textlines [j ]);
197
+ }
198
+ return ERR_NOTXDI ;
199
+ }
195
200
if (strncasecmp (cwords [0 ], TOK_VERSION , strlen (TOK_VERSION )) != 0 ) {
201
+ for (j = 0 ; j <=ilen ; j ++ ) {
202
+ free (textlines [j ]);
203
+ }
196
204
return ERR_NOTXDI ;
197
205
} else {
198
206
firstline = firstline + 5 ;
@@ -247,24 +255,36 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
247
255
*/
248
256
regex_status = slre_match (1 , FAMILYNAME , words [0 ], strlen (words [0 ]));
249
257
if (regex_status != NULL ) {
250
- free (mval );
258
+ xdifile -> nmetadata = ndict + 1 ;
259
+ free (mkey );
260
+ for (k = 0 ; k <=ilen ; k ++ ) {
261
+ free (textlines [k ]);
262
+ }
251
263
return ERR_META_FAMNAME ;
252
264
}
253
265
254
266
regex_status = slre_match (1 , KEYNAME , words [1 ], strlen (words [1 ]));
255
267
if (regex_status != NULL ) {
256
- free (mval );
268
+ xdifile -> nmetadata = ndict + 1 ;
269
+ free (mkey );
270
+ for (k = 0 ; k <=ilen ; k ++ ) {
271
+ free (textlines [k ]);
272
+ }
257
273
return ERR_META_KEYNAME ;
258
274
}
259
275
260
276
COPY_STRING (xdifile -> meta_families [ndict ], words [0 ]);
261
277
COPY_STRING (xdifile -> meta_keywords [ndict ], words [1 ]);
262
278
} else {
263
- free (mval );
279
+ xdifile -> nmetadata = ndict + 1 ;
280
+ free (mkey );
281
+ for (k = 0 ; k <=ilen ; k ++ ) {
282
+ free (textlines [k ]);
283
+ }
264
284
return ERR_META_FORMAT ;
265
285
}
266
286
/* printf(" metadata: %ld | %s | %s\n", ndict, mkey, mval); */
267
- /* ndict, words[0], words[1], xdifile->meta_values[ndict ]);*/
287
+ /* ndict, words[0], words[1], xdifile->meta_valuesn[dict ]);*/
268
288
if (strncasecmp (mkey , TOK_COLUMN , strlen (TOK_COLUMN )) == 0 ) {
269
289
j = atoi (mkey + 7 )- 1 ;
270
290
if ((j > -1 ) && (j < MAX_COLUMNS )) {
@@ -293,7 +313,14 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
293
313
}
294
314
/* MONO D-SPACING */
295
315
} else if (strcasecmp (mkey , TOK_DSPACE ) == 0 ) {
296
- if (0 != xdi_strtod (mval , & dval )) { free (mval ); return ERR_NONNUMERIC ;}
316
+ if (0 != xdi_strtod (mval , & dval )) {
317
+ xdifile -> nmetadata = ndict + 1 ;
318
+ free (mkey );
319
+ for (k = 0 ; k <=ilen ; k ++ ) {
320
+ free (textlines [k ]);
321
+ }
322
+ return ERR_NONNUMERIC ;
323
+ }
297
324
xdifile -> dspacing = dval ;
298
325
/* OUTER ARRAY NAME */
299
326
} else if (strcasecmp (mkey , TOK_OUTER_NAME ) == 0 ) {
@@ -304,7 +331,14 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
304
331
outer_arr0 = dval ;
305
332
} else if (strcasecmp (mkey , TOK_TIMESTAMP ) == 0 ) {
306
333
j = xdi_is_datestring (mval );
307
- if (0 != j ) { free (mval ); return j ;}
334
+ if (0 != j ) {
335
+ xdifile -> nmetadata = ndict + 1 ;
336
+ free (mkey );
337
+ for (k = 0 ; k <=ilen ; k ++ ) {
338
+ free (textlines [k ]);
339
+ }
340
+ return j ;
341
+ }
308
342
}
309
343
/* free(mval); */
310
344
} else if (strncasecmp (mkey , TOK_USERCOM_0 , strlen (TOK_USERCOM_0 )) == 0 ) {
@@ -321,6 +355,11 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
321
355
}
322
356
strncat (comments , fullline , sizeof (comments ) - strlen (comments ) - 1 );
323
357
} else if (mode == 0 ) {
358
+ xdifile -> nmetadata = ndict + 1 ;
359
+ free (mkey );
360
+ for (k = 0 ; k <=ilen ; k ++ ) {
361
+ free (textlines [k ]);
362
+ }
324
363
return ERR_META_FORMAT ;
325
364
}
326
365
free (mkey );
@@ -492,47 +531,56 @@ _EXPORT(int) XDI_get_array_name(XDIFile *xdifile, char *name, double *out) {
492
531
return ERR_NOARR_NAME ;
493
532
}
494
533
534
+
495
535
_EXPORT (void )
496
- XDI_cleanup (XDIFile * xdifile ) {
497
- /* one needs to explicitly free each part of the struct */
536
+ XDI_cleanup (XDIFile * xdifile , long err ) {
537
+ /* free each part of the struct, mindful of the error code reported by the library */
498
538
long j ;
499
- for (j = 0 ; j < xdifile -> narrays ; j ++ ) {
500
- free (xdifile -> array [j ]);
501
- free (xdifile -> array_labels [j ]);
502
- free (xdifile -> array_units [j ]);
503
- }
504
- free (xdifile -> array );
505
- free (xdifile -> array_labels );
506
- free (xdifile -> array_units );
507
539
508
540
free (xdifile -> xdi_libversion );
509
541
free (xdifile -> xdi_version );
510
542
free (xdifile -> extra_version );
511
- free (xdifile -> filename );
512
543
free (xdifile -> element );
513
544
free (xdifile -> edge );
514
545
free (xdifile -> comments );
515
546
free (xdifile -> error_line );
516
-
517
- for (j = 0 ; j < xdifile -> nmetadata ; j ++ ) {
518
- free (xdifile -> meta_families [j ]);
519
- free (xdifile -> meta_keywords [j ]);
520
- free (xdifile -> meta_values [j ]);
521
- }
522
- free (xdifile -> meta_families );
523
- free (xdifile -> meta_keywords );
524
- free (xdifile -> meta_values );
525
-
526
547
free (xdifile -> outer_label );
527
- /* for (j = 0; j < xdifile->nouter; j++) { */
528
- /* free(xdifile->outer_array[j]); */
529
- /* free(xdifile->outer_breakpts[j]); */
530
- /* } */
531
- free (xdifile -> outer_array );
532
- free (xdifile -> outer_breakpts );
533
548
549
+ if ((err != -32 ) && (err != -128 ) && (err != -128 )) {
550
+ free (xdifile -> filename );
551
+ };
534
552
553
+ if ((err < -1 ) || (err == 0 )) {
535
554
555
+ if ((err == 0 ) || (err < -128 )) {
556
+ for (j = 0 ; j < xdifile -> narrays ; j ++ ) {
557
+ free (xdifile -> array [j ]);
558
+ free (xdifile -> array_labels [j ]);
559
+ free (xdifile -> array_units [j ]);
560
+ }
561
+ free (xdifile -> array );
562
+ free (xdifile -> array_labels );
563
+ free (xdifile -> array_units );
564
+ };
565
+
566
+ for (j = 0 ; j < xdifile -> nmetadata ; j ++ ) {
567
+ free (xdifile -> meta_families [j ]);
568
+ free (xdifile -> meta_keywords [j ]);
569
+ free (xdifile -> meta_values [j ]);
570
+ }
571
+ free (xdifile -> meta_families );
572
+ free (xdifile -> meta_keywords );
573
+ free (xdifile -> meta_values );
574
+
575
+ /* for (j = 0; j < xdifile->nouter; j++) { */
576
+ /* free(xdifile->outer_array[j]); */
577
+ /* free(xdifile->outer_breakpts[j]); */
578
+ /* } */
579
+ if (err == 0 ) {
580
+ free (xdifile -> outer_array );
581
+ free (xdifile -> outer_breakpts );
582
+ };
583
+ }
536
584
537
585
free (xdifile );
538
586
}
0 commit comments