DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 1 | /* "$Id: fl_utf8.h 8585 2011-04-13 15:43:22Z ianmacarthur $" |
| 2 | * |
| 3 | * Author: Jean-Marc Lienher ( http://oksid.ch ) |
| 4 | * Copyright 2000-2010 by O'ksi'D. |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Library General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Library General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Library General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 19 | * USA. |
| 20 | * |
| 21 | * Please report all bugs and problems on the following page: |
| 22 | * |
| 23 | * http://www.fltk.org/str.php |
| 24 | */ |
| 25 | |
| 26 | /* Merged in some functionality from the fltk-2 version. IMM. |
| 27 | * The following code is an attempt to merge the functions incorporated in FLTK2 |
| 28 | * with the functions provided in OksiD's fltk-1.1.6-utf8 port |
| 29 | */ |
| 30 | |
| 31 | /** |
| 32 | \file fl_utf8.h |
| 33 | \brief header for Unicode and UTF8 chracter handling |
| 34 | */ |
| 35 | |
| 36 | #ifndef _HAVE_FL_UTF8_HDR_ |
| 37 | #define _HAVE_FL_UTF8_HDR_ |
| 38 | |
| 39 | #include "Fl_Export.H" |
| 40 | #include "fl_types.h" |
| 41 | |
| 42 | #include <stdio.h> |
| 43 | #include <string.h> |
| 44 | #include <stdlib.h> |
| 45 | |
| 46 | #ifdef WIN32 |
| 47 | # include <sys/types.h> |
| 48 | # include <sys/stat.h> |
| 49 | # include <locale.h> |
| 50 | # include <ctype.h> |
| 51 | # define xchar wchar_t |
| 52 | # if !defined(FL_DLL) && !defined(__CYGWIN__) |
| 53 | # undef strdup |
| 54 | # define strdup _strdup |
| 55 | # undef putenv |
| 56 | # define putenv _putenv |
| 57 | # undef stricmp |
| 58 | # define stricmp _stricmp |
| 59 | # undef strnicmp |
| 60 | # define strnicmp _strnicmp |
| 61 | # undef hypot |
| 62 | # define hypot _hypot |
| 63 | # undef chdir |
| 64 | # define chdir _chdir |
| 65 | # endif |
| 66 | #elif defined(__APPLE__) |
| 67 | # include <wchar.h> |
| 68 | # include <sys/stat.h> |
| 69 | # define xchar wchar_t |
| 70 | #else /* X11 */ |
| 71 | # include <sys/types.h> |
| 72 | # include <sys/stat.h> |
| 73 | # include "Xutf8.h" |
| 74 | # include <X11/Xlocale.h> |
| 75 | # include <X11/Xlib.h> |
| 76 | # include <locale.h> |
| 77 | # define xchar unsigned short |
| 78 | #endif |
| 79 | |
| 80 | # ifdef __cplusplus |
| 81 | extern "C" { |
| 82 | # endif |
| 83 | |
| 84 | /** \addtogroup fl_unicode |
| 85 | @{ |
| 86 | */ |
| 87 | |
| 88 | /* F2: comes from FLTK2 */ |
| 89 | /* OD: comes from OksiD */ |
| 90 | |
| 91 | /** |
| 92 | Return the number of bytes needed to encode the given UCS4 character in UTF8. |
| 93 | \param [in] ucs UCS4 encoded character |
| 94 | \return number of bytes required |
| 95 | */ |
| 96 | FL_EXPORT int fl_utf8bytes(unsigned ucs); |
| 97 | |
| 98 | /* OD: returns the byte length of the first UTF-8 char sequence (returns -1 if not valid) */ |
| 99 | FL_EXPORT int fl_utf8len(char c); |
| 100 | |
| 101 | /* OD: returns the byte length of the first UTF-8 char sequence (returns +1 if not valid) */ |
| 102 | FL_EXPORT int fl_utf8len1(char c); |
| 103 | |
| 104 | /* OD: returns the number of Unicode chars in the UTF-8 string */ |
| 105 | FL_EXPORT int fl_utf_nb_char(const unsigned char *buf, int len); |
| 106 | |
| 107 | /* F2: Convert the next UTF8 char-sequence into a Unicode value (and say how many bytes were used) */ |
| 108 | FL_EXPORT unsigned fl_utf8decode(const char* p, const char* end, int* len); |
| 109 | |
| 110 | /* F2: Encode a Unicode value into a UTF8 sequence, return the number of bytes used */ |
| 111 | FL_EXPORT int fl_utf8encode(unsigned ucs, char* buf); |
| 112 | |
| 113 | /* F2: Move forward to the next valid UTF8 sequence start betwen start and end */ |
| 114 | FL_EXPORT const char* fl_utf8fwd(const char* p, const char* start, const char* end); |
| 115 | |
| 116 | /* F2: Move backward to the previous valid UTF8 sequence start */ |
| 117 | FL_EXPORT const char* fl_utf8back(const char* p, const char* start, const char* end); |
| 118 | |
| 119 | /* XX: Convert a single 32-bit Unicode value into UTF16 */ |
| 120 | FL_EXPORT unsigned fl_ucs_to_Utf16(const unsigned ucs, unsigned short *dst, const unsigned dstlen); |
| 121 | |
| 122 | /* F2: Convert a UTF8 string into UTF16 */ |
| 123 | FL_EXPORT unsigned fl_utf8toUtf16(const char* src, unsigned srclen, unsigned short* dst, unsigned dstlen); |
| 124 | |
| 125 | /* F2: Convert a UTF8 string into a wide character string - makes UTF16 on win32, "UCS4" elsewhere */ |
| 126 | FL_EXPORT unsigned fl_utf8towc(const char *src, unsigned srclen, wchar_t *dst, unsigned dstlen); |
| 127 | |
| 128 | /* F2: Convert a wide character string to UTF8 - takes in UTF16 on win32, "UCS4" elsewhere */ |
| 129 | FL_EXPORT unsigned fl_utf8fromwc(char *dst, unsigned dstlen, const wchar_t *src, unsigned srclen); |
| 130 | |
| 131 | /* F2: Convert a UTF8 string into ASCII, eliding untranslatable glyphs */ |
| 132 | FL_EXPORT unsigned fl_utf8toa (const char *src, unsigned srclen, char *dst, unsigned dstlen); |
| 133 | /* OD: convert UTF-8 string to latin1 */ |
| 134 | /* FL_EXPORT int fl_utf2latin1(const unsigned char *src, int srclen, char *dst); */ |
| 135 | |
| 136 | /* F2: Convert 8859-1 string to UTF8 */ |
| 137 | FL_EXPORT unsigned fl_utf8froma (char *dst, unsigned dstlen, const char *src, unsigned srclen); |
| 138 | /* OD: convert latin1 str to UTF-8 */ |
| 139 | /* FL_EXPORT int fl_latin12utf(const unsigned char *src, int srclen, char *dst); */ |
| 140 | |
| 141 | /* F2: Returns true if the current O/S locale is UTF8 */ |
| 142 | FL_EXPORT int fl_utf8locale(); |
| 143 | |
| 144 | /* F2: Examine the first len characters of src, to determine if the input text is UTF8 or not |
| 145 | * NOTE: The value returned is not simply boolean - it contains information about the probable |
| 146 | * type of the src text. */ |
| 147 | FL_EXPORT int fl_utf8test(const char *src, unsigned len); |
| 148 | |
| 149 | /* XX: return width of "raw" ucs character in columns. |
| 150 | * for internal use only */ |
| 151 | FL_EXPORT int fl_wcwidth_(unsigned int ucs); |
| 152 | |
| 153 | /* XX: return width of utf-8 character string in columns. |
| 154 | * NOTE: this may also do C1 control character (0x80 to 0x9f) to CP1252 mapping, |
| 155 | * depending on original build options */ |
| 156 | FL_EXPORT int fl_wcwidth(const char *src); |
| 157 | |
| 158 | /* OD: Return true if the character is non-spacing */ |
| 159 | FL_EXPORT unsigned int fl_nonspacing(unsigned int ucs); |
| 160 | |
| 161 | /* F2: Convert UTF8 to a local multi-byte encoding - mainly for win32? */ |
| 162 | FL_EXPORT unsigned fl_utf8to_mb(const char *src, unsigned srclen, char *dst, unsigned dstlen); |
| 163 | /* OD: Convert UTF8 to a local multi-byte encoding */ |
| 164 | FL_EXPORT char* fl_utf2mbcs(const char *src); |
| 165 | |
| 166 | /* F2: Convert a local multi-byte encoding to UTF8 - mainly for win32? */ |
| 167 | FL_EXPORT unsigned fl_utf8from_mb(char *dst, unsigned dstlen, const char *src, unsigned srclen); |
| 168 | /* OD: Convert a local multi-byte encoding to UTF8 */ |
| 169 | /* FL_EXPORT char* fl_mbcs2utf(const char *src); */ |
| 170 | |
| 171 | /*****************************************************************************/ |
| 172 | #ifdef WIN32 |
| 173 | /* OD: Attempt to convert the UTF8 string to the current locale */ |
| 174 | FL_EXPORT char *fl_utf8_to_locale(const char *s, int len, unsigned int codepage); |
| 175 | |
| 176 | /* OD: Attempt to convert a string in the current locale to UTF8 */ |
| 177 | FL_EXPORT char *fl_locale_to_utf8(const char *s, int len, unsigned int codepage); |
| 178 | #endif |
| 179 | |
| 180 | /***************************************************************************** |
| 181 | * The following functions are intended to provide portable, UTF8 aware |
| 182 | * versions of standard functions |
| 183 | */ |
| 184 | |
| 185 | /* OD: UTF8 aware strncasecmp - converts to lower case Unicode and tests */ |
| 186 | FL_EXPORT int fl_utf_strncasecmp(const char *s1, const char *s2, int n); |
| 187 | |
| 188 | /* OD: UTF8 aware strcasecmp - converts to Unicode and tests */ |
| 189 | FL_EXPORT int fl_utf_strcasecmp(const char *s1, const char *s2); |
| 190 | |
| 191 | /* OD: return the Unicode lower case value of ucs */ |
| 192 | FL_EXPORT int fl_tolower(unsigned int ucs); |
| 193 | |
| 194 | /* OD: return the Unicode upper case value of ucs */ |
| 195 | FL_EXPORT int fl_toupper(unsigned int ucs); |
| 196 | |
| 197 | /* OD: converts the UTF8 string to the lower case equivalent */ |
| 198 | FL_EXPORT int fl_utf_tolower(const unsigned char *str, int len, char *buf); |
| 199 | |
| 200 | /* OD: converts the UTF8 string to the upper case equivalent */ |
| 201 | FL_EXPORT int fl_utf_toupper(const unsigned char *str, int len, char *buf); |
| 202 | |
| 203 | /* OD: Portable UTF8 aware chmod wrapper */ |
| 204 | FL_EXPORT int fl_chmod(const char* f, int mode); |
| 205 | |
| 206 | /* OD: Portable UTF8 aware access wrapper */ |
| 207 | FL_EXPORT int fl_access(const char* f, int mode); |
| 208 | |
| 209 | /* OD: Portable UTF8 aware stat wrapper */ |
| 210 | FL_EXPORT int fl_stat( const char *path, struct stat *buffer ); |
| 211 | |
| 212 | /* OD: Portable UTF8 aware getcwd wrapper */ |
| 213 | FL_EXPORT char* fl_getcwd( char *buf, int maxlen); |
| 214 | |
| 215 | /* OD: Portable UTF8 aware fopen wrapper */ |
| 216 | FL_EXPORT FILE *fl_fopen(const char *f, const char *mode); |
| 217 | |
| 218 | /* OD: Portable UTF8 aware system wrapper */ |
| 219 | FL_EXPORT int fl_system(const char* f); |
| 220 | |
| 221 | /* OD: Portable UTF8 aware execvp wrapper */ |
| 222 | FL_EXPORT int fl_execvp(const char *file, char *const *argv); |
| 223 | |
| 224 | /* OD: Portable UTF8 aware open wrapper */ |
| 225 | FL_EXPORT int fl_open(const char* f, int o, ...); |
| 226 | |
| 227 | /* OD: Portable UTF8 aware unlink wrapper */ |
| 228 | FL_EXPORT int fl_unlink(const char *f); |
| 229 | |
| 230 | /* OD: Portable UTF8 aware rmdir wrapper */ |
| 231 | FL_EXPORT int fl_rmdir(const char *f); |
| 232 | |
| 233 | /* OD: Portable UTF8 aware getenv wrapper */ |
| 234 | FL_EXPORT char* fl_getenv(const char *name); |
| 235 | |
| 236 | /* OD: Portable UTF8 aware execvp wrapper */ |
| 237 | FL_EXPORT int fl_mkdir(const char* f, int mode); |
| 238 | |
| 239 | /* OD: Portable UTF8 aware rename wrapper */ |
| 240 | FL_EXPORT int fl_rename(const char* f, const char *t); |
| 241 | |
| 242 | |
| 243 | /* OD: Given a full pathname, this will create the directory path needed to hold the file named */ |
| 244 | FL_EXPORT void fl_make_path_for_file( const char *path ); |
| 245 | |
| 246 | /* OD: recursively create a path in the file system */ |
| 247 | FL_EXPORT char fl_make_path( const char *path ); |
| 248 | |
| 249 | |
| 250 | /** @} */ |
| 251 | |
| 252 | /*****************************************************************************/ |
| 253 | |
| 254 | #ifdef __cplusplus |
| 255 | } |
| 256 | #endif /* __cplusplus */ |
| 257 | |
| 258 | |
| 259 | #endif /* _HAVE_FL_UTF8_HDR_ */ |
| 260 | |
| 261 | /* |
| 262 | * End of "$Id: fl_utf8.h 8585 2011-04-13 15:43:22Z ianmacarthur $". |
| 263 | */ |