]> git.saurik.com Git - wxWidgets.git/blob - src/common/wxchar.cpp
suppressed compiler warning about unitialised umask value
[wxWidgets.git] / src / common / wxchar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wxchar.cpp
3 // Purpose: wxChar implementation
4 // Author: Ove Kåven
5 // Modified by:
6 // Created: 09/04/99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWindows copyright
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "wxchar.h"
14 #endif
15
16 // ===========================================================================
17 // headers, declarations, constants
18 // ===========================================================================
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #define _ISOC9X_SOURCE 1 // to get vsscanf()
28 #define _BSD_SOURCE 1 // to still get strdup()
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <locale.h>
34
35 #ifndef WX_PRECOMP
36 #include "wx/defs.h"
37 #include "wx/wxchar.h"
38 #include "wx/string.h"
39 #include "wx/hash.h"
40 #endif
41
42 #if wxUSE_WCHAR_T
43 size_t WXDLLEXPORT wxMB2WC(wchar_t *buf, const char *psz, size_t n)
44 {
45 if (buf) {
46 if (!n || !*psz) {
47 if (n) *buf = _T('\0');
48 return 0;
49 }
50 return mbstowcs(buf, psz, n);
51 }
52
53 // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't
54 // honor the 3rd parameter, thus it will happily crash here).
55 #if wxUSE_WCSRTOMBS
56 // don't know if it's really needed (or if we can pass NULL), but better safe
57 // than quick
58 mbstate_t mbstate;
59 return mbsrtowcs((wchar_t *) NULL, &psz, 0, &mbstate);
60 #else // !GNU libc
61 return mbstowcs((wchar_t *) NULL, psz, 0);
62 #endif // GNU
63 }
64
65 size_t WXDLLEXPORT wxWC2MB(char *buf, const wchar_t *pwz, size_t n)
66 {
67 if (buf) {
68 if (!n || !*pwz) {
69 // glibc2.1 chokes on null input
70 if (n) *buf = '\0';
71 return 0;
72 }
73 return wcstombs(buf, pwz, n);
74 }
75
76 // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't
77 // honor the 3rd parameter, thus it will happily crash here).
78 #if wxUSE_WCSRTOMBS
79 // don't know if it's really needed (or if we can pass NULL), but better safe
80 // than quick
81 mbstate_t mbstate;
82 return wcsrtombs((char *) NULL, &pwz, 0, &mbstate);
83 #else // !GNU libc
84 return wcstombs((char *) NULL, pwz, 0);
85 #endif // GNU
86 }
87 #endif
88
89 bool WXDLLEXPORT wxOKlibc()
90 {
91 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__)
92 // GNU libc uses UTF-8 even when it shouldn't
93 wchar_t res;
94 if ((MB_CUR_MAX == 2) &&
95 (wxMB2WC(&res, "\xdd\xa5", 1)>0) &&
96 (res==0x765)) {
97 // this is UTF-8 allright, check whether that's what we want
98 char *cur_locale = setlocale(LC_ALL, NULL);
99 if ((strlen(cur_locale) < 4) ||
100 (strcasecmp(cur_locale + strlen(cur_locale) - 4, "utf8"))) {
101 // nope, don't use libc conversion
102 return FALSE;
103 }
104 }
105 #endif
106 return TRUE;
107 }
108
109 #ifdef wxNEED_WCSLEN
110 size_t WXDLLEXPORT wcslen(const wchar_t *s)
111 {
112 size_t len;
113 while (s[len]) len++;
114 return len;
115 }
116 #endif
117
118 #ifndef wxStrdup
119 wxChar * WXDLLEXPORT wxStrdup(const wxChar *psz)
120 {
121 size_t size = (wxStrlen(psz) + 1) * sizeof(wxChar);
122 wxChar *ret = (wxChar *) malloc(size);
123 memcpy(ret, psz, size);
124 return ret;
125 }
126 #endif
127
128 #ifndef wxStricmp
129 int WXDLLEXPORT wxStricmp(const wxChar *psz1, const wxChar *psz2)
130 {
131 register wxChar c1, c2;
132 do {
133 c1 = wxTolower(*psz1++);
134 c2 = wxTolower(*psz2++);
135 } while ( c1 && (c1 == c2) );
136 return c1 - c2;
137 }
138 #endif
139
140 #ifndef wxStrtok
141 wxChar * WXDLLEXPORT wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_ptr)
142 {
143 if (!psz) psz = *save_ptr;
144 psz += wxStrspn(psz, delim);
145 if (!*psz) {
146 *save_ptr = (wxChar *)NULL;
147 return (wxChar *)NULL;
148 }
149 wxChar *ret = psz;
150 psz = wxStrpbrk(psz, delim);
151 if (!psz) *save_ptr = (wxChar*)NULL;
152 else {
153 *psz = _T('\0');
154 *save_ptr = psz + 1;
155 }
156 return ret;
157 }
158 #endif
159
160 #ifndef wxSetlocale
161 wxChar * WXDLLEXPORT wxSetlocale(int category, const wxChar *locale)
162 {
163 setlocale(category, wxConv_libc.cWX2MB(locale));
164 // FIXME
165 return (wxChar *)NULL;
166 }
167 #endif
168
169 #ifdef wxNEED_WX_STDIO_H
170 int WXDLLEXPORT wxPrintf(const wxChar *fmt, ...)
171 {
172 va_list argptr;
173 int ret;
174
175 va_start(argptr, fmt);
176 ret = wxVprintf(fmt, argptr);
177 va_end(argptr);
178 return ret;
179 }
180
181 int WXDLLEXPORT wxVprintf(const wxChar *fmt, va_list argptr)
182 {
183 wxString str;
184 str.PrintfV(fmt,argptr);
185 printf("%s", (const char*)str.mb_str());
186 return str.Len();
187 }
188
189 int WXDLLEXPORT wxFprintf(FILE *stream, const wxChar *fmt, ...)
190 {
191 va_list argptr;
192 int ret;
193
194 va_start(argptr, fmt);
195 ret = wxVfprintf(stream, fmt, argptr);
196 va_end(argptr);
197 return ret;
198 }
199
200 int WXDLLEXPORT wxVfprintf(FILE *stream, const wxChar *fmt, va_list argptr)
201 {
202 wxString str;
203 str.PrintfV(fmt,argptr);
204 fprintf(stream, "%s", (const char*)str.mb_str());
205 return str.Len();
206 }
207
208 int WXDLLEXPORT wxSprintf(wxChar *buf, const wxChar *fmt, ...)
209 {
210 va_list argptr;
211 int ret;
212
213 va_start(argptr, fmt);
214 ret = wxVsprintf(buf, fmt, argptr);
215 va_end(argptr);
216 return ret;
217 }
218
219 int WXDLLEXPORT wxVsprintf(wxChar *buf, const wxChar *fmt, va_list argptr)
220 {
221 // this might be sort of inefficient, but it doesn't matter since
222 // we'd prefer people to use wxString::Printf directly instead anyway
223 wxString str;
224 str.PrintfV(fmt,argptr);
225 wxStrcpy(buf,str.c_str());
226 return str.Len();
227 }
228
229 int WXDLLEXPORT wxSscanf(const wxChar *buf, const wxChar *fmt, ...)
230 {
231 va_list argptr;
232 int ret;
233
234 va_start(argptr, fmt);
235 ret = wxVsscanf(buf, fmt, argptr);
236 va_end(argptr);
237 return ret;
238 }
239
240 int WXDLLEXPORT wxVsscanf(const wxChar *buf, const wxChar *fmt, va_list argptr)
241 {
242 int ret;
243 // this will work only for numeric conversion! Strings will not be converted correctly
244 // hopefully this is all we'll need
245 ret = vsscanf(wxConv_libc.cWX2MB(buf), wxConv_libc.cWX2MB(fmt), argptr);
246 return ret;
247 }
248 #endif
249
250 #ifndef wxAtof
251 double WXDLLEXPORT wxAtof(const wxChar *psz)
252 {
253 return atof(wxConv_libc.cWX2MB(psz));
254 }
255 #endif
256
257 #ifdef wxNEED_WX_STDLIB_H
258 int WXDLLEXPORT wxAtoi(const wxChar *psz)
259 {
260 return atoi(wxConv_libc.cWX2MB(psz));
261 }
262
263 long WXDLLEXPORT wxAtol(const wxChar *psz)
264 {
265 return atol(wxConv_libc.cWX2MB(psz));
266 }
267
268 wxChar * WXDLLEXPORT wxGetenv(const wxChar *name)
269 {
270 static wxHashTable env;
271 // check if we already have stored the converted env var
272 wxObject *data = env.Get(name);
273 if (!data) {
274 // nope, retrieve it,
275 const char *val = getenv(wxConv_libc.cWX2MB(name));
276 if (!val) return (wxChar *)NULL;
277 // convert it,
278 data = (wxObject *)new wxString(val);
279 // and store it
280 env.Put(name, data);
281 }
282 // return converted env var
283 return (wxChar *)((wxString *)data)->c_str();
284 }
285
286 int WXDLLEXPORT wxSystem(const wxChar *psz)
287 {
288 return system(wxConv_libc.cWX2MB(psz));
289 }
290
291 #endif