/[winpt]/trunk/Src/wptUTF8.cpp
ViewVC logotype

Diff of /trunk/Src/wptUTF8.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 193 by twoaday, Sat Apr 1 12:36:35 2006 UTC revision 328 by twoaday, Fri Sep 25 16:07:38 2009 UTC
# Line 1  Line 1 
1  /* wptUTF8.cpp - UTF8 conversation  /* wptUTF8.cpp - UTF8 conversation
2   *      Copyright (C) 2002, 2004, 2005, 2006 Timo Schulz   *      Copyright (C) 2002, 2004, 2005, 2006, 2009 Timo Schulz
3   *   *
4   * This file is part of WinPT.   * This file is part of WinPT.
5   *   *
# Line 12  Line 12 
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14   * GNU General Public License for more details.   * GNU General Public License for more details.
  *  
  * You should have received a copy of the GNU General Public License  
  * along with WinPT; if not, write to the Free Software Foundation,  
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA  
15   */   */
   
16  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
17  #include <config.h>  #include <config.h>
18  #endif  #endif
# Line 32  Line 27 
27  #include "wptErrors.h"  #include "wptErrors.h"
28    
29    
30    /**
31     * Converts the given intput string, which is encoded with the locale
32     * setting, into UTF-8 representation.
33     */
34  char*  char*
35  native_to_utf8 (const char *string)  native_to_utf8 (const char *string)
36  {  {
# Line 43  native_to_utf8 (const char *string) Line 42  native_to_utf8 (const char *string)
42      if (n < 0)      if (n < 0)
43          return NULL;          return NULL;
44    
45      result = (wchar_t*)malloc ((n+1) * sizeof *result);      result = new wchar_t[n+1];
46      if (!result)      if (!result)
47          BUG (0);          BUG (0);
48    
49      n = MultiByteToWideChar (GetACP (), 0, string, -1, result, n);      n = MultiByteToWideChar (GetACP (), 0, string, -1, result, n);
50      if (n < 0) {      if (n < 0) {
51          free (result);          free_if_alloc (result);
52          return NULL;          return NULL;
53      }      }
54    
# Line 57  native_to_utf8 (const char *string) Line 56  native_to_utf8 (const char *string)
56      if (n < 0)      if (n < 0)
57          return NULL;          return NULL;
58    
59      native = (char*)malloc (n+1);      native = new char[n+1];
60      if (!native)      if (!native)
61          BUG (0);          BUG (0);
62    
63      n = WideCharToMultiByte (CP_UTF8, 0, result, -1, native, n, NULL, NULL);      n = WideCharToMultiByte (CP_UTF8, 0, result, -1, native, n, NULL, NULL);
64      if (n < 0) {      if (n < 0) {
65          free (result);          free_if_alloc (result);
66          return NULL;          return NULL;
67      }      }
68    
69      free (result);      free_if_alloc (result);
70        //native[n] = '\0';        
71      return native;      return native;
72  }  }
73    
74    
75  /* Convert utf8 string @str to native CP. */  /**
76     * Converts the given string, which is encoded in UTF-8,
77     * into the locale setting.
78     */
79  char*  char*
80  utf8_to_native (const char *string)  utf8_to_native (const char *string)
81  {  {
# Line 84  utf8_to_native (const char *string) Line 87  utf8_to_native (const char *string)
87      if (n < 0)      if (n < 0)
88          return NULL;          return NULL;
89    
90      result = (wchar_t*)malloc ((n+1) * sizeof *result);      result = new wchar_t[n+1];
91      if (!result)      if (!result)
92          BUG (0);          BUG (0);
93    
94      n = MultiByteToWideChar (CP_UTF8, 0, string, -1, result, n);      n = MultiByteToWideChar (CP_UTF8, 0, string, -1, result, n);
95      if (n < 0) {      if (n < 0) {
96          free (result);          free_if_alloc (result);
97          return NULL;          return NULL;
98      }      }
99    
# Line 98  utf8_to_native (const char *string) Line 101  utf8_to_native (const char *string)
101      if (n < 0)      if (n < 0)
102          return NULL;          return NULL;
103    
104      native = (char*)malloc (n+1);      native = new char[n+1];
105      if (!native)      if (!native)
106          BUG (0);          BUG (0);
107    
108      n = WideCharToMultiByte (GetACP (), 0, result, -1, native, n, NULL, NULL);      n = WideCharToMultiByte (GetACP (), 0, result, -1, native, n, NULL, NULL);
109      if (n < 0) {      if (n < 0) {
110          free (result);          free_if_alloc (result);
111          return NULL;          return NULL;
112      }      }
113        
114      free (result);      free_if_alloc (result);
115        //native[n] = '\0';
116      return native;      return native;
117  }  }
118    
119    
120  /* Return -1 if the string contains any 8-bit characters. */  /**
121     * Returns -1 if the given string contains any 8-bit characters.
122     * This is a helper to decide when to use UTF8 encoding.
123     */
124  int  int
125  is_8bit_string (const char *str)  is_8bit_string (const char *str)
126  {  {
127      size_t i;      for (size_t i = 0; i < strlen (str); i++) {
   
     for (i = 0; i < strlen (str); i++) {  
128          if (str[i] & 0x80)          if (str[i] & 0x80)
129              return -1;              return -1;
130      }      }

Legend:
Removed from v.193  
changed lines
  Added in v.328

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26