NAME
ort_lang_c_source
—
generate JSON representation of
openradtool configuration
LIBRARY
library “libort-lang-json”
SYNOPSIS
#include
<sys/queue.h>
#include <stdio.h>
#include <ort.h>
#include <ort-lang-json.h>
int
ort_lang_json
(const struct
ort_lang_json *args, const struct config *cfg,
FILE *f);
DESCRIPTION
Outputs a JSON file representing the parsed configuration cfg to f with the parameters in args. The values in args are as follows:
- unsigned int flags
- If set to
ORT_LANG_JSON_FRAGMENT
, do not generate squiggly braces around the JSON content.
The output is not pretty-printed in any way: JSON tokens are only space separated.
To compile and link a simple file sample.c using this function, use pkg-config(1):
% cc `pkg-config --cflags ort-lang-json` -c -o sample.o sample.c % cc -o sample sample.o `pkg-config --libs ort-lang-json`
RETURN VALUES
Returns zero on failure, non-zero on success. Failure only occurs with memory allocation errors or when writing to f.
FILES
- ort-json.ts
- Typescript definition of the output. This is usually installed in /usr/local/share/openradtool, but may be elsewhere.
EXAMPLES
The following emits a configuration as read from standard input to JSON as written to standard output.
struct config *cfg; struct ort_lang_json args; memset(&args, 0, sizeof(struct ort_lang_json)); if ((cfg = ort_config_alloc()) == NULL) err(1, NULL); if (!ort_parse_file(cfg, stdin, "<stdin>")) errx(1, "ort_parse_file"); if (!ort_parse_close(cfg)) errx(1, "ort_parse_close"); if (!ort_lang_json(&args, cfg, stdout)) err(1, "ort_lang_json"); ort_config_free(cfg);
Alternatively, this could be converted into a string:
char *buf = NULL; size_t bufsz = 0; /* Read and parse configuration as above... */ if ((stream = open_memstream(&buf, &bufsz)) == NULL) err(1, "open_memstream"); if (!ort_lang_json(&args, cfg, stream)) err(1, "ort_lang_json"); fclose(stream); /* Do something with buf of size bufsz. */ free(buf);