STK++ 1.0
STK_String_Util.h
Go to the documentation of this file.
00001 /*--------------------------------------------------------------------*/
00002 /*     Copyright (C) 2004-2007  Serge Iovleff
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU Lesser General Public License as
00006     published by the Free Software Foundation; either version 2 of the
00007     License, or (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU Lesser General Public License for more details.
00013 
00014     You should have received a copy of the GNU Lesser General Public
00015     License along with this program; if not, write to the
00016     Free Software Foundation, Inc.,
00017     59 Temple Place,
00018     Suite 330,
00019     Boston, MA 02111-1307
00020     USA
00021 
00022     Contact : Serge.Iovleff@stkpp.org
00023 */
00024 
00025 /*
00026  * Project:  stkpp::STKernel::Base
00027  * Purpose:  Define miscenaleous utility functions for Strings.
00028  * Author:   Serge Iovleff, serge.iovleff@stkpp.org
00029  *
00030  **/
00031 
00037 #ifndef STK_STRING_UTIL_H
00038 #define STK_STRING_UTIL_H
00039 
00040 #include "STK_String.h"
00041 #include "STK_Stream.h"
00042 
00043 #include "STK_Proxy.h"
00044 
00045 namespace STK
00046 {
00053 static const String STRING_NA      = String(_T("."));
00054 
00061 static const int STRING_NA_SIZE  = 1;
00062 
00081 template <class TYPE>
00082 bool stringToType( TYPE  &t
00083                  , String const& s
00084                  , std::ios_base& (*f)(std::ios_base&) = std::dec
00085                  )
00086 {
00087   istringstream iss(s);
00088   Proxy<TYPE> wrapper_t(t);
00089   bool flag1 = (iss >> f >> wrapper_t).fail();
00090   iss >> std::ws;
00091   // ok if the conversion success and the String is exhausted
00092   return ( !flag1 && iss.eof() );
00093 }
00094 
00105 template <class TYPE>
00106 String typeToString( TYPE const& t
00107                    , std::ios_base& (*f)(std::ios_base&) = std::dec
00108                    )
00109 {
00110   ostringstream oss;
00111   oss << f << ConstProxy<TYPE>(t);
00112   return oss.str();
00113 }
00114 
00123 template <class TYPE>
00124 ostream& operator << (ostream& os, const ConstProxy<TYPE>& output)
00125 { if (Arithmetic<TYPE>::isNA(output))
00126     return (os <<  STRING_NA);
00127   else
00128    return (os << static_cast<TYPE const &>(output));
00129 }
00130 
00131 
00149 template <class TYPE>
00150 istream& operator >> (istream& is, Proxy<TYPE>& input)
00151 {
00152   TYPE buff;
00153   // get current position in the stream
00154   typename std::ios::pos_type pos = is.tellg();
00155 
00156   // If the standard Conversion failed
00157   if ((is >> buff).fail())
00158   {
00159     // clear failbit state
00160     is.clear(is.rdstate() & ~std::ios::failbit);
00161     // clear eofbit state if necessary and rewind position
00162     if (is.eof())
00163     {
00164       is.seekg(pos);
00165       is.clear(is.rdstate() & ~std::ios::eofbit);
00166     }
00167     // in all case input is a NA object
00168     input = Arithmetic<TYPE>::NA();
00169 
00170     // get current position in the stream
00171     pos = is.tellg();
00172 
00173     // Create a String buffer
00174     Char Lbuff[STRING_NA_SIZE+1];
00175 
00176     // try to read a NA String
00177     is.getline(Lbuff, STRING_NA_SIZE+1);
00178 
00179     // if we don't get a NA String, rewind stream
00180     if (!(STRING_NA.compare(Lbuff) == 0))
00181     { is.seekg(pos);}
00182   }
00183   else
00184   { input = buff;}
00185   // return the stream
00186   return is;
00187 }
00188 
00194 istream& operator >> (istream& is, Proxy<String>& input);
00195 
00202 String& toUpperString( String& s);
00203 
00210 String toUpperString( String const& s);
00211 
00212 } // namespace STK
00213 
00214 #endif /* STK_STRING_UTIL_H */