FFmpeg
Loading...
Searching...
No Matches
tf_ini.c
Go to the documentation of this file.
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
28#include "libavutil/bprint.h"
29#include "libavutil/opt.h"
30#include "tf_internal.h"
31
32/* Default output */
33
34typedef struct DefaultContext {
35 const AVClass *class;
36 int nokey;
40
41/* INI format output */
42
43typedef struct INIContext {
44 const AVClass *class;
47
48#undef OFFSET
49#define OFFSET(x) offsetof(INIContext, x)
50
51static 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
58
59static char *ini_escape_str(AVBPrint *dst, const char *src)
60{
61 int i = 0;
62 char c;
63
64 while ((c = src[i++])) {
65 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 case '\\':
72 case '#':
73 case '=':
74 case ':':
75 av_bprint_chars(dst, '\\', 1);
77 default:
78 if ((unsigned char)c < 32)
79 av_bprintf(dst, "\\x00%02x", (unsigned char)c);
80 else
82 break;
83 }
84 }
85 return dst->str;
86}
87
88static void ini_print_section_header(AVTextFormatContext *wctx, const void *data)
89{
90 INIContext *ini = wctx->priv;
91 AVBPrint *buf = &wctx->section_pbuf[wctx->level];
92 const AVTextFormatSection *section = tf_get_section(wctx, wctx->level);
93 const AVTextFormatSection *parent_section = tf_get_parent_section(wctx, wctx->level);
94
95 if (!section)
96 return;
97
98 av_bprint_clear(buf);
99 if (!parent_section) {
100 writer_put_str(wctx, "# ffprobe output\n\n");
101 return;
102 }
103
104 if (wctx->nb_item[wctx->level - 1])
105 writer_w8(wctx, '\n');
106
107 av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level - 1].str);
108 if (ini->hierarchical ||
110 av_bprintf(buf, "%s%s", buf->str[0] ? "." : "", wctx->section[wctx->level]->name);
111
112 if (parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY) {
113 unsigned n = parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_NUMBERING_BY_TYPE
114 ? wctx->nb_item_type[wctx->level - 1][section->id]
115 : wctx->nb_item[wctx->level - 1];
116 av_bprintf(buf, ".%u", n);
117 }
118 }
119
121 writer_printf(wctx, "[%s]\n", buf->str);
122}
123
124static void ini_print_str(AVTextFormatContext *wctx, const char *key, const char *value)
125{
126 AVBPrint buf;
127
129 writer_printf(wctx, "%s=", ini_escape_str(&buf, key));
130 av_bprint_clear(&buf);
131 writer_printf(wctx, "%s\n", ini_escape_str(&buf, value));
133}
134
135static void ini_print_int(AVTextFormatContext *wctx, const char *key, int64_t value)
136{
137 writer_printf(wctx, "%s=%"PRId64"\n", key, value);
138}
139
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,
147 .priv_class = &ini_class,
148};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
#define SECTION_MAX_NB_LEVELS
const AVTextFormatter avtextformatter_ini
Definition tf_ini.c:140
#define AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS
#define AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER
the section only contains other sections, but has no data at its own level
#define AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY
the section contains an array of elements of the same type
#define AV_TEXTFORMAT_FLAG_SUPPORTS_MIXED_ARRAY_CONTENT
#define AV_TEXTFORMAT_SECTION_FLAG_NUMBERING_BY_TYPE
the items in this array section should be numbered individually by type
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition bprint.c:122
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition bprint.c:69
AVBPrint public header.
#define AV_BPRINT_SIZE_UNLIMITED
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
double value
Definition eval.c:102
const char * key
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition bprint.c:235
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition bprint.c:130
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition bprint.c:227
Macro definitions for various function/variable attributes.
#define av_fallthrough
Definition attributes.h:67
const char data[16]
Definition mxf.c:149
AVOptions.
Describe the class of an AVClass context structure.
Definition log.h:76
AVOption.
Definition opt.h:428
unsigned int nb_item_type[SECTION_MAX_NB_LEVELS][SECTION_MAX_NB_SECTIONS]
void * priv
private data for use by the filter
const AVTextFormatSection * section[SECTION_MAX_NB_LEVELS]
section per each level
int level
current level, starting from 0
unsigned int nb_item[SECTION_MAX_NB_LEVELS]
number of the item printed in the given section, starting from 0
AVBPrint section_pbuf[SECTION_MAX_NB_LEVELS]
generic print buffer dedicated to each section, used by various formatters
const char * name
int id
unique id identifying a section
int nested_section[SECTION_MAX_NB_LEVELS]
Definition tf_default.c:39
int hierarchical
Definition tf_ini.c:45
#define src
Definition vp8dsp.c:248
static const AVOption ini_options[]
Definition tf_ini.c:51
static char * ini_escape_str(AVBPrint *dst, const char *src)
Definition tf_ini.c:59
static void ini_print_section_header(AVTextFormatContext *wctx, const void *data)
Definition tf_ini.c:88
static void ini_print_str(AVTextFormatContext *wctx, const char *key, const char *value)
Definition tf_ini.c:124
#define OFFSET(x)
Definition tf_ini.c:49
static void ini_print_int(AVTextFormatContext *wctx, const char *key, int64_t value)
Definition tf_ini.c:135
Internal utilities for text formatters.
static const AVTextFormatSection * tf_get_section(AVTextFormatContext *tfc, int level)
Safely validate and access a section at a given level.
Definition tf_internal.h:42
static const AVTextFormatSection * tf_get_parent_section(AVTextFormatContext *tfc, int level)
Safely access the parent section.
Definition tf_internal.h:55
#define DEFINE_FORMATTER_CLASS(name)
Definition tf_internal.h:31
static void writer_w8(AVTextFormatContext *wctx, int b)
Definition tf_internal.h:63
static void writer_printf(AVTextFormatContext *wctx, const char *fmt,...)
Definition tf_internal.h:73
static void writer_put_str(AVTextFormatContext *wctx, const char *str)
Definition tf_internal.h:68
static double c[64]