| 1 |
/* |
| 2 |
* The MIT License |
| 3 |
|
| 4 |
Conograph (powder auto-indexing program) |
| 5 |
|
| 6 |
Copyright (c) <2012> <Ryoko Oishi-Tomiyasu, KEK> |
| 7 |
|
| 8 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
of this software and associated documentation files (the "Software"), to deal |
| 10 |
in the Software without restriction, including without limitation the rights |
| 11 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
copies of the Software, and to permit persons to whom the Software is |
| 13 |
furnished to do so, subject to the following conditions: |
| 14 |
|
| 15 |
The above copyright notice and this permission notice shall be included in |
| 16 |
all copies or substantial portions of the Software. |
| 17 |
|
| 18 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 |
THE SOFTWARE. |
| 25 |
* |
| 26 |
*/ |
| 27 |
#ifndef _ZSTRING_HH_ |
| 28 |
#define _ZSTRING_HH_ |
| 29 |
|
| 30 |
#include <vector> |
| 31 |
#include <sstream> |
| 32 |
#include "../zerror_type/ZErrorType.hh" |
| 33 |
|
| 34 |
class ZErrorMessage; |
| 35 |
|
| 36 |
using namespace std; |
| 37 |
|
| 38 |
// Change the argument type from T to string. |
| 39 |
template<class T> |
| 40 |
inline bool str2num(const string& str, T& ans) |
| 41 |
{ |
| 42 |
istringstream iss(str); |
| 43 |
iss >> ans; |
| 44 |
if( iss.fail() ) return false; |
| 45 |
return true; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
template<class T> |
| 50 |
inline string num2str(const T& num) |
| 51 |
{ |
| 52 |
stringstream strstream; |
| 53 |
strstream << num; |
| 54 |
return strstream.str(); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
template<class T> |
| 59 |
inline string num2str(const T& num, const int& precision) |
| 60 |
{ |
| 61 |
stringstream strstream; |
| 62 |
strstream.precision(precision); |
| 63 |
strstream << num; |
| 64 |
return strstream.str(); |
| 65 |
} |
| 66 |
|
| 67 |
template<class T> |
| 68 |
inline string vec2str(const vector<T>& num) |
| 69 |
{ |
| 70 |
stringstream strstream; |
| 71 |
if( num.empty() ) return ""; |
| 72 |
if( num.size() < 2 ) return num2str(num[0]); |
| 73 |
|
| 74 |
strstream << "(" << *num.begin(); |
| 75 |
for(typename vector<T>::const_iterator it=num.begin()+1; it<num.end(); it++) |
| 76 |
{ |
| 77 |
strstream << "," << *it; |
| 78 |
} |
| 79 |
strstream << ")"; |
| 80 |
return strstream.str(); |
| 81 |
} |
| 82 |
|
| 83 |
// Check if the string is blank. |
| 84 |
bool is_blank(const string&); |
| 85 |
|
| 86 |
// Split the argument string by the delimiter. |
| 87 |
void split(const string&, vector<string>&, const char&); |
| 88 |
|
| 89 |
bool split_term(istream&, vector<string>&); |
| 90 |
|
| 91 |
string getFileExtension (const string& inputFilePathString); |
| 92 |
|
| 93 |
void removeFileExtension(const string&, string&); |
| 94 |
|
| 95 |
inline string remove_blank(const string& str) |
| 96 |
{ |
| 97 |
string ans, str2; |
| 98 |
istringstream iss(str); |
| 99 |
|
| 100 |
iss >> str2; |
| 101 |
while( !iss.fail() ) |
| 102 |
{ |
| 103 |
ans += str2; |
| 104 |
iss >> str2; |
| 105 |
} |
| 106 |
return ans; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
inline ZErrorType getnewline(istream& ifs, string& s) |
| 111 |
{ |
| 112 |
s.clear(); |
| 113 |
char c; |
| 114 |
while( ifs.get(c) ) |
| 115 |
{ |
| 116 |
if( c == '\n' ) return ZErrorNoError; |
| 117 |
if( c == '\r' ) |
| 118 |
{ |
| 119 |
if( ifs.eof() ) return ZErrorNoError; |
| 120 |
c = ifs.peek(); |
| 121 |
if( c == '\n' ) ifs.get(c); |
| 122 |
return ZErrorNoError; |
| 123 |
} |
| 124 |
s += c; |
| 125 |
} |
| 126 |
return ZErrorDelimiterNotFound; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
inline void getfirstword(istream& is, string& label) |
| 131 |
{ |
| 132 |
label=""; |
| 133 |
string str2; |
| 134 |
ZErrorType zerr = ZErrorNoError; |
| 135 |
while( zerr == ZErrorNoError ) |
| 136 |
{ |
| 137 |
zerr = getnewline(is, str2); |
| 138 |
if( !is_blank(str2) ) |
| 139 |
{ |
| 140 |
istringstream iss2(str2); |
| 141 |
iss2 >> label; |
| 142 |
break; |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
// Read the sentence before delim and put it in ans. |
| 149 |
ZErrorMessage getdelim(istream& ifs, string& ans, const string& delim); |
| 150 |
|
| 151 |
#endif /*STRING_HH_*/ |