CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ioutils.h
Go to the documentation of this file.
1 #ifndef UTILITIES_GENERAL_IOUTILS_H
2 #define UTILITIES_GENERAL_IOUTILS_H
3 //
4 // general io utilities...
5 // no
6 // V 1.99999 o 18/04/02
7 // one more change to oss for linux...
8 // V 1.999999 o 09/08/02
9 // one more change to oss for linux...
10 //
11 #include <cstdio>
12 #include <unistd.h>
13 #include <cstdlib>
14 
15 #include <sstream>
16 #include <iterator>
17 #include <string>
18 #include <vector>
19 
20 // generic ato
21 template<class T>
22 struct ato {
23  inline const T & operator () (const T&c) const { return c;}
24 };
25 
26 template<>
27 struct ato<bool> {
28  bool operator () (const std::string &c) const;
29 };
30 
31 template<>
32 struct ato<int> {
33  inline int operator () (const char * c) const { return atoi(c);}
34  inline int operator () (const std::string &c) const { return atoi(c.c_str());}
35 };
36 
37 template<>
38 struct ato<unsigned int> {
39  inline unsigned int operator () (const char * c) const { return strtoul(c,0,0);}
40  inline unsigned int operator () (const std::string &c) const { return strtoul(c.c_str(),0,0);}
41 };
42 
43 template<>
44 struct ato<float> {
45  inline float operator () (const char * c) const { return atof(c);}
46  inline float operator () (const std::string &c) const { return atof(c.c_str());}
47 };
48 
49 template<>
50 struct ato<double> {
51  inline double operator () (const char * c) const { return strtod(c,0);}
52  inline double operator () (const std::string &c) const { return strtod(c.c_str(),0);}
53 };
54 
55 template<>
56 struct ato<char *> {
57  inline const char * operator () (const char * c) const { return c;}
58  inline const char * operator () (const std::string &c) const { return c.c_str();}
59 };
60 
61 template<>
62 struct ato<std::string> {
63  inline const std::string & operator () (const char * c) const { static std::string cs; cs=c;return cs;}
64  inline const std::string & operator () (const std::string &c) const { return c;}
65 };
66 
67 template<typename T>
68 struct ato<std::vector<T> > {
69  inline std::vector<T> operator () (const char * c) const {
70  std::vector<T> v;
71  if (!c) return v;
72  std::istringstream in(c);
73  std::istream_iterator<T> sbegin(in), send;
74  std::copy(sbegin,send,std::inserter(v,v.end()));
75  return v;
76  }
77 
78  inline std::vector<T> operator () (const std::string &cs) const {
79  std::vector<T> v;
80  if (cs.empty()) return v;
81  std::istringstream in(cs.c_str());
82  std::istream_iterator<T> sbegin(in), send;
83  std::copy(sbegin,send,std::inserter(v,v.end()));
84  return v;
85  }
86 
87 };
88 
89 
94 class toa {
95 
96 public:
97 
98  inline toa(){}
99  ~toa();
100 
101 
102  const char * operator () (const unsigned short int&c) const { return operator()(int(c));}
103  const char * operator () (const short int&c) const { return operator()(int(c));}
104  const char * operator () (const unsigned long&c) const { return operator()(int(c));}
105  const char * operator () (const long&c) const { return operator()(int(c));}
106  const char * operator () (const unsigned int&c) const { return operator()(int(c));}
107  const char * operator () (bool c) const;
108  const char * operator () (const int&c) const;
109  const char * operator () (const float&c) const { return operator()(double(c));}
110  const char * operator () (const double&c) const;
111  const char * operator () (const char c[], int n) const;
112  const char * operator () (const char *c) const { return c;}
113  const char * operator () (const std::string&c) const { return c.c_str();}
114  template<typename T>
115  const char * operator () (const std::vector<T>&v) const {
116  std::ostream_iterator<T > oi(oss()," ");
117  std::copy(v.begin(),v.end(),oi);
118  localS() = oss().str();
119  return localS().c_str();
120  }
121 
122 private:
123  mutable std::ostringstream oss_;
124  std::ostringstream & oss() const { return oss_;}
125 
126  static std::string & localS();
127 
128 private:
129  toa(const toa&){}
130  void operator=(const toa&){}
131 };
132 
133 #endif // UTILITIES_GENERAL_IOUTILS_H
Definition: ioutils.h:22
auto_ptr< ClusterSequence > cs
const char * operator()(const unsigned long &c) const
Definition: ioutils.h:104
const char * operator()(const long &c) const
Definition: ioutils.h:105
std::ostringstream oss_
Definition: ioutils.h:123
const T & operator()(const T &c) const
Definition: ioutils.h:23
toa(const toa &)
Definition: ioutils.h:129
Definition: ioutils.h:94
const char * operator()(const unsigned short int &c) const
Definition: ioutils.h:102
const char * operator()(const short int &c) const
Definition: ioutils.h:103
toa()
Definition: ioutils.h:98
static std::string & localS()
Definition: ioutils.cc:36
void operator=(const toa &)
Definition: ioutils.h:130
const char * operator()(const unsigned int &c) const
Definition: ioutils.h:106
~toa()
Definition: ioutils.cc:32
const char * operator()(const float &c) const
Definition: ioutils.h:109
std::ostringstream & oss() const
Definition: ioutils.h:124
long double T
mathSSE::Vec4< T > v