1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
/*
* util_fmt_example - Example program for util_fmt
*
* Copyright IBM Corp. 2024
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <stdlib.h>
#include <string.h>
#include "lib/util_base.h"
#include "lib/util_fmt.h"
#define API_LEVEL 1
static void meta_example(enum util_fmt_t format)
{
util_fmt_init(stdout, format, FMT_DEFAULT, API_LEVEL);
/*
* First call to util_fmt_obj_start() automatically adds meta-data
* object as required by s390-tools convention.
*/
util_fmt_obj_start(FMT_DEFAULT, NULL);
util_fmt_pair(FMT_QUOTE, "key", "value");
util_fmt_obj_end();
util_fmt_exit();
}
static void simple_example(enum util_fmt_t format, int fmt_flags)
{
/*
* Note: Meta-data is excluded in this example for readability but
* must be included in actual tool output.
*/
util_fmt_init(stdout, format, fmt_flags | FMT_NOMETA, API_LEVEL);
/*
* {
* "child": {
* "key": "value",
* "invalid":"invalidvalue" <== Marked as invalid
* }
* }
*/
util_fmt_obj_start(FMT_DEFAULT, NULL);
util_fmt_obj_start(FMT_DEFAULT, "child");
util_fmt_pair(FMT_QUOTE, "key", "value");
util_fmt_pair(FMT_QUOTE | FMT_INVAL, "invalid", "invalidvalue");
util_fmt_obj_end();
util_fmt_obj_end();
util_fmt_exit();
}
static void list_example(enum util_fmt_t format, int flags)
{
int i;
/*
* Note: Meta-data is excluded in this example for readability but
* must be included in actual tool output.
*/
util_fmt_init(stdout, format, flags | FMT_NOMETA, API_LEVEL);
/*
* "cond","key"
* condvalue0,value0
* "",value1
* "",value2
* "",value3
*/
util_fmt_obj_start(FMT_DEFAULT, NULL);
util_fmt_obj_start(FMT_LIST, "list");
for (i = 0; i < 4; i++) {
util_fmt_obj_start(FMT_ROW, NULL);
if (i == 0)
util_fmt_pair(flags, "cond", "condvalue%d", i);
util_fmt_pair(FMT_DEFAULT, "key", "value%d", i);
util_fmt_obj_end();
}
util_fmt_obj_end();
util_fmt_obj_end();
util_fmt_exit();
}
#define NUM_KEYS 4
static void vary_example(enum util_fmt_t format, bool add)
{
const char *keys[NUM_KEYS] = { "key_a", "key_b", "key_c", "key_d" };
int i;
/*
* Note: Meta-data is excluded in this example for readability but
* must be included in actual tool output.
*/
util_fmt_init(stdout, format, FMT_NOMETA, API_LEVEL);
if (add) {
/* Make keys known before starting output. */
for (i = 0; i < NUM_KEYS; i++)
util_fmt_add_key(keys[i]);
}
util_fmt_obj_start(FMT_LIST, "list");
for (i = 0; i < 4; i++) {
util_fmt_obj_start(FMT_ROW, NULL);
util_fmt_pair(FMT_DEFAULT, keys[i], "value%d", i);
util_fmt_obj_end();
}
util_fmt_obj_end();
util_fmt_exit();
}
static void filter_example(enum util_fmt_t format)
{
/*
* Note: Meta-data is excluded in this example for readability but
* must be included in actual tool output.
*/
util_fmt_init(stdout, format, FMT_FILTER | FMT_NOMETA, API_LEVEL);
util_fmt_add_key("key_a");
/*
* {
* "key_a": "value_a",
* "key_b": "value_b" <== Not announced via util_fmt_add_key()
* }
*/
util_fmt_obj_start(FMT_DEFAULT, NULL);
util_fmt_pair(FMT_QUOTE, "key_a", "value_a");
util_fmt_pair(FMT_QUOTE, "key_b", "value_b");
util_fmt_obj_end();
util_fmt_exit();
}
static void prefix_example(enum util_fmt_t format, bool do_prefix)
{
/*
* Note: Meta-data is excluded in this example for readability but
* must be included in actual tool output.
*/
util_fmt_init(stdout, format, FMT_NOMETA, API_LEVEL);
/*
* {
* "key": "value0",
* "obj1": { // Marked as prefix object
* "key": "value1"
* }
* }
*/
util_fmt_obj_start(FMT_DEFAULT, "obj0");
util_fmt_pair(FMT_QUOTE, "key", "value0");
util_fmt_obj_start(do_prefix ? FMT_PREFIX : FMT_DEFAULT, "obj1");
util_fmt_pair(FMT_QUOTE, "key", "value1");
util_fmt_obj_end();
util_fmt_obj_end();
util_fmt_exit();
}
static void announce(const char *example_name)
{
static int example_number;
int i;
if (example_number++ > 0)
printf("\n");
printf("%d. %s\n====", example_number, example_name);
for (i = strlen(example_name); i > 0; i--)
printf("=");
printf("\n");
}
int main(int UNUSED(argc), char *UNUSED(argv[]))
{
announce("JSON output");
simple_example(FMT_JSON, FMT_KEEPINVAL);
announce("JSON without invalid pairs");
simple_example(FMT_JSON, FMT_DEFAULT);
announce("JSON formatted as sequence");
simple_example(FMT_JSONSEQ, FMT_DEFAULT);
announce("Pairs output");
simple_example(FMT_PAIRS, FMT_KEEPINVAL);
announce("Pairs output without invalid pairs");
simple_example(FMT_PAIRS, FMT_DEFAULT);
announce("Pairs without prefix");
simple_example(FMT_PAIRS, FMT_NOPREFIX);
announce("CSV output");
simple_example(FMT_CSV, FMT_KEEPINVAL);
announce("CSV list output");
list_example(FMT_CSV, FMT_DEFAULT);
announce("CSV list with persistent cond value");
list_example(FMT_CSV, FMT_PERSIST);
announce("JSON with filtered key");
filter_example(FMT_JSON);
announce("Pairs with filtered key");
filter_example(FMT_PAIRS);
announce("CSV with filtered key");
filter_example(FMT_CSV);
announce("CSV list with varying keys");
vary_example(FMT_CSV, false);
announce("CSV list with pre-announced varying keys");
vary_example(FMT_CSV, true);
announce("JSON output with meta-data");
meta_example(FMT_JSON);
announce("JSON sequence output with meta-data");
meta_example(FMT_JSONSEQ);
announce("Pairs output with meta-data");
meta_example(FMT_PAIRS);
announce("CSV output with meta-data");
meta_example(FMT_CSV);
announce("JSON output with duplicate keys");
prefix_example(FMT_JSON, false);
announce("CSV output with duplicate keys");
prefix_example(FMT_CSV, false);
announce("CSV output with duplicate keys distinguished by prefix");
prefix_example(FMT_CSV, true);
return 0;
}
|