Line numbers.

This commit is contained in:
Neil 2021-03-21 13:22:06 -07:00
parent 07bce58799
commit 756ef9e485
1 changed files with 10 additions and 6 deletions

View File

@ -9,10 +9,12 @@
#include <ctype.h> /* isdigit */
#include <limits.h> /* LONG_ INT_ _MIN _MAX */
#include <assert.h> /* assert */
#include "min_array.h"
#include "../src/min_array.h"
struct num { size_t line; int num; };
MIN_ARRAY(char, char)
MIN_ARRAY(num, int)
MIN_ARRAY(num, struct num)
/** Concatenates the contents after the file pointer of `fp` to `string`. Zeros
are preserved, as well as appending a null-terminator. @return Success,
@ -37,7 +39,7 @@ int main(void) {
int success = EXIT_FAILURE;
struct char_array str = MIN_ARRAY_IDLE;
struct num_array nums = MIN_ARRAY_IDLE;
int *num;
struct num *num;
long big_num;
char *a, *anum = 0;
size_t i, line = 1 /* Unix: delimited by '\n'. */;
@ -56,14 +58,16 @@ int main(void) {
goto catch; /* Long conversion failed, (not all platforms.) */
if(big_num < INT_MIN || big_num > INT_MAX)
{ errno = ERANGE; goto catch; }
*num = (int)big_num;
num->line = line;
num->num = (int)big_num;
}
fprintf(stderr, "Extracted:\n");
for(i = 0; i < nums.size; i++) printf("%d\n", nums.data[i]);
for(i = 0; i < nums.size; i++) printf("Line %lu: %d\n",
(unsigned long)nums.data[i].line, nums.data[i].num);
success = EXIT_SUCCESS;
goto finally;
catch:
fprintf(stderr, "Line %lu ", line), perror("stdin");
fprintf(stderr, "Line %lu ", (unsigned long)line), perror("stdin");
finally:
char_array_(&str);
num_array_(&nums);