From b1ac3b56e6b5b7342d69aa3b33744c345edb3d1e Mon Sep 17 00:00:00 2001 From: Robert Roebling Date: Sun, 11 Aug 2002 13:09:57 +0000 Subject: [PATCH] Added wxString::FromAscii() wxString::ToAscii(). Removed wxConvFile, made it a define wxConvLocal. Exchanged some wxConvLibc to wxConvLocal calls. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@16453 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/strconv.h | 14 +--------- include/wx/string.h | 12 +++++++++ src/common/filefn.cpp | 5 ++-- src/common/http.cpp | 2 +- src/common/init.cpp | 2 +- src/common/intl.cpp | 16 +++++------ src/common/strconv.cpp | 50 +++++++++++++++++++++++------------ src/common/string.cpp | 39 +++++++++++++++++++++++++++ src/common/wxchar.cpp | 60 +++++++++++++++++++++++++----------------- src/generic/dcpsg.cpp | 7 +++-- src/gtk/app.cpp | 1 + src/gtk1/app.cpp | 1 + 12 files changed, 141 insertions(+), 68 deletions(-) diff --git a/include/wx/strconv.h b/include/wx/strconv.h index 81015e3240..ba1e84d579 100644 --- a/include/wx/strconv.h +++ b/include/wx/strconv.h @@ -58,19 +58,6 @@ public: WXDLLEXPORT_DATA(extern wxMBConv) wxConvLibc; -// ---------------------------------------------------------------------------- -// wxMBConvFile (for conversion to filenames) -// ---------------------------------------------------------------------------- - -class WXDLLEXPORT wxMBConvFile : public wxMBConv -{ -public: - virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) const; - virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) const; -}; - -WXDLLEXPORT_DATA(extern wxMBConvFile) wxConvFile; - // ---------------------------------------------------------------------------- // wxMBConvUTF7 (for conversion using UTF7 encoding) // ---------------------------------------------------------------------------- @@ -146,6 +133,7 @@ private: bool m_deferred; }; +#define wxConvFile wxConvLocal WXDLLEXPORT_DATA(extern wxCSConv) wxConvLocal; WXDLLEXPORT_DATA(extern wxMBConv *) wxConvCurrent; diff --git a/include/wx/string.h b/include/wx/string.h index 7f4245d2aa..b285cdefb6 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -480,6 +480,18 @@ public: // identical to c_str() const wxChar* GetData() const { return m_pchData; } + // conversion to plain ascii: this is usefull for + // converting numbers or strings which are certain + // not to contain special chars (typically system + // functions, X atoms, environment variables etc.) +#if wxUSE_UNICODE + static wxString FromAscii( char *ascii ); + const wxCharBuffer ToAscii() const; +#else + static wxString FromAscii( char *ascii ) { return wxString( ascii ); } + const char *ToAscii() const { return m_pchData; } +#endif + // conversions with (possible) format convertions: have to return a // buffer with temporary data // diff --git a/src/common/filefn.cpp b/src/common/filefn.cpp index 8650b3338d..f8ef9b471f 100644 --- a/src/common/filefn.cpp +++ b/src/common/filefn.cpp @@ -1112,10 +1112,10 @@ wxCopyFile (const wxString& file1, const wxString& file2, bool overwrite) if ( ::DosCopy(file2, file2, overwrite ? DCPY_EXISTING : 0) != 0 ) return FALSE; #else // !Win32 - wxStructStat fbuf; + wxStructStat fbuf; // get permissions of file1 - if ( wxStat( file1, &fbuf) != 0 ) + if ( wxStat( file1.c_str(), &fbuf) != 0 ) { // the file probably doesn't exist or we haven't the rights to read // from it anyhow @@ -1146,6 +1146,7 @@ wxCopyFile (const wxString& file1, const wxString& file2, bool overwrite) // create file2 with the same permissions than file1 and open it for // writing + wxFile fileOut; if ( !fileOut.Create(file2, overwrite, fbuf.st_mode & 0777) ) return FALSE; diff --git a/src/common/http.cpp b/src/common/http.cpp index 0853234490..eca6160a5e 100644 --- a/src/common/http.cpp +++ b/src/common/http.cpp @@ -225,7 +225,7 @@ bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req) wxString buf; buf.Printf(wxT("%s %s HTTP/1.0\r\n"), request, path.c_str()); - const wxWX2MBbuf pathbuf = wxConvLibc.cWX2MB(buf); + const wxWX2MBbuf pathbuf = wxConvLocal.cWX2MB(buf); Write(pathbuf, strlen(wxMBSTRINGCAST pathbuf)); SendHeaders(); Write("\r\n", 2); diff --git a/src/common/init.cpp b/src/common/init.cpp index 0b9a9ae16b..6182c6734f 100644 --- a/src/common/init.cpp +++ b/src/common/init.cpp @@ -153,7 +153,7 @@ int wxEntry(int argc, char **argv) int mb_argc = 0; while (mb_argc < argc) { - wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc])); + wxTheApp->argv[mb_argc] = wxStrdup(wxConvLocal.cMB2WX(argv[mb_argc])); mb_argc++; } wxTheApp->argv[mb_argc] = (wxChar *)NULL; diff --git a/src/common/intl.cpp b/src/common/intl.cpp index 3f592d7881..1176aa6ab2 100644 --- a/src/common/intl.cpp +++ b/src/common/intl.cpp @@ -1276,7 +1276,7 @@ wxString wxLocale::GetSystemEncodingName() } else { - encname = wxConvLibc.cMB2WX(alang); + encname = wxString::FromAscii( alang ); } } else @@ -1285,24 +1285,24 @@ wxString wxLocale::GetSystemEncodingName() // if we can't get at the character set directly, try to see if it's in // the environment variables (in most cases this won't work, but I was // out of ideas) - wxChar *lang = wxGetenv(wxT("LC_ALL")); - wxChar *dot = lang ? wxStrchr(lang, wxT('.')) : (wxChar *)NULL; + char *lang = getenv( "LC_ALL"); + char *dot = lang ? strchr(lang, '.') : (char *)NULL; if (!dot) { - lang = wxGetenv(wxT("LC_CTYPE")); + lang = getenv( "LC_CTYPE" ); if ( lang ) - dot = wxStrchr(lang, wxT('.')); + dot = strchr(lang, '.' ); } if (!dot) { - lang = wxGetenv(wxT("LANG")); + lang = getenv( "LANG"); if ( lang ) - dot = wxStrchr(lang, wxT('.')); + dot = strchr(lang, '.'); } if ( dot ) { - encname = dot+1; + encname = wxString::FromAscii( dot+1 ); } } #endif // Win32/Unix diff --git a/src/common/strconv.cpp b/src/common/strconv.cpp index ffb8be0d21..30934f5ee0 100644 --- a/src/common/strconv.cpp +++ b/src/common/strconv.cpp @@ -186,14 +186,44 @@ static size_t decode_utf16(const wchar_t* input, wxUint32& output) // wxMBConv // ---------------------------------------------------------------------------- +#define IGNORE_LIBC 0 + size_t wxMBConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const { +#if IGNORE_LIBC + if (buf) + { + for (size_t i = 0; i < strlen( psz )+1; i++) + buf[i] = (wchar_t) psz[i]; + // printf( "libc %s\n", buf ); + return strlen( psz ); + } + else + { + return strlen( psz ); + } +#else return wxMB2WC(buf, psz, n); +#endif } size_t wxMBConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const { +#if IGNORE_LIBC + if (buf) + { + for (size_t i = 0; i < wxStrlen( psz )+1; i++) + buf[i] = (char) psz[i]; + // printf( "libc %s\n", buf ); + return wxStrlen( psz ); + } + else + { + return wxStrlen( psz ); + } +#else return wxWC2MB(buf, psz, n); +#endif } const wxWCharBuffer wxMBConv::cMB2WC(const char *psz) const @@ -220,29 +250,13 @@ const wxCharBuffer wxMBConv::cWC2MB(const wchar_t *psz) const return wxCharBuffer((char *) NULL); wxCharBuffer buf(nLen); // this allocates nLen+1 WC2MB((char *)(const char *) buf, psz, nLen+1); + // printf( "str %s\n", (const char*) buf ); return buf; } else return wxCharBuffer((char *) NULL); } -// ---------------------------------------------------------------------------- -// standard file conversion -// ---------------------------------------------------------------------------- - -WXDLLEXPORT_DATA(wxMBConvFile) wxConvFile; - -// just use the libc conversion for now -size_t wxMBConvFile::MB2WC(wchar_t *buf, const char *psz, size_t n) const -{ - return wxMB2WC(buf, psz, n); -} - -size_t wxMBConvFile::WC2MB(char *buf, const wchar_t *psz, size_t n) const -{ - return wxWC2MB(buf, psz, n); -} - // ---------------------------------------------------------------------------- // standard gdk conversion // ---------------------------------------------------------------------------- @@ -962,7 +976,9 @@ void wxCSConv::LoadNow() { wxString name = wxLocale::GetSystemEncodingName(); if ( !name.empty() ) + { SetName(name); + } } // wxGetCharacterSet() complains about NULL name diff --git a/src/common/string.cpp b/src/common/string.cpp index 6956cdd521..e85bcf2b6b 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -826,6 +826,45 @@ wxString operator+(const wxChar *psz, const wxString& str) // other common string functions // =========================================================================== +#if wxUSE_UNICODE +wxString wxString::FromAscii( char *ascii ) +{ + if (!ascii) + return wxEmptyString; + + size_t len = strlen( ascii ); + wxString res; + res.AllocBuffer( len ); + wchar_t *dest = (wchar_t*)(const wchar_t*) res.c_str(); + + for (size_t i = 0; i < len+1; i++) + dest[i] = (wchar_t) ascii[i]; + + return res; +} + +const wxCharBuffer wxString::ToAscii() const +{ + if (IsNull()) + return wxCharBuffer( (const char*)NULL ); + + size_t len = Len(); + wxCharBuffer buffer( len ); // allocates len+1 + + char *dest = (char*)(const char*) buffer; + + for (size_t i = 0; i < len+1; i++) + { + if (m_pchData[i] > 127) + dest[i] = '_'; + else + dest[i] = (char) m_pchData[i]; + } + + return buffer; +} +#endif + // --------------------------------------------------------------------------- // simple sub-string extraction // --------------------------------------------------------------------------- diff --git a/src/common/wxchar.cpp b/src/common/wxchar.cpp index eaaef7ad4a..39efe204fa 100644 --- a/src/common/wxchar.cpp +++ b/src/common/wxchar.cpp @@ -200,9 +200,9 @@ WXDLLEXPORT wxChar * wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_pt #ifndef wxSetlocale WXDLLEXPORT wxWCharBuffer wxSetlocale(int category, const wxChar *locale) { - char *localeOld = setlocale(category, wxConvLibc.cWX2MB(locale)); + char *localeOld = setlocale(category, wxConvLocal.cWX2MB(locale)); - return wxWCharBuffer(wxConvLibc.cMB2WC(localeOld)); + return wxWCharBuffer(wxConvLocal.cMB2WC(localeOld)); } #endif @@ -335,7 +335,7 @@ WXDLLEXPORT double wxStrtod(const wxChar *nptr, wxChar **endptr) } wxString data(nptr, nptr-start); - wxWX2MBbuf dat = data.mb_str(wxConvLibc); + wxWX2MBbuf dat = data.mb_str(wxConvLocal); char *rdat = wxMBSTRINGCAST dat; double ret = strtod(dat, &rdat); @@ -363,7 +363,7 @@ WXDLLEXPORT long int wxStrtol(const wxChar *nptr, wxChar **endptr, int base) (wxIsalpha(*nptr) && (wxToupper(*nptr) - wxT('A') + 10 < base))) nptr++; wxString data(nptr, nptr-start); - wxWX2MBbuf dat = data.mb_str(wxConvLibc); + wxWX2MBbuf dat = data.mb_str(wxConvLocal); char *rdat = wxMBSTRINGCAST dat; long int ret = strtol(dat, &rdat, base); @@ -376,12 +376,20 @@ WXDLLEXPORT long int wxStrtol(const wxChar *nptr, wxChar **endptr, int base) #ifdef wxNEED_WX_STDIO_H WXDLLEXPORT FILE * wxFopen(const wxChar *path, const wxChar *mode) { - return fopen( wxConvFile.cWX2MB(path), wxConvLibc.cWX2MB(mode) ); + char mode_buffer[10]; + for (size_t i = 0; i < wxStrlen(mode)+1; i++) + mode_buffer[i] = (char) mode[i]; + + return fopen( wxConvFile.cWX2MB(path), mode_buffer ); } WXDLLEXPORT FILE * wxFreopen(const wxChar *path, const wxChar *mode, FILE *stream) { - return freopen( wxConvFile.cWX2MB(path), wxConvLibc.cWX2MB(mode), stream ); + char mode_buffer[10]; + for (size_t i = 0; i < wxStrlen(mode)+1; i++) + mode_buffer[i] = (char) mode[i]; + + return freopen( wxConvFile.cWX2MB(path), mode_buffer, stream ); } WXDLLEXPORT int wxRemove(const wxChar *path) @@ -398,19 +406,19 @@ WXDLLEXPORT int wxRename(const wxChar *oldpath, const wxChar *newpath) #ifndef wxAtof double WXDLLEXPORT wxAtof(const wxChar *psz) { - return atof(wxConvLibc.cWX2MB(psz)); + return atof(wxConvLocal.cWX2MB(psz)); } #endif #ifdef wxNEED_WX_STDLIB_H int WXDLLEXPORT wxAtoi(const wxChar *psz) { - return atoi(wxConvLibc.cWX2MB(psz)); + return atoi(wxConvLocal.cWX2MB(psz)); } long WXDLLEXPORT wxAtol(const wxChar *psz) { - return atol(wxConvLibc.cWX2MB(psz)); + return atol(wxConvLocal.cWX2MB(psz)); } wxChar * WXDLLEXPORT wxGetenv(const wxChar *name) @@ -423,7 +431,7 @@ wxChar * WXDLLEXPORT wxGetenv(const wxChar *name) { // nope, retrieve it, #if wxUSE_UNICODE - wxCharBuffer buffer = wxConvLibc.cWX2MB(name); + wxCharBuffer buffer = wxConvLocal.cWX2MB(name); // printf( "buffer %s\n", (const char*) buffer ); const char *val = getenv( (const char *)buffer ); #else @@ -435,7 +443,7 @@ wxChar * WXDLLEXPORT wxGetenv(const wxChar *name) // convert it, #ifdef wxUSE_UNICODE - data = (wxObject *)new wxString(val, wxConvLibc); + data = (wxObject *)new wxString(val, wxConvLocal); #else data = (wxObject *)new wxString(val); #endif @@ -447,9 +455,9 @@ wxChar * WXDLLEXPORT wxGetenv(const wxChar *name) return (wxChar *)((wxString *)data)->c_str(); } -int WXDLLEXPORT wxSystem(const wxChar *psz) +int WXDLLEXPORT wxSystem(const wxChar *psz) { - return system(wxConvLibc.cWX2MB(psz)); + return system(wxConvLocal.cWX2MB(psz)); } #endif @@ -457,17 +465,21 @@ int WXDLLEXPORT wxSystem(const wxChar *psz) #ifdef wxNEED_WX_TIME_H WXDLLEXPORT size_t wxStrftime(wxChar *s, size_t max, const wxChar *fmt, const struct tm *tm) { - if (!max) return 0; - char *buf = (char *)malloc(max); - size_t ret = strftime(buf, max, wxConvLibc.cWX2MB(fmt), tm); - if (ret) { - wxStrcpy(s, wxConvLibc.cMB2WX(buf)); - free(buf); - return wxStrlen(s); - } else { - free(buf); - *s = 0; - return 0; + if (!max) return 0; + + char *buf = (char *)malloc(max); + size_t ret = strftime(buf, max, wxConvLocal.cWX2MB(fmt), tm); + if (ret) + { + wxStrcpy(s, wxConvLocal.cMB2WX(buf)); + free(buf); + return wxStrlen(s); + } + else + { + free(buf); + *s = 0; + return 0; } } #endif diff --git a/src/generic/dcpsg.cpp b/src/generic/dcpsg.cpp index 47421d57b1..fbf73107ff 100644 --- a/src/generic/dcpsg.cpp +++ b/src/generic/dcpsg.cpp @@ -1309,9 +1309,12 @@ void wxPostScriptDC::DoDrawText( const wxString& text, wxCoord x, wxCoord y ) wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") ); #ifdef __WXGTK20__ - int dpi = GetResolution() * 2; + int dpi = GetResolution() * 2; + dpi = 300; + PangoContext *context = pango_ft2_get_context ( dpi, dpi ); + // What are these for? pango_context_set_language (context, pango_language_from_string ("en_US")); @@ -1329,7 +1332,7 @@ void wxPostScriptDC::DoDrawText( const wxString& text, wxCoord x, wxCoord y ) #endif pango_layout_set_text( layout, (const char*) buffer, strlen(buffer) ); -#if 1 +#if 0 double xx = LogicalToDeviceX(x); double yy = LogicalToDeviceY(y /*+ bitmap.GetHeight()*/ ); diff --git a/src/gtk/app.cpp b/src/gtk/app.cpp index 93e2ee3d05..08de37e9ae 100644 --- a/src/gtk/app.cpp +++ b/src/gtk/app.cpp @@ -25,6 +25,7 @@ #include "wx/settings.h" #include "wx/dialog.h" #include "wx/msgdlg.h" +#include "wx/file.h" #if wxUSE_WX_RESOURCES #include "wx/resource.h" diff --git a/src/gtk1/app.cpp b/src/gtk1/app.cpp index 93e2ee3d05..08de37e9ae 100644 --- a/src/gtk1/app.cpp +++ b/src/gtk1/app.cpp @@ -25,6 +25,7 @@ #include "wx/settings.h" #include "wx/dialog.h" #include "wx/msgdlg.h" +#include "wx/file.h" #if wxUSE_WX_RESOURCES #include "wx/resource.h" -- 2.45.2