ESLA

Embeddable Scriting LAnguage

frantz@pangea.stanford.edu

Stanford University, Rock Fracture Project research group

© 2003

string_utils.h

Go to the documentation of this file.
00001 /*************************************************************************
00002  *              ESLA: Embeddable Scripting LAnguage
00003  *                  Copyright (C) 2003 Frantz Maerten
00004  *
00005  *   This  program  is  free  software;  you  can  redistribute it and/or
00006  *   modify  it  under  the  terms  of  the GNU General Public License as
00007  *   published  by  the Free Software Foundation; either version 2 of the
00008  *   License, or (at your option) any later version.
00009  *
00010  *   If  you modify this software, you should contact the author, include
00011  *   a  notice giving the name of the person performing the modification,
00012  *   the date of modification, and the reason for such modification.
00013  *
00014  *   Note   that   the   GNU  General  Public  License  does  not  permit
00015  *   incorporating the Software into proprietary programs.
00016  *
00017  *  Contact: Frantz Maerten
00018  *     frantz@pangea.stanford.edu
00019  *
00020  *     Dept. of Geological & Environmental Sciences
00021  *     Stanford University 
00022  *     Stanford, CA 94305-2115
00023  *     USA
00024  *************************************************************************/
00025 
00026 
00027 #ifndef STRING_UTILS
00028 #define STRING_UTILS
00029 
00030 #include "../esla_namespace.h"
00031 #include <iostream>
00032 #include <strstream>
00033 #include <assert.h>
00034 #include <string>
00035 
00036 #define STRING( x ) _STRING( x )
00037 #define _STRING( x ) #x
00038 
00039 BEGIN_LIB_NAMESPACE
00040 
00041   template <class T>
00042   struct type_traits {} ;
00043 
00044   #define MAP_TYPE(T)\
00045   struct type_traits<T> {\
00046     static std::string type_name() {return STRING(T) ;}\
00047   } ;
00048 
00049   MAP_TYPE(int) ;
00050   MAP_TYPE(short) ;
00051   MAP_TYPE(unsigned int) ;
00052   MAP_TYPE(double) ;
00053   MAP_TYPE(float) ;
00054   MAP_TYPE(bool) ;
00055   MAP_TYPE(char) ;
00056 
00057   template <class T>
00058   std::string convert_to_string(T t) ;
00059 
00060   template<>
00061   std::string convert_to_string<double>(double t) ;
00062 
00063   template<>
00064   std::string convert_to_string<float>(float t) ;
00065  
00066   template <class T> T 
00067   decode(const std::string& s) ;
00068 
00069   // General tool to strip spaces and tabs from both ends:
00070   std::string trim_ends(const std::string& s) ;
00071 
00072 END_LIB_NAMESPACE
00073 
00074 
00075 
00076 
00077 
00078 
00079 
00080 // -------------- implementation -----------------------
00081 
00082 BEGIN_LIB_NAMESPACE
00083 
00084   template <class T>
00085   inline std::string convert_to_string(T t) {
00086     std::strstream sstream;
00087     if (!(sstream << t)) {
00088       std::cerr << "Error: cannot convert " << t << " of type " << type_traits<T>::type_name() << " to string." << std::endl ;
00089       exit(-1) ;
00090     }
00091     std::string result;
00092     sstream >> result ;
00093     return result ;
00094   }
00095    
00096   template <class T> 
00097   inline T decode(const std::string& s) {
00098     if (s.size()==0) {
00099       assert(1==0) ;
00100       std::cerr << "size of string convert = 0" << std::endl ;
00101       exit(-1) ;
00102     }
00103     T val;
00104     std::istrstream is(s.c_str());
00105     is >> val;
00106     if (!is) {
00107       std::cerr << "Error: cannot encode " << s << " string to type " << type_traits<T>::type_name() << std::endl ;
00108       exit(-1) ;
00109     }
00110     return val ;
00111   }
00112 
00113 END_LIB_NAMESPACE
00114 
00115 #endif

Generated on Wed May 14 11:42:31 2003 for Esla-lib by doxygen1.3-rc1