3 #include <stdint.h> /* uint8_t, uint32_t */
4 #include <stdio.h> /* FILE, sprintf() */
5 #include <string.h> /* strlen(), strchr(), strcmp() */
6 #include "../common/readwrite.h" /* try_fgets() */
7 #include "../common/rexit.h" /* exit_err() */
8 #include "../common/try_malloc.h" /* try_malloc() */
12 /* Increments by one on each err_try_fgets() call, servers as a line counter. */
13 static uint32_t err_try_fgets_counter = 0;
15 /* Delimiter to use for err_try_fgets()' 'c' test. */
16 char * err_try_fgets_delim = "";
20 extern void reset_err_try_fgets_counter()
22 err_try_fgets_counter = 0;
27 extern void set_err_try_fgets_delim(char * delim)
29 err_try_fgets_delim = delim;
34 extern void err_line(uint8_t test, char * line, char * intro, char * msg)
40 char * f_name = "err_line()";
41 char * line_intro = " Offending line ";
42 char * err = try_malloc(strlen(intro) + strlen(msg) + strlen(line_intro) +
43 10 + 1 + 1 + strlen(line) + 1, f_name);
44 sprintf(err, "%s%s%s%d:\n%s", intro, msg, line_intro, err_try_fgets_counter,
51 extern void err_try_fgets(char * line, uint32_t linemax, FILE * file,
52 char * context, char * test)
54 char * err_end = "File ended unexpectedly.";
55 char * err_empty = "Hit empty line where non-empty line was expected.";
56 char * err_many = "Too many characters; expected only one.";
57 char * err_int = "Expected valid positive or negative integer number.";
58 char * err_full = "Hit non-empty line where empty line was expected.";
59 char * err_delim = "Expected proper delimiter, found something else.";
60 char * f_name = "err_try_fgets()";
62 try_fgets(line, linemax + 1, file, f_name);
63 err_try_fgets_counter++;
64 err_line(strchr(test, '0') && !(strlen(line)), line, context, err_end);
65 err_line(strchr(test, 'n') && line[strlen(line) - 1] != '\n', line, context,
67 err_line(strchr(test, 'e') && '\n' != line[0], line, context, err_full);
68 err_line(strchr(test, 'f') && '\n' == line[0], line, context, err_empty);
69 err_line(strchr(test, 's') && strlen(line) > 2, line, context, err_many);
70 err_line(strchr(test, 'd') && strcmp(line, err_try_fgets_delim), line,
72 if (strchr(test, 'i'))
74 err_line(!(strchr(test, 'f')) && strlen(line) < 2, line, context,
77 for (i = 0; '\n' != line[i] && '\0' != line[i]; i++)
79 uint8_t test = (0 == i && ('-' == line[i] || '+' == line[i]))
80 || ('0' <= line[i] && line[i] <= '9');
81 err_line(!test, line, context, err_int);
83 err_line(strlen(line) < 2 && ('-' == line[i] || '+' == line[i]),
84 line, context, err_int);