Implemented the various printf() functions under

Unicode with their GNU libc 2.2 funtions. This
    saves us some unicode<->ansi conversion and we
    no longer need the experimental printf() code
    in string.cpp. I had to implement wxSprintf()
    using wxSnprintf() as the former doesn't exist
    in Unicode GNU libc 2.2.


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@16442 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robert Roebling
2002-08-10 11:58:15 +00:00
parent e6ccaf1a99
commit a59c124d06
4 changed files with 84 additions and 100 deletions

View File

@@ -165,16 +165,32 @@ inline int Stricmp(const char *psz1, const char *psz2)
#endif // OS/compiler #endif // OS/compiler
} }
#ifndef wxSnprintf
// wxSnprintf() is like snprintf() if it's available and sprintf() (always // wxSnprintf() is like snprintf() if it's available and sprintf() (always
// available, but dangerous!) if not // available, but dangerous!) if not.
extern int WXDLLEXPORT wxSnprintf(wxChar *buf, size_t len, extern int WXDLLEXPORT wxSnprintf(wxChar *buf, size_t len,
const wxChar *format, const wxChar *format,
...) ATTRIBUTE_PRINTF_3; ...) ATTRIBUTE_PRINTF_3;
#else
// GNU libc 2.2 only has for wxSnprintf for Unicode called swprintf
// so we imitate wxSprintf using it.
extern int WXDLLEXPORT wxSprintf(wxChar *buf,
const wxChar *format,
...) ATTRIBUTE_PRINTF_2;
#endif
#ifndef wxVsnprintf
// and wxVsnprintf() is like vsnprintf() or vsprintf() // and wxVsnprintf() is like vsnprintf() or vsprintf()
extern int WXDLLEXPORT wxVsnprintf(wxChar *buf, size_t len, extern int WXDLLEXPORT wxVsnprintf(wxChar *buf, size_t len,
const wxChar *format, const wxChar *format,
va_list argptr); va_list argptr);
#else
// GNU libc 2.2 only has for wxVsnprintf for Unicode called vswprintf
// so we imitate wxVsprintf using it.
extern int WXDLLEXPORT wxVsprintf(wxChar *buf,
const wxChar *format,
va_list argptr);
#endif
// return an empty wxString // return an empty wxString
class WXDLLEXPORT wxString; // not yet defined class WXDLLEXPORT wxString; // not yet defined

View File

@@ -372,6 +372,29 @@ typedef unsigned __WCHAR_TYPE__ wxUChar;
# define wxStrtoul wcstoul # define wxStrtoul wcstoul
# define wxStrxfrm wcsxfrm # define wxStrxfrm wcsxfrm
# define wxFgetc fgetwc
# define wxFgetchar fgetwchar
# define wxFgets fgetws
# define wxFputc fputwc
# define wxFputchar fputwchar
# define wxFprintf fwprintf
# define wxFscanf fwscanf
# define wxGetc getwc
# define wxGetchar getwchar
# define wxGets getws
# define wxPrintf wprintf
# define wxPutc wputc
# define wxPutchar wputchar
# define wxPuts putws
# define wxScanf wscanf
# define wxSnprintf swprintf
# define wxSscanf swscanf
# define wxUngetc ungetwc
# define wxVfprint vfwprintf
# define wxVprintf vwprintf
# define wxVsscanf vswscanf
# define wxVsnprintf vswprintf
// glibc doesn't have wc equivalents of the other stuff // glibc doesn't have wc equivalents of the other stuff
# define wxNEED_WX_STDIO_H # define wxNEED_WX_STDIO_H
# define wxNEED_WX_STDLIB_H # define wxNEED_WX_STDLIB_H
@@ -482,29 +505,30 @@ typedef unsigned __WCHAR_TYPE__ wxUChar;
# endif # endif
# ifdef wxNEED_WX_STDIO_H # ifdef wxNEED_WX_STDIO_H
# define wxFopen fopen
# define wxFreopen freopen
# define wxPerror perror
# define wxRemove remove
# define wxRename rename
# define wxTmpnam tmpnam
# define wxFgetc fgetc # define wxFgetc fgetc
# define wxFgetchar fgetchar # define wxFgetchar fgetchar
# define wxFgets fgets # define wxFgets fgets
# define wxFopen fopen
# define wxFputc fputc # define wxFputc fputc
# define wxFputchar fputchar # define wxFputchar fputchar
# define wxFprintf fprintf # define wxFprintf fprintf
# define wxFreopen freopen
# define wxFscanf fscanf # define wxFscanf fscanf
# define wxGetc getc # define wxGetc getc
# define wxGetchar getchar # define wxGetchar getchar
# define wxGets gets # define wxGets gets
# define wxPerror perror
# define wxPrintf printf # define wxPrintf printf
# define wxPutc putc # define wxPutc putc
# define wxPutchar putchar # define wxPutchar putchar
# define wxPuts puts # define wxPuts puts
# define wxRemove remove
# define wxRename rename
# define wxScanf scanf # define wxScanf scanf
# define wxSprintf sprintf # define wxSprintf sprintf
# define wxSscanf sscanf # define wxSscanf sscanf
# define wxTmpnam tmpnam
# define wxUngetc ungetc # define wxUngetc ungetc
# define wxVfprint vfprintf # define wxVfprint vfprintf
# define wxVprintf vprintf # define wxVprintf vprintf
@@ -513,6 +537,7 @@ typedef unsigned __WCHAR_TYPE__ wxUChar;
# undef wxNEED_WX_STDIO_H # undef wxNEED_WX_STDIO_H
# endif # endif
# ifdef wxNEED_WX_STDLIB_H # ifdef wxNEED_WX_STDLIB_H
# define wxAtof atof # define wxAtof atof
# define wxAtoi atoi # define wxAtoi atoi
@@ -640,14 +665,6 @@ WXDLLEXPORT FILE * wxFopen(const wxChar *path, const wxChar *mode);
WXDLLEXPORT FILE * wxFreopen(const wxChar *path, const wxChar *mode, FILE *stream); WXDLLEXPORT FILE * wxFreopen(const wxChar *path, const wxChar *mode, FILE *stream);
WXDLLEXPORT int wxRemove(const wxChar *path); WXDLLEXPORT int wxRemove(const wxChar *path);
WXDLLEXPORT int wxRename(const wxChar *oldpath, const wxChar *newpath); WXDLLEXPORT int wxRename(const wxChar *oldpath, const wxChar *newpath);
WXDLLEXPORT int wxPrintf(const wxChar *fmt, ...) ATTRIBUTE_PRINTF_1;
WXDLLEXPORT int wxVprintf(const wxChar *fmt, va_list argptr);
WXDLLEXPORT int wxFprintf(FILE *stream, const wxChar *fmt, ...) ATTRIBUTE_PRINTF_2;
WXDLLEXPORT int wxVfprintf(FILE *stream, const wxChar *fmt, va_list argptr);
WXDLLEXPORT int wxSprintf(wxChar *buf, const wxChar *fmt, ...) ATTRIBUTE_PRINTF_2;
WXDLLEXPORT int wxVsprintf(wxChar *buf, const wxChar *fmt, va_list argptr);
WXDLLEXPORT int wxSscanf(const wxChar *buf, const wxChar *fmt, ...) ATTRIBUTE_PRINTF_2;
WXDLLEXPORT int wxVsscanf(const wxChar *buf, const wxChar *fmt, va_list argptr);
#endif #endif
#ifndef wxAtof #ifndef wxAtof

View File

@@ -48,8 +48,10 @@
#if wxUSE_UNICODE #if wxUSE_UNICODE
#undef wxUSE_EXPERIMENTAL_PRINTF #undef wxUSE_EXPERIMENTAL_PRINTF
#ifndef wvsnprintf
#define wxUSE_EXPERIMENTAL_PRINTF 1 #define wxUSE_EXPERIMENTAL_PRINTF 1
#endif #endif
#endif
// allocating extra space for each string consumes more memory but speeds up // allocating extra space for each string consumes more memory but speeds up
// the concatenation operations (nLen is the current string's length) // the concatenation operations (nLen is the current string's length)
@@ -184,11 +186,11 @@ wxSTD ostream& operator<<(wxSTD ostream& os, const wxString& str)
#endif //std::string compatibility #endif //std::string compatibility
extern int WXDLLEXPORT wxVsnprintf(wxChar *buf, size_t len, #ifndef wxVsnprintf
int WXDLLEXPORT wxVsnprintf(wxChar *buf, size_t len,
const wxChar *format, va_list argptr) const wxChar *format, va_list argptr)
{ {
#if wxUSE_UNICODE #if wxUSE_UNICODE
// FIXME should use wvsnprintf() or whatever if it's available
wxString s; wxString s;
int iLen = s.PrintfV(format, argptr); int iLen = s.PrintfV(format, argptr);
if ( iLen != -1 ) if ( iLen != -1 )
@@ -210,8 +212,19 @@ extern int WXDLLEXPORT wxVsnprintf(wxChar *buf, size_t len,
return rc; return rc;
#endif // Unicode/ANSI #endif // Unicode/ANSI
} }
#else
// GNU libc 2.2 only has for wxVsnprintf for Unicode called vswprintf
// so we imitate wxVsprintf using it.
int WXDLLEXPORT wxVsprintf(wxChar *buf,
const wxChar *format,
va_list argptr)
{
return vswprintf( buf, 10000, format, argptr );
}
#endif
extern int WXDLLEXPORT wxSnprintf(wxChar *buf, size_t len, #ifndef wxSnprintf
int WXDLLEXPORT wxSnprintf(wxChar *buf, size_t len,
const wxChar *format, ...) const wxChar *format, ...)
{ {
va_list argptr; va_list argptr;
@@ -223,6 +236,23 @@ extern int WXDLLEXPORT wxSnprintf(wxChar *buf, size_t len,
return iLen; return iLen;
} }
#else
// GNU libc 2.2 only has for wxSnprintf for Unicode called swprintf
// so we imitate wxSprintf using it.
int WXDLLEXPORT wxSprintf(wxChar *buf,
const wxChar *format,
...) ATTRIBUTE_PRINTF_2
{
va_list argptr;
va_start(argptr, format);
int iLen = swprintf(buf, 10000, format, argptr);
va_end(argptr);
return iLen;
}
#endif
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// private classes // private classes

View File

@@ -393,85 +393,6 @@ WXDLLEXPORT int wxRename(const wxChar *oldpath, const wxChar *newpath)
{ {
return rename( wxConvFile.cWX2MB(oldpath), wxConvFile.cWX2MB(newpath) ); return rename( wxConvFile.cWX2MB(oldpath), wxConvFile.cWX2MB(newpath) );
} }
int WXDLLEXPORT wxPrintf(const wxChar *fmt, ...)
{
va_list argptr;
int ret;
va_start(argptr, fmt);
ret = wxVprintf(fmt, argptr);
va_end(argptr);
return ret;
}
int WXDLLEXPORT wxVprintf(const wxChar *fmt, va_list argptr)
{
wxString str;
str.PrintfV(fmt,argptr);
printf("%s", (const char*)str.mb_str());
return str.Len();
}
int WXDLLEXPORT wxFprintf(FILE *stream, const wxChar *fmt, ...)
{
va_list argptr;
int ret;
va_start(argptr, fmt);
ret = wxVfprintf(stream, fmt, argptr);
va_end(argptr);
return ret;
}
int WXDLLEXPORT wxVfprintf(FILE *stream, const wxChar *fmt, va_list argptr)
{
wxString str;
str.PrintfV(fmt,argptr);
fprintf(stream, "%s", (const char*)str.mb_str());
return str.Len();
}
int WXDLLEXPORT wxSprintf(wxChar *buf, const wxChar *fmt, ...)
{
va_list argptr;
int ret;
va_start(argptr, fmt);
ret = wxVsprintf(buf, fmt, argptr);
va_end(argptr);
return ret;
}
int WXDLLEXPORT wxVsprintf(wxChar *buf, const wxChar *fmt, va_list argptr)
{
// this might be sort of inefficient, but it doesn't matter since
// we'd prefer people to use wxString::Printf directly instead anyway
wxString str;
str.PrintfV(fmt,argptr);
wxStrcpy(buf,str.c_str());
return str.Len();
}
int WXDLLEXPORT wxSscanf(const wxChar *buf, const wxChar *fmt, ...)
{
va_list argptr;
int ret;
va_start(argptr, fmt);
ret = wxVsscanf(buf, fmt, argptr);
va_end(argptr);
return ret;
}
int WXDLLEXPORT wxVsscanf(const wxChar *buf, const wxChar *fmt, va_list argptr)
{
int ret;
// this will work only for numeric conversion! Strings will not be converted correctly
// hopefully this is all we'll need
ret = vsscanf(wxConvLibc.cWX2MB(buf), wxConvLibc.cWX2MB(fmt), argptr);
return ret;
}
#endif #endif
#ifndef wxAtof #ifndef wxAtof