69d4ee2804672e0fda8b344d5038c1f2213dde94
[ctsim.git] / include / sstream
1 #ifndef __CM_SSTREAM__
2 #define __CM_SSTREAM__
3
4 #include <string>
5 #include <cstdio>
6 #include <strstream>
7 #include <algorithm>
8
9 namespace std {
10
11 class ostringstream
12 {
13 public:
14     ostringstream (const string & str = "")
15         : buffer(str) {}
16
17     const string & str() const
18     {
19         return buffer;
20     }
21
22     void str (const string & new_string)
23     {
24         buffer = new_string;
25     }
26
27     ostringstream & operator<< (const string & item)
28     {
29         buffer += item;
30
31         return *this;
32     }
33
34     ostringstream & operator<< (int item)
35     {
36         char temp[100];
37
38         sprintf (temp, "%d", item);
39         buffer += temp;
40
41         return *this;
42     }
43
44     ostringstream & operator<< (unsigned int item)
45     {
46         char temp[100];
47
48         sprintf (temp, "%u", item);
49         buffer += temp;
50
51         return *this;
52     }
53
54     ostringstream & operator<< (char item)
55     {
56         buffer += item;
57         return *this;
58     }
59
60     ostringstream & operator<< (double item)
61     {
62         char temp[1000];
63
64         sprintf (temp, "%g", item);
65         buffer += temp;
66
67         return *this;
68     }
69
70 private:
71     string buffer;
72 };
73
74
75
76 class istringstream
77 {
78     friend istringstream & getline (istringstream &, string &, char = '\n');
79
80 public:
81     istringstream (const string & str = "")
82         : buffer (str.c_str(), str.length()) {}
83
84     template <class T>
85     istringstream & operator>> (T & item)
86     {
87         buffer >> item;
88         return *this;
89     }
90
91     operator void * () const
92     {
93         return (void *) buffer;
94     }
95
96 private:
97     istrstream buffer;
98 };
99
100
101 inline istringstream & getline (istringstream & src_stream, string & str, char separator)
102 {
103     getline (src_stream.buffer, str, separator);
104     return src_stream;
105 }
106
107 }   // End of namespace std
108
109 #endif
110