FFmpeg coverage


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