../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
111 / 127
87.4%
Branches:
66 / 80
82.5%
Functions:
10 / 11
90.9%

fftools/textformat/tf_compact.c
Line Branch Exec Source
1 /*
2 * Copyright (c) The FFmpeg developers
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <inttypes.h>
22 #include <limits.h>
23 #include <stdarg.h>
24 #include <string.h>
25
26 #include "avtextformat.h"
27 #include "libavutil/avutil.h"
28 #include "libavutil/bprint.h"
29 #include "libavutil/error.h"
30 #include "libavutil/opt.h"
31 #include "tf_internal.h"
32
33
34 /* Compact output */
35
36 /**
37 * Apply C-language-like string escaping.
38 */
39 32887 static const char *c_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
40 {
41 const char *p;
42
43
2/2
✓ Branch 13 → 3 taken 223649 times.
✓ Branch 13 → 14 taken 32887 times.
256536 for (p = src; *p; p++) {
44
2/6
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 5 not taken.
✓ Branch 3 → 6 taken 264 times.
✗ Branch 3 → 7 not taken.
✗ Branch 3 → 8 not taken.
✓ Branch 3 → 9 taken 223385 times.
223649 switch (*p) {
45 ✗ case '\b': av_bprintf(dst, "%s", "\\b"); break;
46 ✗ case '\f': av_bprintf(dst, "%s", "\\f"); break;
47 264 case '\n': av_bprintf(dst, "%s", "\\n"); break;
48 ✗ case '\r': av_bprintf(dst, "%s", "\\r"); break;
49 ✗ case '\\': av_bprintf(dst, "%s", "\\\\"); break;
50 223385 default:
51
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 223385 times.
223385 if (*p == sep)
52 ✗ av_bprint_chars(dst, '\\', 1);
53 223385 av_bprint_chars(dst, *p, 1);
54 }
55 }
56 32887 return dst->str;
57 }
58
59 /**
60 * Quote fields containing special characters, check RFC4180.
61 */
62 578 static const char *csv_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
63 {
64 578 char meta_chars[] = { sep, '"', '\n', '\r', '\0' };
65
66 578 int needs_quoting = !!src[strcspn(src, meta_chars)];
67
68
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 577 times.
578 if (needs_quoting)
69 1 av_bprint_chars(dst, '"', 1);
70
71
2/2
✓ Branch 9 → 5 taken 3962 times.
✓ Branch 9 → 10 taken 578 times.
4540 for (; *src; src++) {
72
2/2
✓ Branch 5 → 6 taken 2 times.
✓ Branch 5 → 7 taken 3960 times.
3962 if (*src == '"')
73 2 av_bprint_chars(dst, '"', 1);
74 3962 av_bprint_chars(dst, *src, 1);
75 }
76
2/2
✓ Branch 10 → 11 taken 1 time.
✓ Branch 10 → 12 taken 577 times.
578 if (needs_quoting)
77 1 av_bprint_chars(dst, '"', 1);
78 578 return dst->str;
79 }
80
81 ✗ static const char *none_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
82 {
83 ✗ return src;
84 }
85
86 typedef struct CompactContext {
87 const AVClass *class;
88 char *item_sep_str;
89 char item_sep;
90 int nokey;
91 int print_section;
92 char *escape_mode_str;
93 const char * (*escape_str)(AVBPrint *dst, const char *src, const char sep, void *log_ctx);
94 /** level of the row whose line the fields at this level belong to, -1 for none */
95 int row_level[SECTION_MAX_NB_LEVELS];
96 /** number of fields written to the row at this level */
97 int nb_fields[SECTION_MAX_NB_LEVELS];
98 /** the line of the row at this level */
99 AVBPrint line[SECTION_MAX_NB_LEVELS];
100 /** the lines of the rows nested in the row at this level */
101 AVBPrint rows[SECTION_MAX_NB_LEVELS];
102 } CompactContext;
103
104 #undef OFFSET
105 #define OFFSET(x) offsetof(CompactContext, x)
106
107 static const AVOption compact_options[] = {
108 { "item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, { .str = "|" }, 0, 0 },
109 { "s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, { .str = "|" }, 0, 0 },
110 { "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1 },
111 { "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1 },
112 { "escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, { .str = "c" }, 0, 0 },
113 { "e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, { .str = "c" }, 0, 0 },
114 { "print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1 },
115 { "p", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1 },
116 { NULL },
117 };
118
119 DEFINE_FORMATTER_CLASS(compact);
120
121 126 static av_cold int compact_init(AVTextFormatContext *wctx)
122 {
123 126 CompactContext *compact = wctx->priv;
124
125
2/2
✓ Branch 6 → 3 taken 1512 times.
✓ Branch 6 → 7 taken 126 times.
1638 for (int i = 0; i < SECTION_MAX_NB_LEVELS; i++) {
126 1512 av_bprint_init(&compact->line[i], 1, AV_BPRINT_SIZE_UNLIMITED);
127 1512 av_bprint_init(&compact->rows[i], 1, AV_BPRINT_SIZE_UNLIMITED);
128 }
129
130
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 10 taken 126 times.
126 if (strlen(compact->item_sep_str) != 1) {
131 ✗ av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
132 compact->item_sep_str);
133 ✗ return AVERROR(EINVAL);
134 }
135 126 compact->item_sep = compact->item_sep_str[0];
136
137
1/2
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 126 times.
126 if (!strcmp(compact->escape_mode_str, "none")) {
138 ✗ compact->escape_str = none_escape_str;
139
2/2
✓ Branch 12 → 13 taken 122 times.
✓ Branch 12 → 14 taken 4 times.
126 } else if (!strcmp(compact->escape_mode_str, "c" )) {
140 122 compact->escape_str = c_escape_str;
141
1/2
✓ Branch 14 → 15 taken 4 times.
✗ Branch 14 → 16 not taken.
4 } else if (!strcmp(compact->escape_mode_str, "csv" )) {
142 4 compact->escape_str = csv_escape_str;
143 } else {
144 ✗ av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
145 ✗ return AVERROR(EINVAL);
146 }
147
148 126 return 0;
149 }
150
151 126 static av_cold int compact_uninit(AVTextFormatContext *wctx)
152 {
153 126 CompactContext *compact = wctx->priv;
154
155
2/2
✓ Branch 6 → 3 taken 1512 times.
✓ Branch 6 → 7 taken 126 times.
1638 for (int i = 0; i < SECTION_MAX_NB_LEVELS; i++) {
156 1512 av_bprint_finalize(&compact->line[i], NULL);
157 1512 av_bprint_finalize(&compact->rows[i], NULL);
158 }
159
160 126 return 0;
161 }
162
163 8948 static void bprint_append_bprint(AVBPrint *dst, const AVBPrint *src)
164 {
165 8948 av_bprint_append_data(dst, src->str, FFMIN(src->len, src->size - 1));
166 8948 }
167
168 11934 static void compact_print_section_header(AVTextFormatContext *wctx, const void *data)
169 {
170 11934 CompactContext *compact = wctx->priv;
171 11934 const AVTextFormatSection *section = tf_get_section(wctx, wctx->level);
172 11934 const AVTextFormatSection *parent_section = tf_get_parent_section(wctx, wctx->level);
173 11934 int level = wctx->level;
174
175
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 11934 times.
11934 if (!section)
176 ✗ return;
177
178 11934 av_bprint_clear(&wctx->section_pbuf[level]);
179
2/2
✓ Branch 7 → 8 taken 11808 times.
✓ Branch 7 → 9 taken 126 times.
11934 compact->row_level[level] = level ? compact->row_level[level - 1] : -1;
180
181
2/2
✓ Branch 10 → 11 taken 11808 times.
✓ Branch 10 → 32 taken 126 times.
11934 if (parent_section &&
182
2/2
✓ Branch 11 → 12 taken 10696 times.
✓ Branch 11 → 14 taken 1112 times.
11808 (section->flags & AV_TEXTFORMAT_SECTION_FLAG_HAS_TYPE ||
183
2/2
✓ Branch 12 → 13 taken 9466 times.
✓ Branch 12 → 32 taken 1230 times.
10696 (!(section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY) &&
184
2/2
✓ Branch 13 → 14 taken 582 times.
✓ Branch 13 → 32 taken 8884 times.
9466 !(parent_section->flags & (AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER | AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY))))) {
185
186 /* define a prefix for elements not contained in an array or
187 in a wrapper, or for array elements with a type, their
188 fields continue the line of the enclosing row */
189 1694 const char *element_name = (char *)av_x_if_null(section->element_name, section->name);
190 1694 AVBPrint *section_pbuf = &wctx->section_pbuf[level];
191
192 1694 av_bprintf(section_pbuf, "%s%s",
193 1694 wctx->section_pbuf[level - 1].str, element_name);
194
195
2/2
✓ Branch 16 → 17 taken 1112 times.
✓ Branch 16 → 30 taken 582 times.
1694 if (section->flags & AV_TEXTFORMAT_SECTION_FLAG_HAS_TYPE) {
196 // add /TYPE to prefix
197 1112 av_bprint_chars(section_pbuf, '/', 1);
198
199 // normalize section type, replace special characters and lower case
200
2/2
✓ Branch 29 → 19 taken 19348 times.
✓ Branch 29 → 30 taken 1112 times.
20460 for (const char *p = section->get_type(data); *p; p++) {
201 2440 char c =
202
4/4
✓ Branch 19 → 20 taken 17074 times.
✓ Branch 19 → 21 taken 2274 times.
✓ Branch 20 → 21 taken 16662 times.
✓ Branch 20 → 25 taken 412 times.
19348 (*p >= '0' && *p <= '9') ||
203
3/4
✓ Branch 21 → 22 taken 8288 times.
✓ Branch 21 → 23 taken 10648 times.
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 25 taken 8288 times.
18936 (*p >= 'a' && *p <= 'z') ||
204
4/4
✓ Branch 23 → 24 taken 8374 times.
✓ Branch 23 → 26 taken 2274 times.
✓ Branch 24 → 25 taken 8208 times.
✓ Branch 24 → 26 taken 166 times.
19348 (*p >= 'A' && *p <= 'Z') ? av_tolower(*p) : '_';
205 19348 av_bprint_chars(section_pbuf, c, 1);
206 }
207 }
208 1694 av_bprint_chars(section_pbuf, ':', 1);
209 1694 return;
210 }
211
212
2/2
✓ Branch 32 → 33 taken 1356 times.
✓ Branch 32 → 34 taken 8884 times.
10240 if (section->flags & (AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER | AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY))
213 1356 return;
214
215 /* a row, printed as one line once the section ends */
216 8884 compact->row_level[level] = level;
217 8884 compact->nb_fields[level] = 0;
218 8884 av_bprint_clear(&compact->line[level]);
219 8884 av_bprint_clear(&compact->rows[level]);
220
2/2
✓ Branch 36 → 37 taken 7784 times.
✓ Branch 36 → 38 taken 1100 times.
8884 if (compact->print_section)
221 7784 av_bprintf(&compact->line[level], "%s", section->name);
222 }
223
224 11934 static void compact_print_section_footer(AVTextFormatContext *wctx)
225 {
226 11934 CompactContext *compact = wctx->priv;
227 11934 const AVTextFormatSection *section = tf_get_section(wctx, wctx->level);
228 11934 int level = wctx->level;
229 int parent_row;
230
231
3/4
✓ Branch 3 → 4 taken 11934 times.
✗ Branch 3 → 5 not taken.
✓ Branch 4 → 5 taken 3050 times.
✓ Branch 4 → 6 taken 8884 times.
11934 if (!section || compact->row_level[level] != level)
232 3050 return;
233
234 /* the row's line, followed by the lines of the rows nested in it */
235 8884 av_bprint_chars(&compact->line[level], '\n', 1);
236 8884 bprint_append_bprint(&compact->line[level], &compact->rows[level]);
237
238
1/2
✓ Branch 8 → 9 taken 8884 times.
✗ Branch 8 → 10 not taken.
8884 parent_row = level ? compact->row_level[level - 1] : -1;
239
2/2
✓ Branch 11 → 12 taken 64 times.
✓ Branch 11 → 13 taken 8820 times.
8884 if (parent_row >= 0)
240 64 bprint_append_bprint(&compact->rows[parent_row], &compact->line[level]);
241 else
242 8820 writer_put_str(wctx, compact->line[level].str);
243 }
244
245 66423 static void compact_print_field(AVTextFormatContext *wctx, const char *key, const char *value)
246 {
247 66423 CompactContext *compact = wctx->priv;
248 66423 int row = compact->row_level[wctx->level];
249 66423 AVBPrint tmp, *line = &tmp;
250
251
1/2
✓ Branch 2 → 3 taken 66423 times.
✗ Branch 2 → 6 not taken.
66423 if (row >= 0) {
252 66423 line = &compact->line[row];
253
4/4
✓ Branch 3 → 4 taken 8878 times.
✓ Branch 3 → 5 taken 57545 times.
✓ Branch 4 → 5 taken 7778 times.
✓ Branch 4 → 7 taken 1100 times.
66423 if (compact->nb_fields[row]++ || compact->print_section)
254 65323 av_bprint_chars(line, compact->item_sep, 1);
255 } else {
256 /* a field outside of any row */
257 ✗ av_bprint_init(&tmp, 1, AV_BPRINT_SIZE_UNLIMITED);
258 }
259
260
2/2
✓ Branch 7 → 8 taken 60268 times.
✓ Branch 7 → 9 taken 6155 times.
66423 if (!compact->nokey)
261 60268 av_bprintf(line, "%s%s=", wctx->section_pbuf[wctx->level].str, key);
262 66423 av_bprintf(line, "%s", value);
263
264
1/2
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 13 taken 66423 times.
66423 if (line == &tmp) {
265 ✗ writer_put_str(wctx, tmp.str);
266 ✗ av_bprint_finalize(&tmp, NULL);
267 }
268 66423 }
269
270 33465 static void compact_print_str(AVTextFormatContext *wctx, const char *key, const char *value)
271 {
272 33465 CompactContext *compact = wctx->priv;
273 AVBPrint buf;
274
275 33465 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
276 33465 compact_print_field(wctx, key, compact->escape_str(&buf, value, compact->item_sep, wctx));
277 33465 av_bprint_finalize(&buf, NULL);
278 33465 }
279
280 32958 static void compact_print_int(AVTextFormatContext *wctx, const char *key, int64_t value)
281 {
282 char buf[32];
283
284 32958 snprintf(buf, sizeof(buf), "%"PRId64, value);
285 32958 compact_print_field(wctx, key, buf);
286 32958 }
287
288 const AVTextFormatter avtextformatter_compact = {
289 .name = "compact",
290 .priv_size = sizeof(CompactContext),
291 .init = compact_init,
292 .uninit = compact_uninit,
293 .print_section_header = compact_print_section_header,
294 .print_section_footer = compact_print_section_footer,
295 .print_integer = compact_print_int,
296 .print_string = compact_print_str,
297 .flags = AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS,
298 .priv_class = &compact_class,
299 };
300
301 /* CSV output */
302
303 #undef OFFSET
304 #define OFFSET(x) offsetof(CompactContext, x)
305
306 static const AVOption csv_options[] = {
307 { "item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, { .str = "," }, 0, 0 },
308 { "s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, { .str = "," }, 0, 0 },
309 { "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1 },
310 { "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1 },
311 { "escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, { .str = "csv" }, 0, 0 },
312 { "e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, { .str = "csv" }, 0, 0 },
313 { "print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1 },
314 { "p", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1 },
315 { NULL },
316 };
317
318 DEFINE_FORMATTER_CLASS(csv);
319
320 const AVTextFormatter avtextformatter_csv = {
321 .name = "csv",
322 .priv_size = sizeof(CompactContext),
323 .init = compact_init,
324 .uninit = compact_uninit,
325 .print_section_header = compact_print_section_header,
326 .print_section_footer = compact_print_section_footer,
327 .print_integer = compact_print_int,
328 .print_string = compact_print_str,
329 .flags = AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS,
330 .priv_class = &csv_class,
331 };
332