1 //
2 // aegis - project change supervisor
3 // Copyright (C) 1999, 2001, 2002, 2005, 2006, 2008, 2011, 2012 Peter Miller
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or (at
8 // your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 //
18 
19 #ifndef LIBAEGIS_OUTPUT_FILE_H
20 #define LIBAEGIS_OUTPUT_FILE_H
21 
22 #include <common/nstring.h>
23 #include <libaegis/output.h>
24 
25 struct nstring; // forward
26 
27 /**
28   * The output_file class is used to represent the state of an output
29   * stream to a regular file.
30   */
31 class output_file:
32     public output
33 {
34 public:
35     /**
36       * The destructor.
37       */
38     virtual ~output_file();
39 
40 private:
41     /**
42       * The constructor.  It is private on purpose, use one of the open
43       * class methods instead.
44       *
45       * \param file_name
46       *     The name of the file to be opened for output.
47       *     (Make absolutely sure it is not NULL or empty.)
48       * \param binary
49       *     Whether the file is binary (true) or text (false).
50       */
51     output_file(const nstring &file_name, bool binary = false);
52 
53 public:
54     /**
55       * The open class method is used to open an output file.
56       *
57       * \param fn
58       *     The path name of the file to be opened.  If the poijnter is NULL
59       *     or the string is empty, the standard output is used.
60       * \param binary
61       *     Wherther or not the file is binary.  Defaults to false (i.e. text).
62       */
63     static output::pointer open(const nstring &fn, bool binary = false);
64 
65     /**
66       * The text_open class method is used to open a text output file.
67       *
68       * \param fn
69       *     The path name of the file to be opened.  If the poijnter is NULL
70       *     or the string is empty, the standard output is used.
71       */
72     static inline output::pointer
text_open(string_ty * fn)73     text_open(string_ty *fn)
74     {
75         return open(nstring(fn), false);
76     }
77 
78     /**
79       * The text_open class method is used to open a text output file.
80       *
81       * \param fn
82       *     The path name of the file to be opened.  If the poijnter is NULL
83       *     or the string is empty, the standard output is used.
84       */
85     static inline output::pointer
text_open(const nstring & fn)86     text_open(const nstring &fn)
87     {
88         return open(fn, false);
89     }
90 
91     /**
92       * The binary_open class method is used to open a binary output file.
93       *
94       * \param fn
95       *     The path name of the file to be opened.  If the poijnter is NULL
96       *     or the string is empty, the standard output is used.
97       */
98     inline static output::pointer
binary_open(string_ty * fn)99     binary_open(string_ty *fn)
100     {
101         return open(nstring(fn), true);
102     }
103 
104     /**
105       * The binary_open class method is used to open a binary output file.
106       *
107       * \param fn
108       *     The path name of the file to be opened.  If the poijnter is NULL
109       *     or the string is empty, the standard output is used.
110       */
111     inline static output::pointer
binary_open(const nstring & fn)112     binary_open(const nstring &fn)
113     {
114         return open(fn, true);
115     }
116 
117     /**
118       * The compressed_text_open class method is used to openb a file,
119       * for example a report output, while taking into the file's
120       * extension to determine whether or not to also compress the
121       * output.
122       *
123       * If the file name is ampty or "-" the standard output will be used.
124       *
125       * If the fil ename ends with ".gz" (case insensitive) the output
126       * will be gzipped.  If the file ends with ".bz" or ".bz2" the
127       * output will be bzip2ed.
128       *
129       * @param filename
130       *     The name of the file
131       * @returns
132       *     pointer to a new output instance
133       */
134     static output::pointer compressed_text_open(const nstring &filename);
135 
136 protected:
137     // See base class for documentation.
138     nstring filename(void) const;
139 
140     // See base class for documentation.
141     nstring type_name(void) const;
142 
143     // See base class for documentation.
144     long ftell_inner(void) const;
145 
146     // See base class for documentation.
147     void write_inner(const void *data, size_t length);
148 
149     // See base class for documentation.
150     void end_of_line_inner(void);
151 
152     // See base class for documentation.
153     int page_width(void) const;
154 
155     // See base class for documentation.
156     int page_length(void) const;
157 
158 private:
159     /**
160       * The file_name instance variable is used to remember the name of
161       * the file opened for writing.
162       */
163     nstring file_name;
164 
165     /**
166       * The fd instance variable is used to remember the number of the
167       * file descriptor opened for writing.
168       */
169     int fd;
170 
171     /**
172       * The bol instance variable is used to remember whether or not we
173       * are at the start of a line.
174       */
175     bool bol;
176 
177     /**
178       * The pos instance variable is used to remember the current
179       * position in the output file.
180       */
181     size_t pos;
182 
183     /**
184       * The default constructor.  Do not use.
185       */
186     output_file();
187 
188     /**
189       * The copy constructor.  Do not use.
190       */
191     output_file(const output_file &);
192 
193     /**
194       * The assignment operator.  Do not use.
195       */
196     output_file &operator=(const output_file &);
197 };
198 
199 #endif // LIBAEGIS_OUTPUT_FILE_H
200 // vim: set ts=8 sw=4 et :
201