fl_utf8.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * "$Id: fl_utf8.h 12549 2017-11-10 12:56:00Z AlbrechtS $"
  3. *
  4. * Author: Jean-Marc Lienher ( http://oksid.ch )
  5. * Copyright 2000-2010 by O'ksi'D.
  6. * Copyright 2016-2017 by Bill Spitzak and others.
  7. *
  8. * This library is free software. Distribution and use rights are outlined in
  9. * the file "COPYING" which should have been included with this file. If this
  10. * file is missing or damaged, see the license at:
  11. *
  12. * http://www.fltk.org/COPYING.php
  13. *
  14. * Please report all bugs and problems on the following page:
  15. *
  16. * http://www.fltk.org/str.php
  17. */
  18. /* Merged in some functionality from the fltk-2 version. IMM.
  19. * The following code is an attempt to merge the functions incorporated in FLTK2
  20. * with the functions provided in OksiD's fltk-1.1.6-utf8 port
  21. */
  22. /**
  23. \file fl_utf8.h
  24. \brief header for Unicode and UTF-8 character handling
  25. */
  26. #ifndef _HAVE_FL_UTF8_HDR_
  27. #define _HAVE_FL_UTF8_HDR_
  28. #include "Fl_Export.H"
  29. #include "fl_types.h"
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /** \addtogroup fl_unicode
  37. @{
  38. */
  39. /* F2: comes from FLTK2 */
  40. /* OD: comes from OksiD */
  41. /**
  42. Return the number of bytes needed to encode the given UCS4 character in UTF-8.
  43. \param [in] ucs UCS4 encoded character
  44. \return number of bytes required
  45. */
  46. FL_EXPORT int fl_utf8bytes(unsigned ucs);
  47. /* OD: returns the byte length of the first UTF-8 char sequence (returns -1 if not valid) */
  48. FL_EXPORT int fl_utf8len(char c);
  49. /* OD: returns the byte length of the first UTF-8 char sequence (returns +1 if not valid) */
  50. FL_EXPORT int fl_utf8len1(char c);
  51. /* OD: returns the number of Unicode chars in the UTF-8 string */
  52. FL_EXPORT int fl_utf_nb_char(const unsigned char *buf, int len);
  53. /* F2: Convert the next UTF-8 char-sequence into a Unicode value (and say how many bytes were used) */
  54. FL_EXPORT unsigned fl_utf8decode(const char* p, const char* end, int* len);
  55. /* F2: Encode a Unicode value into a UTF-8 sequence, return the number of bytes used */
  56. FL_EXPORT int fl_utf8encode(unsigned ucs, char* buf);
  57. /* F2: Move forward to the next valid UTF-8 sequence start betwen start and end */
  58. FL_EXPORT const char* fl_utf8fwd(const char* p, const char* start, const char* end);
  59. /* F2: Move backward to the previous valid UTF-8 sequence start */
  60. FL_EXPORT const char* fl_utf8back(const char* p, const char* start, const char* end);
  61. /* XX: Convert a single 32-bit Unicode value into UTF16 */
  62. FL_EXPORT unsigned fl_ucs_to_Utf16(const unsigned ucs, unsigned short *dst, const unsigned dstlen);
  63. /* F2: Convert a UTF-8 string into UTF16 */
  64. FL_EXPORT unsigned fl_utf8toUtf16(const char* src, unsigned srclen, unsigned short* dst, unsigned dstlen);
  65. /* F2: Convert a UTF-8 string into a wide character string - makes UTF16 on win32, "UCS4" elsewhere */
  66. FL_EXPORT unsigned fl_utf8towc(const char *src, unsigned srclen, wchar_t *dst, unsigned dstlen);
  67. /* F2: Convert a wide character string to UTF-8 - takes in UTF16 on win32, "UCS4" elsewhere */
  68. FL_EXPORT unsigned fl_utf8fromwc(char *dst, unsigned dstlen, const wchar_t *src, unsigned srclen);
  69. /* F2: Convert a UTF-8 string into ASCII, eliding untranslatable glyphs */
  70. FL_EXPORT unsigned fl_utf8toa (const char *src, unsigned srclen, char *dst, unsigned dstlen);
  71. /* F2: Convert 8859-1 string to UTF-8 */
  72. FL_EXPORT unsigned fl_utf8froma (char *dst, unsigned dstlen, const char *src, unsigned srclen);
  73. /* F2: Returns true if the current O/S locale is UTF-8 */
  74. FL_EXPORT int fl_utf8locale(void);
  75. /* F2: Examine the first len characters of src, to determine if the input text is UTF-8 or not
  76. * NOTE: The value returned is not simply boolean - it contains information about the probable
  77. * type of the src text. */
  78. FL_EXPORT int fl_utf8test(const char *src, unsigned len);
  79. /* XX: return width of "raw" ucs character in columns.
  80. * for internal use only */
  81. FL_EXPORT int fl_wcwidth_(unsigned int ucs);
  82. /* XX: return width of UTF-8 character string in columns.
  83. * NOTE: this may also do C1 control character (0x80 to 0x9f) to CP1252 mapping,
  84. * depending on original build options */
  85. FL_EXPORT int fl_wcwidth(const char *src);
  86. /* OD: Return true if the character is non-spacing */
  87. FL_EXPORT unsigned int fl_nonspacing(unsigned int ucs);
  88. /* F2: Convert UTF-8 to a local multi-byte encoding - mainly for win32? */
  89. FL_EXPORT unsigned fl_utf8to_mb(const char *src, unsigned srclen, char *dst, unsigned dstlen);
  90. /* OD: Convert UTF-8 to a local multi-byte encoding */
  91. FL_EXPORT char* fl_utf2mbcs(const char *src);
  92. /* F2: Convert a local multi-byte encoding to UTF-8 - mainly for win32? */
  93. FL_EXPORT unsigned fl_utf8from_mb(char *dst, unsigned dstlen, const char *src, unsigned srclen);
  94. /*****************************************************************************/
  95. #ifdef WIN32
  96. /* these two WIN32-only functions are kept for API compatibility */
  97. /* OD: Attempt to convert the UTF-8 string to the current locale */
  98. FL_EXPORT char *fl_utf8_to_locale(const char *s, int len, unsigned int codepage);
  99. /* OD: Attempt to convert a string in the current locale to UTF-8 */
  100. FL_EXPORT char *fl_locale_to_utf8(const char *s, int len, unsigned int codepage);
  101. #endif /* WIN32 */
  102. /*****************************************************************************
  103. * The following functions are intended to provide portable, UTF-8 aware
  104. * versions of standard functions
  105. */
  106. /* OD: UTF-8 aware strncasecmp - converts to lower case Unicode and tests */
  107. FL_EXPORT int fl_utf_strncasecmp(const char *s1, const char *s2, int n);
  108. /* OD: UTF-8 aware strcasecmp - converts to Unicode and tests */
  109. FL_EXPORT int fl_utf_strcasecmp(const char *s1, const char *s2);
  110. /* OD: return the Unicode lower case value of ucs */
  111. FL_EXPORT int fl_tolower(unsigned int ucs);
  112. /* OD: return the Unicode upper case value of ucs */
  113. FL_EXPORT int fl_toupper(unsigned int ucs);
  114. /* OD: converts the UTF-8 string to the lower case equivalent */
  115. FL_EXPORT int fl_utf_tolower(const unsigned char *str, int len, char *buf);
  116. /* OD: converts the UTF-8 string to the upper case equivalent */
  117. FL_EXPORT int fl_utf_toupper(const unsigned char *str, int len, char *buf);
  118. /* OD: Portable UTF-8 aware chmod wrapper */
  119. FL_EXPORT int fl_chmod(const char* f, int mode);
  120. /* OD: Portable UTF-8 aware access wrapper */
  121. FL_EXPORT int fl_access(const char* f, int mode);
  122. /* OD: Portable UTF-8 aware stat wrapper */
  123. FL_EXPORT int fl_stat(const char *path, struct stat *buffer);
  124. /* OD: Portable UTF-8 aware getcwd wrapper */
  125. FL_EXPORT char *fl_getcwd(char *buf, int len);
  126. /* Portable UTF-8 aware chdir wrapper */
  127. FL_EXPORT int fl_chdir(const char *path);
  128. /* OD: Portable UTF-8 aware fopen wrapper */
  129. FL_EXPORT FILE *fl_fopen(const char *f, const char *mode);
  130. /* OD: Portable UTF-8 aware system wrapper */
  131. FL_EXPORT int fl_system(const char* f);
  132. /* OD: Portable UTF-8 aware execvp wrapper */
  133. FL_EXPORT int fl_execvp(const char *file, char *const *argv);
  134. /* OD: Portable UTF-8 aware open wrapper */
  135. FL_EXPORT int fl_open(const char *fname, int oflags, ...);
  136. FL_EXPORT int fl_open_ext(const char *fname, int binary, int oflags, ...);
  137. /* OD: Portable UTF-8 aware unlink wrapper */
  138. FL_EXPORT int fl_unlink(const char *fname);
  139. /* OD: Portable UTF-8 aware rmdir wrapper */
  140. FL_EXPORT int fl_rmdir(const char *f);
  141. /* OD: Portable UTF-8 aware getenv wrapper */
  142. FL_EXPORT char* fl_getenv(const char *name);
  143. /* OD: Portable UTF-8 aware execvp wrapper */
  144. FL_EXPORT int fl_mkdir(const char* f, int mode);
  145. /* OD: Portable UTF-8 aware rename wrapper */
  146. FL_EXPORT int fl_rename(const char* f, const char *t);
  147. /* OD: Given a full pathname, this will create the directory path needed to hold the file named */
  148. FL_EXPORT void fl_make_path_for_file( const char *path );
  149. /* OD: recursively create a path in the file system */
  150. FL_EXPORT char fl_make_path( const char *path );
  151. /** @} */
  152. /*****************************************************************************/
  153. #ifdef __cplusplus
  154. }
  155. #endif /* __cplusplus */
  156. #endif /* _HAVE_FL_UTF8_HDR_ */
  157. /*
  158. * End of "$Id: fl_utf8.h 12549 2017-11-10 12:56:00Z AlbrechtS $".
  159. */