Path: blob/main/contrib/libxo/tests/core/test_08.c
39537 views
/*1* Copyright (c) 2015, Juniper Networks, Inc.2* All rights reserved.3* This SOFTWARE is licensed under the LICENSE provided in the4* ../Copyright file. By downloading, installing, copying, or otherwise5* using the SOFTWARE, you agree to be bound by the terms of that6* LICENSE.7* Phil Shafer, July 20158*/910#include <stdio.h>11#include <stdlib.h>12#include <string.h>13#include <unistd.h>14#include <errno.h>1516#include "xo.h"17#include "xo_encoder.h"1819int20main (int argc, char **argv)21{22struct item {23const char *i_title;24int i_count;25};26struct item list[] = {27{ "gum", 1412 },28{ "rope", 85 },29{ "ladder", 0 },30{ "bolt", 4123 },31{ "water", 17 },32{ NULL, 0 }33};34struct item *ip;35int i;3637argc = xo_parse_args(argc, argv);38if (argc < 0)39return 1;4041for (argc = 1; argv[argc]; argc++) {42if (xo_streq(argv[argc], "xml"))43xo_set_style(NULL, XO_STYLE_XML);44else if (xo_streq(argv[argc], "json"))45xo_set_style(NULL, XO_STYLE_JSON);46else if (xo_streq(argv[argc], "text"))47xo_set_style(NULL, XO_STYLE_TEXT);48else if (xo_streq(argv[argc], "html"))49xo_set_style(NULL, XO_STYLE_HTML);50else if (xo_streq(argv[argc], "pretty"))51xo_set_flags(NULL, XOF_PRETTY);52else if (xo_streq(argv[argc], "xpath"))53xo_set_flags(NULL, XOF_XPATH);54else if (xo_streq(argv[argc], "info"))55xo_set_flags(NULL, XOF_INFO);56else if (xo_streq(argv[argc], "error")) {57close(-1);58xo_err(1, "error detected");59}60}6162xo_set_flags(NULL, XOF_KEYS);63xo_set_program("test");6465xo_open_container_h(NULL, "top");6667xo_open_container("data");68xo_open_container("contents");69xo_open_list("item");7071xo_emit("{T:Item/%-10s}{T:Count/%12s}\n");7273for (ip = list; ip->i_title; ip++) {74xo_open_instance("item");7576xo_emit("{k:name/%-10s/%s}{n:count/%12u/%u}\n",77ip->i_title, ip->i_count);7879xo_close_instance("item");80}8182xo_close_list("item");83xo_close_container("contents");84xo_close_container("data");8586xo_emit("\n\n");8788xo_open_container("data2");89xo_open_container("contents");9091xo_emit("{T:Item/%-10s}{T:Count/%12s}\n");9293for (ip = list; ip->i_title; ip++) {94xo_open_instance("item");9596xo_emit("{k:name/%-10s/%s}{n:count/%12u/%u}\n",97ip->i_title, ip->i_count);98}99100xo_close_container("data2");101102xo_emit("\n\n");103104xo_open_container("data3");105xo_open_marker("m1");106xo_open_container("contents");107108xo_emit("{T:Item/%-10s}{T:Count/%12s}\n");109110for (ip = list; ip->i_title; ip++) {111xo_open_instance("item");112113xo_emit("{k:name/%-10s/%s}{n:count/%12u/%u}\n",114ip->i_title, ip->i_count);115}116117xo_close_container("data3"); /* warn: fails at marker 'm1' */118xo_emit("{:test}", "one");119120xo_close_marker("m1");121xo_close_container("data3"); /* this one works, post-marker */122123xo_emit("\n\n");124125xo_open_container("data4");126xo_open_marker("m1");127xo_open_container("contents");128129xo_emit("{T:Item/%-10s}{T:Count/%12s}\n");130131for (ip = list; ip->i_title; ip++) {132xo_open_instance("item");133134xo_emit("{k:name/%-10s/%s}{n:count/%12u/%u}\n",135ip->i_title, ip->i_count);136137xo_open_marker("m2");138for (i = 0; i < 3; i++) {139xo_open_instance("sub");140xo_emit("{Lwc:/Name}{:name/%d} + 1 = {:next/%d}\n", i, i + 1);141xo_close_container("data4"); /* warn: fails at marker 'm2' */142}143xo_close_marker("m2");144xo_emit("{Lwc:/Last}{:last/%d}\n", i);145}146147xo_close_container("data4"); /* warn: fails at marker 'm1' */148xo_emit("{:test}", "one");149150xo_emit("\n\n");151152xo_close_container_h(NULL, "top");153154xo_finish();155156return 0;157}158159160