Debian Codegraph @

protolib/examples/tokenatorExample.cpp

raw
1
2 #include "protoString.h"
3 #include "protoDefs.h" // for PATH_MAX
4 #include <ctype.h>
5 #include <string.h>
6
7 int main(int argc, char* argv[])
8 {
9 const char* text1 = " trains, ,planes, motorcycles , trucks, cars";
10 const char* text2 = "trains planes motorcycles trucks cars ";
11 const char* text3 = NULL;
12 char token = ' ';
13
14 if (argc > 1) text3 = argv[1];
15 if (argc > 2) token = argv[2][0];
16
17 printf("\nComma-delimited, stripped tokenization of:\n \"%s\"\n", text1);
18 ProtoTokenator tk(text1, ',', true);
19 const char* item;
20 while (NULL != (item = tk.GetNextItem()))
21 printf("\"%s\"\n", item);
22
23 printf("\nReverse, comma-delimited tokenization of:\n \"%s\"\n", text1);
24 ProtoTokenator tkr(text1, ',', false, 0, true);
25 while (NULL != (item = tkr.GetNextItem()))
26 printf("\"%s\"\n", item);
27
28 printf("\nSpace-delimited, stripped tokenization of:\n \"%s\"\n", text2);
29 tk.Reset(text2, ' ');
30 while (NULL != (item = tk.GetNextItem()))
31 printf("\"%s\"\n", item);
32
33 printf("\nReverse, space-delimited tokenization of:\n \"%s\"\n", text2);
34 tkr.Reset(text2, ' ');
35 while (NULL != (item = tkr.GetNextItem()))
36 printf("\"%s\"\n", item);
37
38 if (NULL != text3)
39 {
40 if (isspace(token))
41 printf("\nWhitespace-delimited, stripped tokenization of:\n \"%s\"\n", text3);
42 else
43 printf("\n'%c'-delimited, stripped tokenization of:\n \"%s\"\n", token, text3);
44 tk.Reset(text3, token);
45 while (NULL != (item = tk.GetNextItem()))
46 printf("\"%s\"\n", item);
47 if (isspace(token))
48 printf("\nReverse, whitespace-delimited, stripped tokenization of:\n \"%s\"\n", text3);
49 else
50 printf("\nReverse, '%c'-delimited, tokenization of:\n \"%s\"\n", token, text3);
51 tkr.Reset(text3, token);
52 // This demos the 'detach' option and the need to delete[] the returned text item
53 while (NULL != (item = tkr.GetNextItem(true)))
54 {
55 printf("\"%s\"\n", item);
56 delete[] item;
57 }
58 }
59
60 // This section illustrates using ProtoTokenator to help parse file path strings
61 // (Note the constant "PROTO_PATH_DELIMITER" is available for cross-platform use)
62 // The dirname/basename here works like Python os.path.dirname() and os.path.basename(),
63 // i.e., not exactly like Unix basename() function
64 const char* pathList[] =
65 {
66 "/usr/include/stdio.h",
67 "/usr/local/include//"
68 };
69 char dirname[PATH_MAX + 1];
70 char basename[PATH_MAX + 1];
71 for (int i = 0; i < sizeof(pathList)/sizeof(pathList[0]); i++)
72 {
73 const char* thePath = pathList[i];
74 // Note we "stripTokens" to remove any extra trailing path delimiters on dirname
75 // delimiters on paths
76 ProtoTokenator pk(thePath, '/', true, 1, true, true);
77 const char* item = pk.GetNextItem(); // reverse gets basename first
78 if (NULL != item)
79 strncpy(basename, item, PATH_MAX);
80 else
81 basename[0] = '\0';
82 item = pk.GetNextItem(); // reverse gets dirname second (if present)
83 if (NULL != item)
84 strncpy(dirname, item, PATH_MAX);
85 else
86 dirname[0] = '\0';
87 printf("path: \"%s\" dirname: \"%s\" basename: \"%s\"\n", thePath, dirname, basename);
88 }
89
90 } // end main()