UtilString.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include <cstring>
00009 #include <cstdlib>
00010
00011 #include "UtilString.hxx"
00012 #include <TSK_DBI_Log.hxx>
00013 #include <MsgFormat.h>
00014 using std::endl;
00015
00016
00017
00018
00019
00020 void UtilString::MakePrintable(const char* in,
00021 std::string& out)
00022 {
00023
00024
00025
00026
00027
00028 bool hasSpecial = false;
00029 int index = 0;
00030 while ( unsigned char c = in[index++] ) {
00031 if ( c == '\\' || c == '\n' || c == '\t' || c == '\'' || c == '\"'
00032 || c <= '\x08' || (c >= '\x0b' && c <= '\x1f' ) || c >= '\x7f' ) {
00033 hasSpecial = true;
00034 break;
00035 }
00036 }
00037 if ( ! hasSpecial ) {
00038 out += in;
00039 return;
00040 }
00041 index = 0;
00042 while ( unsigned char c = in[index++] ) {
00043
00044 if ( c <= '\x08' || (c >= '\x0b' && c <= '\x1f' ) || c >= '\x7f' ) continue;
00045 if ( c == '\\' || c == '\n' || c == '\t' || c == '\'' || c == '\"') {
00046 switch ( c ) {
00047 case '\\': out += "\\\\"; break;
00048 case '\n': out += "\\n"; break;
00049 case '\t': out += "\\t"; break;
00050 case '\'': out += "\\\'"; break;
00051 case '\"': out += "\\\""; break;
00052 }
00053 }
00054 else out += c;
00055 }
00056 }
00057
00058
00059
00060 void UtilString::StringTok(std::vector<std::string>& ls,
00061 const std::string& str,
00062 const std::string& tok)
00063 {
00064
00065
00066
00067
00068 const string::size_type S = str.size();
00069 const string::size_type toksz = tok.size();
00070 string::size_type i = 0;
00071
00072 while (i < S) {
00073
00074 while ( (i<S) && (tok.find(str[i])<=toksz) ) {
00075 ++i;
00076 }
00077 if (i == S) return;
00078
00079
00080 string::size_type j = i+1;
00081 while ( (j<S) && !(tok.find(str[j])<=toksz) ) {
00082 ++j;
00083 }
00084
00085
00086 ls.push_back(str.substr(i,j-i));
00087
00088
00089 i = j+1;
00090 }
00091 }
00092
00093
00094
00095 bool UtilString::IsInt(const char* s)
00096 {
00097
00098
00099
00100 char* endptr;
00101 double d = strtod(s, &endptr);
00102 if (endptr==s && d==0.0) return false;
00103
00104
00105 if (strchr(s,'.')) return false;
00106 if (strchr(s,'E')) return false;
00107 if (strchr(s,'e')) return false;
00108
00109
00110 return true;
00111 }
00112
00113
00114
00115
00116 bool UtilString::IsFloat(const char* s)
00117 {
00118
00119
00120
00121 char* endptr;
00122 double d = strtod(s, &endptr);
00123 if (endptr==s && d==0.0) return false;
00124
00125
00126 return true;
00127 }
00128
00129
00130
00131 bool UtilString::IsBool(const char* s)
00132 {
00133
00134
00135
00136 bool isvalid;
00137 atob(s,isvalid);
00138 return isvalid;
00139 }
00140
00141
00142
00143 bool UtilString::atob(const char* s)
00144 {
00145
00146
00147
00148
00149
00150 bool isvalid;
00151 bool value = atob(s,isvalid);
00152 if (isvalid) return value;
00153
00154
00155 SK_DBI_Warn( "Attmept to convert string '" << value << "' to bool. Result is 'false'\n");
00156 return false;
00157
00158 }
00159
00160
00161
00162 bool UtilString::atob(const char* s, bool& isvalid)
00163 {
00164
00165
00166
00167
00168 isvalid = true;
00169
00170 std::string v(s);
00171 if (v == "true") return true;
00172 if (v == "false") return false;
00173 if (v == "kTRUE") return true;
00174 if (v == "kFALSE") return false;
00175 if (v == "TRUE") return true;
00176 if (v == "FALSE") return false;
00177 if (v == "True") return true;
00178 if (v == "False") return false;
00179 if (v == "on") return true;
00180 if (v == "off") return false;
00181 if (v == "On") return true;
00182 if (v == "Off") return false;
00183 if (v == "ON") return true;
00184 if (v == "OFF") return false;
00185
00186 isvalid = false;
00187 return false;
00188 }
00189
00190
00191
00192 int UtilString::cmp_nocase(const std::string& s1, const std::string& s2) {
00193
00194
00195
00196
00197 std::string::const_iterator p1=s1.begin();
00198 std::string::const_iterator p2=s2.begin();
00199 while (p1!=s1.end() && p2!=s2.end()) {
00200 if (toupper(*p1) != toupper(*p2))
00201 return (toupper(*p1)<toupper(*p2)) ? -1 : 1;
00202 ++p1; ++p2;
00203 }
00204 return (s2.size()==s1.size()) ? 0 : (s1.size()<s2.size()) ? -1:1;
00205 }
00206
00207
00208 int UtilString::cmp_wildcard(const std::string& s, const std::string& w) {
00209
00210
00211
00212 std::string::size_type locStar = w.find('*');
00213 if ( locStar == std::string::npos ) return s.compare(w);
00214 return strncmp(s.c_str(),w.c_str(),locStar);
00215 }
00216
00217
00218
00219 std::string UtilString::ToUpper(const std::string & str) {
00220
00221
00222
00223
00224 std::string out(str);
00225 unsigned loc = str.size();
00226 while ( loc ) {
00227 --loc;
00228 out[loc] = toupper(out[loc]);
00229 }
00230 return out;
00231 }
00232
00233
00234
00235 std::string UtilString::ToLower(const std::string & str) {
00236
00237
00238
00239
00240 std::string out(str);
00241 unsigned loc = str.size();
00242 while ( loc ) {
00243 --loc;
00244 out[loc] = tolower(out[loc]);
00245 }
00246 return out;
00247 }
00248
00249
00250