* get the lines from a text file */
| 471 | * get the lines from a text file |
| 472 | */ |
| 473 | static char ** |
| 474 | readfile(const char *path) |
| 475 | { |
| 476 | char **result; |
| 477 | FILE *infile; |
| 478 | StringInfoData line; |
| 479 | int maxlines; |
| 480 | int n; |
| 481 | |
| 482 | if ((infile = fopen(path, "r")) == NULL) |
| 483 | { |
| 484 | pg_log_error("could not open file \"%s\" for reading: %m", path); |
| 485 | exit(1); |
| 486 | } |
| 487 | |
| 488 | initStringInfo(&line); |
| 489 | |
| 490 | maxlines = 1024; |
| 491 | result = (char **) pg_malloc(maxlines * sizeof(char *)); |
| 492 | |
| 493 | n = 0; |
| 494 | while (pg_get_line_buf(infile, &line)) |
| 495 | { |
| 496 | /* make sure there will be room for a trailing NULL pointer */ |
| 497 | if (n >= maxlines - 1) |
| 498 | { |
| 499 | maxlines *= 2; |
| 500 | result = (char **) pg_realloc(result, maxlines * sizeof(char *)); |
| 501 | } |
| 502 | |
| 503 | result[n++] = pg_strdup(line.data); |
| 504 | } |
| 505 | result[n] = NULL; |
| 506 | |
| 507 | pfree(line.data); |
| 508 | |
| 509 | fclose(infile); |
| 510 | |
| 511 | return result; |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * write an array of lines to a file |
no test coverage detected