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
36 gcc_malloc gcc_nonnull_all
38 ConvertUTF8ToWide(const char *p
);
40 gcc_malloc gcc_nonnull_all
42 ConvertACPToWide(const char *p
);
44 gcc_malloc gcc_nonnull_all
46 ConvertWideToUTF8(const TCHAR
*p
);
48 gcc_malloc gcc_nonnull_all
50 ConvertWideToACP(const TCHAR
*p
);
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
59 class UTF8ToWideConverter
{
68 UTF8ToWideConverter(const char *_value
)
69 :value(ConvertUTF8ToWide(_value
)) {}
71 ~UTF8ToWideConverter() {
75 UTF8ToWideConverter(const char *_value
):value(_value
) {
76 assert(_value
!= nullptr);
80 UTF8ToWideConverter(const UTF8ToWideConverter
&other
) = delete;
81 UTF8ToWideConverter
&operator=(const UTF8ToWideConverter
&other
) = delete;
84 bool IsValid() const {
86 return value
!= nullptr;
88 assert(value
!= nullptr);
90 return ValidateUTF8(value
);
94 operator const TCHAR
*() const {
95 assert(value
!= nullptr);
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
{
114 WideToUTF8Converter(const TCHAR
*_value
)
115 :value(ConvertWideToUTF8(_value
)) {}
117 ~WideToUTF8Converter() {
121 WideToUTF8Converter(const char *_value
):value(_value
) {
122 assert(_value
!= nullptr);
126 WideToUTF8Converter(const WideToUTF8Converter
&other
) = delete;
127 WideToUTF8Converter
&operator=(const WideToUTF8Converter
&other
) = delete;
130 bool IsValid() const {
132 return value
!= nullptr;
134 assert(value
!= nullptr);
140 operator const char *() const {
141 assert(value
!= nullptr);
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
{
161 ACPToWideConverter(const char *_value
)
162 :value(ConvertACPToWide(_value
)) {}
164 ~ACPToWideConverter() {
168 ACPToWideConverter(const char *_value
):value(_value
) {
169 assert(_value
!= nullptr);
173 ACPToWideConverter(const ACPToWideConverter
&other
) = delete;
174 ACPToWideConverter
&operator=(const ACPToWideConverter
&other
) = delete;
177 bool IsValid() const {
179 return value
!= nullptr;
181 assert(value
!= nullptr);
187 operator const TCHAR
*() const {
188 assert(value
!= nullptr);
194 * Returns a newly allocated string. It invalidates this object.
198 assert(value
!= nullptr);
201 TCHAR
*result
= value
;
205 return strdup(value
);
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
{
224 WideToACPConverter(const TCHAR
*_value
)
225 :value(ConvertWideToACP(_value
)) {}
227 ~WideToACPConverter() {
231 WideToACPConverter(const char *_value
):value(_value
) {
232 assert(_value
!= nullptr);
236 WideToACPConverter(const WideToACPConverter
&other
) = delete;
237 WideToACPConverter
&operator=(const WideToACPConverter
&other
) = delete;
240 bool IsValid() const {
242 return value
!= nullptr;
244 assert(value
!= nullptr);
250 operator const char *() const {
251 assert(value
!= nullptr);
257 * Returns a newly allocated string. It invalidates this object.
261 assert(value
!= nullptr);
264 char *result
= value
;
268 return strdup(value
);