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