Newer
Older
* Copyright (c) 2012, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.com/LICENSE.txt
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
*/
#include "StringTools.h"
std::list<std::string> splitString(const std::string &str, char delim)
{
std::list<std::string> strList;
std::stringstream ss(str);
std::string item;
while(getline(ss, item, delim)) {
strList.push_back(item);
}
return strList;
}
std::string replaceString(const std::string &searchString, const std::string &replaceString, std::string stringToReplace)
{
std::string::size_type pos = stringToReplace.find(searchString, 0);
int intLengthSearch = searchString.length();
while (std::string::npos != pos) {
stringToReplace.replace(pos, intLengthSearch, replaceString);
pos = stringToReplace.find(searchString, 0);
}
return stringToReplace;
}
void trim(std::string &str, char ch)
{
std::string::size_type pos = str.find_last_not_of(ch);
if(pos != std::string::npos)
{
str.erase(pos + 1);
pos = str.find_first_not_of(ch);
if(pos != std::string::npos) str.erase(0, pos);
}
else str.erase(str.begin(), str.end());
}
Karsten Rink
committed
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
namespace BaseLib {
std::string getFileNameFromPath(const std::string &str, bool with_extension)
{
std::string::size_type beg1 = str.find_last_of('/');
std::string::size_type beg2 = str.find_last_of('\\');
std::string::size_type beg;
if (beg1 == std::string::npos && beg2 == std::string::npos) beg = -1;
else if (beg1 == std::string::npos) beg = beg2;
else if (beg2 == std::string::npos) beg = beg1;
else beg = (beg1<beg2) ? beg2 : beg1;
std::string file ( str.substr(beg+1) );
if (with_extension) return file;
// cut extension
std::string::size_type end = file.find_last_of('.');
return file.substr(0,end);
}
std::string copyPathToFileName(const std::string &file_name, const std::string &source)
{
// check if file_name already contains a full path
size_t pos(file_name.rfind("/")); // linux, mac delimiter
if (pos == std::string::npos)
{
pos = file_name.rfind("\\"); // windows delimiter
if (pos == std::string::npos)
{
std::string path;
BaseLib::extractPath(source, path);
return path.append(file_name);
}
else return std::string(file_name);
}
else return std::string(file_name);
}
void extractPath (std::string const& fname, std::string& path)
{
// extract path for reading external files
size_t pos(fname.rfind("/")); // linux, mac delimiter
if (pos == std::string::npos) {
pos = fname.rfind("\\"); // windows delimiter
if (pos == std::string::npos)
pos = 0;
}
path = fname.substr(0, pos==0 ? pos : pos + 1);
}
} // end namespace BaseLib
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
#ifdef MSVC
void correctScientificNotation(std::string filename, size_t precision)
{
std::ifstream stream;
std::ofstream outputStream;
stream.open(filename.c_str());
std::string tmpFilename = filename + ".tmp";
outputStream.open(tmpFilename.c_str());
if (!stream)
{
std::cout << "correctScientificNotation: fstream is not open" << std::endl;
return;
}
std::string line;
// Iterate over lines in stream
while (getline(stream, line))
{
std::string word;
std::istringstream iss(line);
// Iterate over all words in line
while (iss >> word)
{
// Search for e+0
std::size_t exponentPosition = word.find("e+0", precision);
if (exponentPosition == std::string::npos)
// If not found search for e-0
exponentPosition = word.find("e-0", precision);
if (exponentPosition != std::string::npos)
{
std::size_t wordSize = word.size();
std::size_t exponentSize = wordSize - exponentPosition;
if(exponentSize > 4)
{
// Erase the leading zero considering trailing characters
int i = wordSize - 1;
while (!isdigit(word[i]))
--i;
size_t erasePos = wordSize - 3 - (wordSize - 1 - i);
std::string eraseString = word.substr(erasePos, 1);
if (eraseString.find("0") != std::string::npos)
word.erase(erasePos, 1);
}
}
outputStream << word << " ";
}
outputStream << std::endl;
}
stream.close();
outputStream.close();
remove(filename.c_str());
rename(tmpFilename.c_str(), filename.c_str());
}
#endif