Panda3D

textEncoder.h

00001 // Filename: textEncoder.h
00002 // Created by:  drose (26Mar03)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef TEXTENCODER_H
00016 #define TEXTENCODER_H
00017 
00018 #include "pandabase.h"
00019 #include "unicodeLatinMap.h"
00020 #include "configVariableEnum.h"
00021 #include "typedObject.h"
00022 
00023 class StringDecoder;
00024 
00025 ////////////////////////////////////////////////////////////////////
00026 //       Class : TextEncoder
00027 // Description : This class can be used to convert text between
00028 //               multiple representations, e.g. utf-8 to Unicode.  You
00029 //               may use it as a static class object, passing the
00030 //               encoding each time, or you may create an instance and
00031 //               use that object, which will record the current
00032 //               encoding and retain the current string.
00033 //
00034 //               This class is also a base class of TextNode, which
00035 //               inherits this functionality.
00036 ////////////////////////////////////////////////////////////////////
00037 class EXPCL_PANDAEXPRESS TextEncoder {
00038 PUBLISHED:
00039   enum Encoding {
00040     E_iso8859,
00041     E_utf8,
00042     E_unicode
00043   };
00044 
00045   INLINE TextEncoder();
00046   INLINE TextEncoder(const TextEncoder &copy);
00047 
00048   INLINE void set_encoding(Encoding encoding);
00049   INLINE Encoding get_encoding() const;
00050 
00051   INLINE static void set_default_encoding(Encoding encoding);
00052   INLINE static Encoding get_default_encoding();
00053 
00054   INLINE void set_text(const string &text);
00055   INLINE void set_text(const string &text, Encoding encoding);
00056   INLINE void clear_text();
00057   INLINE bool has_text() const;
00058 
00059   void make_upper();
00060   void make_lower();
00061 
00062   INLINE string get_text() const;
00063   INLINE string get_text(Encoding encoding) const;
00064   INLINE void append_text(const string &text);
00065   INLINE void append_unicode_char(int character);
00066   INLINE int get_num_chars() const;
00067   INLINE int get_unicode_char(int index) const;
00068   INLINE void set_unicode_char(int index, int character);
00069   INLINE string get_encoded_char(int index) const;
00070   INLINE string get_encoded_char(int index, Encoding encoding) const;
00071   INLINE string get_text_as_ascii() const;
00072 
00073   INLINE static string reencode_text(const string &text, Encoding from, Encoding to);
00074 
00075   INLINE static bool unicode_isalpha(int character);
00076   INLINE static bool unicode_isdigit(int character);
00077   INLINE static bool unicode_ispunct(int character);
00078   INLINE static bool unicode_islower(int character);
00079   INLINE static bool unicode_isupper(int character);
00080   INLINE static bool unicode_isspace(int character);
00081   INLINE static int unicode_toupper(int character);
00082   INLINE static int unicode_tolower(int character);
00083 
00084   INLINE static string upper(const string &source);
00085   INLINE static string upper(const string &source, Encoding encoding);
00086   INLINE static string lower(const string &source);
00087   INLINE static string lower(const string &source, Encoding encoding);
00088 
00089   // Direct support for wide-character strings.  Now publishable with
00090   // the new wstring support in interrogate.
00091   INLINE void set_wtext(const wstring &wtext);
00092   INLINE const wstring &get_wtext() const;
00093   INLINE void append_wtext(const wstring &text);
00094   wstring get_wtext_as_ascii() const;
00095   bool is_wtext() const;
00096 
00097   static string encode_wchar(wchar_t ch, Encoding encoding);
00098   INLINE string encode_wtext(const wstring &wtext) const;
00099   static string encode_wtext(const wstring &wtext, Encoding encoding);
00100   INLINE wstring decode_text(const string &text) const;
00101   static wstring decode_text(const string &text, Encoding encoding);
00102 
00103 private:
00104   enum Flags {
00105     F_got_text         =  0x0001,
00106     F_got_wtext        =  0x0002,
00107   };
00108   static wstring decode_text_impl(StringDecoder &decoder);
00109 
00110   int _flags;
00111   Encoding _encoding;
00112   string _text;
00113   wstring _wtext;
00114 
00115   static ConfigVariableEnum<Encoding> _default_encoding;
00116 
00117 public:
00118   static TypeHandle get_class_type() {
00119     return _type_handle;
00120   }
00121   static void init_type() {
00122     register_type(_type_handle, "TextEncoder");
00123   }
00124 
00125 private:
00126   static TypeHandle _type_handle;
00127 };
00128 
00129 EXPCL_PANDAEXPRESS ostream &
00130 operator << (ostream &out, TextEncoder::Encoding encoding);
00131 EXPCL_PANDAEXPRESS istream &
00132 operator >> (istream &in, TextEncoder::Encoding &encoding);
00133 
00134 #include "textEncoder.I"
00135 
00136 #endif
 All Classes Functions Variables Enumerations