]>
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"
42 size_t wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
46 if (n
) *buf
= _T('\0');
49 return mbstowcs(buf
, psz
, n
);
52 // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't
53 // honor the 3rd parameter, thus it will happily crash here).
55 // don't know if it's really needed (or if we can pass NULL), but better safe
58 return mbsrtowcs((wchar_t *) NULL
, &psz
, 0, &mbstate
);
60 return mbstowcs((wchar_t *) NULL
, psz
, 0);
64 size_t wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
68 // glibc2.1 chokes on null input
72 return wcstombs(buf
, pwz
, n
);
75 // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't
76 // honor the 3rd parameter, thus it will happily crash here).
78 // don't know if it's really needed (or if we can pass NULL), but better safe
81 return wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
83 return wcstombs((char *) NULL
, pwz
, 0);
88 wxChar
* WXDLLEXPORT
wxStrdup(const wxChar
*psz
)
90 size_t size
= (wxStrlen(psz
) + 1) * sizeof(wxChar
);
91 wxChar
*ret
= (wxChar
*) malloc(size
);
92 memcpy(ret
, psz
, size
);
98 wxChar
* WXDLLEXPORT
wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
100 if (!psz
) psz
= *save_ptr
;
101 psz
+= wxStrspn(psz
, delim
);
103 *save_ptr
= (wxChar
*)NULL
;
104 return (wxChar
*)NULL
;
107 psz
= wxStrpbrk(psz
, delim
);
108 if (!psz
) *save_ptr
= (wxChar
*)NULL
;
118 wxChar
* WXDLLEXPORT
wxSetlocale(int category
, const wxChar
*locale
)
120 setlocale(category
, wxConv_libc
.cWX2MB(locale
));
122 return (wxChar
*)NULL
;
126 #ifdef wxNEED_WX_STDIO_H
127 int WXDLLEXPORT
wxPrintf(const wxChar
*fmt
, ...)
132 va_start(argptr
, fmt
);
133 ret
= wxVprintf(fmt
, argptr
);
138 int WXDLLEXPORT
wxVprintf(const wxChar
*fmt
, va_list argptr
)
141 str
.PrintfV(fmt
,argptr
);
142 printf("%s", (const char*)str
.mb_str());
146 int WXDLLEXPORT
wxFprintf(FILE *stream
, const wxChar
*fmt
, ...)
151 va_start(argptr
, fmt
);
152 ret
= wxVfprintf(stream
, fmt
, argptr
);
157 int WXDLLEXPORT
wxVfprintf(FILE *stream
, const wxChar
*fmt
, va_list argptr
)
160 str
.PrintfV(fmt
,argptr
);
161 fprintf(stream
, "%s", (const char*)str
.mb_str());
165 int WXDLLEXPORT
wxSprintf(wxChar
*buf
, const wxChar
*fmt
, ...)
170 va_start(argptr
, fmt
);
171 ret
= wxVsprintf(buf
, fmt
, argptr
);
176 int WXDLLEXPORT
wxVsprintf(wxChar
*buf
, const wxChar
*fmt
, va_list argptr
)
178 // this might be sort of inefficient, but it doesn't matter since
179 // we'd prefer people to use wxString::Printf directly instead anyway
181 str
.PrintfV(fmt
,argptr
);
182 wxStrcpy(buf
,str
.c_str());
186 int WXDLLEXPORT
wxSscanf(const wxChar
*buf
, const wxChar
*fmt
, ...)
191 va_start(argptr
, fmt
);
192 ret
= wxVsscanf(buf
, fmt
, argptr
);
197 int WXDLLEXPORT
wxVsscanf(const wxChar
*buf
, const wxChar
*fmt
, va_list argptr
)
200 // this will work only for numeric conversion! Strings will not be converted correctly
201 // hopefully this is all we'll need
202 ret
= vsscanf(wxConv_libc
.cWX2MB(buf
), wxConv_libc
.cWX2MB(fmt
), argptr
);
208 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
210 return atof(wxConv_libc
.cWX2MB(psz
));
214 #ifdef wxNEED_WX_STDLIB_H
215 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
217 return atoi(wxConv_libc
.cWX2MB(psz
));
220 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
222 return atol(wxConv_libc
.cWX2MB(psz
));
225 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
227 static wxHashTable env
;
228 // check if we already have stored the converted env var
229 wxObject
*data
= env
.Get(name
);
231 // nope, retrieve it,
232 const char *val
= getenv(wxConv_libc
.cWX2MB(name
));
233 if (!val
) return (wxChar
*)NULL
;
235 data
= (wxObject
*)new wxString(val
);
239 // return converted env var
240 return (wxChar
*)((wxString
*)data
)->c_str();
243 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
245 return system(wxConv_libc
.cWX2MB(psz
));