]> git.saurik.com Git - wxWidgets.git/blob - src/common/wxchar.cpp
Fixed typo
[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 size_t wxMB2WC(wchar_t *buf, const char *psz, size_t n)
43 {
44 if (buf) {
45 return mbstowcs(buf, psz, n);
46 }
47
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).
50 #if wxUSE_WCSRTOMBS
51 // don't know if it's really needed (or if we can pass NULL), but better safe
52 // than quick
53 mbstate_t mbstate;
54 return mbsrtowcs((wchar_t *) NULL, &psz, 0, &mbstate);
55 #else // !GNU libc
56 return mbstowcs((wchar_t *) NULL, psz, 0);
57 #endif // GNU
58 }
59
60 size_t wxWC2MB(char *buf, const wchar_t *pwz, size_t n)
61 {
62 if (buf) {
63 return wcstombs(buf, pwz, n);
64 }
65
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).
68 #if wxUSE_WCSRTOMBS
69 // don't know if it's really needed (or if we can pass NULL), but better safe
70 // than quick
71 mbstate_t mbstate;
72 return wcsrtombs((char *) NULL, &pwz, 0, &mbstate);
73 #else // !GNU libc
74 return wcstombs((char *) NULL, pwz, 0);
75 #endif // GNU
76 }
77
78 #ifndef wxStrdup
79 wxChar * WXDLLEXPORT wxStrdup(const wxChar *psz)
80 {
81 size_t size = (wxStrlen(psz) + 1) * sizeof(wxChar);
82 wxChar *ret = (wxChar *) malloc(size);
83 memcpy(ret, psz, size);
84 return ret;
85 }
86 #endif
87
88 #ifndef wxStrtok
89 wxChar * WXDLLEXPORT wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_ptr)
90 {
91 if (!psz) psz = *save_ptr;
92 psz += wxStrspn(psz, delim);
93 if (!*psz) {
94 *save_ptr = (wxChar *)NULL;
95 return (wxChar *)NULL;
96 }
97 wxChar *ret = psz;
98 psz = wxStrpbrk(psz, delim);
99 if (!psz) *save_ptr = (wxChar*)NULL;
100 else {
101 *psz = _T('\0');
102 *save_ptr = psz + 1;
103 }
104 return ret;
105 }
106 #endif
107
108 #ifndef wxSetlocale
109 wxChar * WXDLLEXPORT wxSetlocale(int category, const wxChar *locale)
110 {
111 setlocale(category, wxConv_libc.cWX2MB(locale));
112 // FIXME
113 return (wxChar *)NULL;
114 }
115 #endif
116
117 #ifdef wxNEED_WX_STDIO_H
118 int WXDLLEXPORT wxPrintf(const wxChar *fmt, ...)
119 {
120 va_list argptr;
121 int ret;
122
123 va_start(argptr, fmt);
124 ret = wxVprintf(fmt, argptr);
125 va_end(argptr);
126 return ret;
127 }
128
129 int WXDLLEXPORT wxVprintf(const wxChar *fmt, va_list argptr)
130 {
131 wxString str;
132 str.PrintfV(fmt,argptr);
133 printf("%s", (const char*)str.mb_str());
134 return str.Len();
135 }
136
137 int WXDLLEXPORT wxFprintf(FILE *stream, const wxChar *fmt, ...)
138 {
139 va_list argptr;
140 int ret;
141
142 va_start(argptr, fmt);
143 ret = wxVfprintf(stream, fmt, argptr);
144 va_end(argptr);
145 return ret;
146 }
147
148 int WXDLLEXPORT wxVfprintf(FILE *stream, const wxChar *fmt, va_list argptr)
149 {
150 wxString str;
151 str.PrintfV(fmt,argptr);
152 fprintf(stream, "%s", (const char*)str.mb_str());
153 return str.Len();
154 }
155
156 int WXDLLEXPORT wxSprintf(wxChar *buf, const wxChar *fmt, ...)
157 {
158 va_list argptr;
159 int ret;
160
161 va_start(argptr, fmt);
162 ret = wxVsprintf(buf, fmt, argptr);
163 va_end(argptr);
164 return ret;
165 }
166
167 int WXDLLEXPORT wxVsprintf(wxChar *buf, const wxChar *fmt, va_list argptr)
168 {
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
171 wxString str;
172 str.PrintfV(fmt,argptr);
173 wxStrcpy(buf,str.c_str());
174 return str.Len();
175 }
176
177 int WXDLLEXPORT wxSscanf(const wxChar *buf, const wxChar *fmt, ...)
178 {
179 va_list argptr;
180 int ret;
181
182 va_start(argptr, fmt);
183 ret = wxVsscanf(buf, fmt, argptr);
184 va_end(argptr);
185 return ret;
186 }
187
188 int WXDLLEXPORT wxVsscanf(const wxChar *buf, const wxChar *fmt, va_list argptr)
189 {
190 int ret;
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);
194 return ret;
195 }
196 #endif
197
198 #ifndef wxAtof
199 double WXDLLEXPORT wxAtof(const wxChar *psz)
200 {
201 return atof(wxConv_libc.cWX2MB(psz));
202 }
203 #endif
204
205 #ifdef wxNEED_WX_STDLIB_H
206 int WXDLLEXPORT wxAtoi(const wxChar *psz)
207 {
208 return atoi(wxConv_libc.cWX2MB(psz));
209 }
210
211 long WXDLLEXPORT wxAtol(const wxChar *psz)
212 {
213 return atol(wxConv_libc.cWX2MB(psz));
214 }
215
216 wxChar * WXDLLEXPORT wxGetenv(const wxChar *name)
217 {
218 static wxHashTable env;
219 // check if we already have stored the converted env var
220 wxObject *data = env.Get(name);
221 if (!data) {
222 // nope, retrieve it,
223 const char *val = getenv(wxConv_libc.cWX2MB(name));
224 if (!val) return (wxChar *)NULL;
225 // convert it,
226 data = (wxObject *)new wxString(val);
227 // and store it
228 env.Put(name, data);
229 }
230 // return converted env var
231 return (wxChar *)((wxString *)data)->c_str();
232 }
233
234 int WXDLLEXPORT wxSystem(const wxChar *psz)
235 {
236 return system(wxConv_libc.cWX2MB(psz));
237 }
238
239 #endif