lib: fixed iterator start bug in DeviceBuilder, which skipped first DB added
[barry.git] / src / iconv.h
blobfa79ecedb6dd75ed542569042b07f4db4da9c91f
1 ///
2 /// \file iconv.h
3 /// iconv wrapper class, and pluggable interface for records
4 ///
6 /*
7 Copyright (C) 2008-2010, 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__
25 #include "dll.h"
26 #include "data.h"
27 #include <iconv.h>
28 #include <string>
30 namespace Barry {
32 class IConverter;
35 // IConvHandle class
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;
44 iconv_t m_handle;
46 private:
47 // private constructor, used only by IConverter
48 IConvHandle(const char *fromcode, const char *tocode);
50 public:
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);
55 ~IConvHandle();
59 // IConverter
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
67 /// the Blackberry.
68 ///
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:
73 ///
74 /// // application sets up IConverter
75 /// IConverter ic("UTF-8");
76 ///
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);
81 ///
82 /// // and to convert back...
83 /// IConvHandle ucs2_reverse(ic, "UCS2");
84 /// ucs2_string = ic.Convert(ucs2_reverse, application_string_data);
85 ///
86 class BXEXPORT IConverter
88 friend class IConvHandle;
90 IConvHandle m_from;
91 IConvHandle m_to;
92 std::string m_tocode;
94 // internal buffer for fast conversions
95 mutable Data m_buffer;
97 bool m_throw_on_conv_err;
99 private:
100 std::string Convert(iconv_t cd, const std::string &str) const;
102 public:
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);
107 ~IConverter();
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;
118 } // namespace Barry
120 #endif