/[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 278 by twoaday, Mon Jan 15 22:02:04 2007 UTC revision 456 by twoaday, Thu Aug 2 20:10:00 2012 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, 2012 Timo Schulz
3   *   *
4   * This file is part of WinPT.   * This file is part of WinPT.
5   *   *
# Line 27  Line 27 
27  #include "wptErrors.h"  #include "wptErrors.h"
28    
29    
30    /**
31     * Convert 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  {  {
37      wchar_t *result;      int n = MultiByteToWideChar (GetACP (), 0, string, -1, NULL, 0);
     char *native;  
     int n;  
   
     n = MultiByteToWideChar (GetACP (), 0, string, -1, NULL, 0);  
38      if (n < 0)      if (n < 0)
39          return NULL;          return NULL;
40    
41      result = new wchar_t[n+1];      wchar_t *result = new wchar_t[n+1];
42      if (!result)      if (!result)
43          BUG (0);          BUG (0);
44    
# Line 52  native_to_utf8 (const char *string) Line 52  native_to_utf8 (const char *string)
52      if (n < 0)      if (n < 0)
53          return NULL;          return NULL;
54    
55      native = new char[n+1];      char *native = new char[n + 1];
56      if (!native)      if (!native)
57          BUG (0);          BUG (0);
58        memset(native, 0, n + 1);
59    
60      n = WideCharToMultiByte (CP_UTF8, 0, result, -1, native, n, NULL, NULL);      n = WideCharToMultiByte (CP_UTF8, 0, result, -1, native, n, NULL, NULL);
61      if (n < 0) {      if (n < 0) {
# Line 67  native_to_utf8 (const char *string) Line 68  native_to_utf8 (const char *string)
68  }  }
69    
70    
71  /* Convert utf8 string @str to native CP. */  /**
72     * Convert an UTF-8 string into an UTF-16 string.
73     */
74    wchar_t*
75    utf8_to_utf16(const char *string, size_t *retlen)
76    {
77        int n = MultiByteToWideChar(CP_UTF8, 0, string, -1, NULL, 0);
78        if (n < 0)
79            return NULL;
80        
81        wchar_t *result = new wchar_t[n + 1];
82        if (!result)
83            BUG(0);
84        
85        n = MultiByteToWideChar(CP_UTF8, 0, string, -1, result, n);
86        if (n < 0) {
87            free_if_alloc(result);
88            return NULL;
89        }
90        
91        *retlen = n;
92        return result;
93    }
94    
95        
96    /**
97     * Convert the given string, which is encoded in UTF-8,
98     * into the locale setting.
99     */
100  char*  char*
101  utf8_to_native (const char *string)  utf8_to_native (const char *string)
102  {  {
103      wchar_t *result;      int n = MultiByteToWideChar (CP_UTF8, 0, string, -1, NULL, 0);
     char *native;      
     int n;  
   
     n = MultiByteToWideChar (CP_UTF8, 0, string, -1, NULL, 0);  
104      if (n < 0)      if (n < 0)
105          return NULL;          return NULL;
106    
107      result = (wchar_t*)malloc ((n+1) * sizeof *result);      wchar_t *result = new wchar_t[n+1];
108      if (!result)      if (!result)
109          BUG (0);          BUG (0);
110    
111      n = MultiByteToWideChar (CP_UTF8, 0, string, -1, result, n);      n = MultiByteToWideChar (CP_UTF8, 0, string, -1, result, n);
112      if (n < 0) {      if (n < 0) {
113          free (result);          free_if_alloc (result);
114          return NULL;          return NULL;
115      }      }
116    
# Line 93  utf8_to_native (const char *string) Line 118  utf8_to_native (const char *string)
118      if (n < 0)      if (n < 0)
119          return NULL;          return NULL;
120    
121      native = (char*)malloc (n+1);      char *native = new char[n + 1];
122      if (!native)      if (!native)
123          BUG (0);          BUG (0);
124        memset(native, 0, n + 1);
125    
126      n = WideCharToMultiByte (GetACP (), 0, result, -1, native, n, NULL, NULL);      n = WideCharToMultiByte (GetACP (), 0, result, -1, native, n, NULL, NULL);
127      if (n < 0) {      if (n < 0) {
128          free (result);          free_if_alloc (result);
129          return NULL;          return NULL;
130      }      }
131        
132      free (result);      free_if_alloc (result);
133      return native;      return native;
134  }  }
135    
136    
137  /* Return -1 if the string contains any 8-bit characters. */  /**
138     * Return -1 if the given string contains any 8-bit characters.
139     * This is a helper to decide when to use UTF8 encoding.
140     */
141  int  int
142  is_8bit_string (const char *str)  is_8bit_string (const char *str)
143  {  {
144      size_t i;      for (size_t i = 0; i < strlen (str); i++) {
   
     for (i = 0; i < strlen (str); i++) {  
145          if (str[i] & 0x80)          if (str[i] & 0x80)
146              return -1;              return -1;
147      }      }

Legend:
Removed from v.278  
changed lines
  Added in v.456

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26