00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "BasicString.h"
00025
00026 #include "BasicException.h"
00027
00028
00029 #include <stdlib.h>
00030 #include <errno.h>
00031 #include <cstdio>
00032
00033 using namespace std;
00034
00035 BasicString::BasicString(const int x) {
00036 char buf[12];
00037 sprintf(buf, "%i", x);
00038 *this = buf;
00039 }
00040
00041 BasicString::BasicString(const unsigned x) {
00042 char buf[12];
00043 sprintf(buf, "%i", x);
00044 *this = buf;
00045 }
00046
00047 BasicString::BasicString(const long x) {
00048 char buf[12];
00049 sprintf(buf, "%li", x);
00050 *this = buf;
00051 }
00052
00053 BasicString::BasicString(const unsigned long x) {
00054 char buf[12];
00055 sprintf(buf, "%lu", x);
00056 *this = buf;
00057 }
00058
00059 BasicString::BasicString(const double x) {
00060 char buf[16];
00061 sprintf(buf, "%f", x);
00062 *this = buf;
00063 }
00064
00065 unsigned char BasicString::parseUByte(const std::string s) {
00066 unsigned int v = parseUInteger(s);
00067 ASSERT_OR_THROW(string("Byte value '") + s + "'out of range!", v < 256);
00068
00069 return (unsigned char)v;
00070 }
00071
00072 char BasicString::parseByte(const std::string s) {
00073 int v = parseUInteger(s);
00074 ASSERT_OR_THROW(string("Byte value '") + s + "'out of range!",
00075 -128 < v && v < 128);
00076
00077 return (char)v;
00078 }
00079
00080 unsigned short BasicString::parseUShort(const std::string s) {
00081 unsigned int v = parseUInteger(s);
00082 ASSERT_OR_THROW(string("Short value '") + s + "'out of range!", v < 65536);
00083
00084 return (unsigned short)v;
00085 }
00086
00087 short BasicString::parseShort(const std::string s) {
00088 int v = parseUInteger(s);
00089 ASSERT_OR_THROW(string("Byte value '") + s + "'out of range!",
00090 -32768 < v && v < 32768);
00091
00092 return (short)v;
00093 }
00094
00095 unsigned int BasicString::parseUInteger(const std::string s) {
00096 errno = 0;
00097 unsigned long v = strtol(s.c_str(), 0, 10);
00098 ASSERT_OR_THROW(string("parseUInteger() Invalid unsigned integer '") + s +
00099 "'!", errno == 0 && v >= 0);
00100
00101 return (unsigned int)v;
00102 }
00103
00104 int BasicString::parseInteger(const std::string s) {
00105 errno = 0;
00106 long v = strtol(s.c_str(), 0, 10);
00107 ASSERT_OR_THROW(string("parseInteger() Invalid integer '") + s +
00108 "'!", errno == 0);
00109
00110 return (int)v;
00111 }
00112
00113 double BasicString::parseDouble(const std::string s) {
00114 errno = 0;
00115 double v = strtod(s.c_str(), 0);
00116 ASSERT_OR_THROW(string("parseDouble() Invalid double '") + s +
00117 "'!", errno == 0);
00118 return v;
00119 }
00120
00121 bool BasicString::parseBool(const std::string s) {
00122 string v = toLower(trim(s));
00123 if (v == "true") return true;
00124 if (v == "false") return false;
00125 THROW(string("parseBool() Invalid bool '") + s + "'!");
00126 }
00127
00128 string BasicString::trim(const string s) {
00129 string::size_type start = s.find_first_not_of(" \t\n\r");
00130 string::size_type end = s.find_last_not_of(" \t\n\r");
00131
00132 if (start == string::npos) return "";
00133 return s.substr(start, (end - start) + 1);
00134 }
00135
00136 string BasicString::toUpper(const string s) {
00137 string v;
00138 string::size_type len = s.length();
00139 v.resize(len, ' ');
00140
00141 for (string::size_type i = 0; i < len; i++)
00142 v[i] = toupper(s[i]);
00143
00144 return v;
00145 }
00146
00147 string BasicString::toLower(const string s) {
00148 string v;
00149 string::size_type len = s.length();
00150 v.resize(len, ' ');
00151
00152 for (string::size_type i = 0; i < len; i++)
00153 v[i] = tolower(s[i]);
00154
00155 return v;
00156 }