Back to index

texmacs  1.0.7.15
tm_ostream.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : tm_ostream.cpp
00004 * DESCRIPTION: Output stream class
00005 * COPYRIGHT  : (C) 2009  David MICHEL
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include "tm_ostream.hpp"
00013 #include "string.hpp"
00014 
00015 tm_ostream
00016 tm_ostream::private_cout (stdout);
00017 
00018 tm_ostream
00019 tm_ostream::private_cerr (stderr);
00020 
00021 tm_ostream::tm_ostream (const tm_ostream& out) { // private
00022   file= out.file;
00023   is_w= out.is_w;
00024   is_buf= out.is_buf;
00025   *buf= *(out.buf);
00026   is_mine= false;
00027 }
00028 
00029 tm_ostream&
00030 tm_ostream::operator= (const tm_ostream& out) { // private
00031   if (this != &out) {
00032     file= out.file;
00033     is_w= out.is_w;
00034     is_buf= out.is_buf;
00035     *buf= *(out.buf);
00036     is_mine= false;
00037   }
00038   return *this;
00039 }
00040 
00041 tm_ostream&
00042 tm_ostream::cout= private_cout;
00043 
00044 tm_ostream&
00045 tm_ostream::cerr= private_cerr;
00046 
00047 tm_ostream::tm_ostream () :
00048   file (0), is_w (false), is_mine (false), is_buf (false) {
00049   buf= tm_new<string> ();
00050 }
00051 
00052 tm_ostream::tm_ostream (char* fn) :
00053   file (0), is_w (false), is_mine (false), is_buf (false) {
00054   buf= tm_new<string> ();
00055   open (fn);
00056 }
00057 
00058 tm_ostream::tm_ostream (FILE* f) :
00059   file (0), is_w (false), is_mine (false), is_buf (false) {
00060   buf= tm_new<string> ();
00061   open (f);
00062 }
00063 
00064 tm_ostream::~tm_ostream () {
00065   if (file && is_mine) fclose (file);
00066   tm_delete (buf);
00067 }
00068 
00069 bool
00070 tm_ostream::open () {
00071   if (file && is_mine) fclose (file);
00072   file= 0;
00073   *buf= "";
00074   is_w= true;
00075   is_mine= true;
00076   is_buf= false;
00077   return is_w;
00078 }
00079 
00080 /*
00081 bool
00082 tm_ostream::open (url u) {
00083   if (file) fclose (file);
00084   char* _u= as_charp (concretize (u));
00085   file= fopen (_u, "w");
00086   tm_delete_array (_u);
00087   if (file) is_w= true;
00088   else is_w= false;
00089   return is_w;
00090 }
00091 */ 
00092 
00093 bool
00094 tm_ostream::open (char* fn) {
00095   if (file && is_mine) fclose (file);
00096   file= fopen (fn, "w");
00097   *buf= "";
00098   is_buf= false;
00099   if (file) {
00100     is_w= true;
00101     is_mine= true;
00102   } else {
00103     is_w= false;
00104     is_mine= false;
00105   }
00106   return is_w;
00107 }
00108 
00109 bool
00110 tm_ostream::open (FILE* f) {
00111   if (file && is_mine) fclose (file);
00112   file= f;
00113   *buf= "";
00114   is_buf= false;
00115   if (file) is_w= true;
00116   else is_w= false;
00117   is_mine= false;
00118   return is_w;
00119 }
00120 
00121 bool
00122 tm_ostream::is_writable () const {
00123   return is_w;
00124 }
00125 
00126 bool
00127 tm_ostream::is_buffered () const {
00128   return is_buf;
00129 }
00130 
00131 void
00132 tm_ostream::flush () {
00133   if (file && is_w) fflush (file);
00134 }
00135   
00136 void
00137 tm_ostream::close () {
00138   if (file && is_mine) fclose (file);
00139   file= 0;
00140   *buf= "";
00141   is_w= false;
00142   is_mine= false;
00143   is_buf= false;
00144 }
00145 
00146 void
00147 tm_ostream::buffer () {
00148   is_buf= true;
00149   *buf= "";
00150 }
00151 
00152 string
00153 tm_ostream::unbuffer () {
00154   string res= *buf;
00155   *buf= "";
00156   is_buf= false;
00157   return res;
00158 }
00159 
00160 tm_ostream&
00161 tm_ostream::operator << (bool b) {
00162   if (is_buf) {
00163     if (b) *buf << "true";
00164     else *buf << "false";
00165   }
00166   else if (file && is_w) {
00167     if (b) {
00168       if (0 > fprintf (file, "%s", "true")) is_w= false;
00169     } else {
00170       if (0 > fprintf (file, "%s", "false")) is_w= false;
00171     }
00172   }
00173   return *this;
00174 }
00175 
00176 tm_ostream&
00177 tm_ostream::operator << (char c) {
00178   if (is_buf) {
00179     char _buf[8];
00180     sprintf (_buf, "%c", c);
00181     *buf << _buf;
00182   }
00183   else if (file && is_w)
00184     if (0 > fprintf (file, "%c", c)) is_w= false;
00185   return *this;
00186 }
00187 
00188 tm_ostream&
00189 tm_ostream::operator << (short sh) {
00190   if (is_buf) {
00191     char _buf[32];
00192     sprintf (_buf, "%hd", sh);
00193     *buf << _buf;
00194   }
00195   else if (file && is_w)
00196     if (0 > fprintf (file, "%hd", sh)) is_w= false;
00197   return *this;
00198 }
00199 
00200 tm_ostream&
00201 tm_ostream::operator << (unsigned short ush) {
00202   if (is_buf) {
00203     char _buf[32];
00204     sprintf (_buf, "%hu", ush);
00205     *buf << _buf;
00206   }
00207   else if (file && is_w)
00208     if (0 > fprintf (file, "%hu", ush)) is_w= false;
00209   return *this;
00210 }
00211 
00212 tm_ostream&
00213 tm_ostream::operator << (int i) {
00214   if (is_buf) {
00215     char _buf[64];
00216     sprintf (_buf, "%d", i);
00217     *buf << _buf;
00218   }
00219   else if (file && is_w)
00220     if (0 > fprintf (file, "%d", i)) is_w= false;
00221   return *this;
00222 }
00223 
00224 tm_ostream&
00225 tm_ostream::operator << (unsigned int ui) {
00226   if (is_buf) {
00227     char _buf[64];
00228     sprintf (_buf, "%u", ui);
00229     *buf << _buf;
00230   }
00231   else if (file && is_w)
00232     if (0 > fprintf (file, "%u", ui)) is_w= false;
00233   return *this;
00234 }
00235 
00236 tm_ostream&
00237 tm_ostream::operator << (long l) {
00238   if (is_buf) {
00239     char _buf[64];
00240     sprintf (_buf, "%ld", l);
00241     *buf << _buf;
00242   }
00243   else if (file && is_w)
00244     if (0 > fprintf (file, "%ld", l)) is_w= false;
00245   return *this;
00246 }
00247 
00248 tm_ostream&
00249 tm_ostream::operator << (unsigned long ul) {
00250   if (is_buf) {
00251     char _buf[64];
00252     sprintf (_buf, "%lu", ul);
00253     *buf << _buf;
00254   }
00255   else if (file && is_w)
00256     if (0 > fprintf (file, "%lu", ul)) is_w= false;
00257   return *this;
00258 }
00259 
00260 tm_ostream&
00261 tm_ostream::operator << (long long int l) {
00262   if (is_buf) {
00263     char _buf[64];
00264     sprintf (_buf, "%lld", l);
00265     *buf << _buf;
00266   }
00267   else if (file && is_w)
00268     if (0 > fprintf (file, "%lld", l)) is_w= false;
00269   return *this;
00270 }
00271 
00272 tm_ostream&
00273 tm_ostream::operator << (unsigned long long int ul) {
00274   if (is_buf) {
00275     char _buf[64];
00276     sprintf (_buf, "%llu", ul);
00277     *buf << _buf;
00278   }
00279   else if (file && is_w)
00280     if (0 > fprintf (file, "%llu", ul)) is_w= false;
00281   return *this;
00282 }
00283 
00284 tm_ostream&
00285 tm_ostream::operator << (float f) {
00286   if (is_buf) {
00287     char _buf[32];
00288     sprintf (_buf, "%g", f);
00289     *buf << _buf;
00290   }
00291   else if (file && is_w)
00292     if (0 > fprintf (file, "%g", f)) is_w= false;
00293   return *this;
00294 }
00295 
00296 tm_ostream&
00297 tm_ostream::operator << (double d) {
00298   if (is_buf) {
00299     char _buf[64];
00300     sprintf (_buf, "%g", d);
00301     *buf << _buf;
00302   }
00303   else 
00304   if (file && is_w)
00305     if (0 > fprintf (file, "%g", d)) is_w= false;
00306   return *this;
00307 }
00308 
00309 tm_ostream&
00310 tm_ostream::operator << (long double ld) {
00311   if (is_buf) {
00312     char _buf[128];
00313     sprintf (_buf, "%Lg", ld);
00314     *buf << _buf;
00315   }
00316   else if (file && is_w)
00317     if (0 > fprintf (file, "%Lg", ld)) is_w= false;
00318   return *this;
00319 }
00320 
00321 tm_ostream&
00322 tm_ostream::operator << (const char* s) {
00323   if (is_buf) *buf << s;
00324   else if (file && is_w) {
00325     if (0 <= fprintf (file, "%s", s)) {
00326       const char* c= s;
00327       while (*c != 0 && *c != '\n') ++c;
00328       if (*c == '\n') flush ();
00329     } else {
00330       is_w= false;
00331     }
00332   }    
00333   return *this;
00334 }
00335 
00336 bool
00337 tm_ostream::operator == (tm_ostream& out) {
00338   return (&out == this);
00339 }
00340