]>
git.saurik.com Git - wxWidgets.git/blob - src/common/wxchar.cpp
1307daaf50d4243ae6dcf336f2f765a62e8185f1
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"
34 #include "wx/wxchar.h"
35 #include "wx/string.h"
39 size_t wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
42 return mbstowcs(buf
, psz
, n
);
45 // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't
46 // honor the 3rd parameter, thus it will happily crash here).
48 // don't know if it's really needed (or if we can pass NULL), but better safe
51 return mbsrtowcs((wchar_t *) NULL
, &psz
, 0, &mbstate
);
53 return mbstowcs((wchar_t *) NULL
, psz
, 0);
57 size_t wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
60 return wcstombs(buf
, pwz
, n
);
63 // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't
64 // honor the 3rd parameter, thus it will happily crash here).
66 // don't know if it's really needed (or if we can pass NULL), but better safe
69 return wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
71 return wcstombs((char *) NULL
, pwz
, 0);
76 wxChar
* WXDLLEXPORT
wxStrdup(const wxChar
*psz
)
78 size_t size
= (wxStrlen(psz
) + 1) * sizeof(wxChar
);
79 wxChar
*ret
= (wxChar
*) malloc(size
);
80 memcpy(ret
, psz
, size
);
86 wxChar
* WXDLLEXPORT
wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
88 if (!psz
) psz
= *save_ptr
;
89 psz
+= wxStrspn(psz
, delim
);
91 *save_ptr
= (wxChar
*)NULL
;
92 return (wxChar
*)NULL
;
95 psz
= wxStrpbrk(psz
, delim
);
96 if (!psz
) *save_ptr
= (wxChar
*)NULL
;
106 wxChar
* WXDLLEXPORT
wxSetlocale(int category
, const wxChar
*locale
)
108 setlocale(category
, wxConv_libc
.cWX2MB(locale
));
110 return (wxChar
*)NULL
;
114 #ifdef wxNEED_WX_STDIO_H
115 int WXDLLEXPORT
wxPrintf(const wxChar
*fmt
, ...)
120 va_start(argptr
, fmt
);
121 ret
= wxVprintf(fmt
, argptr
);
126 int WXDLLEXPORT
wxVprintf(const wxChar
*fmt
, va_list argptr
)
129 str
.PrintfV(fmt
,argptr
);
130 printf("%s", (const char*)str
.mb_str());
134 int WXDLLEXPORT
wxFprintf(FILE *stream
, const wxChar
*fmt
, ...)
139 va_start(argptr
, fmt
);
140 ret
= wxFvprintf(stream
, fmt
, argptr
);
145 int WXDLLEXPORT
wxFvprintf(FILE *stream
, const wxChar
*fmt
, va_list argptr
)
148 str
.PrintfV(fmt
,argptr
);
149 fprintf(stream
, "%s", (const char*)str
.mb_str());
153 int WXDLLEXPORT
wxSprintf(wxChar
*buf
, const wxChar
*fmt
, ...)
158 va_start(argptr
, fmt
);
159 ret
= wxVsprintf(buf
, fmt
, argptr
);
164 int WXDLLEXPORT
wxVsprintf(wxChar
*buf
, const wxChar
*fmt
, va_list argptr
)
166 // this might be sort of inefficient, but it doesn't matter since
167 // we'd prefer people to use wxString::Printf directly instead anyway
169 str
.PrintfV(fmt
,argptr
);
170 wxStrcpy(buf
,str
.c_str());
176 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
178 return atof(wxConv_libc
.cWX2MB(psz
));
182 #ifdef wxNEED_WX_STDLIB_H
183 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
185 return atoi(wxConv_libc
.cWX2MB(psz
));
188 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
190 return atol(wxConv_libc
.cWX2MB(psz
));
193 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
195 static wxHashTable env
;
196 // check if we already have stored the converted env var
197 wxObject
*data
= env
.Get(name
);
199 // nope, retrieve it,
200 const char *val
= getenv(wxConv_libc
.cWX2MB(name
));
201 if (!val
) return (wxChar
*)NULL
;
203 data
= (wxObject
*)new wxString(val
);
207 // return converted env var
208 return (wxChar
*)((wxString
*)data
)->c_str();
211 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
213 return system(wxConv_libc
.cWX2MB(psz
));