tools/bpf/bpftool/json_writer.c
Source file repositories/reference/linux-study-clean/tools/bpf/bpftool/json_writer.c
File Facts
- System
- Linux kernel
- Corpus path
tools/bpf/bpftool/json_writer.c- Extension
.c- Size
- 6797 bytes
- Lines
- 356
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
stdio.hstdbool.hstdarg.hassert.hmalloc.hinttypes.hstdint.hjson_writer.h
Detected Declarations
struct json_writerfunction jsonw_indentfunction jsonw_eolfunction jsonw_eorfunction jsonw_putsfunction jsonw_destroyfunction jsonw_prettyfunction jsonw_resetfunction jsonw_beginfunction jsonw_endfunction jsonw_namefunction jsonw_vprintf_enquotefunction jsonw_printffunction jsonw_start_objectfunction jsonw_end_objectfunction jsonw_start_arrayfunction jsonw_end_arrayfunction jsonw_stringfunction jsonw_boolfunction jsonw_nullfunction jsonw_float_fmtfunction jsonw_floatfunction jsonw_hufunction jsonw_uintfunction jsonw_lluintfunction jsonw_intfunction jsonw_string_fieldfunction jsonw_bool_fieldfunction jsonw_float_fieldfunction jsonw_float_field_fmtfunction jsonw_uint_fieldfunction jsonw_hu_fieldfunction jsonw_lluint_fieldfunction jsonw_int_fieldfunction jsonw_null_fieldfunction main
Annotated Snippet
struct json_writer {
FILE *out; /* output file */
unsigned depth; /* nesting */
bool pretty; /* optional whitepace */
char sep; /* either nul or comma */
};
/* indentation for pretty print */
static void jsonw_indent(json_writer_t *self)
{
unsigned i;
for (i = 0; i < self->depth; ++i)
fputs(" ", self->out);
}
/* end current line and indent if pretty printing */
static void jsonw_eol(json_writer_t *self)
{
if (!self->pretty)
return;
putc('\n', self->out);
jsonw_indent(self);
}
/* If current object is not empty print a comma */
static void jsonw_eor(json_writer_t *self)
{
if (self->sep != '\0')
putc(self->sep, self->out);
self->sep = ',';
}
/* Output JSON encoded string */
/* Handles C escapes, does not do Unicode */
static void jsonw_puts(json_writer_t *self, const char *str)
{
putc('"', self->out);
for (; *str; ++str)
switch (*str) {
case '\t':
fputs("\\t", self->out);
break;
case '\n':
fputs("\\n", self->out);
break;
case '\r':
fputs("\\r", self->out);
break;
case '\f':
fputs("\\f", self->out);
break;
case '\b':
fputs("\\b", self->out);
break;
case '\\':
fputs("\\\\", self->out);
break;
case '"':
fputs("\\\"", self->out);
break;
default:
putc(*str, self->out);
}
putc('"', self->out);
}
/* Create a new JSON stream */
json_writer_t *jsonw_new(FILE *f)
{
json_writer_t *self = malloc(sizeof(*self));
if (self) {
self->out = f;
self->depth = 0;
self->pretty = false;
self->sep = '\0';
}
return self;
}
/* End output to JSON stream */
void jsonw_destroy(json_writer_t **self_p)
{
json_writer_t *self = *self_p;
assert(self->depth == 0);
fputs("\n", self->out);
fflush(self->out);
free(self);
Annotation
- Immediate include surface: `stdio.h`, `stdbool.h`, `stdarg.h`, `assert.h`, `malloc.h`, `inttypes.h`, `stdint.h`, `json_writer.h`.
- Detected declarations: `struct json_writer`, `function jsonw_indent`, `function jsonw_eol`, `function jsonw_eor`, `function jsonw_puts`, `function jsonw_destroy`, `function jsonw_pretty`, `function jsonw_reset`, `function jsonw_begin`, `function jsonw_end`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.