| 1 | /* |
| 2 | * Copyright (C) 1996-2011 Daniel Waggoner |
| 3 | * |
| 4 | * This free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation, either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * It is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * If you did not received a copy of the GNU General Public License |
| 15 | * with this software, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include "dw_ascii.h" |
| 19 | #include "dw_parse_cmd.h" |
| 20 | #include "dw_array.h" |
| 21 | #include "dw_math.h" |
| 22 | #include "dw_error.h" |
| 23 | #include "dw_std.h" |
| 24 | |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <ctype.h> |
| 28 | #include <stdarg.h> |
| 29 | #include <math.h> |
| 30 | |
| 31 | /* |
| 32 | Attempts to open filename for reading. Returns pointer to file upon success |
| 33 | and prints error message and exits upon failure. The file must exist. |
| 34 | */ |
| 35 | FILE *dw_OpenTextFile(char *filename) |
| 36 | { |
| 37 | FILE *f=fopen(filename,"rt"); |
| 38 | if (!f) dw_FileError(FILE_OPEN_ERR,filename); |
| 39 | return f; |
| 40 | } |
| 41 | |
| 42 | /* |
| 43 | Attempts to create filename for writing. Returns pointer to file upon success |
| 44 | and prints error message and exits upon failure. If the file exists, it is |
| 45 | overwritten. |
| 46 | */ |
| 47 | FILE *dw_CreateTextFile(char *filename) |
| 48 | { |
| 49 | FILE *f=fopen(filename,"wt"); |
| 50 | if (!f) dw_FileError(FILE_OPEN_ERR,filename); |
| 51 | return f; |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | Attempts to create filename for writing. Returns pointer to file upon success |
| 56 | and prints error message and exits upon failure. The file is created if it |
| 57 | does not exist and is opened with the file pointer positioned at the end of |
| 58 | file if it does exist. |
| 59 | */ |
| 60 | FILE *dw_AppendTextFile(char *filename) |
| 61 | { |
| 62 | FILE *f=fopen(filename,"at"); |
| 63 | if (!f) dw_FileError(FILE_OPEN_ERR,filename); |
| 64 | return f; |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | Returns the number of lines in the file. The number of lines in a file is |
| 69 | equal to the number of newlines ('\n') in the file if the last character is |
| 70 | a newline and this number plus one if the last character is not a newline. |
| 71 | An empty file will return 0 and if f is the null pointer -1 is returned. |
| 72 | |
| 73 | If f is not null, upon return the file pointer is set to the beginning of the |
| 74 | file. One of f or filename must be non-null. |
| 75 | */ |
| 76 | int dw_NumberLines(FILE *f, char *filename) |
| 77 | { |
| 78 | int ch, lines=0; |
| 79 | FILE *f_in=f ? f : (filename ? fopen(filename,"rt") : (FILE*)NULL); |
| 80 | if (!f_in) return -1; |
| 81 | fseek(f_in,0,SEEK_SET); |
| 82 | while (EOF != (ch=getc(f_in))) |
| 83 | if (ch == '\n') lines++; |
| 84 | fseek(f_in,-1,SEEK_END); |
| 85 | ch=getc(f_in); |
| 86 | if ((ch != '\n') && (ch != EOF)) lines++; |
| 87 | fseek(f_in,0,SEEK_SET); |
| 88 | if (!f) fclose(f_in); |
| 89 | return lines; |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | Assumes: |
| 94 | f : valid file pointer |
| 95 | buffer : pointer to character or null pointer |
| 96 | n : pointer to integer containing the length of buffer |
| 97 | |
| 98 | Returns: |
| 99 | Pointer to null terminated string containing the characters from the file up |
| 100 | to and including the terminating new line character. A null pointer return |
| 101 | indicates that there was a memory error or no characters to be read. Call |
| 102 | dw_GetError() to determine if a error occured. |
| 103 | |
| 104 | Results: |
| 105 | Reads line, beginning at current position from file f Returns a pointer to |
| 106 | the buffer containing the file and resets *n if necessary. The if the |
| 107 | passed buffer is null or is not large enough to contain the line, buffer is |
| 108 | freed and a new buffer is allocated. Because of this, the passed buffer |
| 109 | must either null or allocated with dw_malloc(), dw_realloc(), or dw_calloc() and the |
| 110 | calling routine is responsible for eventually freeing the memory if the |
| 111 | return value is not null. |
| 112 | |
| 113 | Notes: |
| 114 | If buffer is null, then value pointed to by the pointer n is not used. |
| 115 | */ |
| 116 | #define SIZE_INCREMENT 1024 |
| 117 | char* dw_ReadLine(FILE *f, char *buffer, int *n) |
| 118 | { |
| 119 | char *ptr, *nbuffer; |
| 120 | int i, k=0; |
| 121 | if (!buffer && !(buffer=(char*)dw_malloc(*n=SIZE_INCREMENT))) |
| 122 | { |
| 123 | *n=0; |
| 124 | return (char*)NULL; |
| 125 | } |
| 126 | ptr=buffer; |
| 127 | while (fgets(ptr,*n-k,f)) |
| 128 | if (ptr[(i=(int)strlen(ptr))-1] == '\n') |
| 129 | return buffer; |
| 130 | else |
| 131 | if (!(nbuffer=(char*)dw_realloc(buffer,*n+=SIZE_INCREMENT))) |
| 132 | { |
| 133 | dw_free(buffer); |
| 134 | *n=0; |
| 135 | return (char*)NULL; |
| 136 | } |
| 137 | else |
| 138 | ptr=(buffer=nbuffer) + (k+=i); |
| 139 | if (ptr != buffer) |
| 140 | return buffer; |
| 141 | else |
| 142 | { |
| 143 | dw_free(buffer); |
| 144 | *n=0; |
| 145 | return (char*)NULL; |
| 146 | } |
| 147 | } |
| 148 | #undef SIZE_INCREMENT |
| 149 | |
| 150 | /* |
| 151 | Assumes: |
| 152 | n : non-negative integer |
| 153 | f : valid file pointer |
| 154 | |
| 155 | Results: |
| 156 | Reads and discards n lines. |
| 157 | |
| 158 | */ |
| 159 | void dw_NextLine(FILE *f, int n) |
| 160 | { |
| 161 | char *buffer, *ptr; |
| 162 | if (!(ptr=buffer=(char*)dw_malloc(1024*sizeof(char)))) |
| 163 | dw_Error(MEM_ERR); |
| 164 | else |
| 165 | { |
| 166 | while (ptr && (n-- > 0)) |
| 167 | while ((ptr=fgets(buffer,1024 ,f))) |
| 168 | if (ptr[strlen(ptr)-1] == '\n') break; |
| 169 | dw_free(buffer); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | Assumes: |
| 175 | buffer: a null terminated string |
| 176 | delimiter : field delimiter character |
| 177 | flag : one of the values defined in dw_ascii.h |
| 178 | |
| 179 | Returns: |
| 180 | A string array containing the delimited fields in buffer. The |
| 181 | fields are returned according to flag. |
| 182 | */ |
| 183 | char** dw_ParseDelimitedString(char *buffer, char delimiter, int flag) |
| 184 | { |
| 185 | struct StringList |
| 186 | { |
| 187 | struct StringList *next; |
| 188 | char *string; |
| 189 | int length; |
| 190 | } *head, *ptr; |
| 191 | int k=0, n, m; |
| 192 | char **v; |
| 193 | if (!buffer) return (char**)NULL; |
| 194 | for (head=ptr=(struct StringList*)NULL; *buffer; buffer+=buffer[n] ? n+1 : n) |
| 195 | { |
| 196 | if (flag & STRIP_LEADING_WHITESPACE) |
| 197 | while (*buffer && (*buffer != delimiter) && isspace(*buffer)) buffer++; |
| 198 | for (n=0; buffer[n] && (buffer[n] != delimiter); n++); |
| 199 | if (flag & STRIP_TRAILING_WHITESPACE) |
| 200 | for (m=n-1; (m >= 0) && isspace(buffer[m]); m--); |
| 201 | else |
| 202 | m=n-1; |
| 203 | if ((m >= 0) || !(flag & REMOVE_EMPTY_FIELDS)) |
| 204 | { |
| 205 | ptr=(struct StringList*)dw_malloc(sizeof(struct StringList)); |
| 206 | ptr->string=buffer; |
| 207 | ptr->length=m+1; |
| 208 | ptr->next=head; |
| 209 | head=ptr; |
| 210 | k++; |
| 211 | } |
| 212 | } |
| 213 | v=dw_CreateArray_string(k); |
| 214 | while (--k >= 0) |
| 215 | { |
| 216 | v[k]=(char*)dw_malloc(head->length+1); |
| 217 | if (head->length > 0) memcpy(v[k],head->string,head->length); |
| 218 | v[k][head->length]='\0'; |
| 219 | ptr=head; |
| 220 | head=head->next; |
| 221 | dw_free(ptr); |
| 222 | } |
| 223 | return v; |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | Assumes |
| 228 | f: valid file pointer |
| 229 | delimiter: field deliniter. |
| 230 | flag: one of the values defined in dw_ascii.h |
| 231 | |
| 232 | Returns |
| 233 | One-dimensional string array of the delimited fields of the current line of |
| 234 | the file f or a null pointer. |
| 235 | |
| 236 | Notes |
| 237 | The file is read starting from the current file position. If the file |
| 238 | contains no fields or there is a memory error, then a null pointer is |
| 239 | returned. The delimiter character defines the fields in each row and the |
| 240 | new line character defines the rows. |
| 241 | */ |
| 242 | char** dw_ReadDelimitedLine(FILE *f, char delimiter, int flag) |
| 243 | { |
| 244 | int n=0; |
| 245 | char **v=(char**)NULL, *buffer=dw_ReadLine(f,(char*)NULL,&n); |
| 246 | if (buffer) |
| 247 | { |
| 248 | v=dw_ParseDelimitedString(buffer,delimiter,flag); |
| 249 | dw_free(buffer); |
| 250 | } |
| 251 | return v; |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | Assumes |
| 256 | f: valid file pointer or null pointer. |
| 257 | filename: pointer to null terminated string or null pointer. |
| 258 | delimiter: field deliniter. |
| 259 | flag: one of the values defined in dw_ascii.h |
| 260 | |
| 261 | Returns |
| 262 | Two-dimensional string array of the deliminted fields of f or a null |
| 263 | pointer. |
| 264 | |
| 265 | Notes |
| 266 | One of f and filename should be non-null. If f is non-null, the file is |
| 267 | read starting from the current file position. If f is null, an attempt is |
| 268 | made to open the file. If successful, the file is read from the beginning. |
| 269 | If the file does not exist or contains no fields, then a null pointer is |
| 270 | returned. The delimiter character defines the fields in each row and the |
| 271 | new line character defines the rows. |
| 272 | |
| 273 | */ |
| 274 | char*** dw_ReadDelimitedFile(FILE *f, char* filename, char delimiter, int flag) |
| 275 | { |
| 276 | struct LineList |
| 277 | { |
| 278 | struct LineList *next; |
| 279 | char **line; |
| 280 | } *head=(struct LineList*)NULL, *ptr; |
| 281 | int n=0; |
| 282 | char **v, ***M=(char***)NULL; |
| 283 | FILE *f_in=f ? f : fopen(filename,"rt"); |
| 284 | if (f_in) |
| 285 | { |
| 286 | do |
| 287 | if ((v=dw_ReadDelimitedLine(f_in,delimiter,flag))) |
| 288 | { |
| 289 | ptr=(struct LineList*)dw_malloc(sizeof(struct LineList)); |
| 290 | ptr->line=v; |
| 291 | ptr->next=head; |
| 292 | head=ptr; |
| 293 | n++; |
| 294 | } |
| 295 | while (feof(f_in) || ferror(f_in)); |
| 296 | if (!f) fclose(f_in); |
| 297 | if (n > 0) |
| 298 | { |
| 299 | M=(char***)dw_CreateArray_array(n); |
| 300 | while (--n >= 0) |
| 301 | { |
| 302 | M[n]=head->line; |
| 303 | ptr=head; |
| 304 | head=head->next; |
| 305 | dw_free(ptr); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | return M; |
| 310 | } |
| 311 | |
| 312 | int dw_PrintDelimitedArray(FILE *f, void* array, char delimiter) |
| 313 | { |
| 314 | char format[4]; |
| 315 | format[0]='%'; |
| 316 | format[1]='s'; |
| 317 | format[2]=delimiter; |
| 318 | format[3]='\0'; |
| 319 | return dw_PrintArray(f,array,format); |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | Assumes |
| 324 | f: valid file pointer |
| 325 | delimiter: field deliniter. |
| 326 | flag: one of the values defined in dw_ascii.h |
| 327 | invalid_identifier: PRECISION |
| 328 | |
| 329 | Returns |
| 330 | A PRECISION array containing the value of the fields with valid real numbers. |
| 331 | If the flag sets REMOVE_EMPTY_FIELDS, invalid fields are not included. If |
| 332 | REMOVE_EMPTH_FIELDS is not set, then invalid fields are included and set to |
| 333 | the value invalid_identifier. |
| 334 | |
| 335 | Notes |
| 336 | The file is read starting from the current file position. If the file |
| 337 | contains no fields or there is a memory error, then a null pointer is |
| 338 | returned. The delimiter character defines the fields in each row and the |
| 339 | new line character defines the rows. |
| 340 | |
| 341 | The calling routine must use dw_FreeArray() to deallocate the returned |
| 342 | array. |
| 343 | */ |
| 344 | PRECISION* dw_ReadDelimitedLine_floating(FILE *f, char delimiter, int flag, PRECISION invalid_identifier) |
| 345 | { |
| 346 | char **s; |
| 347 | PRECISION *x, *y; |
| 348 | int i, j, n; |
| 349 | if (!(s=dw_ReadDelimitedLine(f,delimiter,STRIP_WHITESPACE | (flag & REMOVE_EMPTY_FIELDS)))) |
| 350 | return (PRECISION*)NULL; |
| 351 | if (flag & REMOVE_EMPTY_FIELDS) |
| 352 | { |
| 353 | y=(PRECISION*)dw_malloc((n=dw_DimA(s))*sizeof(PRECISION)); |
| 354 | for (i=j=0; i < n; i++) |
| 355 | if (dw_IsFloat(s[i])) y[j++]=atof(s[i]); |
| 356 | if (j > 0) |
| 357 | { |
| 358 | x=dw_CreateArray_floating(j); |
| 359 | memcpy(x,y,j*sizeof(PRECISION)); |
| 360 | } |
| 361 | else |
| 362 | x=(PRECISION*)NULL; |
| 363 | dw_free(y); |
| 364 | } |
| 365 | else |
| 366 | { |
| 367 | x=dw_CreateArray_floating(dw_DimA(s)); |
| 368 | for (i=dw_DimA(s)-1; i >= 0; i--) |
| 369 | x[i]=dw_IsFloat(s[i]) ? atof(s[i]) : invalid_identifier; |
| 370 | } |
| 371 | dw_FreeArray(s); |
| 372 | return x; |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | Assumes |
| 377 | f: valid file pointer or null pointer. |
| 378 | filename: pointer to null terminated string or null pointer. |
| 379 | delimiter: field deliniter. |
| 380 | flag: one of the values defined in dw_ascii.h |
| 381 | invalid_identifier: PRECISION |
| 382 | |
| 383 | Returns |
| 384 | Two-dimensional array of PRECISION or a null pointer. |
| 385 | |
| 386 | Notes |
| 387 | One of f and filename should be non-null. If f is non-null, the file is |
| 388 | read starting from the current file position. If f is null, an attempt is |
| 389 | made to open the file. If successful, the file is read from the beginning. |
| 390 | If the file does not exist or contains no fields, then a null pointer is |
| 391 | returned. The delimiter character defines the fields in each row and the |
| 392 | new line character defines the rows. |
| 393 | |
| 394 | The calling routine must use dw_FreeArray() to deallocate the returned |
| 395 | array. |
| 396 | */ |
| 397 | PRECISION** dw_ReadDelimitedFile_floating(FILE *f, char* filename, char delimiter, int flag, PRECISION invalid_identifier) |
| 398 | { |
| 399 | struct LineList |
| 400 | { |
| 401 | struct LineList *next; |
| 402 | PRECISION *line; |
| 403 | } *head=(struct LineList*)NULL, *ptr; |
| 404 | int n=0; |
| 405 | PRECISION *x, **X=(PRECISION**)NULL; |
| 406 | FILE *f_in=f ? f : fopen(filename,"rt"); |
| 407 | if (f_in) |
| 408 | { |
| 409 | do |
| 410 | if ((x=dw_ReadDelimitedLine_floating(f_in,delimiter,flag,invalid_identifier))) |
| 411 | { |
| 412 | ptr=(struct LineList*)dw_malloc(sizeof(struct LineList)); |
| 413 | ptr->line=x; |
| 414 | ptr->next=head; |
| 415 | head=ptr; |
| 416 | n++; |
| 417 | } |
| 418 | while (!feof(f_in) && !ferror(f_in)); |
| 419 | if (!f) fclose(f_in); |
| 420 | if (n > 0) |
| 421 | { |
| 422 | X=(PRECISION**)dw_CreateArray_array(n); |
| 423 | while (--n >= 0) |
| 424 | { |
| 425 | X[n]=head->line; |
| 426 | ptr=head; |
| 427 | head=head->next; |
| 428 | dw_free(ptr); |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | return X; |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | Assumes: |
| 437 | f : valid file pointer |
| 438 | delimiter : field terminator |
| 439 | terminal : line terminator |
| 440 | flag : determine how characters are processed |
| 441 | buffer : pointer to pointer to character or null pointer |
| 442 | n : pointer to integer containing the length of buffer |
| 443 | |
| 444 | Returns: |
| 445 | 0 : memory error occured |
| 446 | 1 : field read, terminated by delimiter |
| 447 | 2 : field read, terminated by terminal |
| 448 | 3 : field read, terminated by EOF |
| 449 | |
| 450 | Results: |
| 451 | If necessary, memory ia reallocated. The length of this reallocated memory |
| 452 | is stored in n. It is the calling routines responsibility to free the |
| 453 | memory pointed to by *buffer. |
| 454 | |
| 455 | Notes: |
| 456 | flag values |
| 457 | ALLOW_QUOTED_TEXT |
| 458 | If set the delimiter and terminal characters do not stop processing when |
| 459 | encountered between quotes. To produce a quote in quoted text, use two |
| 460 | consectutive quotes. Outside quoted text, a quote always begins quoted |
| 461 | text. |
| 462 | |
| 463 | PRINTABLE_ONLY_IN_QUOTES |
| 464 | |
| 465 | PRINTABLE_ONLY |
| 466 | |
| 467 | STRIP_LEADING_WHITESPACE |
| 468 | |
| 469 | STRIP_TRAILING_WHITESPACE |
| 470 | |
| 471 | STRIP_WHITESPACE |
| 472 | |
| 473 | |
| 474 | */ |
| 475 | //#define INCREMENT 1024 |
| 476 | //int dw_ReadDelimitedField(FILE *f, int delimiter, int terminal, int flag, char **buffer, int *n) |
| 477 | //{ |
| 478 | /* int ch; // next character read */ |
| 479 | /* int k=0; // position to store next char, always less than *n */ |
| 480 | /* int quoted=0; */ |
| 481 | /* int leading=(flag & STRIP_LEADING_WHITESPACE) ? 1 : 0; */ |
| 482 | /* char *ptr; */ |
| 483 | |
| 484 | /* ch=fgetc(f); */ |
| 485 | |
| 486 | /* while (ch != EOF) */ |
| 487 | /* { */ |
| 488 | /* //=== reallocate memory if necessary */ |
| 489 | /* if (k+1 > *n) */ |
| 490 | /* if (!(ptr=(char*)dw_realloc(buffer,*n+=INCREMENT))) */ |
| 491 | /* { */ |
| 492 | /* *n-=INCREMENT; */ |
| 493 | /* return 0; */ |
| 494 | /* } */ |
| 495 | /* else */ |
| 496 | /* buffer=ptr; */ |
| 497 | |
| 498 | /* //=== process character */ |
| 499 | /* if (quoted) */ |
| 500 | /* { */ |
| 501 | /* if (ch == '"') */ |
| 502 | /* if ((ch=fgets(f)) != '"') */ |
| 503 | /* { */ |
| 504 | /* quoted=0; */ |
| 505 | /* continue; */ |
| 506 | /* } */ |
| 507 | /* if (!(flag & PRINTABLE_ONLY_IN_QUOTES) || isprint(ch)) */ |
| 508 | /* buffer[k++]=ch; */ |
| 509 | /* } */ |
| 510 | /* else */ |
| 511 | /* if ((ch == delimiter) || (ch == terminal)) */ |
| 512 | /* break; */ |
| 513 | /* else */ |
| 514 | /* if ((ch == '"') && (flag & ALLOW_QUOTED_TEXT)) */ |
| 515 | /* quoted=1; */ |
| 516 | /* else */ |
| 517 | /* if (!(flag & PRINTABLE_ONLY) || isprint(ch)) */ |
| 518 | /* { */ |
| 519 | /* if ((ch == "\r") && (terminal == '\n')) */ |
| 520 | /* { */ |
| 521 | /* if ((ch=fgetc(f)) == '\n') break; */ |
| 522 | /* if (!leading) buffer[k++]='\r'; */ |
| 523 | /* continue; */ |
| 524 | /* } */ |
| 525 | /* if (leading) */ |
| 526 | /* if (isspace(ch)) */ |
| 527 | /* { */ |
| 528 | /* ch=fgetc(f); */ |
| 529 | /* continue; */ |
| 530 | /* } */ |
| 531 | /* else */ |
| 532 | /* leading=0; */ |
| 533 | /* buffer[k++]=ch; */ |
| 534 | /* } */ |
| 535 | |
| 536 | /* ch=fgets(f); */ |
| 537 | /* } */ |
| 538 | |
| 539 | /* buffer[k]='\0'; */ |
| 540 | |
| 541 | /* return (ch == EOF) ? 3 : (ch == terminal) ? 2 : 1; */ |
| 542 | //} |
| 543 | //#undef INCREMENT |
| 544 | |
| 545 | /* |
| 546 | Returns 1 if the null terminated string id is found at the beginning of a line |
| 547 | in the file and 0 otherwise. The file pointer is set to the line immediately |
| 548 | after the line containing id. The search starts at the current position of |
| 549 | the file. If id is not found, then the file is rewound and the search is |
| 550 | continued until the initial file position is passed. |
| 551 | */ |
| 552 | int dw_SetFilePosition(FILE *f, char *id) |
| 553 | { |
| 554 | char *buffer=(char*)NULL; |
| 555 | int m, n, pos; |
| 556 | if ((n=(int)strlen(id)) > 0) |
| 557 | { |
| 558 | pos=ftell(f); |
| 559 | while ((buffer=dw_ReadLine(f,buffer,&m))) |
| 560 | if (!memcmp(buffer,id,n)) |
| 561 | { |
| 562 | dw_free(buffer); |
| 563 | return 1; |
| 564 | } |
| 565 | if (pos > 0) |
| 566 | { |
| 567 | rewind(f); |
| 568 | while ((ftell(f) < pos) && (buffer=dw_ReadLine(f,buffer,&m))) |
| 569 | if (!memcmp(buffer,id,n)) |
| 570 | { |
| 571 | dw_free(buffer); |
| 572 | return 1; |
| 573 | } |
| 574 | if (buffer) dw_free(buffer); |
| 575 | } |
| 576 | } |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | /* |
| 581 | Returns 1 if the null terminated string id is found at the beginning of a line |
| 582 | in the file and 0 otherwise. The file pointer is set to the line immediately |
| 583 | after the line containing id. The search starts at the current position of |
| 584 | the file. If id is not found, the file is not rewound. |
| 585 | */ |
| 586 | int dw_SetFilePositionNoRewind(FILE *f, char *id) |
| 587 | { |
| 588 | char *buffer=(char*)NULL; |
| 589 | int m, n; |
| 590 | if ((n=(int)strlen(id)) > 0) |
| 591 | { |
| 592 | while ((buffer=dw_ReadLine(f,buffer,&m))) |
| 593 | if (!memcmp(buffer,id,n)) |
| 594 | { |
| 595 | dw_free(buffer); |
| 596 | return 1; |
| 597 | } |
| 598 | if (buffer) dw_free(buffer); |
| 599 | } |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | |
| 604 | /* int dw_SetFilePositionBySection(FILE *f, int n, ...) */ |
| 605 | /* { */ |
| 606 | /* char *arg; */ |
| 607 | /* int i; */ |
| 608 | /* va_list ap; */ |
| 609 | /* rewind(f); */ |
| 610 | /* va_start(ap,n); */ |
| 611 | /* for (i=0; i < n; i++) */ |
| 612 | /* if (!(arg=va_arg(ap,char*)) || !dw_SetFilePositionNoRewind(f,arg)) */ |
| 613 | /* { */ |
| 614 | /* va_end(ap); */ |
| 615 | /* return 0; */ |
| 616 | /* } */ |
| 617 | /* va_end(ap); */ |
| 618 | /* return 1; */ |
| 619 | /* } */ |
| 620 | |
| 621 | char* dw_DuplicateString(char *buffer) |
| 622 | { |
| 623 | char *rtrn=(char*)NULL; |
| 624 | if (buffer && (rtrn=(char*)dw_malloc(strlen(buffer)+1))) strcpy(rtrn,buffer); |
| 625 | return rtrn; |
| 626 | } |
| 627 | |
| 628 | #undef READLINE_NOERR |
| 629 | #undef READLINE_MEMERR |
| 630 | #undef READLINE_EOF |