00001 #ifndef LIBSHERPA_INOSTREAM_HXX
00002 #define LIBSHERPA_INOSTREAM_HXX
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include <assert.h>
00042 #include <stdlib.h>
00043 #include <errno.h>
00044 #include <dirent.h>
00045 #include <string.h>
00046 #include <fstream>
00047 #include <iostream>
00048 #include <string>
00049 #include <sstream>
00050
00051 namespace sherpa {
00052
00053
00054
00055 struct INOstream {
00056 size_t depth;
00057 size_t col;
00058 bool needIndent;
00059 std::ostream &ostrm;
00060
00061
00062
00063 std::string postIndent;
00064
00065 INOstream(std::ostream &os)
00066 :ostrm(os)
00067 {
00068 depth = 0;
00069 col = 0;
00070 needIndent = true;
00071 }
00072
00073 void setPostindent(const std::string& s)
00074 {
00075 postIndent = s;
00076 }
00077
00078 inline void indent(int i)
00079 {
00080 depth += i;
00081 }
00082
00083 size_t indent_for_macro()
00084 {
00085 size_t odepth = depth;
00086 depth = 0;
00087 return odepth;
00088 }
00089
00090 inline int indentToHere()
00091 {
00092 size_t old_depth = depth;
00093 depth = (depth > col) ? depth : col;
00094 return old_depth;
00095 }
00096 inline void setIndent(size_t theCol)
00097 {
00098 depth = theCol;
00099 }
00100
00101 inline void more()
00102 {
00103 indent(2);
00104 }
00105
00106 inline void less()
00107 {
00108 assert(depth >= 2);
00109 indent(-2);
00110 }
00111
00112 void
00113 doIndent()
00114 {
00115
00116 if (needIndent) {
00117 while (col < depth) {
00118 ostrm << ' ';
00119 col++;
00120 }
00121 ostrm << postIndent;
00122 }
00123 needIndent = false;
00124 }
00125
00126 inline
00127 INOstream& operator<<(const char c)
00128 {
00129 INOstream& inostrm = *this;
00130 inostrm.doIndent();
00131
00132 inostrm.ostrm << c;
00133 inostrm.col++;
00134
00135 if (c == '\n') {
00136 inostrm.col = 0;
00137 inostrm.needIndent = true;
00138 }
00139 return inostrm;
00140 }
00141
00142 inline
00143 INOstream& operator<<(const unsigned char c)
00144 {
00145 INOstream& inostrm = *this;
00146 inostrm.doIndent();
00147
00148 inostrm.ostrm << c;
00149 inostrm.col++;
00150 if (c == '\n') {
00151 inostrm.col = 0;
00152 inostrm.needIndent = true;
00153 }
00154 return inostrm;
00155 }
00156
00157
00158
00159
00160
00161
00162
00163
00164 inline
00165 INOstream& operator<<(std::ostream& (*pf)(std::ostream&))
00166 {
00167 INOstream& inostrm = *this;
00168 if (pf == (std::ostream& (*)(std::ostream&)) std::endl) {
00169 inostrm << '\n';
00170 }
00171 else {
00172 inostrm.doIndent();
00173 inostrm.ostrm << pf;
00174 }
00175 return inostrm;
00176 }
00177
00178 inline
00179 INOstream& operator<<(std::ios_base& (*pf)(std::ios_base&))
00180 {
00181 INOstream& inostrm = *this;
00182 inostrm.doIndent();
00183 inostrm.ostrm << pf;
00184 return inostrm;
00185 }
00186
00187 inline
00188 INOstream& operator<<(const char *s)
00189 {
00190 INOstream& inostrm = *this;
00191
00192 for (size_t i = 0; i < strlen(s); i++)
00193 inostrm << s[i];
00194
00195 return inostrm;
00196 }
00197
00198 #if 0
00199 inline
00200 INOstream& operator<<(const unsigned long long ull)
00201 {
00202 char digits[64];
00203 sprintf(digits, "%ull", ull);
00204
00205 INOstream& inostrm = *this;
00206 inostrm << digits;
00207
00208 return inostrm;
00209 }
00210
00211 inline
00212 INOstream& operator<<(const long long ll)
00213 {
00214 char digits[64];
00215 sprintf(digits, "%ll", ll);
00216
00217 INOstream& inostrm = *this;
00218 inostrm << digits;
00219
00220 return inostrm;
00221 }
00222
00223 inline
00224 INOstream& operator<<(const unsigned long ul)
00225 {
00226 char digits[64];
00227 sprintf(digits, "%ul", ul);
00228
00229 INOstream& inostrm = *this;
00230 inostrm << digits;
00231
00232 return inostrm;
00233 }
00234
00235 inline
00236 INOstream& operator<<(const long l)
00237 {
00238 char digits[64];
00239 sprintf(digits, "%l", l);
00240
00241 INOstream& inostrm = *this;
00242 inostrm << digits;
00243
00244 return inostrm;
00245 }
00246
00247 INOstream& operator<<(const unsigned int ui)
00248 {
00249 char digits[64];
00250 sprintf(digits, "%ul", ui);
00251
00252 INOstream& inostrm = *this;
00253 inostrm << digits;
00254
00255 return inostrm;
00256 }
00257
00258 inline
00259 INOstream& operator<<(const int i)
00260 {
00261 char digits[64];
00262 sprintf(digits, "%l", i);
00263
00264 INOstream& inostrm = *this;
00265 inostrm << digits;
00266
00267 return inostrm;
00268 }
00269 #endif
00270
00271 inline
00272 INOstream& operator<<(const std::string& s)
00273 {
00274 INOstream& inostrm = *this;
00275
00276 for (size_t i = 0; i < s.size(); i++)
00277 inostrm << s[i];
00278
00279 return inostrm;
00280 }
00281
00282 template<typename T>
00283 inline
00284 INOstream& operator<<(T ob)
00285 {
00286 std::stringstream ss;
00287 ss << ob;
00288
00289 INOstream& inostrm = *this;
00290 inostrm << ss.str();
00291 return inostrm;
00292 }
00293
00294 };
00295
00296 }
00297
00298 #endif