3 /// iconv wrapper class, and pluggable interface for records
7 Copyright (C) 2008-2009, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #ifndef __BARRY_ICONV_H__
23 #define __BARRY_ICONV_H__
37 /// Wrapper class for a two-way iconv_t handle pair. Automatically
38 /// handles closing in the destructor.
40 class BXEXPORT IConvHandle
42 friend class IConverter
;
47 // private constructor, used only by IConverter
48 IConvHandle(const char *fromcode
, const char *tocode
);
51 // custom conversions from any to IConverter's 'tocode'
52 IConvHandle(const char *fromcode
, const IConverter
&ic
);
53 // custom conversions from IConverter's 'tocode' to any
54 IConvHandle(const IConverter
&ic
, const char *tocode
);
61 /// Main charset conversion class, primarily focused on converting
62 /// between the Blackberry charset and an application-specified one.
63 /// Additional conversions are possible through custom IConvHandle,
64 /// but the goal of this class design is to deal with _one_
65 /// application defined charset, and provide a means to convert
66 /// to/from that charset to/from any other charset needed by
69 /// By default, this class assumes the Blackberry's charset is
70 /// WINDOWS-1252, but some data, such as SMS message bodies, can have
71 /// custom charsets as specified by the records. To convert from
72 /// such a custom charset, use:
74 /// // application sets up IConverter
75 /// IConverter ic("UTF-8");
77 /// // somewhere in the library, needing to convert
78 /// // from UCS2 to whatever the application selected
79 /// IConvHandle ucs2("UCS2", ic);
80 /// application_string = ic.Convert(ucs2, ucs2_string_data);
82 /// // and to convert back...
83 /// IConvHandle ucs2_reverse(ic, "UCS2");
84 /// ucs2_string = ic.Convert(ucs2_reverse, application_string_data);
86 class BXEXPORT IConverter
88 friend class IConvHandle
;
94 // internal buffer for fast conversions
95 mutable Data m_buffer
;
97 bool m_throw_on_conv_err
;
100 std::string
Convert(iconv_t cd
, const std::string
&str
) const;
103 /// Always throws ErrnoError if unable to open iconv.
104 /// If throw_on_conv_err is true, then string conversion operations
105 /// that fail will also throw ErrnoError.
106 explicit IConverter(const char *tocode
= "UTF-8", bool throw_on_conv_err
= false);
109 std::string
FromBB(const std::string
&str
) const;
110 std::string
ToBB(const std::string
&str
) const;
112 // Custom override functions, meant for converting between
113 // non-BLACKBERRY_CHARSET charsets and the tocode set by the
114 // IConverter constructor
115 std::string
Convert(const IConvHandle
&custom
, const std::string
&str
) const;