]>
git.saurik.com Git - wxWidgets.git/blob - src/common/wxchar.cpp
3f70fe66dc397e61c019474d0b3b106c7764a81e
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
)
45 return mbstowcs(buf
, psz
, n
);
48 // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't
49 // honor the 3rd parameter, thus it will happily crash here).
51 // don't know if it's really needed (or if we can pass NULL), but better safe
54 return mbsrtowcs((wchar_t *) NULL
, &psz
, 0, &mbstate
);
56 return mbstowcs((wchar_t *) NULL
, psz
, 0);
60 size_t wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
63 return wcstombs(buf
, pwz
, n
);
66 // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't
67 // honor the 3rd parameter, thus it will happily crash here).
69 // don't know if it's really needed (or if we can pass NULL), but better safe
72 return wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
74 return wcstombs((char *) NULL
, pwz
, 0);
79 wxChar
* WXDLLEXPORT
wxStrdup(const wxChar
*psz
)
81 size_t size
= (wxStrlen(psz
) + 1) * sizeof(wxChar
);
82 wxChar
*ret
= (wxChar
*) malloc(size
);
83 memcpy(ret
, psz
, size
);
89 wxChar
* WXDLLEXPORT
wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
91 if (!psz
) psz
= *save_ptr
;
92 psz
+= wxStrspn(psz
, delim
);
94 *save_ptr
= (wxChar
*)NULL
;
95 return (wxChar
*)NULL
;
98 psz
= wxStrpbrk(psz
, delim
);
99 if (!psz
) *save_ptr
= (wxChar
*)NULL
;
109 wxChar
* WXDLLEXPORT
wxSetlocale(int category
, const wxChar
*locale
)
111 setlocale(category
, wxConv_libc
.cWX2MB(locale
));
113 return (wxChar
*)NULL
;
117 #ifdef wxNEED_WX_STDIO_H
118 int WXDLLEXPORT
wxPrintf(const wxChar
*fmt
, ...)
123 va_start(argptr
, fmt
);
124 ret
= wxVprintf(fmt
, argptr
);
129 int WXDLLEXPORT
wxVprintf(const wxChar
*fmt
, va_list argptr
)
132 str
.PrintfV(fmt
,argptr
);
133 printf("%s", (const char*)str
.mb_str());
137 int WXDLLEXPORT
wxFprintf(FILE *stream
, const wxChar
*fmt
, ...)
142 va_start(argptr
, fmt
);
143 ret
= wxVfprintf(stream
, fmt
, argptr
);
148 int WXDLLEXPORT
wxFvprintf(FILE *stream
, const wxChar
*fmt
, va_list argptr
)
151 str
.PrintfV(fmt
,argptr
);
152 fprintf(stream
, "%s", (const char*)str
.mb_str());
156 int WXDLLEXPORT
wxSprintf(wxChar
*buf
, const wxChar
*fmt
, ...)
161 va_start(argptr
, fmt
);
162 ret
= wxVsprintf(buf
, fmt
, argptr
);
167 int WXDLLEXPORT
wxVsprintf(wxChar
*buf
, const wxChar
*fmt
, va_list argptr
)
169 // this might be sort of inefficient, but it doesn't matter since
170 // we'd prefer people to use wxString::Printf directly instead anyway
172 str
.PrintfV(fmt
,argptr
);
173 wxStrcpy(buf
,str
.c_str());
177 int WXDLLEXPORT
wxSscanf(const wxChar
*buf
, const wxChar
*fmt
, ...)
182 va_start(argptr
, fmt
);
183 ret
= wxVsscanf(buf
, fmt
, argptr
);
188 int WXDLLEXPORT
wxVsscanf(const wxChar
*buf
, const wxChar
*fmt
, va_list argptr
)
191 // this will work only for numeric conversion! Strings will not be converted correctly
192 // hopefully this is all we'll need
193 ret
= vsscanf(wxConv_libc
.cWX2MB(buf
), wxConv_libc
.cWX2MB(fmt
), argptr
);
199 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
201 return atof(wxConv_libc
.cWX2MB(psz
));
205 #ifdef wxNEED_WX_STDLIB_H
206 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
208 return atoi(wxConv_libc
.cWX2MB(psz
));
211 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
213 return atol(wxConv_libc
.cWX2MB(psz
));
216 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
218 static wxHashTable env
;
219 // check if we already have stored the converted env var
220 wxObject
*data
= env
.Get(name
);
222 // nope, retrieve it,
223 const char *val
= getenv(wxConv_libc
.cWX2MB(name
));
224 if (!val
) return (wxChar
*)NULL
;
226 data
= (wxObject
*)new wxString(val
);
230 // return converted env var
231 return (wxChar
*)((wxString
*)data
)->c_str();
234 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
236 return system(wxConv_libc
.cWX2MB(psz
));