21 #include "IException.h"
22 #include "SpecialPixel.h"
48 falses.append(
"false");
56 bool foundMatch =
false;
58 QListIterator<QString> truesIterator(trues);
59 while (!foundMatch && truesIterator.hasNext()) {
60 foundMatch = (
string.compare(truesIterator.next(), Qt::CaseInsensitive) == 0);
66 QListIterator<QString> falsesIterator(falses);
67 while (!foundMatch && falsesIterator.hasNext()) {
68 foundMatch = (
string.compare(falsesIterator.next(), Qt::CaseInsensitive) == 0);
75 QString message = QObject::tr(
"Failed to convert string [%1] to a boolean. "
76 "Please specify one of [%2] for true, or one of [%3] for false.")
77 .arg(
string).arg(trues.join(
", ")).arg(falses.join(
", "));
78 throw IException(IException::Unknown, message, _FILEINFO_);
93 int toInt(
const QString &
string) {
96 int result =
string.toInt(&ok);
99 QString message = QObject::tr(
"Failed to convert string [%1] to an integer").arg(
string);
100 throw IException(IException::Unknown, message, _FILEINFO_);
120 s <<
string.toStdString();
121 s.seekg(0, std::ios::beg);
123 std::ios::iostate state = s.rdstate();
124 if((state & std::ios::failbit) || (state & std::ios::badbit) ||
125 (!(state & std::ios::eofbit))) {
130 QString message = QObject::tr(
"Failed to convert string [%1] to a big integer").arg(
string);
131 throw IException(IException::Unknown, message, _FILEINFO_);
152 if (
string.startsWith(
"16#") &&
string.endsWith(
"#")) {
155 s <<
string.mid(3,
string.size() - 3);
156 s.seekg(0, ios::beg);
159 unsigned int intData;
163 s >> hex >> raw.intData;
165 ios::iostate state = s.rdstate();
166 if((state & ios::failbit) || (state & ios::badbit)) {
170 result = raw.floatData;
173 QString message = QObject::tr(
"Failed to convert HEX string [%1] to a "
174 "double").arg(
string);
175 throw IException(IException::Unknown, message, _FILEINFO_);
180 if (knownStrings.isEmpty()) {
182 knownStrings[
"1.79769313486232e+308"] = DBL_MAX;
183 knownStrings[
"-1.79769313486232e+308"] = -DBL_MAX;
187 if (!knownStrings.contains(
string)) {
188 result =
string.toDouble(&ok);
191 result = knownStrings[string];
195 QString message = QObject::tr(
"Failed to convert string [%1] to a double").arg(
string);
196 throw IException(IException::Unknown, message, _FILEINFO_);
212 return boolToConvert?
"Yes" :
"No";
226 result += QChar(charToConvert);
238 return QString::number(intToConvert);
248 QString
toString(
const unsigned int &intToConvert) {
249 return QString::number(intToConvert);
260 return QString::number(intToConvert);
277 QString
toString(
double doubleToConvert,
int precision) {
282 if(doubleToConvert == 0.0) {
285 else if (std::isnan(doubleToConvert) ) {
289 if(doubleToConvert > DBL_MAX) {
290 doubleToConvert = DBL_MAX;
293 if(doubleToConvert < -DBL_MAX) {
294 doubleToConvert = -DBL_MAX;
303 double temp = qAbs(doubleToConvert);
304 int pre = (int)(log10(temp)) + 1;
319 char doubleString[23];
321 if((log10(temp) > 13.0) || (log10(temp) < -3.0)) {
322 char format[8], buffer[8];
323 snprintf(buffer, 8,
"%de", precision);
324 strcpy(format,
"%21.");
325 strcat(format, buffer);
326 snprintf(doubleString, 23, format, doubleToConvert);
328 char *e = strchr(doubleString,
'e');
331 while(*sptr ==
'0') {
344 e = strchr(doubleString,
'e');
347 for(sptr = e + 2; *sptr; sptr++) {
366 int post = precision - pre;
368 char tempStr[3], format[8];
369 strcpy(format,
"%17.");
370 snprintf(tempStr, 3,
"%d", post);
371 strcat(format, tempStr);
374 snprintf(doubleString, 23, format, doubleToConvert);
377 char *sptr = doubleString + strlen(doubleString) - 1;
378 while((*sptr ==
'0') && (*(sptr - 1) !=
'.')) {
385 while(doubleString[0] ==
' ') {
386 for(
unsigned int i = 0; i < strlen(doubleString); i++) {
387 doubleString[i] = doubleString[i + 1];
391 result = QString(doubleString);
403 IString::IString() : string() {
495 *
this =
toString(num, piPrecision).toStdString();
505 assign(str.toStdString());
544 std::string
IString::Trim(
const std::string &chars,
const std::string &str) {
559 *
this = replace(0, find_first_not_of(chars),
"");
575 result.replace(0, str.find_first_not_of(chars),
"");
588 *
this = erase(find_last_not_of(chars) + 1);
606 result.erase(str.find_last_not_of(chars) + 1);
634 transform(str.begin(), str.end(), sOut.begin(), (int ( *)(
int)) toupper);
661 transform(str.begin(), str.end(), sOut.begin(), (int ( *)(
int))tolower);
678 return(toupper(c1) == toupper(c2));
692 return Equal(str, temp);
706 if(str1.size() != str2.size())
return(
false);
707 return(std::equal(str1.begin(), str1.end(), str2.begin(),
nocase_compare));
736 s.seekg(0, ios::beg);
738 ios::iostate state = s.rdstate();
739 if((state & ios::failbit) || (state & ios::badbit) ||
740 (!(state & ios::eofbit))) {
745 string message =
"Failed to convert string [" + str +
"] to an integer";
776 s.seekg(0, ios::beg);
778 ios::iostate state = s.rdstate();
779 if((state & ios::failbit) || (state & ios::badbit) ||
780 (!(state & ios::eofbit))) {
785 string message =
"Failed to convert string [" + str +
"] to a big "
817 if(str.substr(0, 3) ==
"16#" && str.substr(str.length() - 1, 1) ==
"#") {
820 s << str.substr(3, str.find_last_of(
"#") - 3);
821 s.seekg(0, ios::beg);
830 ios::iostate state = s.rdstate();
831 if((state & ios::failbit) || (state & ios::badbit)) {
837 string message =
"Failed to convert HEX string [" + str +
"] to a "
847 s.seekg(0, ios::beg);
849 ios::iostate state = s.rdstate();
850 if((state & ios::failbit) || (state & ios::badbit) ||
851 (!(state & ios::eofbit))) {
856 string message =
"Failed to convert string [" + str +
"] to a double";
870 return QString::fromStdString(*
this);
881 return(QString::fromStdString(s));
900 for(
unsigned int i = 0; i < size(); i++) {
901 for(
unsigned int sep = 0; sep < separator.size(); sep++) {
902 if(separator[sep] == at(i)) {
903 retstr = substr(0, i);
904 replace(0, i + 1,
"");
912 replace(0, size(),
"");
941 std::vector<std::string> &tokens,
942 bool allowEmptyEntries) {
943 string::size_type idx(0), idx2(0);
944 unsigned int ksize = str.size();
948 if(str[idx] == separator) idx++;
949 while((idx2 = str.find(separator, idx)) != string::npos) {
951 if(allowEmptyEntries) tokens.push_back(
"");
954 string::size_type len(idx2 - idx);
955 tokens.push_back(str.substr(idx, len));
959 if(idx < ksize) tokens.push_back(str.substr(idx));
961 return(tokens.size());
975 *
this =
Compress((
string) *
this, force);
994 int leftquote = result.find_first_of(
"\"\'");
995 while((spaces = result.find(
" ", spaces)) >= 0) {
996 int rightquote = result.find_first_of(
"\"\'", leftquote + 1);
997 if(spaces < leftquote) {
998 result.erase(spaces, 1);
999 leftquote = result.find_first_of(
"\"\'", spaces);
1001 else if((spaces > leftquote) && (spaces < rightquote)) {
1002 spaces = rightquote + 1;
1003 leftquote = result.find_first_of(
"\"\'", rightquote + 1);
1005 else if(leftquote == (
int)npos) {
1006 result.erase(spaces, 1);
1009 leftquote = result.find_first_of(
"\"\'", rightquote + 1);
1016 while((spaces = result.find(
" ", spaces)) >= 0) {
1017 result.erase(spaces, 1);
1038 int maxReplaceCount) {
1039 *
this =
IString(
Replace((
string) *
this, from, to, maxReplaceCount));
1092 const std::string &to,
int maxReplaceCount) {
1093 if(str.empty())
return(str);
1094 if(from.empty())
return(str);
1096 string::size_type pos;
1098 while((nReplaced < maxReplaceCount) &&
1099 (pos = sRet.find(from)) != string::npos) {
1100 sRet.replace(pos, from.size(), to);
1125 *
this =
Replace((
string) *
this, from, to, honorquotes);
1148 const std::string &to ,
bool honorquotes) {
1150 string result = str;
1151 if(honorquotes ==
true) {
1153 int quote = result.find_first_of(
"\"\'");
1154 while((instances = result.find(from, instances)) >= 0) {
1156 int nextquote = result.find_first_of(
"\"\'", quote + 1);
1157 if(instances < quote) {
1158 result.replace(instances, from.length(), to);
1159 quote = result.find_first_of(
"\"\'", instances);
1161 else if((instances > quote) && (instances < nextquote)) {
1162 instances = nextquote + 1;
1163 quote = result.find_first_of(
"\"\'", nextquote);
1165 else if(quote == (
int)npos) {
1166 result.replace(instances, from.length(), to);
1169 quote = result.find_first_of(
"\"\'", nextquote);
1176 while((instances = result.find(from, instances)) >= 0) {
1177 result.replace(instances, from.length(), to);
1197 *
this =
Convert((
string) *
this, listofchars, to);
1218 std::string::size_type pos = 0;
1219 string result = str;
1222 while((pos = result.find_first_of(listofchars, pos)) != npos) {
1223 result.replace(pos, 1, tmp);
1253 return Convert(str,
"\n\r\t\f\v\b",
' ');
1267 std::string::size_type pos;
1268 while((pos = find_first_of(del)) != npos) this->erase(pos, 1);
1286 string::size_type pos;
1288 while((pos = result.find_first_of(del)) != npos) result.erase(pos, 1);
1334 return(str.toStdString());
1348 for(
unsigned int i = 0 ; i < sl.size() ; i++) {
1364 std::vector<std::string> Stdsl;
1365 for(
int i = 0 ; i < sl.size() ; i++) {
1366 Stdsl.push_back(
ToStd(sl.at(i)));
1384 std::ostream &
operator<<(std::ostream &outputStream,
const QString &
string) {
1385 return (outputStream <<
string.toLatin1().data());
1400 std::ostream &
operator<<(std::ostream &outputStream,
const QStringRef &
string) {
1401 return (outputStream <<
string.
toString().toLatin1().data());