]> git.saurik.com Git - wxWidgets.git/blame - src/common/sstream.cpp
compilation fix for WXWIN_COMPATIBILITY_2_4 == 0
[wxWidgets.git] / src / common / sstream.cpp
CommitLineData
121fa06a
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: common/sstream.cpp
3// Purpose: string-based streams implementation
4// Author: Vadim Zeitlin
c5d99d7b 5// Modified by: Ryan Norton (UTF8 UNICODE)
121fa06a
VZ
6// Created: 2004-09-19
7// RCS-ID: $Id$
8// Copyright: (c) 2004 Vadim Zeitlin <vadim@wxwindows.org>
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27#if wxUSE_STREAMS
28
29#include "wx/sstream.h"
30
31// ============================================================================
32// wxStringInputStream implementation
33// ============================================================================
34
c5d99d7b
RN
35// ----------------------------------------------------------------------------
36// construction/destruction
37// ----------------------------------------------------------------------------
38
39wxStringInputStream::wxStringInputStream(const wxString& s)
40#if wxUSE_UNICODE
41 : m_str(s), m_buf(wxMBConvUTF8().cWX2MB(s).release()), m_len(strlen(m_buf))
42#else
43 : m_str(s), m_buf((char*)s.c_str()), m_len(s.length())
44#endif
45{
46#if wxUSE_UNICODE
47 wxASSERT_MSG(m_buf != NULL, _T("Could not convert string to UTF8!"));
48#endif
49 m_pos = 0;
50}
51
52wxStringInputStream::~wxStringInputStream()
53{
54#if wxUSE_UNICODE
6d5a3448 55 delete[] m_buf;
c5d99d7b
RN
56#endif
57}
58
59// ----------------------------------------------------------------------------
60// getlength
61// ----------------------------------------------------------------------------
62
63wxFileOffset wxStringInputStream::GetLength() const
64{
65 return m_len;
66}
67
121fa06a
VZ
68// ----------------------------------------------------------------------------
69// seek/tell
70// ----------------------------------------------------------------------------
71
4004775e 72wxFileOffset wxStringInputStream::OnSysSeek(wxFileOffset ofs, wxSeekMode mode)
121fa06a
VZ
73{
74 switch ( mode )
75 {
76 case wxFromStart:
77 // nothing to do, ofs already ok
78 break;
79
80 case wxFromEnd:
c5d99d7b 81 ofs += m_len;
121fa06a
VZ
82 break;
83
84 case wxFromCurrent:
85 ofs += m_pos;
86 break;
87
88 default:
89 wxFAIL_MSG( _T("invalid seek mode") );
90 return wxInvalidOffset;
91 }
92
c5d99d7b 93 if ( ofs < 0 || wx_static_cast(size_t, ofs) >= m_len )
5a4c7f71
VZ
94 return wxInvalidOffset;
95
121fa06a
VZ
96 m_pos = wx_static_cast(size_t, ofs);
97
98 return ofs;
99}
100
4004775e 101wxFileOffset wxStringInputStream::OnSysTell() const
121fa06a 102{
4004775e 103 return wx_static_cast(wxFileOffset, m_pos);
121fa06a
VZ
104}
105
106// ----------------------------------------------------------------------------
107// actual IO
108// ----------------------------------------------------------------------------
109
110size_t wxStringInputStream::OnSysRead(void *buffer, size_t size)
111{
c5d99d7b 112 const size_t sizeMax = m_len - m_pos;
121fa06a
VZ
113
114 if ( size >= sizeMax )
115 {
116 if ( sizeMax == 0 )
117 {
118 m_lasterror = wxSTREAM_EOF;
119 return 0;
120 }
121
122 size = sizeMax;
123 }
124
c5d99d7b 125 memcpy(buffer, m_buf + m_pos, size);
121fa06a
VZ
126 m_pos += size;
127
128 return size;
129}
130
131// ============================================================================
132// wxStringOutputStream implementation
133// ============================================================================
134
a5ea75bc
VZ
135// ----------------------------------------------------------------------------
136// seek/tell
137// ----------------------------------------------------------------------------
138
4004775e 139wxFileOffset wxStringOutputStream::OnSysTell() const
a5ea75bc 140{
4004775e 141 return wx_static_cast(wxFileOffset, m_pos);
a5ea75bc
VZ
142}
143
121fa06a
VZ
144// ----------------------------------------------------------------------------
145// actual IO
146// ----------------------------------------------------------------------------
147
148size_t wxStringOutputStream::OnSysWrite(const void *buffer, size_t size)
149{
c5d99d7b 150 const char *p = wx_static_cast(const char *, buffer);
121fa06a 151
c5d99d7b
RN
152 // append the input buffer (may not be null terminated - thus
153 // the literal length
154 m_str->Append(wxString(p, m_conv, size));
121fa06a
VZ
155
156 // return number of bytes actually written
c5d99d7b 157 m_pos += size;
a5ea75bc 158
c5d99d7b 159 return size;
121fa06a
VZ
160}
161
162#endif // wxUSE_STREAMS
163