]>
git.saurik.com Git - wxWidgets.git/blob - src/common/wxchar.cpp
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 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 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);
90 wxChar
* WXDLLEXPORT
wxStrdup(const wxChar
*psz
)
92 size_t size
= (wxStrlen(psz
) + 1) * sizeof(wxChar
);
93 wxChar
*ret
= (wxChar
*) malloc(size
);
94 memcpy(ret
, psz
, size
);
100 int WXDLLEXPORT
wxStricmp(const wxChar
*psz1
, const wxChar
*psz2
)
102 register wxChar c1
, c2
;
104 c1
= wxTolower(*psz1
++);
105 c2
= wxTolower(*psz2
++);
106 } while ( c1
&& (c1
== c2
) );
112 wxChar
* WXDLLEXPORT
wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
114 if (!psz
) psz
= *save_ptr
;
115 psz
+= wxStrspn(psz
, delim
);
117 *save_ptr
= (wxChar
*)NULL
;
118 return (wxChar
*)NULL
;
121 psz
= wxStrpbrk(psz
, delim
);
122 if (!psz
) *save_ptr
= (wxChar
*)NULL
;
132 wxChar
* WXDLLEXPORT
wxSetlocale(int category
, const wxChar
*locale
)
134 setlocale(category
, wxConv_libc
.cWX2MB(locale
));
136 return (wxChar
*)NULL
;
140 #ifdef wxNEED_WX_STDIO_H
141 int WXDLLEXPORT
wxPrintf(const wxChar
*fmt
, ...)
146 va_start(argptr
, fmt
);
147 ret
= wxVprintf(fmt
, argptr
);
152 int WXDLLEXPORT
wxVprintf(const wxChar
*fmt
, va_list argptr
)
155 str
.PrintfV(fmt
,argptr
);
156 printf("%s", (const char*)str
.mb_str());
160 int WXDLLEXPORT
wxFprintf(FILE *stream
, const wxChar
*fmt
, ...)
165 va_start(argptr
, fmt
);
166 ret
= wxVfprintf(stream
, fmt
, argptr
);
171 int WXDLLEXPORT
wxVfprintf(FILE *stream
, const wxChar
*fmt
, va_list argptr
)
174 str
.PrintfV(fmt
,argptr
);
175 fprintf(stream
, "%s", (const char*)str
.mb_str());
179 int WXDLLEXPORT
wxSprintf(wxChar
*buf
, const wxChar
*fmt
, ...)
184 va_start(argptr
, fmt
);
185 ret
= wxVsprintf(buf
, fmt
, argptr
);
190 int WXDLLEXPORT
wxVsprintf(wxChar
*buf
, const wxChar
*fmt
, va_list argptr
)
192 // this might be sort of inefficient, but it doesn't matter since
193 // we'd prefer people to use wxString::Printf directly instead anyway
195 str
.PrintfV(fmt
,argptr
);
196 wxStrcpy(buf
,str
.c_str());
200 int WXDLLEXPORT
wxSscanf(const wxChar
*buf
, const wxChar
*fmt
, ...)
205 va_start(argptr
, fmt
);
206 ret
= wxVsscanf(buf
, fmt
, argptr
);
211 int WXDLLEXPORT
wxVsscanf(const wxChar
*buf
, const wxChar
*fmt
, va_list argptr
)
214 // this will work only for numeric conversion! Strings will not be converted correctly
215 // hopefully this is all we'll need
216 ret
= vsscanf(wxConv_libc
.cWX2MB(buf
), wxConv_libc
.cWX2MB(fmt
), argptr
);
222 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
224 return atof(wxConv_libc
.cWX2MB(psz
));
228 #ifdef wxNEED_WX_STDLIB_H
229 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
231 return atoi(wxConv_libc
.cWX2MB(psz
));
234 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
236 return atol(wxConv_libc
.cWX2MB(psz
));
239 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
241 static wxHashTable env
;
242 // check if we already have stored the converted env var
243 wxObject
*data
= env
.Get(name
);
245 // nope, retrieve it,
246 const char *val
= getenv(wxConv_libc
.cWX2MB(name
));
247 if (!val
) return (wxChar
*)NULL
;
249 data
= (wxObject
*)new wxString(val
);
253 // return converted env var
254 return (wxChar
*)((wxString
*)data
)->c_str();
257 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
259 return system(wxConv_libc
.cWX2MB(psz
));