-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
205 lines (179 loc) · 5.98 KB
/
Utils.cpp
File metadata and controls
205 lines (179 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//
// Created by Stephen Clyde on 2/3/17.
//
#include <algorithm>
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#include <cmath>
#include "Utils.h"
std::string getStringInput( std::string& prompt )
{
std::string userInput;
std::cout << std::endl;
std::cout << prompt << std::endl;
std::getline( std::cin, userInput );
return userInput;
}
// Splits a string up by the specified delimiter and stores the pieces into an array of strings, and checks
// to see if the expected number of pieces were found in the string
//
// Return a true if the string was split into the expected number of pieces, otherwise a it return a false.
bool split( const std::string& s, char delimiter, std::string pieces[], int expectedNumberOfPieces )
{
std::stringstream ss;
ss.str( s );
std::string item;
int i = 0;
while ( std::getline( ss, item, delimiter ) && i < expectedNumberOfPieces )
{
pieces[ i++ ] = trim( item );
}
return ( i == expectedNumberOfPieces );
}
int convertStringToInt( const std::string& s, bool* valid )
{
int result = 0;
if ( valid != nullptr )
*valid = false;
std::size_t numberOfConvertedCharacters = 0;
if ( !s.empty())
{
try
{
std::string trimmedString = trim( s );
result = std::stoi( trimmedString, &numberOfConvertedCharacters );
if ( valid != nullptr && numberOfConvertedCharacters == trimmedString.length())
*valid = true;
else if ( numberOfConvertedCharacters != trimmedString.length())
result = 0;
}
catch ( std::exception )
{
// do nothing, let the result remain 0 and the valid flag false
}
}
return result;
}
unsigned int convertStringToUnsignedInt( const std::string& s, bool* valid )
{
unsigned int result = 0;
if ( valid != nullptr )
*valid = false;
std::size_t numberOfConvertedCharacters = 0;
if ( !s.empty())
{
try
{
std::string trimmedString = trim( s );
if ( trimmedString.substr( 0, 1 ) != "-" )
{
unsigned long tmp = std::stoul( trimmedString, &numberOfConvertedCharacters );
if ( tmp <= UINT32_MAX)
{
result = ( unsigned int ) tmp;
if ( valid != nullptr && numberOfConvertedCharacters == trimmedString.length())
*valid = true;
else if ( numberOfConvertedCharacters != trimmedString.length())
result = 0;
}
}
}
catch ( std::exception )
{
// do nothing, let the result remain 0 and the valid flag false
}
}
return result;
}
std::size_t convertStringToSizeT( const std::string& s, bool* valid )
{
std::size_t result = 0;
if ( valid != nullptr )
*valid = false;
std::size_t numberOfConvertedCharacters = 0;
if ( !s.empty())
{
try
{
std::string trimmedString = trim( s );
if ( trimmedString.substr( 0, 1 ) != "-" )
{
result = std::stoul( trimmedString, &numberOfConvertedCharacters );
if ( valid != nullptr && numberOfConvertedCharacters == trimmedString.length())
*valid = true;
else if ( numberOfConvertedCharacters != trimmedString.length())
result = 0;
}
}
catch ( std::exception )
{
// do nothing, let the result remain 0 and the valid flag false
}
}
return result;
}
// Converts a string to a double
//
// Return the double value represented (0 if the string is not value) and set a valid flag, if provided, if the string
// represented an acceptable double. The valid flag is a point to a bool. If the pointer is null, then the function
// will still try to convert the string, but it will not try to set the valid flag.
double convertStringToDouble( const std::string& s, bool* valid )
{
double result = 0;
if ( valid != nullptr )
*valid = false;
std::size_t numberOfConvertedCharacters = 0;
if ( !s.empty())
{
try
{
std::string trimmedString = trim( s );
result = std::stod( trimmedString, &numberOfConvertedCharacters );
if ( valid != nullptr && numberOfConvertedCharacters == trimmedString.length())
*valid = true;
else if ( numberOfConvertedCharacters != trimmedString.length())
result = 0;
}
catch ( std::exception )
{
// do nothing, let the result remain 0 and the valid flag false
}
}
return result;
}
// Note: trim, leftTrim, and rightTrim were adapted from
// http://stackoverflow.com/questions/25829143/c-trim-whitespace-from-a-string
// Removes leading whitespace, include space, tabs, newlines, and returns
std::string leftTrim( const std::string& inputStr )
{
std::string str = inputStr;
auto it2 = std::find_if( str.begin(), str.end(), IsNotWhiteSpace );
str.erase( str.begin(), it2 );
return str;
}
// Removes trailing whitespace, include space, tabs, newlines, and returns
std::string rightTrim( const std::string& inputStr )
{
std::string str = inputStr;
auto it1 = std::find_if( str.rbegin(), str.rend(), IsNotWhiteSpace );
str.erase( it1.base(), str.end());
return str;
}
// Removes leading and trailing whitespace, include space, tabs, newlines, and returns
std::string trim( const std::string& str )
{
return leftTrim( rightTrim( str ));
}
// Function to check if a character is a not a whitespace character, namely
// space (0x20, ' ')
// form feed (0x0c, '\f')
// line feed (0x0a, '\n')
// carriage return (0x0d, '\r')
// horizontal tab (0x09, '\t')
// vertical tab (0x0b, '\v')
bool IsNotWhiteSpace( char ch )
{
return !std::isspace< char >( ch, std::locale::classic());
}