CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/Utilities/General/interface/ioutils.h

Go to the documentation of this file.
00001 #ifndef UTILITIES_GENERAL_IOUTILS_H
00002 #define UTILITIES_GENERAL_IOUTILS_H
00003 //
00004 // general io utilities...
00005 // no
00006 //  V 1.99999 o  18/04/02
00007 //  one more change to oss for linux...
00008 //  V 1.999999 o  09/08/02
00009 //  one more change to oss for linux...
00010 //
00011 #include <cstdio>
00012 #include <unistd.h>
00013 #include <cstdlib>
00014 
00015 #include <sstream>
00016 #include <iterator>
00017 #include <string>
00018 #include <vector>
00019 
00020 // generic ato
00021 template<class T>
00022 struct ato {
00023   inline const T & operator () (const T&c) const { return c;}
00024 };
00025 
00026 template<>
00027 struct ato<bool> {
00028   bool operator () (const std::string &c) const;
00029 };
00030 
00031 template<>
00032 struct ato<int> {
00033   inline int operator () (const char * c) const { return atoi(c);}
00034   inline int operator () (const std::string &c) const { return atoi(c.c_str());}
00035 };
00036 
00037 template<>
00038 struct ato<unsigned int> {
00039   inline unsigned int operator () (const char * c) const { return strtoul(c,0,0);}
00040   inline unsigned int operator () (const std::string &c) const { return strtoul(c.c_str(),0,0);}
00041 };
00042 
00043 template<>
00044 struct ato<float> {
00045   inline float operator () (const char * c) const { return atof(c);}
00046   inline float operator () (const std::string &c) const { return atof(c.c_str());}
00047 };
00048 
00049 template<>
00050 struct ato<double> {
00051   inline double operator () (const char * c) const { return strtod(c,0);}
00052   inline double operator () (const std::string &c) const { return strtod(c.c_str(),0);}
00053 };
00054 
00055 template<>
00056 struct ato<char *> {
00057   inline const char * operator () (const char * c) const { return c;}
00058   inline const char * operator () (const std::string &c) const { return c.c_str();}
00059 };
00060 
00061 template<>
00062 struct ato<std::string> {
00063   inline const std::string & operator () (const char * c) const { static std::string cs; cs=c;return cs;}
00064   inline const std::string & operator () (const std::string &c) const { return c;}
00065 };
00066 
00067 template<typename T>
00068 struct ato<std::vector<T> > {
00069   inline std::vector<T> operator () (const char * c) const { 
00070     std::vector<T> v;
00071     if (!c) return v;
00072     std::istringstream in(c);
00073     std::istream_iterator<T> sbegin(in), send;
00074     std::copy(sbegin,send,std::inserter(v,v.end()));
00075     return v;
00076   }
00077   
00078   inline std::vector<T>  operator () (const std::string &cs) const { 
00079     std::vector<T> v;
00080     if (cs.empty()) return v;
00081     std::istringstream in(cs.c_str());
00082     std::istream_iterator<T> sbegin(in), send;
00083     std::copy(sbegin,send,std::inserter(v,v.end()));
00084     return v;
00085   }
00086  
00087 };
00088 
00089 
00094 class toa {
00095 
00096 public:
00097 
00098   inline toa(){}
00099   ~toa();
00100 
00101 
00102   const char * operator () (const unsigned short int&c) const { return operator()(int(c));}
00103   const char * operator () (const short int&c) const { return operator()(int(c));}
00104   const char * operator () (const unsigned long&c) const { return operator()(int(c));}
00105   const char * operator () (const long&c) const { return operator()(int(c));}
00106   const char * operator () (const unsigned int&c) const { return operator()(int(c));}
00107   const char * operator () (bool c) const;
00108   const char * operator () (const int&c) const;
00109   const char * operator () (const float&c) const { return operator()(double(c));}
00110   const char * operator () (const double&c) const;
00111   const char * operator () (const char c[], int n) const;
00112   const char * operator () (const char *c) const { return c;}
00113   const char * operator () (const std::string&c) const { return c.c_str();}
00114   template<typename T>
00115   const char * operator () (const std::vector<T>&v) const { 
00116     std::ostream_iterator<T > oi(oss()," ");
00117     std::copy(v.begin(),v.end(),oi);
00118     localS() = oss().str();
00119     return localS().c_str();
00120   }
00121 
00122 private:
00123   mutable std::ostringstream oss_;
00124   std::ostringstream & oss() const { return oss_;}
00125 
00126   static std::string & localS();
00127 
00128 private:
00129   toa(const toa&){}
00130   void operator=(const toa&){}
00131 };
00132 
00133 #endif // UTILITIES_GENERAL_IOUTILS_H