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
main(int argc,char ** argv)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 xo_open_list("item");
71
72 xo_emit("{T:Item/%-10s}{T:Count/%12s}\n");
73
74 for (ip = list; ip->i_title; ip++) {
75 xo_open_instance("item");
76
77 xo_emit("{k:name/%-10s/%s}{n:count/%12u/%u}\n",
78 ip->i_title, ip->i_count);
79
80 xo_close_instance("item");
81 }
82
83 xo_close_list("item");
84 xo_close_container("contents");
85 xo_close_container("data");
86
87 xo_emit("\n\n");
88
89 xo_open_container("data2");
90 xo_open_container("contents");
91
92 xo_emit("{T:Item/%-10s}{T:Count/%12s}\n");
93
94 for (ip = list; ip->i_title; ip++) {
95 xo_open_instance("item");
96
97 xo_emit("{k:name/%-10s/%s}{n:count/%12u/%u}\n",
98 ip->i_title, ip->i_count);
99 }
100
101 xo_close_container("data2");
102
103 xo_emit("\n\n");
104
105 xo_open_container("data3");
106 xo_open_marker("m1");
107 xo_open_container("contents");
108
109 xo_emit("{T:Item/%-10s}{T:Count/%12s}\n");
110
111 for (ip = list; ip->i_title; ip++) {
112 xo_open_instance("item");
113
114 xo_emit("{k:name/%-10s/%s}{n:count/%12u/%u}\n",
115 ip->i_title, ip->i_count);
116 }
117
118 xo_close_container("data3"); /* warn: fails at marker 'm1' */
119 xo_emit("{:test}", "one");
120
121 xo_close_marker("m1");
122 xo_close_container("data3"); /* this one works, post-marker */
123
124 xo_emit("\n\n");
125
126 xo_open_container("data4");
127 xo_open_marker("m1");
128 xo_open_container("contents");
129
130 xo_emit("{T:Item/%-10s}{T:Count/%12s}\n");
131
132 for (ip = list; ip->i_title; ip++) {
133 xo_open_instance("item");
134
135 xo_emit("{k:name/%-10s/%s}{n:count/%12u/%u}\n",
136 ip->i_title, ip->i_count);
137
138 xo_open_marker("m2");
139 for (i = 0; i < 3; i++) {
140 xo_open_instance("sub");
141 xo_emit("{Lwc:/Name}{:name/%d} + 1 = {:next/%d}\n", i, i + 1);
142 xo_close_container("data4"); /* warn: fails at marker 'm2' */
143 }
144 xo_close_marker("m2");
145 xo_emit("{Lwc:/Last}{:last/%d}\n", i);
146 }
147
148 xo_close_container("data4"); /* warn: fails at marker 'm1' */
149 xo_emit("{:test}", "one");
150
151 xo_emit("\n\n");
152
153 xo_close_container_h(NULL, "top");
154
155 xo_finish();
156
157 return 0;
158 }
159