Debian Codegraph @

src/lib/helper/renderer/check_renderer_csv.c

raw
1 /* 'checkit_tiff' is a conformance checker for baseline TIFFs
2 *
3 * author: Andreas Romeyke, 2015-2022
4 * licensed under conditions of libtiff
5 * (see http://libtiff.maptools.org/misc.html)
6 *
7 */
8
9
10 #include "check_renderer.h"
11 #include "ctstring.h"
12 #include <assert.h>
13 #include <ctype.h>
14 #include <stdbool.h>
15 #include <stdio.h>
16
17 static const char delim = ',';
18 static const char delim_string[2] = {
19 #ifdef __TINYC__
20 ',', '\0'
21 #else
22 delim,
23 '\0'
24 #endif
25 };
26 static const char stringquote = '\\';
27 static const char stringcsv_open = '"';
28 static const char stringcsv_close = '"';
29
30
31 static bool is_num_string(string_t s) {
32 for (unsigned int i = 0; i < s.len; i++) {
33 if (
34 (!isdigit(s.string[i]))
35 && ( s.string[i] != '.' )
36 ) {
37 return false;
38 }
39 }
40 return true;
41 }
42 static string_t quote_csv(string_t s) {
43 if (s.len == 0) { return s;}
44 if (is_num_string(s)) {return s;}
45 /* we found text */
46 unsigned int current_buf_size = s.len;
47 char *buffer = calloc(s.len+3, sizeof(char)); /* textquotes plus '\0' */
48 if (NULL == buffer) {
49 perror("could not allocate memory");
50 exit(EXIT_FAILURE);
51 }
52 unsigned int j = 0;
53 buffer[j++] = stringcsv_open;
54 for (unsigned int i = 0; i < s.len; i++) {
55 /* realloc if needed */
56 if (j > current_buf_size) {
57 #ifdef COMPAT_NEED_REALLOCARRAY
58 char *newbuf = realloc(buffer, 2 * current_buf_size * sizeof(char));
59 #else
60 char *newbuf = reallocarray(buffer, 2 * current_buf_size, sizeof(char));
61 #endif
62 if (NULL == newbuf) {
63 perror("could not allocate memory");
64 exit(EXIT_FAILURE);
65 }
66 buffer = newbuf;
67 }
68 /* now quote if needed */
69 if (s.string[i] == stringquote) {
70 buffer[j++] = stringquote;
71 buffer[j++] = s.string[i];
72 } else if (s.string[i] == stringcsv_open) {
73 buffer[j++] = stringquote;
74 buffer[j++] = s.string[i];
75 } else if (s.string[i] == stringcsv_close) {
76 buffer[j++] = stringquote;
77 buffer[j++] = s.string[i];
78 } else if (s.string[i] == '\r') {
79 buffer[j++] = ' ';
80 } else if (s.string[i] == '\n') {
81 buffer[j++] = ' ';
82 } else {
83 buffer[j++] = s.string[i];
84 }
85 }
86 buffer[j++] = stringcsv_close;
87 buffer[j] = 0;
88 // fprintf(stderr,"string = >%s<\n", s.string);
89 // fprintf(stderr, "quoted = >%s<\n", buffer);
90 if (j == s.len) {
91 free(buffer);
92 return s;
93 } else {
94 if (!s.is_const) {
95 //free(s.string);
96 }
97 s.string = buffer;
98 s.len = j;
99 return s;
100 }
101 }
102 typedef enum {
103 field_result,
104 field_tag_mode,
105 field_rule,
106 field_expected,
107 field_errordesc,
108 field_value,
109 field_lineno,
110 field_end
111 } field_cycle_t;
112
113 static stringbuf_t render_table_header (stringbuf_t res) {
114 // print header
115 res = secstrcat_string(res, quote_csv(str("is_valid/file/summary")));
116 res = secstrcat_cstr(res, delim_string);
117 res = secstrcat_string(res, quote_csv(str("ifd/tag/filename")));
118 res = secstrcat_cstr(res, delim_string);
119 res = secstrcat_string(res, quote_csv(str("rule_description")));
120 res = secstrcat_cstr(res, delim_string);
121 res = secstrcat_string(res, quote_csv(str("expected_specific_values")));
122 res = secstrcat_cstr(res, delim_string);
123 res = secstrcat_string(res, quote_csv(str("errordescription")));
124 res = secstrcat_cstr(res, delim_string);
125 res = secstrcat_string(res, quote_csv(str("value_found")));
126 res = secstrcat_cstr(res, delim_string);
127 res = secstrcat_string(res, quote_csv(str("lineno_in_config")));
128 res = secstrcat_cstr(res, "\n");
129 return res;
130 }
131
132 static stringbuf_t render_table_line (stringbuf_t res, string_t fields[field_end] ) {
133 /* check if first field set */
134 if (fields[0].len > 0) {
135 res = secstrcat_string(res, fields[0]);
136 fields[0] = empty_str();
137 for (int i = 1; i < field_end; i++) {
138 res = secstrcat_cstr(res, delim_string);
139 if (fields[i].len > 1) {
140 res = secstrcat_string(res, fields[i]);
141 }
142 fields[i] = empty_str();
143 }
144 res = secstrcat_cstr(res, "\n");
145 }
146 return res;
147 }
148
149 stringbuf_t renderer_csv ( const retmsg_t * ret) {
150 assert (ret != NULL);
151 stringbuf_t res = empty_stringbuf();
152 const retmsg_t * startp = ret;
153 rc_tristate_t rc_is_valid = rc_undef;
154 string_t fields[field_end];
155 static bool header_already_rendered = false;
156 if (header_already_rendered == false) {
157 res = render_table_header(res);
158 header_already_rendered = true;
159 }
160 for (int i=0; i<field_end; i++) {
161 fields[i] = empty_str();
162 }
163 while (NULL != startp) {
164 if (startp->rm_type == rm_is_valid) {rc_is_valid = rc_valid;}
165 else if (startp->rm_type == rm_error || startp->rm_type == rm_hard_error) {rc_is_valid = rc_invalid;;}
166 if ( check_if_quiet(startp, rc_is_valid) ) { /*do nothing */ }
167 else {
168 switch (startp->rm_type) {
169 case rm_rule:
170 fields[field_rule] = quote_csv(startp->rm_msg);
171 break;
172 case rm_tag:
173 case rm_mode:
174 fields[field_tag_mode] = quote_csv(startp->rm_msg);
175 break;
176 case rm_value:
177 fields[field_value] = quote_csv(startp->rm_msg);
178 break;
179 case rm_expected:
180 fields[field_expected] = quote_csv(startp->rm_msg);
181 break;
182 case rm_hard_error:
183 fields[field_result] = quote_csv(str("hard error"));
184 break;
185 case rm_error:
186 fields[field_result] = quote_csv(str("error"));
187 break;
188 case rm_error_description:
189 fields[field_errordesc] = quote_csv(startp->rm_msg);
190 break;
191 case rm_warning:
192 fields[field_result] = quote_csv(str("warning"));
193 break;
194 case rm_logicalor_error:
195 fields[field_result] = quote_csv(str("logical error"));
196 break;
197 case rm_file:
198 fields[field_result] = quote_csv(str("file"));
199 fields[field_tag_mode] = quote_csv(startp->rm_msg);
200 res = render_table_line(res, fields);
201 rc_is_valid = rc_undef;;
202 break;
203 case rm_lineno:
204 fields[field_lineno] = quote_csv(startp->rm_msg);
205 break;
206 case rm_endrule:
207 case rm_endtiff:
208 rc_is_valid = rc_undef;;
209 res = render_table_line(res, fields);
210 break;
211 case rm_is_valid:
212 fields[field_result] = quote_csv(str("valid"));
213 rc_is_valid = rc_valid;
214 break;
215 case rm_count_valid:
216 case rm_count_invalid:
217 case rm_summary_valid:
218 case rm_summary_invalid:
219 break;
220
221 default:
222 res = secstrcat_string(res, startp->rm_msg);
223 }
224 }
225 startp = startp->next;
226 }
227 res=secstrcat_cstr( res, "\n");
228 return res;
229 }
230 /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 smarttab expandtab :*/