Path: blob/main/contrib/libxo/tests/core/test_09.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");6970xo_emit("{T:Item/%-10s}{T:Count/%12s}\n");7172for (ip = list; ip->i_title; ip++) {73xo_emit("Name: {l:name/%-10s/%s}\n", ip->i_title);74}7576xo_close_container("contents");7778xo_emit("\n\n");79xo_open_container("contents");8081xo_emit("{T:Item/%-10s}{T:Count/%12s}\n");8283for (ip = list; ip->i_title; ip++) {84xo_emit("Name: {l:item/%-10s/%s}\n", ip->i_title);85}8687xo_close_container("contents");8889xo_emit("\n\n");9091xo_open_container("contents");92xo_emit("{T:Test/%-10s}{T:Three/%12s}\n");9394xo_open_list("item");95for (ip = list; ip->i_title; ip++) {96xo_emit("Name: {l:item/%-10s/%s}\n", ip->i_title);97}98xo_emit("{Lwc:/Total:}{:total}\n", "six");99100xo_emit("{:one}", "one");101xo_emit("{l:two}", "two");102xo_emit("{:three}", "three");103104105xo_close_container("contents");106107xo_emit("\n\n");108109xo_close_container_h(NULL, "top");110111xo_finish();112113return 0;114}115116117