Panda3D
 All Classes Functions Variables Enumerations
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 "dtoolbase.h"
00019 #include "unicodeLatinMap.h"
00020 
00021 class StringDecoder;
00022 
00023 ////////////////////////////////////////////////////////////////////
00024 //       Class : TextEncoder
00025 // Description : This class can be used to convert text between
00026 //               multiple representations, e.g. utf-8 to Unicode.  You
00027 //               may use it as a static class object, passing the
00028 //               encoding each time, or you may create an instance and
00029 //               use that object, which will record the current
00030 //               encoding and retain the current string.
00031 //
00032 //               This class is also a base class of TextNode, which
00033 //               inherits this functionality.
00034 ////////////////////////////////////////////////////////////////////
00035 class EXPCL_DTOOL TextEncoder {
00036 PUBLISHED:
00037   enum Encoding {
00038     E_iso8859,
00039     E_utf8,
00040     E_unicode
00041   };
00042 
00043   INLINE TextEncoder();
00044   INLINE TextEncoder(const TextEncoder &copy);
00045 
00046   INLINE void set_encoding(Encoding encoding);
00047   INLINE Encoding get_encoding() const;
00048 
00049   INLINE static void set_default_encoding(Encoding encoding);
00050   INLINE static Encoding get_default_encoding();
00051 
00052   INLINE void set_text(const string &text);
00053   INLINE void set_text(const string &text, Encoding encoding);
00054   INLINE void clear_text();
00055   INLINE bool has_text() const;
00056 
00057   void make_upper();
00058   void make_lower();
00059 
00060   INLINE string get_text() const;
00061   INLINE string get_text(Encoding encoding) const;
00062   INLINE void append_text(const string &text);
00063   INLINE void append_unicode_char(int character);
00064   INLINE int get_num_chars() const;
00065   INLINE int get_unicode_char(int index) const;
00066   INLINE void set_unicode_char(int index, int character);
00067   INLINE string get_encoded_char(int index) const;
00068   INLINE string get_encoded_char(int index, Encoding encoding) const;
00069   INLINE string get_text_as_ascii() const;
00070 
00071   INLINE static string reencode_text(const string &text, Encoding from, Encoding to);
00072 
00073   INLINE static bool unicode_isalpha(int character);
00074   INLINE static bool unicode_isdigit(int character);
00075   INLINE static bool unicode_ispunct(int character);
00076   INLINE static bool unicode_islower(int character);
00077   INLINE static bool unicode_isupper(int character);
00078   INLINE static bool unicode_isspace(int character);
00079   INLINE static int unicode_toupper(int character);
00080   INLINE static int unicode_tolower(int character);
00081 
00082   INLINE static string upper(const string &source);
00083   INLINE static string upper(const string &source, Encoding encoding);
00084   INLINE static string lower(const string &source);
00085   INLINE static string lower(const string &source, Encoding encoding);
00086 
00087   // Direct support for wide-character strings.  Now publishable with
00088   // the new wstring support in interrogate.
00089   INLINE void set_wtext(const wstring &wtext);
00090   INLINE const wstring &get_wtext() const;
00091   INLINE void append_wtext(const wstring &text);
00092   wstring get_wtext_as_ascii() const;
00093   bool is_wtext() const;
00094 
00095   static string encode_wchar(wchar_t ch, Encoding encoding);
00096   INLINE string encode_wtext(const wstring &wtext) const;
00097   static string encode_wtext(const wstring &wtext, Encoding encoding);
00098   INLINE wstring decode_text(const string &text) const;
00099   static wstring decode_text(const string &text, Encoding encoding);
00100 
00101 private:
00102   enum Flags {
00103     F_got_text         =  0x0001,
00104     F_got_wtext        =  0x0002,
00105   };
00106   static wstring decode_text_impl(StringDecoder &decoder);
00107 
00108   int _flags;
00109   Encoding _encoding;
00110   string _text;
00111   wstring _wtext;
00112 
00113   static Encoding _default_encoding;
00114 };
00115 
00116 EXPCL_DTOOL ostream &
00117 operator << (ostream &out, TextEncoder::Encoding encoding);
00118 EXPCL_DTOOL istream &
00119 operator >> (istream &in, TextEncoder::Encoding &encoding);
00120 
00121 // We'll define the output operator for wstring here, too.  Presumably
00122 // this will not be automatically defined by any system libraries.
00123 
00124 // This function is declared inline to minimize the risk of link
00125 // conflicts should another third-party module also define the same
00126 // output operator.
00127 INLINE EXPCL_DTOOL ostream &
00128 operator << (ostream &out, const wstring &str);
00129 
00130 #include "textEncoder.I"
00131 
00132 #endif
 All Classes Functions Variables Enumerations