Miniscule program to test a couple modes of Util::expandEnvVar Caller should have an environment variable SRC with some sensible definition.
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 {
20 std::cout << "Translated name (right delimiters) is " << name << std::endl;
22 std::cout << "Translated name (wrong delimiters) is " << nameAgain << std::endl;
24 { std::cout << "Failed to completely translate " << name << std::endl; }
25
26
27 std::string multi = std::string( "$(FACILITIESROOT)/$(SRC)" );
28
29 try
30 {
32
33 std::cout << "Translated name is " << multi << std::endl;
34 std::cout << ntrans << " variables were translated." << std::endl;
36 { std::cout << "Failed to completely translate " << multi << std::endl; }
37
38 std::cout << std::endl << "Test itoa " << std::endl;
39
40 std::string intStr;
42 std::cout << "My String is " << intStr << std::endl;
43
44
45 std::string unixname( "/a/path/myUnixFile.txt" );
46 std::string wname( "C:\\a\\longer\\path\\myWindowsFile.txt" );
47
48 std::vector<std::string> tokens;
49 unsigned int i;
50
51 std::cout << std::endl << "Test stringTokenize and basename" << std::endl;
53
54 std::cout << "Processing string " << unixname << std::endl;
55 for ( i = 0; i < tokens.size(); i++ )
56 { std::cout << "Token " << i << " is: " << tokens[i] << std::endl; }
57
59
61
62 std::cout << "Processing string " << wname << std::endl;
63 for ( i = 0; i < tokens.size(); i++ )
64 { std::cout << "Token " << i << " is: " << tokens[i] << std::endl; }
65
67
68
69 std::cout << std::endl << "Test keyValueTokenize " << std::endl;
70 std::string input1( "apple=green,lemon=yellow,blueberry=blue" );
71 std::cout << "Input string: '" << input1 << "'" << std::endl;
72 std::map<std::string, std::string> maptokens;
74 std::map<std::string, std::string>::const_iterator tokens_itr = maptokens.begin();
75 while ( tokens_itr != maptokens.end() )
76 {
77 std::cout << "Token key " << ( *tokens_itr ).first
78 << " and value: " << ( *tokens_itr ).second << std::endl;
79 tokens_itr++;
80 }
81
82 std::cout << "appending to previous map:" << std::endl;
83 std::string input2( "apple2/green2,lemon2/yellow2,blueberry2/blue2" );
84 std::cout << "New string is '" << input2 << "'" << std::endl;
86 tokens_itr = maptokens.begin();
87 while ( tokens_itr != maptokens.end() )
88 {
89 std::cout << "Token key " << ( *tokens_itr ).first
90 << " and value: " << ( *tokens_itr ).second << std::endl;
91 tokens_itr++;
92 }
93
94 std::cout << "clearing the map first:" << std::endl;
96 tokens_itr = maptokens.begin();
97 while ( tokens_itr != maptokens.end() )
98 {
99 std::cout << "Token key " << ( *tokens_itr ).first
100 << " and value: " << ( *tokens_itr ).second << std::endl;
101 tokens_itr++;
102 }
103
104 std::cout << "Use a multi-character pairDelimiter argument " << std::endl;
105 std::string input3( "apple2:=green2 lemon2:=yellow2 blueberry2:=blue2" );
106 std::cout << "input is: '" << input3 << "'" << std::endl;
108 tokens_itr = maptokens.begin();
109 while ( tokens_itr != maptokens.end() )
110 {
111 std::cout << "Token key " << ( *tokens_itr ).first
112 << " and value: " << ( *tokens_itr ).second << std::endl;
113 tokens_itr++;
114 }
115
116
117 std::cout << std::endl << "Test stringToDouble " << std::endl;
118 std::string okDouble( "3.14159" );
119 std::string badDouble( "3.garbage56" );
120
121 double result = -1;
122
123 try
124 {
126 std::cout << "Converted (string) " << okDouble << " to (double) " << result << std::endl;
128 { std::cout <<
"Failed with exception " << ex.
getMsg() << std::endl; }
129
130 try
131 {
133 std::cout << "Converted (string) " << badDouble << " to (double) " << result << std::endl;
135 { std::cout <<
"Failed with exception " << ex.
getMsg() << std::endl; }
136
137
138 std::cout << std::endl << "Test stringToInt " << std::endl;
139
140 std::string okInt( "33550" );
141 std::string badInt1( "3garbage56" );
142 std::string badInt2( "garbage356" );
143
144 int intResult = -1;
145
146 try
147 {
149 std::cout << "Converted (string) " << okInt << " to (int) " << intResult << std::endl;
151 { std::cout <<
"Failed with exception " << ex.
getMsg() << std::endl; }
152
153 try
154 {
156 std::cout << "Converted (string) " << badInt1 << " to (int) " << intResult << std::endl;
158 { std::cout <<
"Failed with exception " << ex.
getMsg() << std::endl; }
159
160 try
161 {
163 std::cout << "Converted (string) " << badInt2 << " to (int) " << intResult << std::endl;
165 { std::cout <<
"Failed with exception " << ex.
getMsg() << std::endl; }
166
167
168 std::cout << std::endl << "Test trimTrailing " << std::endl;
169 std::string string1( "ends with 2 blanks " );
170 std::string string2( "ends with newline\n" );
171 std::string string3( "no trailing whitespace" );
172
174 std::cout << "Trimmed " << nTrimmed << " from string1; has new value : " << string1
175 << "*EOS*" << std::endl;
176
178 std::cout << "Trimmed " << nTrimmed << " from string2; has new value : " << string2 << "*EOS"
179 << std::endl;
180
182 std::cout << "Trimmed " << nTrimmed << " from string3; has new value : " << string3 << "*EOS"
183 << std::endl;
184
185 return 0;
186}
Exception class used by expandEnvVar.
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.