4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #ifndef XCSOAR_IO_TEXT_WRITER_HPP
25 #define XCSOAR_IO_TEXT_WRITER_HPP
27 #include "FileHandle.hpp"
29 #include "Util/ReusableArray.hpp"
41 * Writer for an UTF-8 text file with native line endings. All "const
42 * char *" arguments must be either valid UTF-8 or 7 bit ASCII.
44 * The end-of-line marker is platform specific, and must not be passed
45 * to this class. Use the methods NewLine(), WriteLine() or
46 * FormatLine() to finish a line.
53 ReusableArray
<TCHAR
> format_buffer
;
54 ReusableArray
<char> convert_buffer
;
59 * Creates a new text file. Truncates the old file if it exists,
60 * unless the parameter "append" is true.
62 TextWriter(const char *path
, bool append
=false);
65 TextWriter(const TCHAR
*path
, bool append
=false);
68 TextWriter(TextWriter
&&other
)
69 :file(std::move(other
.file
))
71 , format_buffer(std::move(other
.format_buffer
)),
72 convert_buffer(std::move(other
.convert_buffer
))
76 TextWriter
&operator=(TextWriter
&&other
) {
77 file
= std::move(other
.file
);
79 format_buffer
= std::move(other
.format_buffer
);
80 convert_buffer
= std::move(other
.convert_buffer
);
86 * Returns false if opening the file has failed. This must be
87 * checked before calling any other method.
94 * Ensure that all pending writes have been passed to the operating
95 * system. This does not guarantee that these have been written to
96 * the physical device; they might still reside in the filesystem
100 assert(file
.IsOpen());
105 * Write one character.
107 void Write(char ch
) {
108 assert(file
.IsOpen());
116 * Finish the current line.
119 assert(file
.IsOpen());
122 file
.Write((int)'\r');
124 file
.Write((int)'\n');
129 * Write a chunk of text to the file.
131 bool Write(const char *s
, size_t length
) {
132 assert(file
.IsOpen());
133 assert(memchr(s
, '\r', length
) == NULL
);
134 assert(memchr(s
, '\n', length
) == NULL
);
136 return file
.Write(s
, sizeof(*s
), length
) == length
;
140 * Write a string to the file.
142 bool Write(const char *s
) {
143 assert(file
.IsOpen());
144 assert(strchr(s
, '\r') == NULL
);
145 assert(strchr(s
, '\n') == NULL
);
147 return file
.Write(s
) >= 0;
151 * Write a string to the file, and finish the current line.
153 bool WriteLine(const char *s
) {
154 return Write(s
) && NewLine();
158 bool Write(const TCHAR
*s
, size_t length
);
160 bool Write(const TCHAR
*s
);
162 bool WriteLine(const TCHAR
*s
) {
163 return Write(s
) && NewLine();
167 template<typename
... Args
>
168 void Format(const char *fmt
, Args
&&... args
) {
169 assert(file
.IsOpen());
170 assert(strchr(fmt
, '\r') == NULL
);
171 assert(strchr(fmt
, '\n') == NULL
);
173 file
.WriteFormatted(fmt
, args
...);
176 template<typename
... Args
>
177 void FormatLine(const char *fmt
, Args
&&... args
) {
178 assert(file
.IsOpen());
179 assert(strchr(fmt
, '\r') == NULL
);
180 assert(strchr(fmt
, '\n') == NULL
);
182 file
.WriteFormatted(fmt
, args
...);
187 bool Format(const TCHAR
*s
, ...);
189 template<typename
... Args
>
190 bool FormatLine(const TCHAR
*fmt
, Args
&&... args
) {
191 return Format(fmt
, args
...) && NewLine();