FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/fftools/textformat/tf_ini.c
Date: 2026-10-30 17:37:53
Exec Total Coverage
Lines: 43 51 84.3%
Functions: 4 4 100.0%
Branches: 19 29 65.5%

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
25 #include "avtextformat.h"
26
27 #include "libavutil/attributes.h"
28 #include "libavutil/bprint.h"
29 #include "libavutil/opt.h"
30 #include "tf_internal.h"
31
32 /* INI format output */
33
34 typedef struct INIContext {
35 const AVClass *class;
36 int hierarchical;
37 } INIContext;
38
39 #undef OFFSET
40 #define OFFSET(x) offsetof(INIContext, x)
41
42 static const AVOption ini_options[] = {
43 { "hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1 },
44 { "h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1 },
45 { NULL },
46 };
47
48 DEFINE_FORMATTER_CLASS(ini);
49
50 740 static char *ini_escape_str(AVBPrint *dst, const char *src)
51 {
52 740 int i = 0;
53 char c;
54
55
2/2
✓ Branch 0 taken 6309 times.
✓ Branch 1 taken 740 times.
7049 while ((c = src[i++])) {
56
2/7
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 14 times.
✓ Branch 6 taken 6295 times.
6309 switch (c) {
57 ✗ case '\b': av_bprintf(dst, "%s", "\\b"); break;
58 ✗ case '\f': av_bprintf(dst, "%s", "\\f"); break;
59 ✗ case '\n': av_bprintf(dst, "%s", "\\n"); break;
60 ✗ case '\r': av_bprintf(dst, "%s", "\\r"); break;
61 ✗ case '\t': av_bprintf(dst, "%s", "\\t"); break;
62 14 case '\\':
63 case '#':
64 case '=':
65 case ':':
66 14 av_bprint_chars(dst, '\\', 1);
67 av_fallthrough;
68 6309 default:
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6309 times.
6309 if ((unsigned char)c < 32)
70 ✗ av_bprintf(dst, "\\x00%02x", (unsigned char)c);
71 else
72 6309 av_bprint_chars(dst, c, 1);
73 6309 break;
74 }
75 }
76 740 return dst->str;
77 }
78
79 42 static void ini_print_section_header(AVTextFormatContext *wctx, const void *data)
80 {
81 42 INIContext *ini = wctx->priv;
82 42 AVBPrint *buf = &wctx->section_pbuf[wctx->level];
83 42 const AVTextFormatSection *section = tf_get_section(wctx, wctx->level);
84 42 const AVTextFormatSection *parent_section = tf_get_parent_section(wctx, wctx->level);
85
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!section)
87 ✗ return;
88
89 42 av_bprint_clear(buf);
90
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 41 times.
42 if (!parent_section) {
91 1 writer_put_str(wctx, "# ffprobe output\n\n");
92 1 return;
93 }
94
95
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 3 times.
41 if (wctx->nb_item[wctx->level - 1])
96 38 writer_w8(wctx, '\n');
97
98 41 av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level - 1].str);
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
41 if (ini->hierarchical ||
100 ✗ !(section->flags & (AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY | AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER))) {
101
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 3 times.
41 av_bprintf(buf, "%s%s", buf->str[0] ? "." : "", wctx->section[wctx->level]->name);
102
103
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 10 times.
41 if (parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY) {
104 62 unsigned n = parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_NUMBERING_BY_TYPE
105 28 ? wctx->nb_item_type[wctx->level - 1][section->id]
106
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 3 times.
31 : wctx->nb_item[wctx->level - 1];
107 31 av_bprintf(buf, ".%u", n);
108 }
109 }
110
111
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 2 times.
41 if (!(section->flags & (AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY | AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER)))
112 39 writer_printf(wctx, "[%s]\n", buf->str);
113 }
114
115 370 static void ini_print_str(AVTextFormatContext *wctx, const char *key, const char *value)
116 {
117 AVBPrint buf;
118
119 370 av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
120 370 writer_printf(wctx, "%s=", ini_escape_str(&buf, key));
121 370 av_bprint_clear(&buf);
122 370 writer_printf(wctx, "%s\n", ini_escape_str(&buf, value));
123 370 av_bprint_finalize(&buf, NULL);
124 370 }
125
126 318 static void ini_print_int(AVTextFormatContext *wctx, const char *key, int64_t value)
127 {
128 318 writer_printf(wctx, "%s=%"PRId64"\n", key, value);
129 318 }
130
131 const AVTextFormatter avtextformatter_ini = {
132 .name = "ini",
133 .priv_size = sizeof(INIContext),
134 .print_section_header = ini_print_section_header,
135 .print_integer = ini_print_int,
136 .print_string = ini_print_str,
137 .flags = AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS | AV_TEXTFORMAT_FLAG_SUPPORTS_MIXED_ARRAY_CONTENT,
138 .priv_class = &ini_class,
139 };
140