xref: /freebsd/contrib/libxo/tests/core/test_05.c (revision b00ab754)
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 #include <wchar.h>
15 
16 #include "xo.h"
17 #include "xo_config.h"
18 
19 #ifdef LIBXO_WCWIDTH
20 #include "xo_wcwidth.h"
21 #else /* LIBXO_WCWIDTH */
22 #define xo_wcwidth(_x) wcwidth(_x)
23 #endif /* LIBXO_WCWIDTH */
24 
25 xo_info_t info[] = {
26     { "employee", "object", "Employee data" },
27     { "first-name", "string", "First name of employee" },
28     { "last-name", "string", "Last name of employee" },
29     { "department", "number", "Department number" },
30     { "percent-time", "number", "Percentage of full & part time (%)" },
31 };
32 int info_count = (sizeof(info) / sizeof(info[0]));
33 
34 int
35 main (int argc, char **argv)
36 {
37     struct employee {
38 	const char *e_first;
39 	const char *e_nic;
40 	const char *e_last;
41 	unsigned e_dept;
42 	unsigned e_percent;
43     } employees[] = {
44 	{ "Jim", "რეგტ", "გთხოვთ ახ", 431, 90 },
45 	{ "Terry", "<one", "Οὐχὶ ταὐτὰ παρίσταταί μοι Jones", 660, 90 },
46 	{ "Leslie", "Les", "Patterson", 341,60 },
47 	{ "Ashley", "Ash", "Meter & Smith", 1440, 40 },
48 	{ "0123456789", "0123456789", "012345678901234567890", 1440, 40 },
49 	{ "ახლა", "გაიარო", "საერთაშორისო", 123, 90 },
50 	{ "෴ණ්ණ෴෴ණ්ණ෴", "Mick",
51 	  "෴ණ්ණ෴෴ණ්ණ෴෴ණ්ණ෴෴෴", 110, 20 },
52 	{ NULL, NULL }
53     }, *ep = employees;
54     int rc, i;
55 
56     argc = xo_parse_args(argc, argv);
57     if (argc < 0)
58 	return 1;
59 
60     xo_set_info(NULL, info, info_count);
61     xo_set_flags(NULL, XOF_COLUMNS);
62 
63     xo_open_container("indian-languages");
64 
65     xo_emit("{T:Sample text}\n");
66     xo_emit("This sample text was taken from the Punjabi Wikipedia "
67 	    "article on Lahore and transliterated into the Latin script.\n");
68 
69     xo_emit("{T:Gurmukhi:}\n");
70     xo_emit("{:gurmukhi}\n",
71 	    "ਲਹੌਰ ਪਾਕਿਸਤਾਨੀ ਪੰਜਾਬ ਦੀ ਰਾਜਧਾਨੀ ਹੈ । ਲੋਕ ਗਿਣਤੀ ਦੇ ਨਾਲ ਕਰਾਚੀ ਤੋਂ ਬਾਅਦ ਲਹੌਰ ਦੂਜਾ ਸਭ ਤੋਂ ਵੱਡਾ ਸ਼ਹਿਰ ਹੈ । ਲਹੌਰ ਪਾਕਿਸਤਾਨ ਦਾ ਸਿਆਸੀ, ਰਹਤਲੀ ਤੇ ਪੜ੍ਹਾਈ ਦਾ ਗੜ੍ਹ ਹੈ ਅਤੇ ਇਸ ਲਈ ਇਹਨੂੰ ਪਾਕਿਸਤਾਨ ਦਾ ਦਿਲ ਵੀ ਕਿਹਾ ਜਾਂਦਾ ਹੈ । ਲਹੌਰ ਦਰਿਆ-ਏ-ਰਾਵੀ ਦੇ ਕੰਢੇ ਤੇ ਵਸਦਾ ਹੈ ਤੇ ਇਸਦੀ ਲੋਕ ਗਿਣਤੀ ਇੱਕ ਕਰੋੜ ਦੇ ਨੇੜੇ ਹੈ ।");
72 
73 
74     xo_emit("{T:Shahmukhi:}\n");
75     xo_emit("{:shahmukhi}\n",
76 	    "لہور پاکستانی پنجاب دا دارالحکومت اے۔ لوک گنتی دے نال کراچی توں بعد لہور دوجا سبھ توں وڈا شہر اے۔ لہور پاکستان دا سیاسی، رہتلی تے پڑھائی دا گڑھ اے تے اس لئی ایھنوں پاکستان دا دل وی کیھا جاندا اے۔ لہور دریاۓ راوی دے کنڈھے تے وسدا اے اسدی لوک گنتی اک کروڑ دے نیڑے اے ۔");
77 
78     xo_emit("{T:Transliteration}:\n");
79     xo_emit("{:tranliteration}\n",
80 	    "lahor pākistān panjāb dā dārul hakūmat ē. lōk giṇtī dē nāḷ karācī tō᷈ bāad lahor dūjā sab tō᷈ vaḍḍā shahr ē. lahor pākistān dā siāsī, rahtalī tē paṛā̀ī dā gā́ṛ ē tē is laī ihnū᷈ pākistān dā dil vī kehā jāndā ē. lahor dariāē rāvī dē kanḍē tē vasdā ē. isdī lōk giṇtī ikk karōṛ dē nēṛē ē.");
81 
82     xo_close_container("indian-languages");
83 
84     xo_open_container("employees");
85 
86     wchar_t wc[] = { L'෴', L'ණ', L'්', L'ණ', 0x17D2, L'෴', 0 };
87     for (i = 0; wc[i]; i++)
88 	xo_emit("Wide char: {lq:wc/%lc - %#lx - %d}\n",
89 		wc[i], (unsigned long) wc[i], xo_wcwidth(wc[i]));
90 
91     wchar_t msg[] = { L'1', 0x034f, L'2', 0x20dd, 0 };
92     for (i = 0; msg[i]; i++)
93 	xo_emit("Wide char: {lq:wc/%lc - %#lx - %d}\n",
94 		msg[i], (unsigned long) msg[i], xo_wcwidth((int) msg[i]));
95     xo_emit("Cool: [{:fancy/%ls}]\n", msg);
96 
97     xo_emit("Οὐχὶ ταὐτὰ παρίσταταί μοι {:v1/%s}, {:v2/%s}\n",
98 	    "γιγνώσκειν", "ὦ ἄνδρες ᾿Αθηναῖοι");
99 
100     rc = xo_emit("გთხოვთ {:v1/%s} {:v2/%s}\n",
101 	    "ახლავე გაიაროთ რეგისტრაცია",
102 	    "Unicode-ის მეათე საერთაშორისო");
103     xo_emit("{Twc:Width}{:width/%d}\n", rc);
104 
105     /* Okay, Sinhala is uber cool ... */
106     rc = xo_emit("[{:sinhala}]\n", "෴ණ්ණ෴");
107     xo_emit("{Twc:Width}{:width/%d}\n", rc);
108 
109     rc = xo_emit("[{:sinhala}]\n", "෴");
110     xo_emit("{Twc:Width}{:width/%d}\n", rc);
111 
112     rc = xo_emit("[{:sinhala/%-4..4s/%s}]\n", "෴ණ්ණ෴෴ණ්ණ෴");
113     xo_emit("{Twc:Width}{:width/%d}\n", rc);
114 
115     xo_emit("[{:not-sinhala/%-4..4s/%s}]\n", "123456");
116 
117     rc = xo_emit("[{:tag/%s}]\n", "ර්‍ඝ");
118     xo_emit("{Twc:Width}{:width/%d}\n", rc);
119 
120     xo_open_list("employee");
121 
122     xo_emit("{T:First Name/%-25s}{T:Last Name/%-14s}"
123 	    "{T:/%-12s}{T:Time (%)}\n", "Department");
124     for ( ; ep->e_first; ep++) {
125 	xo_open_instance("employee");
126 	xo_emit("{[:-25}{:first-name/%s} ({:nic-name/\"%s\"}){]:}"
127 		"{:last-name/%-14..14s/%s}"
128 		"{:department/%8u}{:percent-time/%8u}\n",
129 		ep->e_first, ep->e_nic, ep->e_last, ep->e_dept, ep->e_percent);
130 	if (ep->e_percent > 50) {
131 	    xo_attr("full-time", "%s", "honest & for true");
132 	    xo_emit("{e:benefits/%s}", "full");
133 	}
134 	xo_close_instance("employee");
135     }
136 
137     xo_close_list("employee");
138     xo_close_container("employees");
139 
140     xo_finish();
141 
142     return 0;
143 }
144