Aegis  4.25.D505
/home/archives/aegis/branch.4/branch.25/delta28933.505/libaegis/output.h
Go to the documentation of this file.
00001 //
00002 // aegis - project change supervisor
00003 // Copyright (C) 1999, 2002-2006, 2008, 2011, 2012 Peter Miller
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 3 of the License, or (at
00008 // your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program. If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 
00019 #ifndef LIBAEGIS_OUTPUT_H
00020 #define LIBAEGIS_OUTPUT_H
00021 
00022 #include <common/ac/stdarg.h>
00023 #include <common/ac/stddef.h>
00024 #include <common/nstring.h>
00025 #include <common/ac/shared_ptr.h>
00026 
00027 #include <libaegis/functor/stack.h>
00028 
00029 struct string_ty; // forward
00030 
00031 
00037 class output
00038 {
00039 public:
00044     typedef aegis_shared_ptr<output> pointer;
00045 
00046     typedef void (*delete_callback_ty)(output *, void *);
00047 
00051     virtual ~output();
00052 
00053 protected:
00058     output();
00059 
00060 public:
00064     virtual nstring filename(void) const = 0;
00065 
00070     long ftell(void) const;
00071 
00075     void write(const void *data, size_t nbytes);
00076 
00081     void flush(void);
00082 
00087     virtual int page_width(void) const;
00088 
00093     virtual int page_length(void) const;
00094 
00099     void end_of_line(void);
00100 
00105     virtual nstring type_name(void) const = 0;
00106 
00111     void
00112     fputc(char c)
00113     {
00114         if (buffer_position < buffer_end)
00115             *buffer_position++ = c;
00116         else
00117             overflow(c);
00118     }
00119 
00127     void fputs(const char *str);
00128 
00140     void fputs_xml(const char *str, bool paragraphs = false);
00141 
00148     void fputs(string_ty *str);
00149 
00161     void fputs_xml(string_ty *str, bool paragraphs = false);
00162 
00169     void fputs(const nstring &str);
00170 
00182     void fputs_xml(const nstring &str, bool paragraphs = false);
00183 
00194     void fprintf(const char *fmt, ...)                        ATTR_PRINTF(2, 3);
00195 
00203     void vfprintf(const char *fmt, va_list)                   ATTR_PRINTF(2, 0);
00204 
00209     void register_delete_callback(functor::pointer fp);
00210 
00215     void unregister_delete_callback(functor::pointer fp);
00216 
00217 private:
00222     virtual long ftell_inner(void) const = 0;
00223 
00228     virtual void write_inner(const void *data, size_t length) = 0;
00229 
00235     virtual void end_of_line_inner(void) = 0;
00236 
00242     virtual void flush_inner(void);
00243 
00249     void overflow(char c);
00250 
00255     functor_stack callback;
00256 
00262     unsigned char *buffer;
00263 
00268     size_t buffer_size;
00269 
00274     unsigned char *buffer_position;
00275 
00280     unsigned char *buffer_end;
00281 
00285     output(const output &);
00286 
00290     output &operator=(const output &);
00291 };
00292 
00293 
00294 inline output::pointer &
00295 operator<<(output::pointer &os, char c)
00296 {
00297     os->fputc(c);
00298     return os;
00299 }
00300 
00301 
00302 inline output::pointer &
00303 operator<<(output::pointer &os, const char *s)
00304 {
00305     os->fputs(s);
00306     return os;
00307 }
00308 
00309 
00310 inline output::pointer &
00311 operator<<(output::pointer &os, const nstring &s)
00312 {
00313     os->fputs(s);
00314     return os;
00315 }
00316 
00317 
00318 class input; // forward
00319 
00320 output::pointer &operator<<(output::pointer &os, input &is);
00321 
00322 #endif // LIBAEGIS_OUTPUT_H
00323 // vim: set ts=8 sw=4 et :