From 1ef9a375d2bf694350c1d6f42bc3b999552a60db Mon Sep 17 00:00:00 2001 From: vdhoeven Date: Sat, 9 Jan 2010 13:19:05 +0000 Subject: [PATCH] Add missing files git-svn-id: svn://svn.savannah.gnu.org/texmacs/trunk@2909 64cb5145-927a-446d-8aed-2fb7b4773692 --- src/src/System/Files/tm_ostream.cpp | 218 ++++++++++++++++++++++++++++++++++++ src/src/System/Files/tm_ostream.hpp | 65 +++++++++++ 2 files changed, 283 insertions(+) create mode 100644 src/src/System/Files/tm_ostream.cpp create mode 100644 src/src/System/Files/tm_ostream.hpp diff --git a/src/src/System/Files/tm_ostream.cpp b/src/src/System/Files/tm_ostream.cpp new file mode 100644 index 00000000..7646ecbc --- /dev/null +++ b/src/src/System/Files/tm_ostream.cpp @@ -0,0 +1,218 @@ + +/****************************************************************************** +* MODULE : tm_ostream.cpp +* DESCRIPTION: Output stream class +* COPYRIGHT : (C) 2009 David MICHEL +******************************************************************************* +* This software falls under the GNU general public license version 3 or later. +* It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE +* in the root directory or . +******************************************************************************/ + +#include "tm_ostream.hpp" + +tm_ostream +tm_ostream::private_cout (stdout); + +tm_ostream +tm_ostream::private_cerr (stderr); + +tm_ostream::tm_ostream (const tm_ostream& out) { // private + file= out.file; + is_w= out.is_w; + is_mine= false; +} + +tm_ostream& +tm_ostream::operator= (const tm_ostream& out) { // private + if (this != &out) { + file= out.file; + is_w= out.is_w; + is_mine= false; + } else { + return *this; + } +} + +tm_ostream& +tm_ostream::cout= private_cout; + +tm_ostream& +tm_ostream::cerr= private_cerr; + +tm_ostream::tm_ostream () : file (0), is_w (false), is_mine (false) {} + +tm_ostream::tm_ostream (char* fn) : file (0), is_w (false), is_mine (false) { + open (fn); +} + +tm_ostream::tm_ostream (FILE* f) : file (0), is_w (false), is_mine (false) { + open (f); +} + +tm_ostream::~tm_ostream () { + if (file && is_mine) fclose (file); +} + +bool +tm_ostream::open () { + if (file && is_mine) fclose (file); + file= 0; + is_w= true; + is_mine= true; +} +/* +bool +tm_ostream::open (url u) { + if (file) fclose (file); + char* _u= as_charp (concretize (u)); + file= fopen (_u, "w"); + tm_delete_array (_u); + if (file) is_w= true; + else is_w= false; + return is_w; +} +*/ +bool +tm_ostream::open (char* fn) { + if (file && is_mine) fclose (file); + file= fopen (fn, "w"); + if (file) { + is_w= true; + is_mine= true; + } else { + is_w= false; + is_mine= false; + } + return is_w; +} + +bool +tm_ostream::open (FILE* f) { + if (file && is_mine) fclose (file); + file= f; + if (file) is_w= true; + else is_w= false; + is_mine= false; + return is_w; +} + +bool +tm_ostream::is_writable () const { + return is_w; +} + +void +tm_ostream::flush () { + if (file && is_w) fflush (file); +} + +void +tm_ostream::close () { + if (file && is_mine) fclose (file); + file= 0; + is_w= false; + is_mine= false; +} + +tm_ostream& +tm_ostream::operator << (bool b) { + if (file && is_w) { + if (b) { + if (0 > fprintf (file, "%s", "true")) is_w= false; + } else { + if (0 > fprintf (file, "%s", "false")) is_w= false; + } + } + return *this; +} + +tm_ostream& +tm_ostream::operator << (char c) { + if (file && is_w) + if (0 > fprintf (file, "%c", c)) is_w= false; + return *this; +} + +tm_ostream& +tm_ostream::operator << (short sh) { + if (file && is_w) + if (0 > fprintf (file, "%hd", sh)) is_w= false; + return *this; +} + +tm_ostream& +tm_ostream::operator << (unsigned short ush) { + if (file && is_w) + if (0 > fprintf (file, "%hu", ush)) is_w= false; + return *this; +} + +tm_ostream& +tm_ostream::operator << (int i) { + if (file && is_w) + if (0 > fprintf (file, "%d", i)) is_w= false; + return *this; +} + +tm_ostream& +tm_ostream::operator << (unsigned int ui) { + if (file && is_w) + if (0 > fprintf (file, "%u", ui)) is_w= false; + return *this; +} + +tm_ostream& +tm_ostream::operator << (long l) { + if (file && is_w) + if (0 > fprintf (file, "%ld", l)) is_w= false; + return *this; +} + +tm_ostream& +tm_ostream::operator << (unsigned long ul) { + if (file && is_w) + if (0 > fprintf (file, "%lu", ul)) is_w= false; + return *this; +} + +tm_ostream& +tm_ostream::operator << (float f) { + if (file && is_w) + if (0 > fprintf (file, "%g", f)) is_w= false; + return *this; +} + +tm_ostream& +tm_ostream::operator << (double d) { + if (file && is_w) + if (0 > fprintf (file, "%g", d)) is_w= false; + return *this; +} + +tm_ostream& +tm_ostream::operator << (long double ld) { + if (file && is_w) + if (0 > fprintf (file, "%Lg", ld)) is_w= false; + return *this; +} + +tm_ostream& +tm_ostream::operator << (const char* s) { + if (file && is_w) { + if (0 <= fprintf (file, "%s", s)) { + const char* c= s; + while (*c != 0 && *c != '\n') ++c; + if (*c == '\n') flush (); + } else { + is_w= false; + } + } + return *this; +} + +bool +tm_ostream::operator == (tm_ostream& out) { + return (&out == this); +} + diff --git a/src/src/System/Files/tm_ostream.hpp b/src/src/System/Files/tm_ostream.hpp new file mode 100644 index 00000000..adff616f --- /dev/null +++ b/src/src/System/Files/tm_ostream.hpp @@ -0,0 +1,65 @@ + +/****************************************************************************** +* MODULE : tm_ostream.hpp +* DESCRIPTION: Output stream class +* COPYRIGHT : (C) 2009 David MICHEL +******************************************************************************* +* This software falls under the GNU general public license version 3 or later. +* It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE +* in the root directory or . +******************************************************************************/ + +#ifndef OUT_STREAM_HPP +#define OUT_STREAM_HPP + +//#include "url.hpp" +#include + +class tm_ostream { + FILE *file; + bool is_w; + bool is_mine; + + static tm_ostream private_cout; + static tm_ostream private_cerr; + + tm_ostream (const tm_ostream&); + tm_ostream& operator= (const tm_ostream&); + +public: + static tm_ostream& cout; + static tm_ostream& cerr; + + tm_ostream (); + tm_ostream (char*); + tm_ostream (FILE*); + ~tm_ostream (); + + bool open (); +// bool open (url); + bool open (char*); + bool open (FILE*); + bool is_writable () const; + void flush (); + void close (); + + tm_ostream& operator << (bool); + tm_ostream& operator << (char); + tm_ostream& operator << (short); + tm_ostream& operator << (unsigned short); + tm_ostream& operator << (int); + tm_ostream& operator << (unsigned int); + tm_ostream& operator << (long); + tm_ostream& operator << (unsigned long); + tm_ostream& operator << (float); + tm_ostream& operator << (double); + tm_ostream& operator << (long double); + tm_ostream& operator << (const char*); + + bool operator == (tm_ostream&); +}; + +extern tm_ostream& cout; +extern tm_ostream& cerr; + +#endif // defined OUT_STREAM_HPP -- 2.11.4.GIT