]> git.saurik.com Git - wxWidgets.git/blob - src/common/wxchar.cpp
Another one bites the dust.
[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 <stdlib.h>
28 #include <string.h>
29 #include <locale.h>
30
31 #ifndef WX_PRECOMP
32 #include "wx/defs.h"
33 #include "wx/wxchar.h"
34 #include "wx/string.h"
35 #include "wx/hash.h"
36 #endif
37
38 size_t wxMB2WC(wchar_t *buf, const char *psz, size_t n)
39 {
40 if (buf) {
41 return mbstowcs(buf, psz, n);
42 }
43
44 // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't
45 // honor the 3rd parameter, thus it will happily crash here).
46 #if wxUSE_WCSRTOMBS
47 // don't know if it's really needed (or if we can pass NULL), but better safe
48 // than quick
49 mbstate_t mbstate;
50 return mbsrtowcs((wchar_t *) NULL, &psz, 0, &mbstate);
51 #else // !GNU libc
52 return mbstowcs((wchar_t *) NULL, psz, 0);
53 #endif // GNU
54 }
55
56 size_t wxWC2MB(char *buf, const wchar_t *pwz, size_t n)
57 {
58 if (buf) {
59 return wcstombs(buf, pwz, n);
60 }
61
62 // NB: GNU libc5 wcstombs() is completely broken, don't use it (it doesn't
63 // honor the 3rd parameter, thus it will happily crash here).
64 #if wxUSE_WCSRTOMBS
65 // don't know if it's really needed (or if we can pass NULL), but better safe
66 // than quick
67 mbstate_t mbstate;
68 return wcsrtombs((char *) NULL, &pwz, 0, &mbstate);
69 #else // !GNU libc
70 return wcstombs((char *) NULL, pwz, 0);
71 #endif // GNU
72 }
73
74 #ifndef wxStrdup
75 wxChar * WXDLLEXPORT wxStrdup(const wxChar *psz)
76 {
77 size_t size = (wxStrlen(psz) + 1) * sizeof(wxChar);
78 wxChar *ret = (wxChar *) malloc(size);
79 memcpy(ret, psz, size);
80 return ret;
81 }
82 #endif
83
84 #ifndef wxStrtok
85 wxChar * WXDLLEXPORT wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_ptr)
86 {
87 if (!psz) psz = *save_ptr;
88 psz += wxStrspn(psz, delim);
89 if (!*psz) {
90 *save_ptr = (wxChar *)NULL;
91 return (wxChar *)NULL;
92 }
93 wxChar *ret = psz;
94 psz = wxStrpbrk(psz, delim);
95 if (!psz) *save_ptr = (wxChar*)NULL;
96 else {
97 *psz = _T('\0');
98 *save_ptr = psz + 1;
99 }
100 return ret;
101 }
102 #endif
103
104 #ifndef wxSetlocale
105 wxChar * WXDLLEXPORT wxSetlocale(int category, const wxChar *locale)
106 {
107 setlocale(category, wxConv_libc.cWX2MB(locale));
108 // FIXME
109 return (wxChar *)NULL;
110 }
111 #endif
112
113 #ifdef wxNEED_WX_STDIO_H
114 int WXDLLEXPORT wxSprintf(wxChar *buf, const wxChar *fmt, ...)
115 {
116 va_list argptr;
117 int ret;
118
119 va_start(argptr, fmt);
120 ret = wxVsprintf(buf, fmt, argptr);
121 va_end(argptr);
122 return ret;
123 }
124
125 int WXDLLEXPORT wxVsprintf(wxChar *buf, const wxChar *fmt, va_list argptr)
126 {
127 // this might be sort of inefficient, but it doesn't matter since
128 // we'd prefer people to use wxString::Printf directly instead anyway
129 wxString str;
130 str.PrintfV(fmt,argptr);
131 wxStrcpy(buf,str.c_str());
132 return str.Len();
133 }
134 #endif
135
136 #ifndef wxAtof
137 double WXDLLEXPORT wxAtof(const wxChar *psz)
138 {
139 return atof(wxConv_libc.cWX2MB(psz));
140 }
141 #endif
142
143 #ifdef wxNEED_WX_STDLIB_H
144 int WXDLLEXPORT wxAtoi(const wxChar *psz)
145 {
146 return atoi(wxConv_libc.cWX2MB(psz));
147 }
148
149 long WXDLLEXPORT wxAtol(const wxChar *psz)
150 {
151 return atol(wxConv_libc.cWX2MB(psz));
152 }
153
154 wxChar * WXDLLEXPORT wxGetenv(const wxChar *name)
155 {
156 static wxHashTable env;
157 // check if we already have stored the converted env var
158 wxObject *data = env.Get(name);
159 if (!data) {
160 // nope, retrieve it,
161 const char *val = getenv(wxConv_libc.cWX2MB(name));
162 if (!val) return (wxChar *)NULL;
163 // convert it,
164 data = (wxObject *)new wxString(val);
165 // and store it
166 env.Put(name, data);
167 }
168 // return converted env var
169 return (wxChar *)((wxString *)data)->c_str();
170 }
171
172 int WXDLLEXPORT wxSystem(const wxChar *psz)
173 {
174 return system(wxConv_libc.cWX2MB(psz));
175 }
176
177 #endif