BOSS 7.0.1
BESIII Offline Software System
Loading...
Searching...
No Matches
testUtil.cxx File Reference
#include <string>
#include <iostream>
#include "facilities/Util.h"

Go to the source code of this file.

Functions

int main (int, char **)
 

Function Documentation

◆ main()

int main ( int  ,
char **   
)

Miniscule program to test a couple modes of Util::expandEnvVar Caller should have an environment variable SRC with some sensible definition.

Definition at line 9 of file testUtil.cxx.

9 {
10 std::string name = std::string("${GLAST_EXT}/xerces");
11 std::string nameAgain = std::string("${GLAST_EXT}/xerces");
12 std::string oDelim = std::string ("${");
13 std::string cDelim = std::string ("}");
14 int ntrans;
15
16 std::cout << "Test expandEnvVar: " << std::endl;
17 try {
18 ntrans = facilities::Util::expandEnvVar(&name, oDelim, cDelim);
19 std::cout << "Translated name (right delimiters) is "
20 << name << std::endl;
21 ntrans = facilities::Util::expandEnvVar(&nameAgain);
22 std::cout << "Translated name (wrong delimiters) is "
23 << nameAgain << std::endl;
24 }
25 catch (facilities::Untranslatable err) {
26 std::cout << "Failed to completely translate " << name << std::endl;
27 }
28
29
30
31 // Process running this must have environment variable SRC
32 std::string multi = std::string("$(FACILITIESROOT)/$(SRC)");
33
34 try {
35 ntrans = facilities::Util::expandEnvVar(&multi);
36
37 std::cout << "Translated name is " << multi << std::endl;
38 std::cout << ntrans << " variables were translated." << std::endl;
39 }
40 catch (facilities::Untranslatable err) {
41 std::cout << "Failed to completely translate " << multi << std::endl;
42 }
43
44 std::cout << std::endl << "Test itoa " << std::endl;
45 // Test the new itoa routine
46 std::string intStr;
47 facilities::Util::itoa(12, intStr);
48 std::cout << "My String is " << intStr << std::endl;
49
50 // basename & stringTokenize
51 std::string unixname("/a/path/myUnixFile.txt");
52 std::string wname("C:\\a\\longer\\path\\myWindowsFile.txt");
53
54 std::vector<std::string> tokens;
55 unsigned int i;
56
57 std::cout << std::endl << "Test stringTokenize and basename" << std::endl;
58 facilities::Util::stringTokenize(unixname, "/", tokens);
59
60 std::cout << "Processing string " << unixname << std::endl;
61 for (i = 0; i < tokens.size(); i++) {
62 std::cout << "Token " << i << " is: " << tokens[i] << std::endl;
63 }
64
65 std::cout << "basename is " << facilities::Util::basename(unixname) << std::endl;
66
67 facilities::Util::stringTokenize(wname, "\\", tokens);
68
69 std::cout << "Processing string " << wname << std::endl;
70 for (i = 0; i < tokens.size(); i++) {
71 std::cout << "Token " << i << " is: " << tokens[i] << std::endl;
72 }
73
74 std::cout << "basename is " << facilities::Util::basename(wname) << std::endl;
75
76 //test keyValueTokenize to map routine
77 std::cout << std::endl << "Test keyValueTokenize " << std::endl;
78 std::string input1("apple=green,lemon=yellow,blueberry=blue");
79 std::cout << "Input string: '" << input1 << "'" << std::endl;
80 std::map<std::string,std::string> maptokens;
81 facilities::Util::keyValueTokenize(input1,",",maptokens,"=", false);
82 std::map<std::string,std::string>::const_iterator tokens_itr=maptokens.begin();
83 while (tokens_itr!=maptokens.end()) {
84 std::cout << "Token key " <<(*tokens_itr).first << " and value: "
85 << (*tokens_itr).second << std::endl;
86 tokens_itr++;
87 }
88
89 std::cout <<"appending to previous map:"<<std::endl;
90 std::string input2("apple2/green2,lemon2/yellow2,blueberry2/blue2");
91 std::cout << "New string is '" << input2 << "'" << std::endl;
92 facilities::Util::keyValueTokenize(input2,",",maptokens,"/",false);
93 tokens_itr=maptokens.begin();
94 while (tokens_itr!=maptokens.end()) {
95 std::cout << "Token key " <<(*tokens_itr).first << " and value: "
96 << (*tokens_itr).second << std::endl;
97 tokens_itr++;
98 }
99
100 std::cout <<"clearing the map first:"<<std::endl;
101 facilities::Util::keyValueTokenize(input2,",",maptokens,"/",true);
102 tokens_itr=maptokens.begin();
103 while (tokens_itr!=maptokens.end()) {
104 std::cout << "Token key " <<(*tokens_itr).first << " and value: "
105 << (*tokens_itr).second << std::endl;
106 tokens_itr++;
107 }
108
109 std::cout << "Use a multi-character pairDelimiter argument " << std::endl;
110 std::string input3("apple2:=green2 lemon2:=yellow2 blueberry2:=blue2");
111 std::cout << "input is: '" << input3 << "'" << std::endl;
112 facilities::Util::keyValueTokenize(input3," ",maptokens,":=");
113 tokens_itr=maptokens.begin();
114 while (tokens_itr!=maptokens.end()) {
115 std::cout << "Token key " <<(*tokens_itr).first << " and value: "
116 << (*tokens_itr).second << std::endl;
117 tokens_itr++;
118 }
119
120
121 // Test stringToDouble routine
122 std::cout << std::endl << "Test stringToDouble " << std::endl;
123 std::string okDouble("3.14159");
124 std::string badDouble("3.garbage56");
125
126 double result = -1;
127
128 try {
129 result = facilities::Util::stringToDouble(okDouble);
130 std::cout << "Converted (string) " << okDouble << " to (double) "
131 << result << std::endl;
132 }
133 catch (facilities::WrongType ex) {
134 std::cout << "Failed with exception " << ex.getMsg()
135 << std::endl;
136 }
137
138 try {
139 result = facilities::Util::stringToDouble(badDouble);
140 std::cout << "Converted (string) " << badDouble << " to (double) "
141 << result << std::endl;
142 }
143 catch (facilities::WrongType ex) {
144 std::cout << "Failed with exception " << ex.getMsg()
145 << std::endl;
146 }
147
148 // Test stringToInt routine
149 std::cout << std::endl << "Test stringToInt " << std::endl;
150
151 std::string okInt("33550");
152 std::string badInt1("3garbage56");
153 std::string badInt2("garbage356");
154
155 int intResult = -1;
156
157 try {
158 intResult = facilities::Util::stringToInt(okInt);
159 std::cout << "Converted (string) " << okInt << " to (int) "
160 << intResult << std::endl;
161 }
162 catch (facilities::WrongType ex) {
163 std::cout << "Failed with exception " << ex.getMsg()
164 << std::endl;
165 }
166
167 try {
168 intResult = facilities::Util::stringToInt(badInt1);
169 std::cout << "Converted (string) " << badInt1 << " to (int) "
170 << intResult << std::endl;
171 }
172 catch (facilities::WrongType ex) {
173 std::cout << "Failed with exception " << ex.getMsg()
174 << std::endl;
175 }
176
177 try {
178 intResult = facilities::Util::stringToInt(badInt2);
179 std::cout << "Converted (string) " << badInt2 << " to (int) "
180 << intResult << std::endl;
181 }
182 catch (facilities::WrongType ex) {
183 std::cout << "Failed with exception " << ex.getMsg()
184 << std::endl;
185 }
186
187 // Try out trimTrailing method
188 std::cout << std::endl << "Test trimTrailing " << std::endl;
189 std::string string1("ends with 2 blanks ");
190 std::string string2("ends with newline\n");
191 std::string string3("no trailing whitespace");
192
193 unsigned nTrimmed = facilities::Util::trimTrailing(&string1);
194 std::cout << "Trimmed " << nTrimmed << " from string1; has new value : "
195 << string1 << "*EOS*" << std::endl;
196
197 nTrimmed = facilities::Util::trimTrailing(&string2);
198 std::cout << "Trimmed " << nTrimmed << " from string2; has new value : "
199 << string2 << "*EOS" << std::endl;
200
201 nTrimmed = facilities::Util::trimTrailing(&string3);
202 std::cout << "Trimmed " << nTrimmed << " from string3; has new value : "
203 << string3 << "*EOS" << std::endl;
204
205
206
207 return 0;
208}
static double stringToDouble(const std::string &InStr)
static int expandEnvVar(std::string *toExpand, const std::string &openDel=std::string("$("), const std::string &closeDel=std::string(")"))
static void stringTokenize(std::string input, const std::string &delimiters, std::vector< std::string > &tokens, bool clear=true)
static std::string basename(const std::string &path)
static int stringToInt(const std::string &InStr)
static void keyValueTokenize(std::string input, const std::string &delimiters, std::map< std::string, std::string > &tokenMap, const std::string &pairDelimiter=std::string("="), bool clear=true)
static const char * itoa(int val, std::string &outStr)
static unsigned trimTrailing(std::string *toTrim)
Exception class used when converting from string to numeric type.