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 <limits.h> | ||
22 | #include <stdarg.h> | ||
23 | #include <stdint.h> | ||
24 | #include <stdio.h> | ||
25 | #include <string.h> | ||
26 | |||
27 | #include "avtextformat.h" | ||
28 | #include "libavutil/bprint.h" | ||
29 | #include "libavutil/error.h" | ||
30 | #include "libavutil/opt.h" | ||
31 | #include "tf_internal.h" | ||
32 | |||
33 | /* Flat output */ | ||
34 | |||
35 | typedef struct FlatContext { | ||
36 | const AVClass *class; | ||
37 | const char *sep_str; | ||
38 | char sep; | ||
39 | int hierarchical; | ||
40 | } FlatContext; | ||
41 | |||
42 | #undef OFFSET | ||
43 | #define OFFSET(x) offsetof(FlatContext, x) | ||
44 | |||
45 | static const AVOption flat_options[] = { | ||
46 | { "sep_char", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, { .str = "." }, 0, 0 }, | ||
47 | { "s", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, { .str = "." }, 0, 0 }, | ||
48 | { "hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1 }, | ||
49 | { "h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1 }, | ||
50 | { NULL }, | ||
51 | }; | ||
52 | |||
53 | DEFINE_FORMATTER_CLASS(flat); | ||
54 | |||
55 | 2 | static av_cold int flat_init(AVTextFormatContext *wctx) | |
56 | { | ||
57 | 2 | FlatContext *flat = wctx->priv; | |
58 | |||
59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (strlen(flat->sep_str) != 1) { |
60 | ✗ | av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n", | |
61 | flat->sep_str); | ||
62 | ✗ | return AVERROR(EINVAL); | |
63 | } | ||
64 | 2 | flat->sep = flat->sep_str[0]; | |
65 | |||
66 | 2 | return 0; | |
67 | } | ||
68 | |||
69 | 374 | static const char *flat_escape_key_str(AVBPrint *dst, const char *src, const char sep) | |
70 | { | ||
71 | const char *p; | ||
72 | |||
73 |
2/2✓ Branch 0 taken 3931 times.
✓ Branch 1 taken 374 times.
|
4305 | for (p = src; *p; p++) { |
74 |
3/4✓ Branch 0 taken 3931 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3930 times.
✓ Branch 3 taken 1 times.
|
3931 | if (!((*p >= '0' && *p <= '9') || |
75 |
3/4✓ Branch 0 taken 3535 times.
✓ Branch 1 taken 395 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3535 times.
|
3930 | (*p >= 'a' && *p <= 'z') || |
76 |
3/4✓ Branch 0 taken 395 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 394 times.
✓ Branch 3 taken 1 times.
|
395 | (*p >= 'A' && *p <= 'Z'))) |
77 | 394 | av_bprint_chars(dst, '_', 1); | |
78 | else | ||
79 | 3537 | av_bprint_chars(dst, *p, 1); | |
80 | } | ||
81 | 374 | return dst->str; | |
82 | } | ||
83 | |||
84 | 374 | static const char *flat_escape_value_str(AVBPrint *dst, const char *src) | |
85 | { | ||
86 | const char *p; | ||
87 | |||
88 |
2/2✓ Branch 0 taken 2481 times.
✓ Branch 1 taken 374 times.
|
2855 | for (p = src; *p; p++) { |
89 |
2/7✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 2479 times.
|
2481 | switch (*p) { |
90 | ✗ | case '\n': av_bprintf(dst, "%s", "\\n"); break; | |
91 | ✗ | case '\r': av_bprintf(dst, "%s", "\\r"); break; | |
92 | ✗ | case '\\': av_bprintf(dst, "%s", "\\\\"); break; | |
93 | 2 | case '"': av_bprintf(dst, "%s", "\\\""); break; | |
94 | ✗ | case '`': av_bprintf(dst, "%s", "\\`"); break; | |
95 | ✗ | case '$': av_bprintf(dst, "%s", "\\$"); break; | |
96 | 2479 | default: av_bprint_chars(dst, *p, 1); break; | |
97 | } | ||
98 | } | ||
99 | 374 | return dst->str; | |
100 | } | ||
101 | |||
102 | 48 | static void flat_print_section_header(AVTextFormatContext *wctx, const void *data) | |
103 | { | ||
104 | 48 | FlatContext *flat = wctx->priv; | |
105 | 48 | AVBPrint *buf = &wctx->section_pbuf[wctx->level]; | |
106 | 48 | const AVTextFormatSection *section = tf_get_section(wctx, wctx->level); | |
107 | 48 | const AVTextFormatSection *parent_section = tf_get_parent_section(wctx, wctx->level); | |
108 | |||
109 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (!section) |
110 | ✗ | return; | |
111 | |||
112 | /* build section header */ | ||
113 | 48 | av_bprint_clear(buf); | |
114 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 46 times.
|
48 | if (!parent_section) |
115 | 2 | return; | |
116 | |||
117 | 46 | av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level - 1].str); | |
118 | |||
119 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
|
46 | if (flat->hierarchical || |
120 | ✗ | !(section->flags & (AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY | AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER))) { | |
121 | 46 | av_bprintf(buf, "%s%s", wctx->section[wctx->level]->name, flat->sep_str); | |
122 | |||
123 |
2/2✓ Branch 0 taken 34 times.
✓ Branch 1 taken 12 times.
|
46 | if (parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY) { |
124 | 68 | int n = parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_NUMBERING_BY_TYPE | |
125 | 28 | ? wctx->nb_item_type[wctx->level - 1][section->id] | |
126 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 6 times.
|
34 | : wctx->nb_item[wctx->level - 1]; |
127 | |||
128 | 34 | av_bprintf(buf, "%d%s", n, flat->sep_str); | |
129 | } | ||
130 | } | ||
131 | } | ||
132 | |||
133 | 318 | static void flat_print_int(AVTextFormatContext *wctx, const char *key, int64_t value) | |
134 | { | ||
135 | 318 | writer_printf(wctx, "%s%s=%"PRId64"\n", wctx->section_pbuf[wctx->level].str, key, value); | |
136 | 318 | } | |
137 | |||
138 | 374 | static void flat_print_str(AVTextFormatContext *wctx, const char *key, const char *value) | |
139 | { | ||
140 | 374 | FlatContext *flat = wctx->priv; | |
141 | AVBPrint buf; | ||
142 | |||
143 | 374 | writer_put_str(wctx, wctx->section_pbuf[wctx->level].str); | |
144 | 374 | av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED); | |
145 | 374 | writer_printf(wctx, "%s=", flat_escape_key_str(&buf, key, flat->sep)); | |
146 | 374 | av_bprint_clear(&buf); | |
147 | 374 | writer_printf(wctx, "\"%s\"\n", flat_escape_value_str(&buf, value)); | |
148 | 374 | av_bprint_finalize(&buf, NULL); | |
149 | 374 | } | |
150 | |||
151 | const AVTextFormatter avtextformatter_flat = { | ||
152 | .name = "flat", | ||
153 | .priv_size = sizeof(FlatContext), | ||
154 | .init = flat_init, | ||
155 | .print_section_header = flat_print_section_header, | ||
156 | .print_integer = flat_print_int, | ||
157 | .print_string = flat_print_str, | ||
158 | .flags = AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS | AV_TEXTFORMAT_FLAG_SUPPORTS_MIXED_ARRAY_CONTENT, | ||
159 | .priv_class = &flat_class, | ||
160 | }; | ||
161 |