Path: blob/main/contrib/libxo/tests/core/test_03.c
39537 views
/*1* Copyright (c) 2014, Juniper Networks, Inc.2* All rights reserved.3* This SOFTWARE is licensed under the LICENSE provided in the4* ../Copyright file. By downloading, installing, copying, or otherwise5* using the SOFTWARE, you agree to be bound by the terms of that6* LICENSE.7* Phil Shafer, July 20148*/910#include <stdio.h>11#include <stdlib.h>12#include <string.h>1314#include "xo.h"15#include "xo_encoder.h"1617xo_info_t info[] = {18{ "employee", "object", "Employee data" },19{ "first-name", "string", "First name of employee" },20{ "last-name", "string", "Last name of employee" },21{ "department", "number", "Department number" },22};23int info_count = (sizeof(info) / sizeof(info[0]));2425int26main (int argc, char **argv)27{28unsigned opt_count = 1;29unsigned opt_extra = 0;3031struct employee {32const char *e_first;33const char *e_last;34unsigned e_dept;35} employees[] = {36{ "Terry", "Jones", 660 },37{ "Leslie", "Patterson", 341 },38{ "Ashley", "Smith", 1440 },39{ NULL, NULL }40}, *ep;4142argc = xo_parse_args(argc, argv);43if (argc < 0)44return 1;4546for (argc = 1; argv[argc]; argc++) {47if (xo_streq(argv[argc], "count")) {48if (argv[argc + 1])49opt_count = atoi(argv[++argc]);50} else if (xo_streq(argv[argc], "extra")) {51if (argv[argc + 1])52opt_extra = atoi(argv[++argc]);53}54}5556xo_set_info(NULL, info, info_count);5758xo_open_container("employees");59xo_open_list("employee");6061xo_emit("[{:extra/%*s}]\n", opt_extra, "");6263xo_emit("{T:/%13s} {T:/%5s} {T:/%6s} {T:/%7s} {T:/%8s} {T:Size(s)}\n",64"Type", "InUse", "MemUse", "HighUse", "Requests");65xo_open_list("memory");66xo_open_instance("memory");6768#define PRIu64 "llu"69#define TO_ULL(_x) ((unsigned long long) _x)70xo_emit("{k:type/%13s} {:in-use/%5" PRIu64 "} "71"{:memory-use/%5" PRIu64 "}{U:K} {:high-use/%7s} "72"{:requests/%8" PRIu64 "} ",73"name", TO_ULL(12345), TO_ULL(54321), "-", TO_ULL(32145));7475int first = 1, i;76#if 077xo_open_list("size");78for (i = 0; i < 32; i++) {79if (!first)80xo_emit(",");81xo_emit("{l:size/%d}", 1 << (i + 4));82first = 0;83}84xo_close_list("size");85#endif86xo_close_instance("memory");87xo_emit("\n");88xo_close_list("memory");8990while (opt_count-- != 0) {91for (ep = employees; ep->e_first; ep++) {92xo_open_instance("employee");93xo_emit("{:first-name} {:last-name} works in "94"dept #{:department/%u}\n",95ep->e_first, ep->e_last, ep->e_dept);96xo_close_instance("employee");97}98}99100xo_emit("done\n");101102xo_close_list("employee");103xo_close_container("employees");104105xo_finish();106107return 0;108}109110111