xref: /freebsd/contrib/libxo/tests/core/test_03.c (revision d6b92ffa)
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 
17 xo_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 };
23 int info_count = (sizeof(info) / sizeof(info[0]));
24 
25 int
26 main (int argc, char **argv)
27 {
28     unsigned opt_count = 1;
29     unsigned opt_extra = 0;
30 
31     struct employee {
32 	const char *e_first;
33 	const char *e_last;
34 	unsigned e_dept;
35     } employees[] = {
36 	{ "Terry", "Jones", 660 },
37 	{ "Leslie", "Patterson", 341 },
38 	{ "Ashley", "Smith", 1440 },
39 	{ NULL, NULL }
40     }, *ep;
41 
42     argc = xo_parse_args(argc, argv);
43     if (argc < 0)
44 	return 1;
45 
46     for (argc = 1; argv[argc]; argc++) {
47 	if (strcmp(argv[argc], "count") == 0) {
48 	    if (argv[argc + 1])
49 		opt_count = atoi(argv[++argc]);
50 	} else if (strcmp(argv[argc], "extra") == 0) {
51 	    if (argv[argc + 1])
52 		opt_extra = atoi(argv[++argc]);
53 	}
54     }
55 
56     xo_set_info(NULL, info, info_count);
57 
58     xo_open_container("employees");
59     xo_open_list("employee");
60 
61     xo_emit("[{:extra/%*s}]\n", opt_extra, "");
62 
63     xo_emit("{T:/%13s} {T:/%5s} {T:/%6s} {T:/%7s} {T:/%8s}  {T:Size(s)}\n",
64 	    "Type", "InUse", "MemUse", "HighUse", "Requests");
65     xo_open_list("memory");
66     xo_open_instance("memory");
67 
68 #define PRIu64 "llu"
69 #define TO_ULL(_x) ((unsigned long long) _x)
70     xo_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));
74 
75     int first = 1, i;
76 #if 0
77     xo_open_list("size");
78     for (i = 0; i < 32; i++) {
79 	if (!first)
80 	    xo_emit(",");
81 	xo_emit("{l:size/%d}", 1 << (i + 4));
82 	first = 0;
83     }
84     xo_close_list("size");
85 #endif
86     xo_close_instance("memory");
87     xo_emit("\n");
88     xo_close_list("memory");
89 
90     while (opt_count-- != 0) {
91 	for (ep = employees; ep->e_first; ep++) {
92 	    xo_open_instance("employee");
93 	    xo_emit("{:first-name} {:last-name} works in "
94 		    "dept #{:department/%u}\n",
95 		    ep->e_first, ep->e_last, ep->e_dept);
96 	    xo_close_instance("employee");
97 	}
98     }
99 
100     xo_emit("done\n");
101 
102     xo_close_list("employee");
103     xo_close_container("employees");
104 
105     xo_finish();
106 
107     return 0;
108 }
109