home · contact · privacy
7f399a9b883d92d3463ecf9ee47342f349893464
[plomrogue] / src / common / err_try_fgets.c
1 /* err_try_fgets.c */
2
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() */
9
10
11
12 /* Increments by one on each err_try_fgets() call, servers as a line counter. */
13 static uint32_t err_try_fgets_counter = 0;
14
15 /* Delimiter to use for err_try_fgets()' 'c' test. */
16 char * err_try_fgets_delim = "";
17
18
19
20 extern void reset_err_try_fgets_counter()
21 {
22     err_try_fgets_counter = 0;
23 }
24
25
26
27 extern void set_err_try_fgets_delim(char * delim)
28 {
29     err_try_fgets_delim = delim;
30 }
31
32
33
34 extern void err_line(uint8_t test, char * line, char * intro, char * msg)
35 {
36     if (!test)
37     {
38         return;
39     }
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,
45                                  line);
46     exit_err(1, err);
47 }
48
49
50
51 extern void err_try_fgets(char * line, uint32_t linemax, FILE * file,
52                           char * context, char * test)
53 {
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()";
61     line[0] = '\0';
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,
66              err_end);
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,
71              context, err_delim);
72     if (strchr(test, 'i'))
73     {
74         err_line(!(strchr(test, 'f')) && strlen(line) < 2, line, context,
75                  err_int);
76         uint8_t i;
77         for (i = 0; '\n' != line[i] && '\0' != line[i]; i++)
78         {
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);
82         }
83         err_line(strlen(line) < 2 && ('-' == line[i] || '+' == line[i]),
84                  line, context, err_int);
85     }
86 }