1 /* 2 * Copyright (c) 2014, 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 2014 9 */ 10 11 #include <stdio.h> 12 #include <stdlib.h> 13 #include <string.h> 14 15 #include "xo.h" 16 17 int 18 main (int argc, char **argv) 19 { 20 static char base_grocery[] = "GRO"; 21 static char base_hardware[] = "HRD"; 22 struct item { 23 const char *i_title; 24 int i_sold; 25 int i_instock; 26 int i_onorder; 27 const char *i_sku_base; 28 int i_sku_num; 29 }; 30 struct item list[] = { 31 { "gum", 1412, 54, 10, base_grocery, 415 }, 32 { "rope", 85, 4, 2, base_hardware, 212 }, 33 { "ladder", 0, 2, 1, base_hardware, 517 }, 34 { "bolt", 4123, 144, 42, base_hardware, 632 }, 35 { "water", 17, 14, 2, base_grocery, 2331 }, 36 { NULL, 0, 0, 0, NULL, 0 } 37 }; 38 struct item list2[] = { 39 { "fish", 1321, 45, 1, base_grocery, 533 }, 40 { NULL, 0, 0, 0, NULL, 0 } 41 }; 42 struct item *ip; 43 xo_info_t info[] = { 44 { "in-stock", "number", "Number of items in stock" }, 45 { "name", "string", "Name of the item" }, 46 { "on-order", "number", "Number of items on order" }, 47 { "sku", "string", "Stock Keeping Unit" }, 48 { "sold", "number", "Number of items sold" }, 49 { NULL, NULL, NULL }, 50 }; 51 int info_count = (sizeof(info) / sizeof(info[0])) - 1; 52 53 argc = xo_parse_args(argc, argv); 54 if (argc < 0) 55 return 1; 56 57 for (argc = 1; argv[argc]; argc++) { 58 if (strcmp(argv[argc], "xml") == 0) 59 xo_set_style(NULL, XO_STYLE_XML); 60 else if (strcmp(argv[argc], "json") == 0) 61 xo_set_style(NULL, XO_STYLE_JSON); 62 else if (strcmp(argv[argc], "text") == 0) 63 xo_set_style(NULL, XO_STYLE_TEXT); 64 else if (strcmp(argv[argc], "html") == 0) 65 xo_set_style(NULL, XO_STYLE_HTML); 66 else if (strcmp(argv[argc], "pretty") == 0) 67 xo_set_flags(NULL, XOF_PRETTY); 68 else if (strcmp(argv[argc], "xpath") == 0) 69 xo_set_flags(NULL, XOF_XPATH); 70 else if (strcmp(argv[argc], "info") == 0) 71 xo_set_flags(NULL, XOF_INFO); 72 } 73 74 xo_set_info(NULL, info, info_count); 75 xo_set_flags(NULL, XOF_KEYS); 76 77 xo_open_container_h(NULL, "top"); 78 79 xo_open_container("data"); 80 xo_open_list("item"); 81 82 xo_emit("{T:Item/%-10s}{T:Total Sold/%12s}{T:In Stock/%12s}" 83 "{T:On Order/%12s}{T:SKU/%5s}\n"); 84 85 for (ip = list; ip->i_title; ip++) { 86 xo_open_instance("item"); 87 88 xo_emit("{keq:sku/%s-%u/%s-000-%u}" 89 "{k:name/%-10s/%s}{n:sold/%12u/%u}{:in-stock/%12u/%u}" 90 "{:on-order/%12u/%u}{qkd:sku/%5s-000-%u/%s-000-%u}\n", 91 ip->i_sku_base, ip->i_sku_num, 92 ip->i_title, ip->i_sold, ip->i_instock, ip->i_onorder, 93 ip->i_sku_base, ip->i_sku_num); 94 95 xo_close_instance("item"); 96 } 97 98 xo_close_list("item"); 99 xo_close_container("data"); 100 101 xo_emit("\n\n"); 102 103 xo_open_container("data"); 104 xo_open_list("item"); 105 106 for (ip = list; ip->i_title; ip++) { 107 xo_open_instance("item"); 108 109 xo_emit("{keq:sku/%s-%u/%s-000-%u}", ip->i_sku_base, ip->i_sku_num); 110 xo_emit("{L:Item} '{k:name/%s}':\n", ip->i_title); 111 xo_emit("{P: }{L:Total sold}: {n:sold/%u%s}\n", 112 ip->i_sold, ip->i_sold ? ".0" : ""); 113 xo_emit("{P: }{Lcw:In stock}{:in-stock/%u}\n", ip->i_instock); 114 xo_emit("{P: }{Lcw:On order}{:on-order/%u}\n", ip->i_onorder); 115 xo_emit("{P: }{L:SKU}: {qkd:sku/%s-000-%u}\n", 116 ip->i_sku_base, ip->i_sku_num); 117 118 xo_close_instance("item"); 119 } 120 121 xo_close_list("item"); 122 xo_close_container("data"); 123 124 xo_open_container("data"); 125 xo_open_list("item"); 126 127 for (ip = list2; ip->i_title; ip++) { 128 xo_open_instance("item"); 129 130 xo_emit("{keq:sku/%s-%u/%s-000-%u}", ip->i_sku_base, ip->i_sku_num); 131 xo_emit("{L:Item} '{k:name/%s}':\n", ip->i_title); 132 xo_emit("{P: }{L:Total sold}: {n:sold/%u%s}\n", 133 ip->i_sold, ip->i_sold ? ".0" : ""); 134 xo_emit("{P: }{Lcw:In stock}{:in-stock/%u}\n", ip->i_instock); 135 xo_emit("{P: }{Lcw:On order}{:on-order/%u}\n", ip->i_onorder); 136 xo_emit("{P: }{L:SKU}: {qkd:sku/%s-000-%u}\n", 137 ip->i_sku_base, ip->i_sku_num); 138 139 xo_close_instance("item"); 140 } 141 142 xo_close_list("item"); 143 xo_close_container("data"); 144 145 xo_close_container_h(NULL, "top"); 146 147 xo_finish(); 148 149 return 0; 150 } 151