Path: blob/main/contrib/libxo/tests/core/test_10.c
39537 views
/*1* Copyright (c) 2014, 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 20148*/910#include <stdio.h>11#include <stdlib.h>12#include <string.h>13#include <unistd.h>14#include <errno.h>15#include <ctype.h>1617#include "xo.h"18#include "xo_encoder.h"1920int21main (int argc, char **argv)22{23static char base_grocery[] = "GRO";24static char base_hardware[] = "HRD";25struct item {26const char *i_title;27int i_sold;28int i_instock;29int i_onorder;30const char *i_sku_base;31int i_sku_num;32};33struct item list[] = {34{ "gum", 1412, 54, 10, base_grocery, 415 },35{ "rope", 85, 4, 2, base_hardware, 212 },36{ "ladder", 0, 2, 1, base_hardware, 517 },37{ "bolt", 4123, 144, 42, base_hardware, 632 },38{ "water", 17, 14, 2, base_grocery, 2331 },39{ NULL, 0, 0, 0, NULL, 0 }40};41struct item list2[] = {42{ "fish", 1321, 45, 1, base_grocery, 533 },43{ NULL, 0, 0, 0, NULL, 0 }44};45struct item *ip;46xo_info_t info[] = {47{ "in-stock", "number", "Number of items in stock" },48{ "name", "string", "Name of the item" },49{ "on-order", "number", "Number of items on order" },50{ "sku", "string", "Stock Keeping Unit" },51{ "sold", "number", "Number of items sold" },52{ NULL, NULL, NULL },53};54int info_count = (sizeof(info) / sizeof(info[0])) - 1;5556argc = xo_parse_args(argc, argv);57if (argc < 0)58return 1;5960for (argc = 1; argv[argc]; argc++) {61if (xo_streq(argv[argc], "xml"))62xo_set_style(NULL, XO_STYLE_XML);63else if (xo_streq(argv[argc], "json"))64xo_set_style(NULL, XO_STYLE_JSON);65else if (xo_streq(argv[argc], "text"))66xo_set_style(NULL, XO_STYLE_TEXT);67else if (xo_streq(argv[argc], "html"))68xo_set_style(NULL, XO_STYLE_HTML);69else if (xo_streq(argv[argc], "pretty"))70xo_set_flags(NULL, XOF_PRETTY);71else if (xo_streq(argv[argc], "xpath"))72xo_set_flags(NULL, XOF_XPATH);73else if (xo_streq(argv[argc], "info"))74xo_set_flags(NULL, XOF_INFO);75else if (xo_streq(argv[argc], "error")) {76close(-1);77xo_err(1, "error detected");78}79}8081xo_set_info(NULL, info, info_count);82xo_set_flags(NULL, XOF_KEYS);8384/* Normally one would use "XOF_COLOR_ALLOWED", but we want to force it */85xo_set_flags(NULL, XOF_COLOR);8687xo_set_version("3.1.4");8889xo_open_container_h(NULL, "top");9091xo_attr("test", "value");92xo_open_container("data");93xo_open_list("item");94xo_attr("test2", "value2");9596static const char *colors[] =97{ "blue", "green", "red", "yellow", "default", NULL };9899int i;100for (i = 0; colors[i]; i++) {101if (i > 0)102xo_emit("{C:/bg-%s}", colors[i-1]);103xo_emit("{C:/fg-%s}{T:/%s}", colors[i], colors[i]);104}105xo_emit("{C:reset}\n");106107xo_emit("{C:bold}{:data} {C:underline}{:data} {C:inverse}{:data} "108"{C:no-bold}{:data} {C:no-inverse}{:data} "109"{C:no-underline}{:data}\n",110"bold", "bold-ul", "triple", "inv-ul", "underline", "plain");111112xo_emit("{T:Item/%-10s}{C:bold,underline}{T:Total Sold/%12s}{C:no-bold}"113"{T:In Stock/%12s}{C:/%s}"114"{T:On Order/%12s}{C:normal}{T:SKU/%5s}\n", "inverse");115116#if 0117xo_finish();118return 0;119#endif120121for (ip = list; ip->i_title; ip++) {122xo_open_instance("item");123xo_attr("test3", "value3");124125xo_emit("{keq:sku/%s-%u/%s-000-%u}"126"{k:name/%-10s/%s}{n:sold/%12u/%u}"127"{C:/%s}{:in-stock/%12u/%u}{C:normal}"128"{C:/fg-%s}{:on-order/%12u/%u}{C:/fg-default}"129"{qkd:sku/%5s-000-%u/%s-000-%u}\n",130ip->i_sku_base, ip->i_sku_num,131ip->i_title, ip->i_sold,132(ip->i_instock < 5) ? "inverse" : "normal", ip->i_instock,133(ip->i_onorder > 5) ? "yellow" : "default", ip->i_onorder,134ip->i_sku_base, ip->i_sku_num);135136xo_close_instance("item");137}138139xo_close_list("item");140xo_close_container("data");141142xo_emit("\n\n");143144xo_open_container("data");145xo_open_list("item");146147for (ip = list; ip->i_title; ip++) {148xo_open_instance("item");149150xo_emit("{keq:sku/%s-%u/%s-000-%u}", ip->i_sku_base, ip->i_sku_num);151xo_emit("{L:Item} '{k:name/%s}':\n", ip->i_title);152xo_emit("{P: }{L:Total sold}: {n:sold/%u%s}\n",153ip->i_sold, ip->i_sold ? ".0" : "");154xo_emit("{P: }{Lcw:In stock}{C:inverse}{:in-stock/%u}{C:}\n",155ip->i_instock);156xo_emit("{P: }{Lcw:On order}{:on-order/%u}\n", ip->i_onorder);157xo_emit("{P: }{L:SKU}: {qkd:sku/%s-000-%u}\n",158ip->i_sku_base, ip->i_sku_num);159160xo_close_instance("item");161}162163xo_close_list("item");164xo_close_container("data");165166xo_open_container("data");167xo_open_list("item");168169for (ip = list2; ip->i_title; ip++) {170xo_open_instance("item");171172xo_emit("{keq:sku/%s-%u/%s-000-%u}", ip->i_sku_base, ip->i_sku_num);173xo_emit("{L:Item} '{k:name/%s}':\n", ip->i_title);174xo_emit("{P: }{C:bg-blue , fg-white, bold }{L:Total sold}: "175"{n:sold/%u%s}{C:}\n",176ip->i_sold, ip->i_sold ? ".0" : "");177xo_emit("{P: }{Lcw:In stock}{:in-stock/%u}\n", ip->i_instock);178xo_emit("{P: }{Lcw:On order}{:on-order/%u}\n", ip->i_onorder);179xo_emit("{P: }{L:SKU}: {qkd:sku/%s-000-%u}\n",180ip->i_sku_base, ip->i_sku_num);181182xo_close_instance("item");183}184185xo_close_list("item");186xo_close_container("data");187188xo_open_container("data");189xo_open_list("item");190191for (ip = list; ip->i_title; ip++) {192xo_attr("test4", "value4");193xo_emit("{Lwc:Item}{l:item}\n", ip->i_title);194}195196xo_close_list("item");197xo_close_container("data");198199xo_emit("X{P:}X", "epic fail");200xo_emit("X{T:}X", "epic fail");201xo_emit("X{N:}X", "epic fail");202xo_emit("X{L:}X\n", "epic fail");203204xo_emit("X{P: }X{Lwc:Cost}{:cost/%u}\n", 425);205xo_emit("X{P:/%30s}X{Lwc:Cost}{:cost/%u}\n", "", 455);206207xo_close_container_h(NULL, "top");208209xo_finish();210211return 0;212}213214215