00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00035 #include "miscfunctions.h"
00036 #include <cstring>
00037 #include <cstdlib>
00038 #include <iostream>
00039
00041 using namespace std;
00042
00043 long double MiscFunctions::getNumericFromString(const char *str) {
00044 bool ws_only = true;
00045
00046 for (unsigned i = 0; i < strlen(str); i++) {
00047 if (!isspace(str[i])) {
00048 ws_only = false;
00049 break;
00050 }
00051 }
00052 if (ws_only) {
00053 throw
00054 InvalidCastException
00055 ("Method \"getNumericFromString()\" Cannot Convert Whitespaces To A Number",
00056 eid_cast_stringnumeric);
00057 }
00058
00059 char *nstr = (char *) str;
00060
00061 if (REPLACE_DECIMAL_POINT_BEFORE) {
00062 while (char *pComma = strchr(nstr, ',')) {
00063 *pComma = '.';
00064 }
00065 }
00066
00067 errno = 0;
00068 char *pEnd;
00069 long double result = strtold(nstr, &pEnd);
00070
00071 if (isnan(result)) {
00072
00073 } else if (isinf(result)) {
00074
00075 } else if (result == HUGE_VALL) {
00076 throw
00077 InvalidCastException
00078 ("Result From Method \"getNumericFromString()\" Is Too Large (HUGE_VALL)",
00079 eid_cast_stringnumeric);
00080 } else if (result == -HUGE_VALL) {
00081 throw
00082 InvalidCastException
00083 ("Result From Method \"getNumericFromString()\" Is Too Small (-HUGE_VALL)",
00084 eid_cast_stringnumeric);
00085 } else if (errno == ERANGE) {
00086 throw
00087 InvalidCastException
00088 ("Result From Method \"getNumericFromString()\" Is Either Too Large (HUGE_VALL) Or Too Small (-HUGE_VALL)",
00089 eid_cast_stringnumeric);
00090 } else if (errno) {
00091 throw
00092 InvalidCastException
00093 ("Result From Method \"getNumericFromString()\" Is Wrong Due To An Unknown Error",
00094 eid_cast_stringnumeric);
00095 } else if (*pEnd && !isdigit(*pEnd)) {
00096 while (*pEnd) {
00097 if (!isspace(*pEnd)) {
00098 throw
00099 InvalidCastException
00100 ("Result From Method \"getNumericFromString()\" Still Contains Not Castable Characters",
00101 eid_cast_stringnumeric);
00102 }
00103 pEnd++;
00104 }
00105 }
00106
00107 return result;
00108 }
00109
00110 const char *MiscFunctions::getStringFromNumerical(long double num) {
00111 ostringstream o;
00112
00113 if (!(o << num)) {
00114 throw
00115 InvalidCastException
00116 ("Result From Method \"getStringFromNumerical()\" Is Wrong Due To Bad Conversion",
00117 eid_cast_numericstring);
00118 }
00119
00120 size_t str_len = strlen(o.str().c_str()) + 1;
00121 char *cpy = new char[str_len];
00122
00123 strcpy(cpy, o.str().c_str());
00124 cpy[str_len - 1] = '\0';
00125
00126 if (REPLACE_DECIMAL_POINT_AFTER) {
00127 while (char *pComma = strchr(cpy, '.')) {
00128 *pComma = ',';
00129 }
00130 }
00131
00132 return cpy;
00133 }
00134
00135 long double MiscFunctions::getSummationFrom(long double left, long double right) {
00136 errno = 0;
00137 long double sum = (left + right);
00138
00139 if (isnan(sum)) {
00140
00141 } else if (isinf(sum)) {
00142
00143 } else if (sum == HUGE_VALL) {
00144 throw
00145 InvalidCastException
00146 ("Result From Method \"getSummationFrom()\" Is Too Large (HUGE_VALL)",
00147 eid_cast_summation);
00148 } else if (sum == -HUGE_VALL) {
00149 throw
00150 InvalidCastException
00151 ("Result From Method \"getSummationFrom()\" Is Too Small (-HUGE_VALL)",
00152 eid_cast_summation);
00153 } else if (errno == ERANGE) {
00154 throw
00155 InvalidCastException
00156 ("Result From Method \"getSummationFrom()\" Is Either Too Large (HUGE_VALL) Or Too Small (-HUGE_VALL)",
00157 eid_cast_summation);
00158 } else if (errno) {
00159 throw
00160 InvalidCastException
00161 ("Result From Method \"getSummationFrom()\" Is Wrong Due To An Unknown Error",
00162 eid_cast_summation);
00163 }
00164
00165 return sum;
00166 }
00167
00168 long double MiscFunctions::getSubtractionFrom(long double left,
00169 long double right) {
00170 return getSummationFrom(left, -right);
00171 }
00172
00173 long double MiscFunctions::getPowerFrom(long double base, long double exponent) {
00174 errno = 0;
00175 long double power = pow(base, exponent);
00176
00177 if (isnan(power)) {
00178
00179 } else if (isinf(power)) {
00180
00181 } else if (power == HUGE_VALL) {
00182 throw
00183 InvalidCastException
00184 ("Result From Method \"getPowerFrom()\" Is Too Large (HUGE_VALL)",
00185 eid_cast_power);
00186 } else if (power == -HUGE_VALL) {
00187 throw
00188 InvalidCastException
00189 ("Result From Method \"getPowerFrom()\" Is Too Small (-HUGE_VALL)",
00190 eid_cast_power);
00191 } else if (errno == ERANGE) {
00192 throw
00193 InvalidCastException
00194 ("Result From Method \"getPowerFrom()\" Is Either Too Large (HUGE_VALL) Or Too Small (-HUGE_VALL)",
00195 eid_cast_power);
00196 } else if (errno == EDOM) {
00197 throw
00198 InvalidCastException
00199 ("Result From Method \"getPowerFrom()\" Is Wrong Due To An \"Domain Error\"",
00200 eid_cast_power);
00201 } else if (errno) {
00202 throw
00203 InvalidCastException
00204 ("Result From Method \"getPowerFrom()\" Is Wrong Due To An Unknown Error",
00205 eid_cast_power);
00206 }
00207
00208 return power;
00209 }
00210
00211 long double MiscFunctions::getRadicalFrom(long double base, long double radix) {
00212 if (base < (long double) 0) {
00213 throw
00214 InvalidCastException
00215 ("Result From Method \"getRadicalFrom()\" Is Wrong Due To Negative Base",
00216 eid_cast_radical);
00217 } else if (radix == (long double) 0) {
00218 throw
00219 InvalidCastException
00220 ("Result From Method \"getRadicalFrom()\" Is Wrong Due To Division By Zero",
00221 eid_cast_radical);
00222 }
00223
00224 return getPowerFrom(base, (1 / radix));
00225 }
00226
00227 long double MiscFunctions::getDivisionFrom(long double numerator,
00228 long double denominator) {
00229 if (denominator == (long double) 0) {
00230 throw
00231 InvalidCastException
00232 ("Result From Method \"getDivisionFrom()\" Is Wrong Due To Division By Zero",
00233 eid_cast_division);
00234 }
00235
00236 errno = 0;
00237 long double fraction = (numerator / denominator);
00238
00239 if (isnan(fraction)) {
00240
00241 } else if (isinf(fraction)) {
00242
00243 } else if (fraction == HUGE_VALL) {
00244 throw
00245 InvalidCastException
00246 ("Result From Method \"getDivisionFrom()\" Is Too Large (HUGE_VALL)",
00247 eid_cast_division);
00248 } else if (fraction == -HUGE_VALL) {
00249 throw
00250 InvalidCastException
00251 ("Result From Method \"getDivisionFrom()\" Is Too Small (-HUGE_VALL)",
00252 eid_cast_division);
00253 } else if (errno == ERANGE) {
00254 throw
00255 InvalidCastException
00256 ("Result From Method \"getDivisionFrom()\" Is Either Too Large (HUGE_VALL) Or Too Small (-HUGE_VALL)",
00257 eid_cast_division);
00258 } else if (errno) {
00259 throw
00260 InvalidCastException
00261 ("Result From Method \"getDivisionFrom()\" Is Wrong Due To An Unknown Error",
00262 eid_cast_division);
00263 }
00264
00265 return fraction;
00266 }
00267
00268 long double MiscFunctions::getRoundFrom(long double num, unsigned places) {
00269 double v[] = { 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
00270 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21,
00271 1e22
00272 };
00273
00274 return (floor(num * v[places] + 0.5) / v[places]);
00275 }
00276
00277 bool MiscFunctions::needsTrim(const char *s) {
00278 unsigned l = strlen(s);
00279
00280 return (l > 0 && (isspace(s[0]) || isspace(s[l - 1])));
00281 }
00282
00283 const char *MiscFunctions::trim(const char *s) {
00284
00285 int first_nonws = 0;
00286 int last_nonws = strlen(s) + 1;
00287
00288
00289 bool search_finished = false;
00290
00291 for (int i = 0; i < (int) strlen(s) - 1 && !search_finished; i++) {
00292 if (isspace(s[i])) {
00293 first_nonws = i;
00294 search_finished = true;
00295 }
00296 }
00297
00298 search_finished = false;
00299 for (int i = strlen(s) - 1; i >= 0 && !search_finished; i--) {
00300 if (isspace(s[i])) {
00301 last_nonws = i;
00302 search_finished = true;
00303 }
00304 }
00305
00306 char *ret = NULL;
00307
00308 if (last_nonws < first_nonws || last_nonws == (int) strlen(s) + 1) {
00309 ret = new char[1];
00310
00311 ret[0] = '\0';
00312 } else {
00313 ret =
00314 new char[(last_nonws - first_nonws) * sizeof (char) +
00315 2 * sizeof (char)];
00316 strncpy(ret, s + first_nonws,
00317 (last_nonws - first_nonws + sizeof (char)));
00318 }
00319
00320
00321 return ret;
00322 }