xref: /freebsd/contrib/libxo/tests/core/test_09.c (revision a3557ef0)
1 /*
2  * Copyright (c) 2015, 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 2015
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 #include "xo_encoder.h"
19 
20 int
21 main (int argc, char **argv)
22 {
23     struct item {
24 	const char *i_title;
25 	int i_count;
26     };
27     struct item list[] = {
28 	{ "gum", 1412 },
29 	{ "rope", 85 },
30 	{ "ladder", 0 },
31 	{ "bolt", 4123 },
32 	{ "water", 17 },
33 	{ NULL, 0 }
34     };
35     struct item *ip;
36     int i;
37 
38     argc = xo_parse_args(argc, argv);
39     if (argc < 0)
40 	return 1;
41 
42     for (argc = 1; argv[argc]; argc++) {
43 	if (xo_streq(argv[argc], "xml"))
44 	    xo_set_style(NULL, XO_STYLE_XML);
45 	else if (xo_streq(argv[argc], "json"))
46 	    xo_set_style(NULL, XO_STYLE_JSON);
47 	else if (xo_streq(argv[argc], "text"))
48 	    xo_set_style(NULL, XO_STYLE_TEXT);
49 	else if (xo_streq(argv[argc], "html"))
50 	    xo_set_style(NULL, XO_STYLE_HTML);
51 	else if (xo_streq(argv[argc], "pretty"))
52 	    xo_set_flags(NULL, XOF_PRETTY);
53 	else if (xo_streq(argv[argc], "xpath"))
54 	    xo_set_flags(NULL, XOF_XPATH);
55 	else if (xo_streq(argv[argc], "info"))
56 	    xo_set_flags(NULL, XOF_INFO);
57         else if (xo_streq(argv[argc], "error")) {
58             close(-1);
59             xo_err(1, "error detected");
60         }
61     }
62 
63     xo_set_flags(NULL, XOF_KEYS);
64     xo_set_program("test");
65 
66     xo_open_container_h(NULL, "top");
67 
68     xo_open_container("data");
69     xo_open_container("contents");
70 
71     xo_emit("{T:Item/%-10s}{T:Count/%12s}\n");
72 
73     for (ip = list; ip->i_title; ip++) {
74 	xo_emit("Name: {l:name/%-10s/%s}\n", ip->i_title);
75     }
76 
77     xo_close_container("contents");
78 
79     xo_emit("\n\n");
80     xo_open_container("contents");
81 
82     xo_emit("{T:Item/%-10s}{T:Count/%12s}\n");
83 
84     for (ip = list; ip->i_title; ip++) {
85 	xo_emit("Name: {l:item/%-10s/%s}\n", ip->i_title);
86     }
87 
88     xo_close_container("contents");
89 
90     xo_emit("\n\n");
91 
92     xo_open_container("contents");
93     xo_emit("{T:Test/%-10s}{T:Three/%12s}\n");
94 
95     xo_open_list("item");
96     for (ip = list; ip->i_title; ip++) {
97 	xo_emit("Name: {l:item/%-10s/%s}\n", ip->i_title);
98     }
99     xo_emit("{Lwc:/Total:}{:total}\n", "six");
100 
101     xo_emit("{:one}", "one");
102     xo_emit("{l:two}", "two");
103     xo_emit("{:three}", "three");
104 
105 
106     xo_close_container("contents");
107 
108     xo_emit("\n\n");
109 
110     xo_close_container_h(NULL, "top");
111 
112     xo_finish();
113 
114     return 0;
115 }
116