Commit e8d76392 authored by Oleg.Korshul's avatar Oleg.Korshul Committed by Alexander Trofimov

работающий CString::Format под MacOS

git-svn-id: svn://fileserver/activex/AVS/Sources/TeamlabOffice/trunk/ServerComponents@67608 954022d7-b5bf-4e40-9824-e11837661b57
parent 1e0b2ea3
...@@ -9,6 +9,15 @@ ...@@ -9,6 +9,15 @@
#include "unicode_util.h" #include "unicode_util.h"
#include <stdlib.h> #include <stdlib.h>
#if defined(MAC) || defined(_MAC)
#if !defined(__APPLE__)
#define __APPLE__
#endif
#endif
#ifdef __APPLE__
#include "./strings_hack_printf.h"
#endif
#include <string> #include <string>
#include <string.h> #include <string.h>
...@@ -19,7 +28,10 @@ ...@@ -19,7 +28,10 @@
#define FMT_BLOCK_SIZE 2048 // # of bytes to increment per try #define FMT_BLOCK_SIZE 2048 // # of bytes to increment per try
#define BUFSIZE_1ST 256 #define BUFSIZE_1ST 256
#define BUFSIZE_2ND 512 #define BUFSIZE_2ND 512
#ifndef STD_BUF_SIZE
#define STD_BUF_SIZE 1024 #define STD_BUF_SIZE 1024
#endif
#define WriteUtf16_WCHAR(code, p) \ #define WriteUtf16_WCHAR(code, p) \
if (code < 0x10000) \ if (code < 0x10000) \
...@@ -2691,41 +2703,6 @@ public: ...@@ -2691,41 +2703,6 @@ public:
// themselves. Why am I doing this? Well, if you had any idea the // themselves. Why am I doing this? Well, if you had any idea the
// number of times I've been emailed by people about this // number of times I've been emailed by people about this
// "incompatability" in my code, you wouldn't ask. // "incompatability" in my code, you wouldn't ask.
#ifdef SS_ANSI
#ifdef __APPLE__
int apple_vscwprintf(MYTYPE& str, const wchar_t *format, va_list argptr)
{
// Unlike vsnprintf(), vswprintf() does not tell you how many
// characters would have been written if there was space enough in
// the buffer - it just reports an error when there is not enough
// space. Assume a moderately large machine so kilobytes of wchar_t
// on the stack is not a problem.
int buf_size = STD_BUF_SIZE;
while (buf_size < STD_BUF_SIZE * STD_BUF_SIZE)
{
va_list args;
va_copy(args, argptr);
wchar_t buffer[buf_size];
int fmt_size = vswprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), format, args);
if (fmt_size >= 0) {
str = MYTYPE(buffer);
return fmt_size;
}
buf_size *= 2;
}
return -1;
}
#endif
#endif
void Fmt(const CT* szFmt, ...) void Fmt(const CT* szFmt, ...)
{ {
va_list argList; va_list argList;
...@@ -2733,9 +2710,9 @@ public: ...@@ -2733,9 +2710,9 @@ public:
#ifdef SS_ANSI #ifdef SS_ANSI
MYTYPE str; MYTYPE str;
#if defined(__APPLE__) && !defined(_MAC_NO_APPLE) #if defined(__APPLE__)
va_start(argList, szFmt); va_start(argList, szFmt);
apple_vscwprintf(str, szFmt, argList); strings_hack_printf(str, szFmt, argList);
va_end(argList); va_end(argList);
*this = str; *this = str;
return; return;
......
#ifndef STRINGS_HACK_PRINTF_H
#define STRINGS_HACK_PRINTF_H
#include <string>
#include <string.h>
#include <wchar.h>
#include <stdarg.h>
#ifndef STD_BUF_SIZE
#define STD_BUF_SIZE 1024
#endif
int strings_hack_printf_internal(wchar_t* _buffer, size_t _size_alloc, wchar_t* _format, va_list va)
{
size_t write_size = 0;
wchar_t* tmp_format = _format;
wchar_t* tmp_buffer = _buffer;
do
{
wchar_t* buffer_cur = tmp_format;
int nSkip = 0;
while (*buffer_cur)
{
if (buffer_cur[0] == '%')
{
if (buffer_cur[1] == 's') // not crash (last symbol - 0)
{
nSkip = 2;
break;
}
else if (buffer_cur[1] == 'l' && buffer_cur[2] == 's') // not crash (last symbol - 0)
{
nSkip = 3;
break;
}
}
++buffer_cur;
}
if (nSkip > 0)
{
buffer_cur[0] = '\0';
}
if (write_size >= _size_alloc)
return -1;
if (*tmp_format)
{
int fmt_size = vswprintf(tmp_buffer, _size_alloc - write_size, tmp_format, va);
if (fmt_size < 0)
return -1;
tmp_buffer += fmt_size;
write_size += fmt_size;
}
if (nSkip)
{
wchar_t* _string_param = va_arg(va, wchar_t*);
size_t _len = wcslen(_string_param);
if (_size_alloc > (_len + write_size))
memcpy(tmp_buffer, _string_param, _len * sizeof(wchar_t));
write_size += _len;
tmp_buffer += _len;
}
tmp_format = buffer_cur + nSkip;
} while (*tmp_format);
if (write_size >= _size_alloc)
return -1;
tmp_buffer[0] = '\0';
return (int)write_size;
}
int strings_hack_printf_internal_a(char* _buffer, size_t _size_alloc, char* _format, va_list va)
{
size_t write_size = 0;
char* tmp_format = _format;
char* tmp_buffer = _buffer;
do
{
char* buffer_cur = tmp_format;
int nSkip = 0;
while (*buffer_cur)
{
if (buffer_cur[0] == '%')
{
if (buffer_cur[1] == 's') // not crash (last symbol - 0)
{
nSkip = 2;
break;
}
else if (buffer_cur[1] == 'l' && buffer_cur[2] == 's') // not crash (last symbol - 0)
{
nSkip = 3;
break;
}
}
++buffer_cur;
}
if (nSkip > 0)
{
buffer_cur[0] = '\0';
}
if (write_size >= _size_alloc)
return -1;
if (*tmp_format)
{
int fmt_size = vsnprintf(tmp_buffer, _size_alloc - write_size, tmp_format, va);
if (fmt_size < 0)
return -1;
tmp_buffer += fmt_size;
write_size += fmt_size;
}
if (nSkip)
{
char* _string_param = va_arg(va, char*);
size_t _len = strlen(_string_param);
if (_size_alloc > (_len + write_size))
memcpy(tmp_buffer, _string_param, _len * sizeof(char));
write_size += _len;
tmp_buffer += _len;
}
tmp_format = buffer_cur + nSkip;
} while (*tmp_format);
if (write_size >= _size_alloc)
return -1;
tmp_buffer[0] = '\0';
return (int)write_size;
}
template <typename T>
int strings_hack_printf(T& str, const wchar_t *format, va_list argptr)
{
int buf_size = STD_BUF_SIZE;
size_t nFormatLen = wcslen(format);
if (0 == nFormatLen)
return -1;
wchar_t* tmp_format = new wchar_t[nFormatLen];
memcpy(tmp_format, format, nFormatLen * sizeof(wchar_t));
while (buf_size < STD_BUF_SIZE * STD_BUF_SIZE)
{
va_list args;
va_copy(args, argptr);
wchar_t buffer[buf_size];
int fmt_size = strings_hack_printf_internal(buffer, sizeof(buffer)/sizeof(buffer[0]), tmp_format, args);
if (fmt_size >= 0)
{
str = T(buffer);
return fmt_size;
}
buf_size *= 2;
}
delete [] tmp_format;
return -1;
}
template <typename T>
int strings_hack_printf(T& str, const char* format, va_list argptr)
{
int buf_size = STD_BUF_SIZE;
size_t nFormatLen = strlen(format);
if (0 == nFormatLen)
return -1;
char* tmp_format = new char[nFormatLen];
memcpy(tmp_format, format, nFormatLen * sizeof(char));
while (buf_size < STD_BUF_SIZE * STD_BUF_SIZE)
{
va_list args;
va_copy(args, argptr);
char buffer[buf_size];
int fmt_size = strings_hack_printf_internal_a(buffer, sizeof(buffer)/sizeof(buffer[0]), tmp_format, args);
if (fmt_size >= 0)
{
str = T(buffer);
return fmt_size;
}
buf_size *= 2;
}
delete [] tmp_format;
return -1;
}
template<typename T, typename CHAR_TYPE>
void strings_hack_printf_exec(T& sRes, const CHAR_TYPE* szFmt, ...)
{
va_list argList;
va_start(argList, szFmt);
strings_hack_printf(sRes, szFmt, argList);
va_end(argList);
}
#endif // STRINGS_HACK_PRINTF_H
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment