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
This commit is contained in:
Robert Roebling
2002-08-11 13:09:57 +00:00
parent 43330cc9de
commit b1ac3b56e6
12 changed files with 141 additions and 68 deletions

View File

@@ -58,19 +58,6 @@ public:
WXDLLEXPORT_DATA(extern wxMBConv) wxConvLibc; 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) // wxMBConvUTF7 (for conversion using UTF7 encoding)
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -146,6 +133,7 @@ private:
bool m_deferred; bool m_deferred;
}; };
#define wxConvFile wxConvLocal
WXDLLEXPORT_DATA(extern wxCSConv) wxConvLocal; WXDLLEXPORT_DATA(extern wxCSConv) wxConvLocal;
WXDLLEXPORT_DATA(extern wxMBConv *) wxConvCurrent; WXDLLEXPORT_DATA(extern wxMBConv *) wxConvCurrent;

View File

@@ -480,6 +480,18 @@ public:
// identical to c_str() // identical to c_str()
const wxChar* GetData() const { return m_pchData; } 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 // conversions with (possible) format convertions: have to return a
// buffer with temporary data // buffer with temporary data
// //

View File

@@ -1112,10 +1112,10 @@ wxCopyFile (const wxString& file1, const wxString& file2, bool overwrite)
if ( ::DosCopy(file2, file2, overwrite ? DCPY_EXISTING : 0) != 0 ) if ( ::DosCopy(file2, file2, overwrite ? DCPY_EXISTING : 0) != 0 )
return FALSE; return FALSE;
#else // !Win32 #else // !Win32
wxStructStat fbuf;
wxStructStat fbuf;
// get permissions of file1 // 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 // the file probably doesn't exist or we haven't the rights to read
// from it anyhow // 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 // create file2 with the same permissions than file1 and open it for
// writing // writing
wxFile fileOut; wxFile fileOut;
if ( !fileOut.Create(file2, overwrite, fbuf.st_mode & 0777) ) if ( !fileOut.Create(file2, overwrite, fbuf.st_mode & 0777) )
return FALSE; return FALSE;

View File

@@ -225,7 +225,7 @@ bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req)
wxString buf; wxString buf;
buf.Printf(wxT("%s %s HTTP/1.0\r\n"), request, path.c_str()); 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)); Write(pathbuf, strlen(wxMBSTRINGCAST pathbuf));
SendHeaders(); SendHeaders();
Write("\r\n", 2); Write("\r\n", 2);

View File

@@ -153,7 +153,7 @@ int wxEntry(int argc, char **argv)
int mb_argc = 0; int mb_argc = 0;
while (mb_argc < argc) 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++; mb_argc++;
} }
wxTheApp->argv[mb_argc] = (wxChar *)NULL; wxTheApp->argv[mb_argc] = (wxChar *)NULL;

View File

@@ -1276,7 +1276,7 @@ wxString wxLocale::GetSystemEncodingName()
} }
else else
{ {
encname = wxConvLibc.cMB2WX(alang); encname = wxString::FromAscii( alang );
} }
} }
else 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 // 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 // the environment variables (in most cases this won't work, but I was
// out of ideas) // out of ideas)
wxChar *lang = wxGetenv(wxT("LC_ALL")); char *lang = getenv( "LC_ALL");
wxChar *dot = lang ? wxStrchr(lang, wxT('.')) : (wxChar *)NULL; char *dot = lang ? strchr(lang, '.') : (char *)NULL;
if (!dot) if (!dot)
{ {
lang = wxGetenv(wxT("LC_CTYPE")); lang = getenv( "LC_CTYPE" );
if ( lang ) if ( lang )
dot = wxStrchr(lang, wxT('.')); dot = strchr(lang, '.' );
} }
if (!dot) if (!dot)
{ {
lang = wxGetenv(wxT("LANG")); lang = getenv( "LANG");
if ( lang ) if ( lang )
dot = wxStrchr(lang, wxT('.')); dot = strchr(lang, '.');
} }
if ( dot ) if ( dot )
{ {
encname = dot+1; encname = wxString::FromAscii( dot+1 );
} }
} }
#endif // Win32/Unix #endif // Win32/Unix

View File

@@ -186,14 +186,44 @@ static size_t decode_utf16(const wchar_t* input, wxUint32& output)
// wxMBConv // wxMBConv
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
#define IGNORE_LIBC 0
size_t wxMBConv::MB2WC(wchar_t *buf, const char *psz, size_t n) const 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); return wxMB2WC(buf, psz, n);
#endif
} }
size_t wxMBConv::WC2MB(char *buf, const wchar_t *psz, size_t n) const 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); return wxWC2MB(buf, psz, n);
#endif
} }
const wxWCharBuffer wxMBConv::cMB2WC(const char *psz) const 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); return wxCharBuffer((char *) NULL);
wxCharBuffer buf(nLen); // this allocates nLen+1 wxCharBuffer buf(nLen); // this allocates nLen+1
WC2MB((char *)(const char *) buf, psz, nLen+1); WC2MB((char *)(const char *) buf, psz, nLen+1);
// printf( "str %s\n", (const char*) buf );
return buf; return buf;
} }
else else
return wxCharBuffer((char *) NULL); 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 // standard gdk conversion
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -962,7 +976,9 @@ void wxCSConv::LoadNow()
{ {
wxString name = wxLocale::GetSystemEncodingName(); wxString name = wxLocale::GetSystemEncodingName();
if ( !name.empty() ) if ( !name.empty() )
{
SetName(name); SetName(name);
}
} }
// wxGetCharacterSet() complains about NULL name // wxGetCharacterSet() complains about NULL name

View File

@@ -826,6 +826,45 @@ wxString operator+(const wxChar *psz, const wxString& str)
// other common string functions // 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 // simple sub-string extraction
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------

View File

@@ -200,9 +200,9 @@ WXDLLEXPORT wxChar * wxStrtok(wxChar *psz, const wxChar *delim, wxChar **save_pt
#ifndef wxSetlocale #ifndef wxSetlocale
WXDLLEXPORT wxWCharBuffer wxSetlocale(int category, const wxChar *locale) 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 #endif
@@ -335,7 +335,7 @@ WXDLLEXPORT double wxStrtod(const wxChar *nptr, wxChar **endptr)
} }
wxString data(nptr, nptr-start); wxString data(nptr, nptr-start);
wxWX2MBbuf dat = data.mb_str(wxConvLibc); wxWX2MBbuf dat = data.mb_str(wxConvLocal);
char *rdat = wxMBSTRINGCAST dat; char *rdat = wxMBSTRINGCAST dat;
double ret = strtod(dat, &rdat); 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++; (wxIsalpha(*nptr) && (wxToupper(*nptr) - wxT('A') + 10 < base))) nptr++;
wxString data(nptr, nptr-start); wxString data(nptr, nptr-start);
wxWX2MBbuf dat = data.mb_str(wxConvLibc); wxWX2MBbuf dat = data.mb_str(wxConvLocal);
char *rdat = wxMBSTRINGCAST dat; char *rdat = wxMBSTRINGCAST dat;
long int ret = strtol(dat, &rdat, base); 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 #ifdef wxNEED_WX_STDIO_H
WXDLLEXPORT FILE * wxFopen(const wxChar *path, const wxChar *mode) 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) 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) WXDLLEXPORT int wxRemove(const wxChar *path)
@@ -398,19 +406,19 @@ WXDLLEXPORT int wxRename(const wxChar *oldpath, const wxChar *newpath)
#ifndef wxAtof #ifndef wxAtof
double WXDLLEXPORT wxAtof(const wxChar *psz) double WXDLLEXPORT wxAtof(const wxChar *psz)
{ {
return atof(wxConvLibc.cWX2MB(psz)); return atof(wxConvLocal.cWX2MB(psz));
} }
#endif #endif
#ifdef wxNEED_WX_STDLIB_H #ifdef wxNEED_WX_STDLIB_H
int WXDLLEXPORT wxAtoi(const wxChar *psz) int WXDLLEXPORT wxAtoi(const wxChar *psz)
{ {
return atoi(wxConvLibc.cWX2MB(psz)); return atoi(wxConvLocal.cWX2MB(psz));
} }
long WXDLLEXPORT wxAtol(const wxChar *psz) long WXDLLEXPORT wxAtol(const wxChar *psz)
{ {
return atol(wxConvLibc.cWX2MB(psz)); return atol(wxConvLocal.cWX2MB(psz));
} }
wxChar * WXDLLEXPORT wxGetenv(const wxChar *name) wxChar * WXDLLEXPORT wxGetenv(const wxChar *name)
@@ -423,7 +431,7 @@ wxChar * WXDLLEXPORT wxGetenv(const wxChar *name)
{ {
// nope, retrieve it, // nope, retrieve it,
#if wxUSE_UNICODE #if wxUSE_UNICODE
wxCharBuffer buffer = wxConvLibc.cWX2MB(name); wxCharBuffer buffer = wxConvLocal.cWX2MB(name);
// printf( "buffer %s\n", (const char*) buffer ); // printf( "buffer %s\n", (const char*) buffer );
const char *val = getenv( (const char *)buffer ); const char *val = getenv( (const char *)buffer );
#else #else
@@ -435,7 +443,7 @@ wxChar * WXDLLEXPORT wxGetenv(const wxChar *name)
// convert it, // convert it,
#ifdef wxUSE_UNICODE #ifdef wxUSE_UNICODE
data = (wxObject *)new wxString(val, wxConvLibc); data = (wxObject *)new wxString(val, wxConvLocal);
#else #else
data = (wxObject *)new wxString(val); data = (wxObject *)new wxString(val);
#endif #endif
@@ -447,9 +455,9 @@ wxChar * WXDLLEXPORT wxGetenv(const wxChar *name)
return (wxChar *)((wxString *)data)->c_str(); 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 #endif
@@ -457,17 +465,21 @@ int WXDLLEXPORT wxSystem(const wxChar *psz)
#ifdef wxNEED_WX_TIME_H #ifdef wxNEED_WX_TIME_H
WXDLLEXPORT size_t wxStrftime(wxChar *s, size_t max, const wxChar *fmt, const struct tm *tm) WXDLLEXPORT size_t wxStrftime(wxChar *s, size_t max, const wxChar *fmt, const struct tm *tm)
{ {
if (!max) return 0; if (!max) return 0;
char *buf = (char *)malloc(max);
size_t ret = strftime(buf, max, wxConvLibc.cWX2MB(fmt), tm); char *buf = (char *)malloc(max);
if (ret) { size_t ret = strftime(buf, max, wxConvLocal.cWX2MB(fmt), tm);
wxStrcpy(s, wxConvLibc.cMB2WX(buf)); if (ret)
free(buf); {
return wxStrlen(s); wxStrcpy(s, wxConvLocal.cMB2WX(buf));
} else { free(buf);
free(buf); return wxStrlen(s);
*s = 0; }
return 0; else
{
free(buf);
*s = 0;
return 0;
} }
} }
#endif #endif

View File

@@ -1309,9 +1309,12 @@ void wxPostScriptDC::DoDrawText( const wxString& text, wxCoord x, wxCoord y )
wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") ); wxCHECK_RET( m_ok && m_pstream, wxT("invalid postscript dc") );
#ifdef __WXGTK20__ #ifdef __WXGTK20__
int dpi = GetResolution() * 2;
int dpi = GetResolution() * 2;
dpi = 300;
PangoContext *context = pango_ft2_get_context ( dpi, dpi ); PangoContext *context = pango_ft2_get_context ( dpi, dpi );
// What are these for? // What are these for?
pango_context_set_language (context, pango_language_from_string ("en_US")); 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 #endif
pango_layout_set_text( layout, (const char*) buffer, strlen(buffer) ); pango_layout_set_text( layout, (const char*) buffer, strlen(buffer) );
#if 1 #if 0
double xx = LogicalToDeviceX(x); double xx = LogicalToDeviceX(x);
double yy = LogicalToDeviceY(y /*+ bitmap.GetHeight()*/ ); double yy = LogicalToDeviceY(y /*+ bitmap.GetHeight()*/ );

View File

@@ -25,6 +25,7 @@
#include "wx/settings.h" #include "wx/settings.h"
#include "wx/dialog.h" #include "wx/dialog.h"
#include "wx/msgdlg.h" #include "wx/msgdlg.h"
#include "wx/file.h"
#if wxUSE_WX_RESOURCES #if wxUSE_WX_RESOURCES
#include "wx/resource.h" #include "wx/resource.h"

View File

@@ -25,6 +25,7 @@
#include "wx/settings.h" #include "wx/settings.h"
#include "wx/dialog.h" #include "wx/dialog.h"
#include "wx/msgdlg.h" #include "wx/msgdlg.h"
#include "wx/file.h"
#if wxUSE_WX_RESOURCES #if wxUSE_WX_RESOURCES
#include "wx/resource.h" #include "wx/resource.h"