]>
git.saurik.com Git - wxWidgets.git/blob - src/common/wxchar.cpp
38eb0aea22057f89752b81e618a2d911157ef630
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxChar implementation
8 // Copyright: (c) wxWindows copyright
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "wxchar.h"
16 // ===========================================================================
17 // headers, declarations, constants
18 // ===========================================================================
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #define _ISOC9X_SOURCE 1 // to get vsscanf()
28 #define _BSD_SOURCE 1 // to still get strdup()
37 #include "wx/wxchar.h"
38 #include "wx/string.h"
43 size_t WXDLLEXPORT
wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
47 if (n
) *buf
= _T('\0');
50 return mbstowcs(buf
, psz
, n
);
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).
56 // don't know if it's really needed (or if we can pass NULL), but better safe
59 return mbsrtowcs((wchar_t *) NULL
, &psz
, 0, &mbstate
);
61 return mbstowcs((wchar_t *) NULL
, psz
, 0);
65 size_t WXDLLEXPORT
wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
69 // glibc2.1 chokes on null input
73 return wcstombs(buf
, pwz
, n
);
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).
79 // don't know if it's really needed (or if we can pass NULL), but better safe
82 return wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
84 return wcstombs((char *) NULL
, pwz
, 0);
89 bool WXDLLEXPORT
wxOKlibc()
91 #if wxUSE_WCHAR_T && defined(__UNIX__) && defined(__GLIBC__)
92 // GNU libc uses UTF-8 even when it shouldn't
94 if ((MB_CUR_MAX
== 2) &&
95 (wxMB2WC(&res
, "\xdd\xa5", 1)>0) &&
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
110 wxChar
* WXDLLEXPORT
wxStrdup(const wxChar
*psz
)
112 size_t size
= (wxStrlen(psz
) + 1) * sizeof(wxChar
);
113 wxChar
*ret
= (wxChar
*) malloc(size
);
114 memcpy(ret
, psz
, size
);
120 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
122 register wxChar c1
, c2
;
124 c1
= wxTolower(*psz1
++);
125 c2
= wxTolower(*psz2
++);
126 } while ( c1
&& (c1
== c2
) );
132 wxChar
* WXDLLEXPORT
wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
134 if (!psz
) psz
= *save_ptr
;
135 psz
+= wxStrspn(psz
, delim
);
137 *save_ptr
= (wxChar
*)NULL
;
138 return (wxChar
*)NULL
;
141 psz
= wxStrpbrk(psz
, delim
);
142 if (!psz
) *save_ptr
= (wxChar
*)NULL
;
152 wxChar
* WXDLLEXPORT
wxSetlocale(int category
, const wxChar
*locale
)
154 setlocale(category
, wxConv_libc
.cWX2MB(locale
));
156 return (wxChar
*)NULL
;
160 #ifdef wxNEED_WX_STDIO_H
161 int WXDLLEXPORT
wxPrintf(const wxChar
*fmt
, ...)
166 va_start(argptr
, fmt
);
167 ret
= wxVprintf(fmt
, argptr
);
172 int WXDLLEXPORT
wxVprintf(const wxChar
*fmt
, va_list argptr
)
175 str
.PrintfV(fmt
,argptr
);
176 printf("%s", (const char*)str
.mb_str());
180 int WXDLLEXPORT
wxFprintf(FILE *stream
, const wxChar
*fmt
, ...)
185 va_start(argptr
, fmt
);
186 ret
= wxVfprintf(stream
, fmt
, argptr
);
191 int WXDLLEXPORT
wxVfprintf(FILE *stream
, const wxChar
*fmt
, va_list argptr
)
194 str
.PrintfV(fmt
,argptr
);
195 fprintf(stream
, "%s", (const char*)str
.mb_str());
199 int WXDLLEXPORT
wxSprintf(wxChar
*buf
, const wxChar
*fmt
, ...)
204 va_start(argptr
, fmt
);
205 ret
= wxVsprintf(buf
, fmt
, argptr
);
210 int WXDLLEXPORT
wxVsprintf(wxChar
*buf
, const wxChar
*fmt
, va_list argptr
)
212 // this might be sort of inefficient, but it doesn't matter since
213 // we'd prefer people to use wxString::Printf directly instead anyway
215 str
.PrintfV(fmt
,argptr
);
216 wxStrcpy(buf
,str
.c_str());
220 int WXDLLEXPORT
wxSscanf(const wxChar
*buf
, const wxChar
*fmt
, ...)
225 va_start(argptr
, fmt
);
226 ret
= wxVsscanf(buf
, fmt
, argptr
);
231 int WXDLLEXPORT
wxVsscanf(const wxChar
*buf
, const wxChar
*fmt
, va_list argptr
)
234 // this will work only for numeric conversion! Strings will not be converted correctly
235 // hopefully this is all we'll need
236 ret
= vsscanf(wxConv_libc
.cWX2MB(buf
), wxConv_libc
.cWX2MB(fmt
), argptr
);
242 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
244 return atof(wxConv_libc
.cWX2MB(psz
));
248 #ifdef wxNEED_WX_STDLIB_H
249 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
251 return atoi(wxConv_libc
.cWX2MB(psz
));
254 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
256 return atol(wxConv_libc
.cWX2MB(psz
));
259 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
261 static wxHashTable env
;
262 // check if we already have stored the converted env var
263 wxObject
*data
= env
.Get(name
);
265 // nope, retrieve it,
266 const char *val
= getenv(wxConv_libc
.cWX2MB(name
));
267 if (!val
) return (wxChar
*)NULL
;
269 data
= (wxObject
*)new wxString(val
);
273 // return converted env var
274 return (wxChar
*)((wxString
*)data
)->c_str();
277 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
279 return system(wxConv_libc
.cWX2MB(psz
));