@@ -95,7 +95,7 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
95
95
char * header [MAX_LINES ];
96
96
char * words [MAX_WORDS ], * cwords [2 ];
97
97
char * col_labels [MAX_COLUMNS ], * col_units [MAX_COLUMNS ];
98
- char * c , * line , * fullline , * mkey , * mval ;
98
+ char * c , * line , * firstline , * interline , * fullline , * mkey , * mval ;
99
99
char * reword ;
100
100
char tlabel [32 ] = {'\0' };
101
101
char comments [1025 ] = {'\0' };
@@ -156,17 +156,6 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
156
156
xdifile -> outer_label = calloc (MAX_LINE_LENGTH + 1 , sizeof (char ));
157
157
strncpy (xdifile -> outer_label , outerlabel , MAX_LINE_LENGTH );
158
158
159
-
160
- /* COPY_STRING(xdifile->xdi_libversion, XDI_VERSION); */
161
- /* COPY_STRING(xdifile->xdi_version, ""); */
162
- /* COPY_STRING(xdifile->extra_version, ""); */
163
- /* COPY_STRING(xdifile->element, "__"); */
164
- /* COPY_STRING(xdifile->edge, "_"); */
165
- /* COPY_STRING(xdifile->comments, comments); */
166
- /* COPY_STRING(xdifile->error_line, ""); */
167
- /* COPY_STRING(xdifile->outer_label, ""); */
168
-
169
-
170
159
/* initialize numeric attributes of thr XDIFile struct */
171
160
172
161
xdifile -> nouter = 1 ;
@@ -181,8 +170,10 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
181
170
182
171
for (i = 0 ; i < MAX_COLUMNS ; i ++ ) {
183
172
sprintf (tlabel , "col%ld" , i + 1 );
184
- COPY_STRING (col_labels [i ], tlabel );
185
- COPY_STRING (col_units [i ], "" );
173
+ /* COPY_STRING(col_labels[i], tlabel); */
174
+ /* COPY_STRING(col_units[i], ""); */
175
+ col_labels [i ] = tlabel ;
176
+ col_units [i ] = "" ;
186
177
}
187
178
188
179
/* read file to text lines: an array of trimmed strings */
@@ -197,15 +188,15 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
197
188
198
189
/* check first line for XDI header, get version info */
199
190
if (strncmp (textlines [0 ], TOK_COMM , 1 ) == 0 ) {
200
- line = textlines [0 ]; line ++ ;
201
- line [strcspn (line , CRLF )] = '\0' ;
202
- nwords = make_words (line , cwords , 2 );
191
+ firstline = textlines [0 ]; firstline ++ ;
192
+ firstline [strcspn (firstline , CRLF )] = '\0' ;
193
+ nwords = make_words (firstline , cwords , 2 );
203
194
if (nwords < 1 ) { return ERR_NOTXDI ; }
204
195
if (strncasecmp (cwords [0 ], TOK_VERSION , strlen (TOK_VERSION )) != 0 ) {
205
196
return ERR_NOTXDI ;
206
197
} else {
207
- line = line + 5 ;
208
- strcpy (xdifile -> xdi_version , line );
198
+ firstline = firstline + 5 ;
199
+ strcpy (xdifile -> xdi_version , firstline );
209
200
}
210
201
if (nwords > 1 ) { /* extra version tags */
211
202
strcpy (xdifile -> extra_version , cwords [1 ]);
@@ -227,23 +218,25 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
227
218
xdifile -> meta_keywords = calloc (nheader , sizeof (char * ));
228
219
xdifile -> meta_values = calloc (nheader , sizeof (char * ));
229
220
230
-
231
221
mode = 0 ; /* metadata (Family.Member: Value) mode */
232
222
for (i = 1 ; i < nheader ; i ++ ) {
233
223
xdifile -> error_lineno = i ;
234
- COPY_STRING (xdifile -> error_line , textlines [i ]);
224
+ strcpy (xdifile -> error_line , textlines [i ]);
235
225
236
226
if (strncmp (textlines [i ], TOK_COMM , 1 ) == 0 ) {
237
- COPY_STRING (line , textlines [i ]);
238
- COPY_STRING (fullline , textlines [i ]);
227
+ /* COPY_STRING(line, textlines[i]); */
228
+ /* COPY_STRING(fullline, textlines[i]); */
229
+ line = textlines [i ];
230
+ fullline = textlines [i ];
239
231
line ++ ;
240
232
fullline ++ ;
241
233
nwords = split_on (line , TOK_DELIM , words );
242
234
if (nwords < 1 ) { continue ; }
243
235
COPY_STRING (mkey , words [0 ]);
244
236
245
237
if ((mode == 0 ) && (nwords == 2 )) { /* metadata */
246
- COPY_STRING (mval , words [1 ]);
238
+ /* COPY_STRING(mval, words[1]); */
239
+ mval = words [1 ];
247
240
nwords = split_on (words [0 ], TOK_DOT , words );
248
241
if (nwords > 1 ) {
249
242
ndict ++ ;
@@ -254,20 +247,23 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
254
247
*/
255
248
regex_status = slre_match (1 , FAMILYNAME , words [0 ], strlen (words [0 ]));
256
249
if (regex_status != NULL ) {
250
+ free (mval );
257
251
return ERR_META_FAMNAME ;
258
252
}
259
253
260
254
regex_status = slre_match (1 , KEYNAME , words [1 ], strlen (words [1 ]));
261
255
if (regex_status != NULL ) {
256
+ free (mval );
262
257
return ERR_META_KEYNAME ;
263
258
}
264
259
265
260
COPY_STRING (xdifile -> meta_families [ndict ], words [0 ]);
266
261
COPY_STRING (xdifile -> meta_keywords [ndict ], words [1 ]);
267
262
} else {
263
+ free (mval );
268
264
return ERR_META_FORMAT ;
269
265
}
270
- /* printf(" metadata: %d %s %s\n", ndict, mkey, mval); */
266
+ /* printf(" metadata: %ld | %s | %s\n", ndict, mkey, mval); */
271
267
/* ndict, words[0], words[1], xdifile->meta_values[ndict]);*/
272
268
if (strncasecmp (mkey , TOK_COLUMN , strlen (TOK_COLUMN )) == 0 ) {
273
269
j = atoi (mkey + 7 )- 1 ;
@@ -297,19 +293,20 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
297
293
}
298
294
/* MONO D-SPACING */
299
295
} else if (strcasecmp (mkey , TOK_DSPACE ) == 0 ) {
300
- if (0 != xdi_strtod (mval , & dval )) { return ERR_NONNUMERIC ;}
296
+ if (0 != xdi_strtod (mval , & dval )) { free ( mval ); return ERR_NONNUMERIC ;}
301
297
xdifile -> dspacing = dval ;
302
298
/* OUTER ARRAY NAME */
303
299
} else if (strcasecmp (mkey , TOK_OUTER_NAME ) == 0 ) {
304
- COPY_STRING (xdifile -> outer_label , mval );
300
+ strcpy (xdifile -> outer_label , mval );
305
301
/* OUTER ARRAY VALUE */
306
302
} else if (strcasecmp (mkey , TOK_OUTER_VAL ) == 0 ) {
307
- if (0 != xdi_strtod (mval , & dval )) { return ERR_NONNUMERIC ;}
303
+ if (0 != xdi_strtod (mval , & dval )) { free ( mval ); return ERR_NONNUMERIC ;}
308
304
outer_arr0 = dval ;
309
305
} else if (strcasecmp (mkey , TOK_TIMESTAMP ) == 0 ) {
310
306
j = xdi_is_datestring (mval );
311
- if (0 != j ) return j ;
307
+ if (0 != j ) { free ( mval ); return j ;}
312
308
}
309
+ /* free(mval); */
313
310
} else if (strncasecmp (mkey , TOK_USERCOM_0 , strlen (TOK_USERCOM_0 )) == 0 ) {
314
311
mode = 1 ;
315
312
} else if (strncasecmp (mkey , TOK_USERCOM_1 , strlen (TOK_USERCOM_1 )) == 0 ) {
@@ -326,6 +323,7 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
326
323
} else if (mode == 0 ) {
327
324
return ERR_META_FORMAT ;
328
325
}
326
+ free (mkey );
329
327
} else {
330
328
if ((ignored_headerline < 0 ) && (has_minusline == 0 )) {
331
329
ignored_headerline = i ;
@@ -384,6 +382,7 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
384
382
}
385
383
}
386
384
385
+
387
386
/* check for mono d-spacing if angle is given but not energy*/
388
387
if ((has_angle == 1 ) && (has_energy == 0 ) && (xdifile -> dspacing < 0 )) {
389
388
iret = ERR_NODSPACE ;
@@ -392,7 +391,7 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
392
391
/* set size of data arrays */
393
392
xdifile -> array = calloc (ncols , sizeof (double * ));
394
393
for (j = 0 ; j < ncols ; j ++ ) {
395
- xdifile -> array [j ] = calloc (npts_ , sizeof (double ));
394
+ xdifile -> array [j ] = calloc (npts_ + 1 , sizeof (double ));
396
395
if (0 != xdi_strtod (words [j ], & dval )) { return ERR_NONNUMERIC ;}
397
396
xdifile -> array [j ][0 ] = dval ;
398
397
}
@@ -402,35 +401,48 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
402
401
iouter = 1 ;
403
402
for (i = nheader - 2 ; i <= ilen ; i ++ ) {
404
403
/* may find a header line interspersed in array data */
405
- COPY_STRING (line , textlines [i ]);
404
+ /* COPY_STRING(line, textlines[i]); */
405
+ interline = textlines [i ];
406
406
xdifile -> error_lineno = i ;
407
- COPY_STRING (xdifile -> error_line , line );
407
+ strcpy (xdifile -> error_line , interline );
408
408
409
409
if (strncmp (textlines [i ], TOK_COMM , 1 ) == 0 ) {
410
- line ++ ;
411
- nwords = split_on (line , TOK_DELIM , words );
410
+ interline ++ ;
411
+ nwords = split_on (interline , TOK_DELIM , words );
412
412
if (nwords < 2 ) { continue ; }
413
413
COPY_STRING (mkey , words [0 ]);
414
414
if (strcasecmp (mkey , TOK_OUTER_VAL ) == 0 ) {
415
- if (0 != xdi_strtod (words [1 ], & dval )) { return ERR_NONNUMERIC ;}
415
+ if (0 != xdi_strtod (words [1 ], & dval )) {
416
+ free (outer_arr );
417
+ free (outer_pts );
418
+ return ERR_NONNUMERIC ;
419
+ }
416
420
outer_arr [iouter ] = dval ;
417
421
outer_pts [iouter ] = ipt ;
418
422
++ iouter ;
419
423
}
424
+ free (mkey );
420
425
} else {
421
- COPY_STRING (line , textlines [i ]);
422
- icol = make_words (line , words , MAX_WORDS );
426
+ /* COPY_STRING(line, textlines[i]); */
427
+ icol = make_words (textlines [ i ] , words , MAX_WORDS );
423
428
if (icol != ncols ) {
429
+ free (outer_arr );
430
+ free (outer_pts );
424
431
return ERR_NCOLS_CHANGE ;
425
432
}
426
433
icol = min (ncols , icol );
427
434
for (j = 0 ; j < icol ; j ++ ) {
428
- if (0 != xdi_strtod (words [j ], & dval )) { return ERR_NONNUMERIC ;}
429
- xdifile -> array [j ][ipt ] = dval ;
435
+ if (0 != xdi_strtod (words [j ], & dval )) {
436
+ free (outer_arr );
437
+ free (outer_pts );
438
+ return ERR_NONNUMERIC ;
439
+ }
440
+ xdifile -> array [j ][ipt ] = dval ;
430
441
}
431
442
++ ipt ;
432
443
}
433
444
}
445
+
434
446
/* success */
435
447
xdifile -> error_lineno = 0 ;
436
448
strcpy (xdifile -> error_line , "" );
@@ -446,7 +458,13 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
446
458
xdifile -> outer_array [j ] = outer_arr [j ];
447
459
xdifile -> outer_breakpts [j ] = outer_pts [j ];
448
460
}
449
-
461
+ free (line );
462
+ free (outer_arr );
463
+ free (outer_pts );
464
+ /* this was the space used to hold the lines of the file, allocated in readlines */
465
+ for (j = 0 ; j <=ilen ; j ++ ) {
466
+ free (textlines [j ]);
467
+ }
450
468
return iret ;
451
469
452
470
}
@@ -456,7 +474,7 @@ _EXPORT(int) XDI_get_array_index(XDIFile *xdifile, long n, double *out) {
456
474
long j ;
457
475
if (n < xdifile -> narrays ) {
458
476
for (j = 0 ; j < xdifile -> npts ; j ++ ) {
459
- out [j ] = xdifile -> array [n ][j ] ;
477
+ out [j ] = xdifile -> array [n ][j ];
460
478
}
461
479
return 0 ;
462
480
}
0 commit comments