Exemple #1
0
size_t ConvertFromUTF8(const wxCharBuffer& utf8_buff, const wxMBConv& conv, wxWCharBuffer& wchar_buff,
						size_t& wchar_buff_len, wxCharBuffer& dest_buff, size_t& dest_buff_len,
						size_t char_len) { // static
	// Calculate length of conversion to widechar
	size_t wchar_len = wxConvUTF8.MB2WC(NULL, utf8_buff, 0);
	if (wchar_len == (size_t)-1) return (size_t)-1; // invalid conversion

	// Extend widechar buffer if needed
	if (wchar_buff_len < wchar_len + sizeof(wxChar)) {
		wchar_buff_len = wchar_len + sizeof(wxChar);
		wchar_buff = wxWCharBuffer(wchar_buff_len);
	}

	// Convert to widechar
	wchar_len = wxConvUTF8.MB2WC(wchar_buff.data(), utf8_buff, wchar_buff_len);
	if (wchar_len == (size_t)-1) return (size_t)-1; // invalid conversion

	// Calculate length of conversion to dest encoding
	size_t dest_len = conv.WC2MB(NULL, wchar_buff, 0);
	if (dest_len == (size_t)-1) return (size_t)-1; // invalid conversion

	// Extend dest buffer if needed
	if (dest_buff_len < dest_len + char_len) {
		dest_buff_len = dest_len + char_len;
		dest_buff = wxCharBuffer(dest_buff_len);
	}

	// Convert to dest encoding
	dest_len = conv.WC2MB(dest_buff.data(), wchar_buff,  dest_buff_len);
	if (dest_len == (size_t)-1) return (size_t)-1; // invalid conversion

	return dest_len;
}
Exemple #2
0
    // test that the conversion between str and wcs (subject to flags) succeeds
    //
    // the first argument is the index in the test array and is used solely for
    // diagnostics
    void Test(size_t n, wxMBConv& conv) const
    {
        if ( str )
        {
            wxWCharBuffer wbuf = conv.cMB2WC(str);

            if ( wcs )
            {
                CPPUNIT_ASSERT_MESSAGE
                (
                    Message(n, "MB2WC failed"),
                    wbuf.data()
                );

                CPPUNIT_ASSERT_MESSAGE
                (
                    Message(n, "MB2WC", wbuf, wcs),
                    wxStrcmp(wbuf, wcs) == 0
                );
            }
            else // conversion is supposed to fail
            {
                CPPUNIT_ASSERT_MESSAGE
                (
                    Message(n, "MB2WC succeeded"),
                    !wbuf.data()
                );
            }
        }

        if ( wcs && !(flags & ONLY_MB2WC) )
        {
            wxCharBuffer buf = conv.cWC2MB(wcs);

            if ( str )
            {
                CPPUNIT_ASSERT_MESSAGE
                (
                    Message(n, "WC2MB failed"),
                    buf.data()
                );

                CPPUNIT_ASSERT_MESSAGE
                (
                    Message(n, "WC2MB", buf, str),
                    strcmp(buf, str) == 0
                );
            }
            else
            {
                CPPUNIT_ASSERT_MESSAGE
                (
                    Message(n, "WC2MB succeeded"),
                    !buf.data()
                );
            }
        }
    }
Exemple #3
0
wxTextInputStream::wxTextInputStream(wxInputStream &s,
                                     const wxString &sep,
                                     const wxMBConv& conv)
  : m_input(s), m_separators(sep), m_conv(conv.Clone())
{
    memset((void*)m_lastBytes, 0, 10);
}
Exemple #4
0
void FileTestCase::DoRoundTripTest(const wxMBConv& conv)
{
    TestFile tf;

    const wxString data = "Hello\0UTF";

    {
        wxFile fout(tf.GetName(), wxFile::write);
        CPPUNIT_ASSERT( fout.IsOpened() );

        CPPUNIT_ASSERT( fout.Write(data, conv) );
    }

    {
        wxFile fin(tf.GetName(), wxFile::read);
        CPPUNIT_ASSERT( fin.IsOpened() );

        const ssize_t len = fin.Length();
        wxCharBuffer buf(len);
        CPPUNIT_ASSERT_EQUAL( len, fin.Read(buf.data(), len) );

        wxWCharBuffer wbuf(conv.cMB2WC(buf));
#if wxUSE_UNICODE
        CPPUNIT_ASSERT_EQUAL( data, wbuf );
#else // !wxUSE_UNICODE
        CPPUNIT_ASSERT
        (
            memcmp(wbuf, L"Hello\0UTF", data.length()*sizeof(wchar_t)) == 0
        );
#endif // wxUSE_UNICODE/!wxUSE_UNICODE
    }
}
Exemple #5
0
size_t ConvertToUTF8(const char* source, const size_t source_len, const wxMBConv& conv, wxCharBuffer& temp_buff,
					 size_t& temp_buff_len, wxWCharBuffer& wchar_buff, size_t& wchar_buff_len,
					 wxCharBuffer& utf8_buff, size_t& utf8_buff_len, size_t char_len) { // static
	// We have to copy the source string to a temporary buffer so that we can
	// make it null terminated.
	if (temp_buff_len < source_len+char_len) {
		temp_buff_len = source_len+char_len;
		temp_buff = wxCharBuffer(temp_buff_len);
	}
	memcpy(temp_buff.data(), source, source_len);
	for (unsigned int i = 0; i < char_len; ++i) temp_buff.data()[source_len+i] = '\0';

	// Calculate length of conversion to widechar
	size_t wchar_len = conv.MB2WC(NULL, temp_buff, 0);
	if (wchar_len == (size_t)-1) return (size_t)-1; // invalid conversion

	// Extend widechar buffer if needed
	if (wchar_buff_len < wchar_len + sizeof(wxChar)) {
		wchar_buff_len = wchar_len + sizeof(wxChar);
		wchar_buff = wxWCharBuffer(wchar_buff_len);
	}

	// Convert to widechar
	wchar_len = conv.MB2WC(wchar_buff.data(), temp_buff, wchar_buff_len);
	if (wchar_len == (size_t)-1) return (size_t)-1; // invalid conversion

	// Calculate length of conversion to UTF-8
	size_t utf8_len = wxConvUTF8.WC2MB(NULL, wchar_buff, 0);
	if (utf8_len == (size_t)-1) return (size_t)-1; // invalid conversion

	// Extend UTF-8 buffer if needed
	if (utf8_buff_len < utf8_len) {
		utf8_buff_len = utf8_len + 1;
		utf8_buff = wxCharBuffer(utf8_buff_len);
	}

	// Convert to UTF-8
	utf8_len = wxConvUTF8.WC2MB(utf8_buff.data(), wchar_buff, utf8_buff_len);
	if (utf8_len == (size_t)-1) return (size_t)-1; // invalid conversion

	return utf8_len;
}
Exemple #6
0
wxDataInputStream::wxDataInputStream(wxInputStream& s, const wxMBConv& conv)
    : m_input(&s), m_be_order(false), m_conv(conv.Clone())
#else
wxDataInputStream::wxDataInputStream(wxInputStream& s)
    : m_input(&s), m_be_order(false)
#endif
{
}

wxDataInputStream::~wxDataInputStream()
{
#if wxUSE_UNICODE
    delete m_conv;
#endif // wxUSE_UNICODE
}

#if wxUSE_UNICODE
void wxDataInputStream::SetConv( const wxMBConv &conv )
{
    delete m_conv;
    m_conv = conv.Clone();
}
wxGzipOutputStream::wxGzipOutputStream(
                        wxOutputStream& stream,
                        const wxString& originalName /*=wxEmptyString*/,
#if wxUSE_DATETIME
                        const wxDateTime& originalTime /*=wxDateTime::Now()*/,
#endif
                        int level /*=-1*/,
                        wxMBConv& conv /*=wxConvFile*/)
  : wxFilterOutputStream(stream)
{
    m_comp = NULL;
    m_crc = crc32(0, Z_NULL, 0);

    wxFileName filename(originalName);

    wxUint32 timestamp = 0;
#if wxUSE_DATETIME
    if (originalTime.IsValid())
        timestamp = (originalTime.GetValue() / 1000L).GetLo();
#endif

    // RFC-1952 specifies ISO-8859-1 for the name. Also it should be just the
    // name part, no directory, folded to lowercase if case insensitive
    wxString name = filename.GetFullName();
    const wxWX2MBbuf mbName = conv.cWX2MB(name);
    
    wxDataOutputStream ds(*m_parent_o_stream);

    // write signature, method, flags, timestamp, extra flags and OS-code
    ds.Write16(GZ_MAGIC);
    ds.Write8(Z_DEFLATED);
    ds.Write8(mbName && *mbName ? GZ_ORIG_NAME : 0);
    ds.Write32(timestamp);
    ds.Write8(level == 1 ? GZ_FASTEST : level == 9 ? GZ_SLOWEST : 0);
    ds.Write8(255);

    if (mbName && *mbName)
        m_parent_o_stream->Write(mbName, strlen(mbName) + 1);

    m_lasterror = wxSTREAM_WRITE_ERROR;
    if (!*m_parent_o_stream) {
        wxLogDebug(wxT("Error writing Gzip header"));
        return;
    }

    m_comp = new wxZlibOutputStream(*m_parent_o_stream, level, wxZLIB_NO_HEADER);

    if (m_comp)
        m_lasterror = m_comp->GetLastError();
}
Exemple #8
0
wxDataStreamBase::wxDataStreamBase(const wxMBConv& conv)
#if wxUSE_UNICODE
    : m_conv(conv.Clone())
#endif // wxUSE_UNICODE
{
    // It is unused in non-Unicode build, so suppress a warning there.
    wxUnusedVar(conv);

    m_be_order = false;

    // For compatibility with the existing data files, we use extended
    // precision if it is available, i.e. if wxUSE_APPLE_IEEE is on.
#if wxUSE_APPLE_IEEE
    m_useExtendedPrecision = true;
#endif // wxUSE_APPLE_IEEE
}
Exemple #9
0
wxString CTextFile::GetNextLine(const wxMBConv& conv)
{
	wxCHECK_MSG(m_file.IsOpened(), wxEmptyString, wxT("Trying to read from closed file."));
	wxCHECK_MSG(!m_file.Eof(), wxEmptyString, wxT("Trying to read past EOF"));
	wxCHECK_MSG((m_mode == read), wxEmptyString, wxT("Trying to read from non-readable file."));


	wxString line;
	char buffer[TXTBUF_SIZE];

	// Loop until EOF (fgets will then return NULL) or a newline is read.
	while (fgets(buffer, TXTBUF_SIZE, m_file.fp())) {
		// NB: The majority of the time spent by this function is
		//     spent converting the multibyte string to wide-char.
		line += conv.cMB2WC(buffer);

		// Remove any newlines, carriage returns, etc.
		if (line.Length() && (line.Last() == wxT('\n'))) {
			if ((line.Length() > 1)) {
				if (line[line.Length() - 2] == wxT('\r')) {
					// Carriage return + newline
					line.RemoveLast(2);
				} else {
					// Only a newline.
					line.RemoveLast(1);					
				}
			} else {
				// Empty line
				line.Clear();
			}

			// We've read an entire line.
			break;
		}
	}

	return line;
}
Exemple #10
0
bool CTextFile::WriteLine(const wxString& line, const wxMBConv& conv)
{
	wxCHECK_MSG(m_file.IsOpened(), false, wxT("Trying to read from closed file."));
	wxCHECK_MSG((m_mode == write), false, wxT("Trying to read from non-readable file."));

	// Ensures that use of newlines/carriage-returns matches the OS
	wxString result = wxTextBuffer::Translate(line);
	
	// Only add line-breaks between lines, as otherwise the number of
	// lines would grow as the result of the addition of an empty line,
	// at the end of the file.
	if (m_file.Tell() > 0) {
		result = wxTextBuffer::GetEOL() + result;
	}

	wxCharBuffer strBuffer = conv.cWC2MB(result);
	if (strBuffer) {
		const size_t length = strlen(strBuffer);

		return (m_file.Write(strBuffer, length) == length);
	}
	
	return false;
}
Exemple #11
0
wxDataInputStream::wxDataInputStream(wxInputStream& s, const wxMBConv& conv)
  : m_input(&s), m_be_order(false), m_conv(conv.Clone())
#else
wxDataInputStream::wxDataInputStream(wxInputStream& s)
  : m_input(&s), m_be_order(false)
#endif
{
}

wxDataInputStream::~wxDataInputStream()
{
#if wxUSE_UNICODE
    delete m_conv;
#endif // wxUSE_UNICODE
}

#if wxHAS_INT64
wxUint64 wxDataInputStream::Read64()
{
  wxUint64 tmp;
  Read64(&tmp, 1);
  return tmp;
}
#endif // wxHAS_INT64

wxUint32 wxDataInputStream::Read32()
{
  wxUint32 i32;

  m_input->Read(&i32, 4);

  if (m_be_order)
    return wxUINT32_SWAP_ON_LE(i32);
  else
    return wxUINT32_SWAP_ON_BE(i32);
}

wxUint16 wxDataInputStream::Read16()
{
  wxUint16 i16;

  m_input->Read(&i16, 2);

  if (m_be_order)
    return wxUINT16_SWAP_ON_LE(i16);
  else
    return wxUINT16_SWAP_ON_BE(i16);
}

wxUint8 wxDataInputStream::Read8()
{
  wxUint8 buf;

  m_input->Read(&buf, 1);
  return (wxUint8)buf;
}

double wxDataInputStream::ReadDouble()
{
#if wxUSE_APPLE_IEEE
  char buf[10];

  m_input->Read(buf, 10);
  return ConvertFromIeeeExtended((const wxInt8 *)buf);
#else
  return 0.0;
#endif
}
Exemple #12
0
void wxDataStreamBase::SetConv( const wxMBConv &conv )
{
    delete m_conv;
    m_conv = conv.Clone();
}