xref: /freebsd/contrib/libxo/tests/core/test_03.c (revision 19261079b74319502c6ffa1249920079f0f69a72)
1 /*
2  * Copyright (c) 2014, Juniper Networks, Inc.
3  * All rights reserved.
4  * This SOFTWARE is licensed under the LICENSE provided in the
5  * ../Copyright file. By downloading, installing, copying, or otherwise
6  * using the SOFTWARE, you agree to be bound by the terms of that
7  * LICENSE.
8  * Phil Shafer, July 2014
9  */
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include "xo.h"
16 #include "xo_encoder.h"
17 
18 xo_info_t info[] = {
19     { "employee", "object", "Employee data" },
20     { "first-name", "string", "First name of employee" },
21     { "last-name", "string", "Last name of employee" },
22     { "department", "number", "Department number" },
23 };
24 int info_count = (sizeof(info) / sizeof(info[0]));
25 
26 int
27 main (int argc, char **argv)
28 {
29     unsigned opt_count = 1;
30     unsigned opt_extra = 0;
31 
32     struct employee {
33 	const char *e_first;
34 	const char *e_last;
35 	unsigned e_dept;
36     } employees[] = {
37 	{ "Terry", "Jones", 660 },
38 	{ "Leslie", "Patterson", 341 },
39 	{ "Ashley", "Smith", 1440 },
40 	{ NULL, NULL }
41     }, *ep;
42 
43     argc = xo_parse_args(argc, argv);
44     if (argc < 0)
45 	return 1;
46 
47     for (argc = 1; argv[argc]; argc++) {
48 	if (xo_streq(argv[argc], "count")) {
49 	    if (argv[argc + 1])
50 		opt_count = atoi(argv[++argc]);
51 	} else if (xo_streq(argv[argc], "extra")) {
52 	    if (argv[argc + 1])
53 		opt_extra = atoi(argv[++argc]);
54 	}
55     }
56 
57     xo_set_info(NULL, info, info_count);
58 
59     xo_open_container("employees");
60     xo_open_list("employee");
61 
62     xo_emit("[{:extra/%*s}]\n", opt_extra, "");
63 
64     xo_emit("{T:/%13s} {T:/%5s} {T:/%6s} {T:/%7s} {T:/%8s}  {T:Size(s)}\n",
65 	    "Type", "InUse", "MemUse", "HighUse", "Requests");
66     xo_open_list("memory");
67     xo_open_instance("memory");
68 
69 #define PRIu64 "llu"
70 #define TO_ULL(_x) ((unsigned long long) _x)
71     xo_emit("{k:type/%13s} {:in-use/%5" PRIu64 "} "
72 	    "{:memory-use/%5" PRIu64 "}{U:K} {:high-use/%7s} "
73 	    "{:requests/%8" PRIu64 "}  ",
74 	    "name", TO_ULL(12345), TO_ULL(54321), "-", TO_ULL(32145));
75 
76     int first = 1, i;
77 #if 0
78     xo_open_list("size");
79     for (i = 0; i < 32; i++) {
80 	if (!first)
81 	    xo_emit(",");
82 	xo_emit("{l:size/%d}", 1 << (i + 4));
83 	first = 0;
84     }
85     xo_close_list("size");
86 #endif
87     xo_close_instance("memory");
88     xo_emit("\n");
89     xo_close_list("memory");
90 
91     while (opt_count-- != 0) {
92 	for (ep = employees; ep->e_first; ep++) {
93 	    xo_open_instance("employee");
94 	    xo_emit("{:first-name} {:last-name} works in "
95 		    "dept #{:department/%u}\n",
96 		    ep->e_first, ep->e_last, ep->e_dept);
97 	    xo_close_instance("employee");
98 	}
99     }
100 
101     xo_emit("done\n");
102 
103     xo_close_list("employee");
104     xo_close_container("employees");
105 
106     xo_finish();
107 
108     return 0;
109 }
110