PLearn 0.1
lexical_cast.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // lexical_cast.cc
00004 //
00005 // Copyright (C) 2005 Christian Dorion 
00006 // Copyright (C) 2006 University of Montreal
00007 // 
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions are met:
00010 // 
00011 //  1. Redistributions of source code must retain the above copyright
00012 //     notice, this list of conditions and the following disclaimer.
00013 // 
00014 //  2. Redistributions in binary form must reproduce the above copyright
00015 //     notice, this list of conditions and the following disclaimer in the
00016 //     documentation and/or other materials provided with the distribution.
00017 // 
00018 //  3. The name of the authors may not be used to endorse or promote
00019 //     products derived from this software without specific prior written
00020 //     permission.
00021 // 
00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 // 
00033 // This file is part of the PLearn library. For more information on the PLearn
00034 // library, go to the PLearn Web site at www.plearn.org
00035 
00036 /* *******************************************************      
00037  * $Id: lexical_cast.cc 9622 2008-10-29 15:23:58Z tihocan $ 
00038  ******************************************************* */
00039 
00040 // Authors: Christian Dorion
00041 
00045 #include "lexical_cast.h"
00046 #include <plearn/base/plerror.h>
00047 #include <plearn/math/pl_math.h>
00048 
00049 // For removeblanks: REMOVE AS SOON AS POSSIBLE
00050 #include <plearn/base/stringutils.h> 
00051 
00052 namespace PLearn {
00053 using namespace std;
00054 
00056 // pl_strtod //
00058 double pl_strtod(const char* nptr, char** endptr)
00059 {
00060 #ifdef WIN32
00061     int i = 0;
00062     char c;
00063     while (isspace(c = nptr[i++])) {}
00064     const char* end_parsing;
00065     bool success = false;
00066     bool minus = false;
00067     bool infinity = false;
00068     bool missing = false;
00069     if (c != 0) {
00070         if (c == '+')
00071             c = nptr[i++];
00072         else if (c == '-') {
00073             c = nptr[i++];
00074             minus = true;
00075         }
00076         if (c == 'i' || c == 'I') {
00077             // Try to read an 'inf'.
00078             c = nptr[i++];
00079             if (c == 'n' || c == 'N') {
00080                 c = nptr[i++];
00081                 if (c == 'f' || c == 'F') {
00082                     success = true;
00083                     infinity = true;
00084                     end_parsing = nptr + i;
00085                 }
00086             }
00087         } else if (c == 'n' || c == 'N') {
00088             // Try to read a 'nan'.
00089             c = nptr[i++];
00090             if (c == 'a' || c == 'A') {
00091                 c = nptr[i++];
00092                 if (c == 'n' || c == 'N') {
00093                     success = true;
00094                     missing = true;
00095                     end_parsing = nptr + i;
00096                 }
00097             }
00098         } else {
00099             // Try to read a 'normal' number. In such a case the standard
00100             // 'strtod' function should work properly.
00101             return strtod(nptr, endptr);
00102         }
00103     }
00104     if (success) {
00105         // Ensure there are no weird trailing characters.
00106         while (isspace(c = nptr[i++])) {}
00107         if (c != 0)
00108             success = false;
00109     }
00110     if (!success) {
00111         // Could not perform the conversion.
00112         if (endptr)
00113             *endptr = (char*) nptr;
00114         return 0;
00115     }
00116     if (endptr)
00117         *endptr = (char*) end_parsing;
00118     if (missing)
00119         return MISSING_VALUE;
00120     if (infinity)
00121         return minus ? - INFINITY : INFINITY;
00122     PLASSERT( false );
00123     return 0;
00124 #else
00125     // Under other operating systems, there shoud be no problems with NaN and
00126     // Infinity.
00127     return strtod(nptr, endptr);
00128 #endif
00129 }
00130 
00132 // pl_strtof //
00134 float pl_strtof(const char* nptr, char** endptr)
00135 {
00136 #ifdef WARN_STRTOF_ROUND
00137     //We do it this way to detect if we have enought precision to store it in a float.
00138     //Their is no error generated if the string is "84672690528" event if 84672692224f is returned.
00139     float f;
00140     double d = pl_strtod(nptr, endptr);
00141     f = float(d);
00142     if(d!=f && !is_missing(f))
00143         PLWARNING("In pl_strtof() - float does not have enough precision to"
00144                   " store %s. It is stored as %f. Float has 24 binary digits"
00145                   " of precision (6 decimal digits of precision)", nptr, f);
00146     return f;
00147 #elif defined(WIN32)
00148     return float(pl_strtod(nptr, endptr));
00149 #else
00150     return strtof(nptr, endptr);
00151 #endif
00152 }
00153 
00155 // pl_isnumber //
00157 bool pl_isnumber(const string& str, double* dbl)
00158 {
00159     double d;
00160     string s=removeblanks(str);
00161     char* l;
00162     d = pl_strtod(s.c_str(),&l);
00163     if(s.empty())
00164         d = MISSING_VALUE;
00165     if(dbl)
00166         *dbl=d;
00167     if(s.empty())
00168         return false;
00169     else
00170         return ((unsigned char)(l-s.c_str())==s.length());
00171 }
00172 
00173 bool pl_isnumber(const string& str, float* dbl) {
00174     float d;
00175     string s=removeblanks(str);
00176     char* l;
00177     d = pl_strtof(s.c_str(),&l);
00178     if(s.empty())
00179         d = MISSING_VALUE;
00180     if(dbl)
00181         *dbl=d;
00182     if(s.empty())
00183         return false;
00184     else
00185         return ((unsigned char)(l-s.c_str())==s.length());
00186 }
00187 
00188 // Return true if conversion to a long is possible
00189 bool pl_islong(const string& s)
00190 {
00191     // c_str() might yield very short-lived temporaries, don't assume those
00192     // pointers live beyond the syntactic expression.
00193     const char *nptr;
00194     char *endptr;
00195     strtol((nptr=s.c_str()), &endptr, 10);
00196     return endptr && (endptr - nptr == int(s.size()));
00197 }
00198 
00199 
00200 long tolong(const string& s, int base)
00201 {
00202     const char* nptr = s.c_str();
00203     char* endptr;
00204     long result = strtol(nptr,&endptr,base);
00205     if(endptr==nptr) { // no character to be read
00206         string err = string("in toint string is not an int: ") + s;
00207         PLERROR(err.c_str());
00208     }
00209     return result;
00210 }
00211 
00212 double todouble(const string& s)
00213 {
00214     const char* nptr = s.c_str();
00215     char* endptr;
00216     double result = pl_strtod(nptr,&endptr);
00217     if(endptr==nptr) // no character to be read
00218         result = MISSING_VALUE;
00219     return result;
00220 }
00221 
00222 bool tobool(const string& s)
00223 {
00224     if (s=="true" || s=="1") return true;
00225     if (s=="false" || s=="0") return false;
00226     PLERROR("tobool: can't convert string %s into a boolean",s.c_str());
00227     return false;
00228 }
00229 
00230 } // end of namespace PLearn
00231 
00232 
00233 /*
00234   Local Variables:
00235   mode:c++
00236   c-basic-offset:4
00237   c-file-style:"stroustrup"
00238   c-file-offsets:((innamespace . 0)(inline-open . 0))
00239   indent-tabs-mode:nil
00240   fill-column:79
00241   End:
00242 */
00243 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines