android/GlueIOIOPort: fix spurious errors after IOIO baud rate change
[xcsoar.git] / src / Util / ConvertString.hpp
blob3edf920a50668ab94a90a2280e6337e27de70f9f
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_CONVERT_STRING_HPP
25 #define XCSOAR_CONVERT_STRING_HPP
27 #include "UTF8.hpp"
28 #include "Compiler.h"
30 #include <assert.h>
31 #include <string.h>
32 #include <tchar.h>
34 #ifdef _UNICODE
36 gcc_malloc gcc_nonnull_all
37 TCHAR *
38 ConvertUTF8ToWide(const char *p);
40 gcc_malloc gcc_nonnull_all
41 TCHAR *
42 ConvertACPToWide(const char *p);
44 gcc_malloc gcc_nonnull_all
45 char *
46 ConvertWideToUTF8(const TCHAR *p);
48 gcc_malloc gcc_nonnull_all
49 char *
50 ConvertWideToACP(const TCHAR *p);
52 #endif
54 /**
55 * Convert a UTF-8 string to a TCHAR string. The source buffer passed
56 * to the constructor must be valid as long as this object is being
57 * used.
59 class UTF8ToWideConverter {
60 #ifdef _UNICODE
61 TCHAR *value;
62 #else
63 const char *value;
64 #endif
66 public:
67 #ifdef _UNICODE
68 UTF8ToWideConverter(const char *_value)
69 :value(ConvertUTF8ToWide(_value)) {}
71 ~UTF8ToWideConverter() {
72 delete[] value;
74 #else
75 UTF8ToWideConverter(const char *_value):value(_value) {
76 assert(_value != nullptr);
78 #endif
80 UTF8ToWideConverter(const UTF8ToWideConverter &other) = delete;
81 UTF8ToWideConverter &operator=(const UTF8ToWideConverter &other) = delete;
83 gcc_pure
84 bool IsValid() const {
85 #ifdef _UNICODE
86 return value != nullptr;
87 #else
88 assert(value != nullptr);
90 return ValidateUTF8(value);
91 #endif
94 operator const TCHAR *() const {
95 assert(value != nullptr);
97 return value;
102 * Convert a TCHAR string to UTF-8. The source buffer passed to the
103 * constructor must be valid as long as this object is being used.
105 class WideToUTF8Converter {
106 #ifdef _UNICODE
107 char *value;
108 #else
109 const char *value;
110 #endif
112 public:
113 #ifdef _UNICODE
114 WideToUTF8Converter(const TCHAR *_value)
115 :value(ConvertWideToUTF8(_value)) {}
117 ~WideToUTF8Converter() {
118 delete[] value;
120 #else
121 WideToUTF8Converter(const char *_value):value(_value) {
122 assert(_value != nullptr);
124 #endif
126 WideToUTF8Converter(const WideToUTF8Converter &other) = delete;
127 WideToUTF8Converter &operator=(const WideToUTF8Converter &other) = delete;
129 gcc_pure
130 bool IsValid() const {
131 #ifdef _UNICODE
132 return value != nullptr;
133 #else
134 assert(value != nullptr);
136 return true;
137 #endif
140 operator const char *() const {
141 assert(value != nullptr);
143 return value;
148 * Convert an ACP string (Windows ANSI code page) to a TCHAR string.
149 * The source buffer passed to the constructor must be valid as long
150 * as this object is being used.
152 class ACPToWideConverter {
153 #ifdef _UNICODE
154 TCHAR *value;
155 #else
156 const char *value;
157 #endif
159 public:
160 #ifdef _UNICODE
161 ACPToWideConverter(const char *_value)
162 :value(ConvertACPToWide(_value)) {}
164 ~ACPToWideConverter() {
165 delete[] value;
167 #else
168 ACPToWideConverter(const char *_value):value(_value) {
169 assert(_value != nullptr);
171 #endif
173 ACPToWideConverter(const ACPToWideConverter &other) = delete;
174 ACPToWideConverter &operator=(const ACPToWideConverter &other) = delete;
176 gcc_pure
177 bool IsValid() const {
178 #ifdef _UNICODE
179 return value != nullptr;
180 #else
181 assert(value != nullptr);
183 return true;
184 #endif
187 operator const TCHAR *() const {
188 assert(value != nullptr);
190 return value;
194 * Returns a newly allocated string. It invalidates this object.
196 gcc_malloc
197 TCHAR *StealDup() {
198 assert(value != nullptr);
200 #ifdef _UNICODE
201 TCHAR *result = value;
202 value = nullptr;
203 return result;
204 #else
205 return strdup(value);
206 #endif
211 * Convert a TCHAR string to ACP (Windows ANSI code page). The source
212 * buffer passed to the constructor must be valid as long as this
213 * object is being used.
215 class WideToACPConverter {
216 #ifdef _UNICODE
217 char *value;
218 #else
219 const char *value;
220 #endif
222 public:
223 #ifdef _UNICODE
224 WideToACPConverter(const TCHAR *_value)
225 :value(ConvertWideToACP(_value)) {}
227 ~WideToACPConverter() {
228 delete[] value;
230 #else
231 WideToACPConverter(const char *_value):value(_value) {
232 assert(_value != nullptr);
234 #endif
236 WideToACPConverter(const WideToACPConverter &other) = delete;
237 WideToACPConverter &operator=(const WideToACPConverter &other) = delete;
239 gcc_pure
240 bool IsValid() const {
241 #ifdef _UNICODE
242 return value != nullptr;
243 #else
244 assert(value != nullptr);
246 return true;
247 #endif
250 operator const char *() const {
251 assert(value != nullptr);
253 return value;
257 * Returns a newly allocated string. It invalidates this object.
259 gcc_malloc
260 char *StealDup() {
261 assert(value != nullptr);
263 #ifdef _UNICODE
264 char *result = value;
265 value = nullptr;
266 return result;
267 #else
268 return strdup(value);
269 #endif
273 #endif