xref: /freebsd/contrib/libxo/tests/core/test_01.c (revision 5b9c547c)
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 #include <unistd.h>
15 #include <errno.h>
16 
17 #include "xo.h"
18 
19 int
20 main (int argc, char **argv)
21 {
22     static char base_grocery[] = "GRO";
23     static char base_hardware[] = "HRD";
24     struct item {
25 	const char *i_title;
26 	int i_sold;
27 	int i_instock;
28 	int i_onorder;
29 	const char *i_sku_base;
30 	int i_sku_num;
31     };
32     struct item list[] = {
33 	{ "gum", 1412, 54, 10, base_grocery, 415 },
34 	{ "rope", 85, 4, 2, base_hardware, 212 },
35 	{ "ladder", 0, 2, 1, base_hardware, 517 },
36 	{ "bolt", 4123, 144, 42, base_hardware, 632 },
37 	{ "water", 17, 14, 2, base_grocery, 2331 },
38 	{ NULL, 0, 0, 0, NULL, 0 }
39     };
40     struct item list2[] = {
41 	{ "fish", 1321, 45, 1, base_grocery, 533 },
42 	{ NULL, 0, 0, 0, NULL, 0 }
43     };
44     struct item *ip;
45     xo_info_t info[] = {
46 	{ "in-stock", "number", "Number of items in stock" },
47 	{ "name", "string", "Name of the item" },
48 	{ "on-order", "number", "Number of items on order" },
49 	{ "sku", "string", "Stock Keeping Unit" },
50 	{ "sold", "number", "Number of items sold" },
51 	{ NULL, NULL, NULL },
52     };
53     int info_count = (sizeof(info) / sizeof(info[0])) - 1;
54 
55     argc = xo_parse_args(argc, argv);
56     if (argc < 0)
57 	return 1;
58 
59     for (argc = 1; argv[argc]; argc++) {
60 	if (strcmp(argv[argc], "xml") == 0)
61 	    xo_set_style(NULL, XO_STYLE_XML);
62 	else if (strcmp(argv[argc], "json") == 0)
63 	    xo_set_style(NULL, XO_STYLE_JSON);
64 	else if (strcmp(argv[argc], "text") == 0)
65 	    xo_set_style(NULL, XO_STYLE_TEXT);
66 	else if (strcmp(argv[argc], "html") == 0)
67 	    xo_set_style(NULL, XO_STYLE_HTML);
68 	else if (strcmp(argv[argc], "pretty") == 0)
69 	    xo_set_flags(NULL, XOF_PRETTY);
70 	else if (strcmp(argv[argc], "xpath") == 0)
71 	    xo_set_flags(NULL, XOF_XPATH);
72 	else if (strcmp(argv[argc], "info") == 0)
73 	    xo_set_flags(NULL, XOF_INFO);
74         else if (strcmp(argv[argc], "error") == 0) {
75             close(-1);
76             xo_err(1, "error detected");
77         }
78     }
79 
80     xo_set_info(NULL, info, info_count);
81     xo_set_flags(NULL, XOF_KEYS);
82 
83     xo_open_container_h(NULL, "top");
84 
85     xo_attr("test", "value");
86     xo_open_container("data");
87     xo_open_list("item");
88     xo_attr("test2", "value2");
89 
90     xo_emit("{T:Item/%-10s}{T:Total Sold/%12s}{T:In Stock/%12s}"
91 	    "{T:On Order/%12s}{T:SKU/%5s}\n");
92 
93     for (ip = list; ip->i_title; ip++) {
94 	xo_open_instance("item");
95 	xo_attr("test3", "value3");
96 
97 	xo_emit("{keq:sku/%s-%u/%s-000-%u}"
98 		"{k:name/%-10s/%s}{n:sold/%12u/%u}{:in-stock/%12u/%u}"
99 		"{:on-order/%12u/%u}{qkd:sku/%5s-000-%u/%s-000-%u}\n",
100 		ip->i_sku_base, ip->i_sku_num,
101 		ip->i_title, ip->i_sold, ip->i_instock, ip->i_onorder,
102 		ip->i_sku_base, ip->i_sku_num);
103 
104 	xo_close_instance("item");
105     }
106 
107     xo_close_list("item");
108     xo_close_container("data");
109 
110     xo_emit("\n\n");
111 
112     xo_open_container("data");
113     xo_open_list("item");
114 
115     for (ip = list; ip->i_title; ip++) {
116 	xo_open_instance("item");
117 
118 	xo_emit("{keq:sku/%s-%u/%s-000-%u}", ip->i_sku_base, ip->i_sku_num);
119 	xo_emit("{L:Item} '{k:name/%s}':\n", ip->i_title);
120 	xo_emit("{P:   }{L:Total sold}: {n:sold/%u%s}\n",
121 		ip->i_sold, ip->i_sold ? ".0" : "");
122 	xo_emit("{P:   }{Lcw:In stock}{:in-stock/%u}\n", ip->i_instock);
123 	xo_emit("{P:   }{Lcw:On order}{:on-order/%u}\n", ip->i_onorder);
124 	xo_emit("{P:   }{L:SKU}: {qkd:sku/%s-000-%u}\n",
125 		ip->i_sku_base, ip->i_sku_num);
126 
127 	xo_close_instance("item");
128     }
129 
130     xo_close_list("item");
131     xo_close_container("data");
132 
133     xo_open_container("data");
134     xo_open_list("item");
135 
136     for (ip = list2; ip->i_title; ip++) {
137 	xo_open_instance("item");
138 
139 	xo_emit("{keq:sku/%s-%u/%s-000-%u}", ip->i_sku_base, ip->i_sku_num);
140 	xo_emit("{L:Item} '{k:name/%s}':\n", ip->i_title);
141 	xo_emit("{P:   }{L:Total sold}: {n:sold/%u%s}\n",
142 		ip->i_sold, ip->i_sold ? ".0" : "");
143 	xo_emit("{P:   }{Lcw:In stock}{:in-stock/%u}\n", ip->i_instock);
144 	xo_emit("{P:   }{Lcw:On order}{:on-order/%u}\n", ip->i_onorder);
145 	xo_emit("{P:   }{L:SKU}: {qkd:sku/%s-000-%u}\n",
146 		ip->i_sku_base, ip->i_sku_num);
147 
148 	xo_close_instance("item");
149     }
150 
151     xo_close_list("item");
152     xo_close_container("data");
153 
154     xo_open_container("data");
155     xo_open_list("item");
156 
157     for (ip = list; ip->i_title; ip++) {
158 	xo_attr("test4", "value4");
159 	xo_emit("{Lwc:Item}{l:item}\n", ip->i_title);
160     }
161 
162     xo_close_list("item");
163     xo_close_container("data");
164 
165     xo_emit("X{P:}X", "epic fail");
166     xo_emit("X{T:}X", "epic fail");
167     xo_emit("X{N:}X", "epic fail");
168     xo_emit("X{L:}X\n", "epic fail");
169 
170     xo_emit("X{P:        }X{Lwc:Cost}{:cost/%u}\n", 425);
171     xo_emit("X{P:/%30s}X{Lwc:Cost}{:cost/%u}\n", "", 455);
172 
173     xo_close_container_h(NULL, "top");
174 
175     xo_finish();
176 
177     return 0;
178 }
179