Panda3D
 All Classes Functions Variables Enumerations
textEncoder.h
1 // Filename: textEncoder.h
2 // Created by: drose (26Mar03)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef TEXTENCODER_H
16 #define TEXTENCODER_H
17 
18 #include "dtoolbase.h"
19 #include "unicodeLatinMap.h"
20 
21 #include <ctype.h>
22 
23 class StringDecoder;
24 
25 ////////////////////////////////////////////////////////////////////
26 // Class : TextEncoder
27 // Description : This class can be used to convert text between
28 // multiple representations, e.g. utf-8 to Unicode. You
29 // may use it as a static class object, passing the
30 // encoding each time, or you may create an instance and
31 // use that object, which will record the current
32 // encoding and retain the current string.
33 //
34 // This class is also a base class of TextNode, which
35 // inherits this functionality.
36 ////////////////////////////////////////////////////////////////////
37 class EXPCL_DTOOL TextEncoder {
38 PUBLISHED:
39  enum Encoding {
40  E_iso8859,
41  E_utf8,
42  E_unicode
43  };
44 
45  INLINE TextEncoder();
46  INLINE TextEncoder(const TextEncoder &copy);
47 
48  INLINE void set_encoding(Encoding encoding);
49  INLINE Encoding get_encoding() const;
50 
51  INLINE static void set_default_encoding(Encoding encoding);
52  INLINE static Encoding get_default_encoding();
53 
54  INLINE void set_text(const string &text);
55  INLINE void set_text(const string &text, Encoding encoding);
56  INLINE void clear_text();
57  INLINE bool has_text() const;
58 
59  void make_upper();
60  void make_lower();
61 
62  INLINE string get_text() const;
63  INLINE string get_text(Encoding encoding) const;
64  INLINE void append_text(const string &text);
65  INLINE void append_unicode_char(int character);
66  INLINE int get_num_chars() const;
67  INLINE int get_unicode_char(int index) const;
68  INLINE void set_unicode_char(int index, int character);
69  INLINE string get_encoded_char(int index) const;
70  INLINE string get_encoded_char(int index, Encoding encoding) const;
71  INLINE string get_text_as_ascii() const;
72 
73  INLINE static string reencode_text(const string &text, Encoding from, Encoding to);
74 
75  INLINE static bool unicode_isalpha(int character);
76  INLINE static bool unicode_isdigit(int character);
77  INLINE static bool unicode_ispunct(int character);
78  INLINE static bool unicode_islower(int character);
79  INLINE static bool unicode_isupper(int character);
80  INLINE static bool unicode_isspace(int character);
81  INLINE static int unicode_toupper(int character);
82  INLINE static int unicode_tolower(int character);
83 
84  INLINE static string upper(const string &source);
85  INLINE static string upper(const string &source, Encoding encoding);
86  INLINE static string lower(const string &source);
87  INLINE static string lower(const string &source, Encoding encoding);
88 
89  // Direct support for wide-character strings. Now publishable with
90  // the new wstring support in interrogate.
91  INLINE void set_wtext(const wstring &wtext);
92  INLINE const wstring &get_wtext() const;
93  INLINE void append_wtext(const wstring &text);
94  wstring get_wtext_as_ascii() const;
95  bool is_wtext() const;
96 
97  static string encode_wchar(wchar_t ch, Encoding encoding);
98  INLINE string encode_wtext(const wstring &wtext) const;
99  static string encode_wtext(const wstring &wtext, Encoding encoding);
100  INLINE wstring decode_text(const string &text) const;
101  static wstring decode_text(const string &text, Encoding encoding);
102 
103 private:
104  enum Flags {
105  F_got_text = 0x0001,
106  F_got_wtext = 0x0002,
107  };
108  static wstring decode_text_impl(StringDecoder &decoder);
109 
110  int _flags;
111  Encoding _encoding;
112  string _text;
113  wstring _wtext;
114 
115  static Encoding _default_encoding;
116 };
117 
118 EXPCL_DTOOL ostream &
119 operator << (ostream &out, TextEncoder::Encoding encoding);
120 EXPCL_DTOOL istream &
121 operator >> (istream &in, TextEncoder::Encoding &encoding);
122 
123 // We'll define the output operator for wstring here, too. Presumably
124 // this will not be automatically defined by any system libraries.
125 
126 // This function is declared inline to minimize the risk of link
127 // conflicts should another third-party module also define the same
128 // output operator.
129 INLINE EXPCL_DTOOL ostream &
130 operator << (ostream &out, const wstring &str);
131 
132 #include "textEncoder.I"
133 
134 #endif
This class can be used to convert text between multiple representations, e.g.
Definition: textEncoder.h:37
The base class to a family of classes that decode various kinds of encoded byte streams.
Definition: stringDecoder.h:28