dlgConfiguration: remove polar panel
[xcsoar.git] / src / IO / TextWriter.hpp
blobf87e89b61f5aa584887bf5b59d0efbabfa58eae8
1 /*
2 Copyright_License {
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"
28 #include "Compiler.h"
29 #include "Util/ReusableArray.hpp"
31 #include <assert.h>
32 #include <string.h>
33 #include <stddef.h>
34 #include <stdio.h>
36 #ifdef _UNICODE
37 #include <tchar.h>
38 #endif
40 /**
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.
48 class TextWriter {
49 private:
50 FileHandle file;
52 #ifdef _UNICODE
53 ReusableArray<TCHAR> format_buffer;
54 ReusableArray<char> convert_buffer;
55 #endif
57 public:
58 /**
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);
64 #ifdef _UNICODE
65 TextWriter(const TCHAR *path, bool append=false);
66 #endif
68 TextWriter(TextWriter &&other)
69 :file(std::move(other.file))
70 #ifdef _UNICODE
71 , format_buffer(std::move(other.format_buffer)),
72 convert_buffer(std::move(other.convert_buffer))
73 #endif
76 TextWriter &operator=(TextWriter &&other) {
77 file = std::move(other.file);
78 #ifdef _UNICODE
79 format_buffer = std::move(other.format_buffer);
80 convert_buffer = std::move(other.convert_buffer);
81 #endif
82 return *this;
85 /**
86 * Returns false if opening the file has failed. This must be
87 * checked before calling any other method.
89 bool IsOpen() const {
90 return file.IsOpen();
93 /**
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
97 * cache.
99 bool Flush() {
100 assert(file.IsOpen());
101 return file.Flush();
105 * Write one character.
107 void Write(char ch) {
108 assert(file.IsOpen());
109 assert(ch != '\r');
110 assert(ch != '\n');
112 file.Write((int)ch);
116 * Finish the current line.
118 bool NewLine() {
119 assert(file.IsOpen());
121 #ifndef HAVE_POSIX
122 file.Write((int)'\r');
123 #endif
124 file.Write((int)'\n');
125 return true;
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();
157 #ifdef _UNICODE
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();
165 #endif
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...);
183 NewLine();
186 #ifdef _UNICODE
187 bool Format(const TCHAR *s, ...);
189 template<typename... Args>
190 bool FormatLine(const TCHAR *fmt, Args&&... args) {
191 return Format(fmt, args...) && NewLine();
193 #endif
196 #endif