unicode.H

00001 /*
00002  * This file is part of the "Archon" framework.
00003  * (http://files3d.sourceforge.net)
00004  *
00005  * Copyright © 2002 by Kristian Spangsege and Brian Kristiansen.
00006  *
00007  * Permission to use, copy, modify, and distribute this software and
00008  * its documentation under the terms of the GNU General Public License is
00009  * hereby granted. No representations are made about the suitability of
00010  * this software for any purpose. It is provided "as is" without express
00011  * or implied warranty. See the GNU General Public License
00012  * (http://www.gnu.org/copyleft/gpl.html) for more details.
00013  *
00014  * The characters in this file are ISO8859-1 encoded.
00015  *
00016  * The documentation in this file is in "Doxygen" style
00017  * (http://www.doxygen.org).
00018  */
00019 
00020 #ifndef ARCHON_UTILITIES_UNICODE_H
00021 #define ARCHON_UTILITIES_UNICODE_H
00022 
00023 #include <string>
00024 
00025 #if !defined __GNUC__ || __GNUC__ > 2
00026 namespace std 
00027 {
00028   template<>
00029   struct char_traits<unsigned>
00030   {
00031     typedef unsigned char_type;
00032     typedef unsigned int_type;
00033     typedef streampos pos_type;
00034     typedef streamoff off_type;
00035     typedef mbstate_t state_type;
00036       
00037     static void assign(char_type &__c1, const char_type &__c2)
00038     {
00039       __c1 = __c2;
00040     }
00041 
00042     static bool eq(const char_type &__c1, const char_type &__c2)
00043     {
00044       return __c1 == __c2;
00045     }
00046 
00047     static bool lt(const char_type &__c1, const char_type &__c2)
00048     {
00049       return __c1 < __c2;
00050     }
00051 
00052     static int compare(const char_type *__s1, const char_type *__s2, size_t __n)
00053     { 
00054       for (size_t __i = 0; __i < __n; ++__i)
00055         if (!eq(__s1[__i], __s2[__i]))
00056           return lt(__s1[__i], __s2[__i]) ? -1 : 1;
00057       return 0; 
00058     }
00059 
00060     static size_t length(const char_type *__s)
00061     { 
00062       const char_type *__p = __s; 
00063       while (*__p) ++__p; 
00064       return (__p - __s); 
00065     }
00066 
00067     static const char_type *find(const char_type *__s, size_t __n, const char_type &__a)
00068     {
00069       for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
00070         if (*__p == __a) return __p;
00071       return 0;
00072     }
00073 
00074     static char_type *move(char_type *__s1, const char_type *__s2, size_t __n)
00075     {
00076       return static_cast<char_type *>(memmove(__s1, __s2, __n * sizeof(char_type)));
00077     }
00078 
00079     static char_type *copy(char_type *__s1, const char_type *__s2, size_t __n)
00080     {
00081       return static_cast<char_type *>(memcpy(__s1, __s2, __n * sizeof(char_type)));
00082     }
00083 
00084     static char_type *assign(char_type *__s, size_t __n, char_type __a)
00085     { 
00086       for (char_type *__p = __s; __p < __s + __n; ++__p) 
00087         assign(*__p, __a);
00088       return __s; 
00089     }
00090 
00091     static char_type to_char_type(const int_type &__c)
00092     {
00093       return char_type(__c);
00094     }
00095 
00096     static int_type to_int_type(const char_type &__c)
00097     {
00098       return int_type(__c);
00099     }
00100 
00101     static bool eq_int_type(const int_type &__c1, const int_type &__c2)
00102     {
00103       return __c1 == __c2;
00104     }
00105 
00106     static int_type eof()
00107     {
00108       return static_cast<int_type>(-1);
00109     }
00110 
00111     static int_type not_eof(const int_type &__c)
00112     {
00113       return eq_int_type(__c, eof()) ? int_type(0) : __c;
00114     }
00115   };
00116 }
00117 #endif
00118 
00119 namespace Archon
00120 {
00121   namespace Utilities
00122   {
00123     using namespace std;
00124 
00125     typedef unsigned uchar;              // One unicode character MUST ALWAYS BE EXACTLY 4 BYTES WIDE
00126     typedef basic_string<uchar> ustring; // A string of unicode characters
00127 
00128     namespace Unicode
00129     {
00133       string encodeUtf8(ustring);
00134 
00139       ustring decodeUtf8(string s);
00140 
00151       int decodeUtf8(const char *in, int inSize, uchar *out, int &outSize);
00152     }
00153   }
00154 }
00155 
00156 #endif // ARCHON_UTILITIES_UNICODE_H

Generated on Sun Jul 30 22:55:46 2006 for Archon by  doxygen 1.4.4