]> git.saurik.com Git - wxWidgets.git/blob - src/common/wxchar.cpp
Small bugfix for wxString::Printf regarding %hs...
[wxWidgets.git] / src / common / wxchar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wxchar.cpp
3 // Purpose: wxChar implementation
4 // Author: Ove Kåven
5 // Modified by:
6 // Created: 09/04/99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWindows copyright
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "wxchar.h"
14 #endif
15
16 // ===========================================================================
17 // headers, declarations, constants
18 // ===========================================================================
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #define _ISOC9X_SOURCE 1 // to get vsscanf()
28 #define _BSD_SOURCE 1 // to still get strdup()
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <locale.h>
34
35 #ifndef WX_PRECOMP
36 #include "wx/defs.h"
37 #include "wx/wxchar.h"
38 #include "wx/string.h"
39 #include "wx/hash.h"
40 #endif
41
42 #if wxUSE_WCHAR_T
43 size_t wxMB2WC(wchar_t *buf, const char *psz, size_t n)
44 {
45 if (buf) {
46 if (!n || !*psz) {
47 if (n) *buf = _T('\0');
48 return 0;
49 }
50 return mbstowcs(buf, psz, n);
51 }
52
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).
55 #if wxUSE_WCSRTOMBS
56 // don't know if it's really needed (or if we can pass NULL), but better safe
57 // than quick
58 mbstate_t mbstate;
59 return mbsrtowcs((wchar_t *) NULL, &psz, 0, &mbstate);
60 #else // !GNU libc
61 return mbstowcs((wchar_t *) NULL, psz, 0);
62 #endif // GNU
63 }
64
65 size_t wxWC2MB(char *buf, const wchar_t *pwz, size_t n)
66 {
67 if (buf) {
68 if (!n || !*pwz) {
69 // glibc2.1 chokes on null input
70 if (n) *buf = '\0';
71 return 0;
72 }
73 return wcstombs(buf, pwz, n);
74 }
75
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).
78 #if wxUSE_WCSRTOMBS
79 // don't know if it's really needed (or if we can pass NULL), but better safe
80 // than quick
81 mbstate_t mbstate;
82 return wcsrtombs((char *) NULL, &pwz, 0, &mbstate);
83 #else // !GNU libc
84 return wcstombs((char *) NULL, pwz, 0);
85 #endif // GNU
86 }
87 #endif
88
89 #ifndef wxStrdup
90 wxChar * WXDLLEXPORT wxStrdup(const wxChar *psz)
91 {
92 size_t size = (wxStrlen(psz) + 1) * sizeof(wxChar);
93 wxChar *ret = (wxChar *) malloc(size);
94 memcpy(ret, psz, size);
95 return ret;
96 }
97 #endif
98
99 #ifndef wxStricmp
100 int WXDLLEXPORT wxStricmp(const wxChar *psz1, const wxChar *psz2)
101 {
102 register wxChar c1, c2;
103 do {
104 c1 = wxTolower(*psz1++);
105 c2 = wxTolower(*psz2++);
106 } while ( c1 && (c1 == c2) );
107 return c1 - c2;
108 }
109 #endif
110
111 #ifndef wxStrtok
112 wxChar * WXDLLEXPORT wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_ptr)
113 {
114 if (!psz) psz = *save_ptr;
115 psz += wxStrspn(psz, delim);
116 if (!*psz) {
117 *save_ptr = (wxChar *)NULL;
118 return (wxChar *)NULL;
119 }
120 wxChar *ret = psz;
121 psz = wxStrpbrk(psz, delim);
122 if (!psz) *save_ptr = (wxChar*)NULL;
123 else {
124 *psz = _T('\0');
125 *save_ptr = psz + 1;
126 }
127 return ret;
128 }
129 #endif
130
131 #ifndef wxSetlocale
132 wxChar * WXDLLEXPORT wxSetlocale(int category, const wxChar *locale)
133 {
134 setlocale(category, wxConv_libc.cWX2MB(locale));
135 // FIXME
136 return (wxChar *)NULL;
137 }
138 #endif
139
140 #ifdef wxNEED_WX_STDIO_H
141 int WXDLLEXPORT wxPrintf(const wxChar *fmt, ...)
142 {
143 va_list argptr;
144 int ret;
145
146 va_start(argptr, fmt);
147 ret = wxVprintf(fmt, argptr);
148 va_end(argptr);
149 return ret;
150 }
151
152 int WXDLLEXPORT wxVprintf(const wxChar *fmt, va_list argptr)
153 {
154 wxString str;
155 str.PrintfV(fmt,argptr);
156 printf("%s", (const char*)str.mb_str());
157 return str.Len();
158 }
159
160 int WXDLLEXPORT wxFprintf(FILE *stream, const wxChar *fmt, ...)
161 {
162 va_list argptr;
163 int ret;
164
165 va_start(argptr, fmt);
166 ret = wxVfprintf(stream, fmt, argptr);
167 va_end(argptr);
168 return ret;
169 }
170
171 int WXDLLEXPORT wxVfprintf(FILE *stream, const wxChar *fmt, va_list argptr)
172 {
173 wxString str;
174 str.PrintfV(fmt,argptr);
175 fprintf(stream, "%s", (const char*)str.mb_str());
176 return str.Len();
177 }
178
179 int WXDLLEXPORT wxSprintf(wxChar *buf, const wxChar *fmt, ...)
180 {
181 va_list argptr;
182 int ret;
183
184 va_start(argptr, fmt);
185 ret = wxVsprintf(buf, fmt, argptr);
186 va_end(argptr);
187 return ret;
188 }
189
190 int WXDLLEXPORT wxVsprintf(wxChar *buf, const wxChar *fmt, va_list argptr)
191 {
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
194 wxString str;
195 str.PrintfV(fmt,argptr);
196 wxStrcpy(buf,str.c_str());
197 return str.Len();
198 }
199
200 int WXDLLEXPORT wxSscanf(const wxChar *buf, const wxChar *fmt, ...)
201 {
202 va_list argptr;
203 int ret;
204
205 va_start(argptr, fmt);
206 ret = wxVsscanf(buf, fmt, argptr);
207 va_end(argptr);
208 return ret;
209 }
210
211 int WXDLLEXPORT wxVsscanf(const wxChar *buf, const wxChar *fmt, va_list argptr)
212 {
213 int ret;
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);
217 return ret;
218 }
219 #endif
220
221 #ifndef wxAtof
222 double WXDLLEXPORT wxAtof(const wxChar *psz)
223 {
224 return atof(wxConv_libc.cWX2MB(psz));
225 }
226 #endif
227
228 #ifdef wxNEED_WX_STDLIB_H
229 int WXDLLEXPORT wxAtoi(const wxChar *psz)
230 {
231 return atoi(wxConv_libc.cWX2MB(psz));
232 }
233
234 long WXDLLEXPORT wxAtol(const wxChar *psz)
235 {
236 return atol(wxConv_libc.cWX2MB(psz));
237 }
238
239 wxChar * WXDLLEXPORT wxGetenv(const wxChar *name)
240 {
241 static wxHashTable env;
242 // check if we already have stored the converted env var
243 wxObject *data = env.Get(name);
244 if (!data) {
245 // nope, retrieve it,
246 const char *val = getenv(wxConv_libc.cWX2MB(name));
247 if (!val) return (wxChar *)NULL;
248 // convert it,
249 data = (wxObject *)new wxString(val);
250 // and store it
251 env.Put(name, data);
252 }
253 // return converted env var
254 return (wxChar *)((wxString *)data)->c_str();
255 }
256
257 int WXDLLEXPORT wxSystem(const wxChar *psz)
258 {
259 return system(wxConv_libc.cWX2MB(psz));
260 }
261
262 #endif