| 1 |
rtomiyasu |
3 |
/* |
| 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 |
|
|
#include "../zerror_type/error_out.hh" |
| 28 |
|
|
#include "zstring.hh" |
| 29 |
|
|
|
| 30 |
|
|
// Check if the string is blank. |
| 31 |
|
|
bool is_blank(const string& s) |
| 32 |
|
|
{ |
| 33 |
|
|
istringstream strstream(s); |
| 34 |
|
|
|
| 35 |
|
|
string str; |
| 36 |
|
|
strstream >> str; |
| 37 |
|
|
|
| 38 |
|
|
return strstream.fail(); |
| 39 |
|
|
} |
| 40 |
|
|
|
| 41 |
|
|
// Split the argument str by the delimiter delim. |
| 42 |
|
|
void split(const string& str, vector<string>& word, const char& delim) |
| 43 |
|
|
{ |
| 44 |
|
|
string::size_type index = str.find_first_of(delim); |
| 45 |
|
|
|
| 46 |
|
|
word.clear(); |
| 47 |
|
|
word.push_back( str.substr(0, index) ); |
| 48 |
|
|
|
| 49 |
|
|
string::size_type index0; |
| 50 |
|
|
while( index != string::npos ){ |
| 51 |
|
|
index0 = index + 1; |
| 52 |
rtomiyasu |
31 |
index = str.find_first_of(delim, index0); |
| 53 |
rtomiyasu |
3 |
word.push_back( str.substr(index0, index-index0) ); |
| 54 |
|
|
} |
| 55 |
|
|
} |
| 56 |
|
|
|
| 57 |
|
|
// Split the argument str by the delimiter delim. |
| 58 |
|
|
bool split_term(istream& iss, vector<string>& word) |
| 59 |
|
|
{ |
| 60 |
|
|
word.clear(); |
| 61 |
|
|
char c; |
| 62 |
|
|
if( !iss.get(c) ) return false; |
| 63 |
|
|
|
| 64 |
|
|
Int4 index = 0; |
| 65 |
|
|
if( c != '+' ) word.push_back( string(1,c) ); |
| 66 |
|
|
|
| 67 |
|
|
while( iss.get(c) ) |
| 68 |
|
|
{ |
| 69 |
|
|
if( c == '+' ) |
| 70 |
|
|
{ |
| 71 |
|
|
if( word[index].empty() || word[index] == "-" ) return false; |
| 72 |
|
|
index++; |
| 73 |
|
|
word.resize(index+1); |
| 74 |
|
|
} |
| 75 |
|
|
else if( c == '-') |
| 76 |
|
|
{ |
| 77 |
|
|
if( word[index].empty() || word[index] == "-" ) return false; |
| 78 |
|
|
index++; |
| 79 |
|
|
word.push_back("-"); |
| 80 |
|
|
} |
| 81 |
|
|
else |
| 82 |
|
|
{ |
| 83 |
|
|
word[index] += c; |
| 84 |
|
|
} |
| 85 |
|
|
} |
| 86 |
|
|
return true; |
| 87 |
|
|
} |
| 88 |
|
|
|
| 89 |
|
|
string getFileExtension(const string& inputFilePathString) |
| 90 |
|
|
{ |
| 91 |
|
|
int stringSize = inputFilePathString.size(); |
| 92 |
|
|
const char* filePath = inputFilePathString.c_str(); |
| 93 |
|
|
bool isFindDotCharacter = false; |
| 94 |
|
|
while(0 < stringSize){ |
| 95 |
|
|
if(filePath[--stringSize] == '.'){ |
| 96 |
|
|
isFindDotCharacter = true; |
| 97 |
|
|
break; |
| 98 |
|
|
} |
| 99 |
|
|
} |
| 100 |
|
|
string returnString; |
| 101 |
|
|
|
| 102 |
|
|
if(false != isFindDotCharacter){ |
| 103 |
|
|
returnString = inputFilePathString.substr(stringSize+1, inputFilePathString.size()-stringSize); |
| 104 |
|
|
} |
| 105 |
|
|
|
| 106 |
|
|
return returnString; |
| 107 |
|
|
} |
| 108 |
|
|
|
| 109 |
|
|
void removeFileExtension(const string& fname0, string& fname) |
| 110 |
|
|
{ |
| 111 |
|
|
// Remove the file extension. |
| 112 |
|
|
fname = fname0; |
| 113 |
|
|
string::size_type index = fname.rfind("."); |
| 114 |
|
|
if( index != string::npos ) fname = fname.substr(0, index); |
| 115 |
|
|
} |
| 116 |
|
|
|
| 117 |
|
|
// Read the sentence before delim and put it in ans. |
| 118 |
|
|
ZErrorMessage getdelim(istream& ifs, string& ans, const string& delim) |
| 119 |
|
|
{ |
| 120 |
|
|
ans.clear(); |
| 121 |
|
|
if( delim.empty() ) return ZErrorMessage(ZErrorArgument, "Delimiter is empty", __FILE__, __LINE__, __FUNCTION__); |
| 122 |
|
|
|
| 123 |
rtomiyasu |
33 |
const int ilength = delim.length(); |
| 124 |
|
|
int count = 0; |
| 125 |
rtomiyasu |
3 |
char c; |
| 126 |
|
|
while( ifs.get(c) ) |
| 127 |
|
|
{ |
| 128 |
|
|
if( c == delim.at(count) ) |
| 129 |
|
|
{ |
| 130 |
|
|
count++; |
| 131 |
|
|
if( count >= ilength ) |
| 132 |
|
|
{ |
| 133 |
|
|
return ZErrorMessage(); |
| 134 |
|
|
} |
| 135 |
|
|
} |
| 136 |
|
|
else |
| 137 |
|
|
{ |
| 138 |
rtomiyasu |
33 |
int i; |
| 139 |
rtomiyasu |
3 |
for(i=count-1; i>=0; i--) |
| 140 |
|
|
{ |
| 141 |
|
|
if( delim.at(i) == c ) |
| 142 |
|
|
{ |
| 143 |
|
|
if( i == 0 || delim.substr(0, i) == delim.substr(count-i, i) ) break; |
| 144 |
|
|
} |
| 145 |
|
|
} |
| 146 |
|
|
if( i >= 0 ) |
| 147 |
|
|
{ |
| 148 |
|
|
ans += delim.substr(0, count-i); |
| 149 |
|
|
count = i + 1; |
| 150 |
|
|
} |
| 151 |
|
|
else |
| 152 |
|
|
{ |
| 153 |
|
|
ans += delim.substr(0, count) + c; |
| 154 |
|
|
count = 0; |
| 155 |
|
|
} |
| 156 |
|
|
} |
| 157 |
|
|
} |
| 158 |
|
|
return ZErrorMessage(ZErrorDelimiterNotFound, "The Delimiter \""+delim+"\" is not found", __FILE__, __LINE__, __FUNCTION__); |
| 159 |
|
|
} |