]>
git.saurik.com Git - wxWidgets.git/blob - src/common/wxchar.cpp
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"
33 #include "wx/wxchar.h"
34 #include "wx/string.h"
38 size_t wxMB2WC(wchar_t *buf
, const char *psz
, size_t n
)
41 return mbstowcs(buf
, psz
, n
);
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).
47 // don't know if it's really needed (or if we can pass NULL), but better safe
50 return mbsrtowcs((wchar_t *) NULL
, &psz
, 0, &mbstate
);
52 return mbstowcs((wchar_t *) NULL
, psz
, 0);
56 size_t wxWC2MB(char *buf
, const wchar_t *pwz
, size_t n
)
59 return wcstombs(buf
, pwz
, n
);
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).
65 // don't know if it's really needed (or if we can pass NULL), but better safe
68 return wcsrtombs((char *) NULL
, &pwz
, 0, &mbstate
);
70 return wcstombs((char *) NULL
, pwz
, 0);
75 wxChar
* WXDLLEXPORT
wxStrdup(const wxChar
*psz
)
77 size_t size
= (wxStrlen(psz
) + 1) * sizeof(wxChar
);
78 wxChar
*ret
= (wxChar
*) malloc(size
);
79 memcpy(ret
, psz
, size
);
85 wxChar
* WXDLLEXPORT
wxStrtok(wxChar
*psz
, const wxChar
*delim
, wxChar
**save_ptr
)
87 if (!psz
) psz
= *save_ptr
;
88 psz
+= wxStrspn(psz
, delim
);
90 *save_ptr
= (wxChar
*)NULL
;
91 return (wxChar
*)NULL
;
94 psz
= wxStrpbrk(psz
, delim
);
95 if (!psz
) *save_ptr
= (wxChar
*)NULL
;
105 wxChar
* WXDLLEXPORT
wxSetlocale(int category
, const wxChar
*locale
)
107 setlocale(category
, wxConv_libc
.cWX2MB(locale
));
109 return (wxChar
*)NULL
;
113 #ifdef wxNEED_WX_STDIO_H
114 int WXDLLEXPORT
wxSprintf(wxChar
*buf
, const wxChar
*fmt
, ...)
119 va_start(argptr
, fmt
);
120 ret
= wxVsprintf(buf
, fmt
, argptr
);
125 int WXDLLEXPORT
wxVsprintf(wxChar
*buf
, const wxChar
*fmt
, va_list argptr
)
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
130 str
.PrintfV(fmt
,argptr
);
131 wxStrcpy(buf
,str
.c_str());
137 double WXDLLEXPORT
wxAtof(const wxChar
*psz
)
139 return atof(wxConv_libc
.cWX2MB(psz
));
143 #ifdef wxNEED_WX_STDLIB_H
144 int WXDLLEXPORT
wxAtoi(const wxChar
*psz
)
146 return atoi(wxConv_libc
.cWX2MB(psz
));
149 long WXDLLEXPORT
wxAtol(const wxChar
*psz
)
151 return atol(wxConv_libc
.cWX2MB(psz
));
154 wxChar
* WXDLLEXPORT
wxGetenv(const wxChar
*name
)
156 static wxHashTable env
;
157 // check if we already have stored the converted env var
158 wxObject
*data
= env
.Get(name
);
160 // nope, retrieve it,
161 const char *val
= getenv(wxConv_libc
.cWX2MB(name
));
162 if (!val
) return (wxChar
*)NULL
;
164 data
= (wxObject
*)new wxString(val
);
168 // return converted env var
169 return (wxChar
*)((wxString
*)data
)->c_str();
172 int WXDLLEXPORT
wxSystem(const wxChar
*psz
)
174 return system(wxConv_libc
.cWX2MB(psz
));