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/opt.h" | ||
30 | |||
31 | #define writer_w8(wctx_, b_) (wctx_)->writer->writer->writer_w8((wctx_)->writer, b_) | ||
32 | #define writer_put_str(wctx_, str_) (wctx_)->writer->writer->writer_put_str((wctx_)->writer, str_) | ||
33 | #define writer_printf(wctx_, fmt_, ...) (wctx_)->writer->writer->writer_printf((wctx_)->writer, fmt_, __VA_ARGS__) | ||
34 | |||
35 | #define DEFINE_FORMATTER_CLASS(name) \ | ||
36 | static const char *name##_get_name(void *ctx) \ | ||
37 | { \ | ||
38 | return #name ; \ | ||
39 | } \ | ||
40 | static const AVClass name##_class = { \ | ||
41 | .class_name = #name, \ | ||
42 | .item_name = name##_get_name, \ | ||
43 | .option = name##_options \ | ||
44 | } | ||
45 | |||
46 | /* Default output */ | ||
47 | |||
48 | typedef struct DefaultContext { | ||
49 | const AVClass *class; | ||
50 | int nokey; | ||
51 | int noprint_wrappers; | ||
52 | int nested_section[SECTION_MAX_NB_LEVELS]; | ||
53 | } DefaultContext; | ||
54 | |||
55 | #undef OFFSET | ||
56 | #define OFFSET(x) offsetof(DefaultContext, x) | ||
57 | |||
58 | static const AVOption default_options[] = { | ||
59 | { "noprint_wrappers", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 }, | ||
60 | { "nw", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 }, | ||
61 | { "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 }, | ||
62 | { "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1 }, | ||
63 | {NULL}, | ||
64 | }; | ||
65 | |||
66 | ✗ | DEFINE_FORMATTER_CLASS(default); | |
67 | |||
68 | /* lame uppercasing routine, assumes the string is lower case ASCII */ | ||
69 | 4728 | static inline char *upcase_string(char *dst, size_t dst_size, const char *src) | |
70 | { | ||
71 | int i; | ||
72 |
3/4✓ Branch 0 taken 33192 times.
✓ Branch 1 taken 4728 times.
✓ Branch 2 taken 33192 times.
✗ Branch 3 not taken.
|
37920 | for (i = 0; src[i] && i < dst_size-1; i++) |
73 | 33192 | dst[i] = av_toupper(src[i]); | |
74 | 4728 | dst[i] = 0; | |
75 | 4728 | return dst; | |
76 | } | ||
77 | |||
78 | 3091 | static void default_print_section_header(AVTextFormatContext *wctx, const void *data) | |
79 | { | ||
80 | 3091 | DefaultContext *def = wctx->priv; | |
81 | char buf[32]; | ||
82 | 3091 | const struct AVTextFormatSection *section = wctx->section[wctx->level]; | |
83 | 6182 | const struct AVTextFormatSection *parent_section = wctx->level ? | |
84 |
2/2✓ Branch 0 taken 2984 times.
✓ Branch 1 taken 107 times.
|
3091 | wctx->section[wctx->level-1] : NULL; |
85 | |||
86 | 3091 | av_bprint_clear(&wctx->section_pbuf[wctx->level]); | |
87 |
2/2✓ Branch 0 taken 2984 times.
✓ Branch 1 taken 107 times.
|
3091 | if (parent_section && |
88 |
2/2✓ Branch 0 taken 848 times.
✓ Branch 1 taken 2136 times.
|
2984 | !(parent_section->flags & (AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER|AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY))) { |
89 | 848 | def->nested_section[wctx->level] = 1; | |
90 | 848 | av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:", | |
91 | 848 | wctx->section_pbuf[wctx->level-1].str, | |
92 | upcase_string(buf, sizeof(buf), | ||
93 | 848 | av_x_if_null(section->element_name, section->name))); | |
94 | } | ||
95 | |||
96 |
4/4✓ Branch 0 taken 3083 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 848 times.
✓ Branch 3 taken 2235 times.
|
3091 | if (def->noprint_wrappers || def->nested_section[wctx->level]) |
97 | 856 | return; | |
98 | |||
99 |
2/2✓ Branch 0 taken 1940 times.
✓ Branch 1 taken 295 times.
|
2235 | if (!(section->flags & (AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER|AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY))) |
100 | 1940 | writer_printf(wctx, "[%s]\n", upcase_string(buf, sizeof(buf), section->name)); | |
101 | } | ||
102 | |||
103 | 3091 | static void default_print_section_footer(AVTextFormatContext *wctx) | |
104 | { | ||
105 | 3091 | DefaultContext *def = wctx->priv; | |
106 | 3091 | const struct AVTextFormatSection *section = wctx->section[wctx->level]; | |
107 | char buf[32]; | ||
108 | |||
109 |
4/4✓ Branch 0 taken 3083 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 848 times.
✓ Branch 3 taken 2235 times.
|
3091 | if (def->noprint_wrappers || def->nested_section[wctx->level]) |
110 | 856 | return; | |
111 | |||
112 |
2/2✓ Branch 0 taken 1940 times.
✓ Branch 1 taken 295 times.
|
2235 | if (!(section->flags & (AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER|AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY))) |
113 | 1940 | writer_printf(wctx, "[/%s]\n", upcase_string(buf, sizeof(buf), section->name)); | |
114 | } | ||
115 | |||
116 | 8850 | static void default_print_str(AVTextFormatContext *wctx, const char *key, const char *value) | |
117 | { | ||
118 | 8850 | DefaultContext *def = wctx->priv; | |
119 | |||
120 |
2/2✓ Branch 0 taken 8846 times.
✓ Branch 1 taken 4 times.
|
8850 | if (!def->nokey) |
121 | 8846 | writer_printf(wctx, "%s%s=", wctx->section_pbuf[wctx->level].str, key); | |
122 | 8850 | writer_printf(wctx, "%s\n", value); | |
123 | 8850 | } | |
124 | |||
125 | 8922 | static void default_print_int(AVTextFormatContext *wctx, const char *key, int64_t value) | |
126 | { | ||
127 | 8922 | DefaultContext *def = wctx->priv; | |
128 | |||
129 |
1/2✓ Branch 0 taken 8922 times.
✗ Branch 1 not taken.
|
8922 | if (!def->nokey) |
130 | 8922 | writer_printf(wctx, "%s%s=", wctx->section_pbuf[wctx->level].str, key); | |
131 | 8922 | writer_printf(wctx, "%"PRId64"\n", value); | |
132 | 8922 | } | |
133 | |||
134 | const AVTextFormatter avtextformatter_default = { | ||
135 | .name = "default", | ||
136 | .priv_size = sizeof(DefaultContext), | ||
137 | .print_section_header = default_print_section_header, | ||
138 | .print_section_footer = default_print_section_footer, | ||
139 | .print_integer = default_print_int, | ||
140 | .print_string = default_print_str, | ||
141 | .flags = AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS, | ||
142 | .priv_class = &default_class, | ||
143 | }; | ||
144 |