]> git.saurik.com Git - wxWidgets.git/blame - src/common/sstream.cpp
define wxNO_RTTI/EXCEPTIONS/THREADS if corresponding USE_XXX is set to 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
da6e77b8
MB
55 // Note: wx[W]CharBuffer uses malloc()/free()
56 free(m_buf);
c5d99d7b
RN
57#endif
58}
59
60// ----------------------------------------------------------------------------
61// getlength
62// ----------------------------------------------------------------------------
63
64wxFileOffset wxStringInputStream::GetLength() const
65{
66 return m_len;
67}
68
121fa06a
VZ
69// ----------------------------------------------------------------------------
70// seek/tell
71// ----------------------------------------------------------------------------
72
4004775e 73wxFileOffset wxStringInputStream::OnSysSeek(wxFileOffset ofs, wxSeekMode mode)
121fa06a
VZ
74{
75 switch ( mode )
76 {
77 case wxFromStart:
78 // nothing to do, ofs already ok
79 break;
80
81 case wxFromEnd:
c5d99d7b 82 ofs += m_len;
121fa06a
VZ
83 break;
84
85 case wxFromCurrent:
86 ofs += m_pos;
87 break;
88
89 default:
90 wxFAIL_MSG( _T("invalid seek mode") );
91 return wxInvalidOffset;
92 }
93
c5d99d7b 94 if ( ofs < 0 || wx_static_cast(size_t, ofs) >= m_len )
5a4c7f71
VZ
95 return wxInvalidOffset;
96
121fa06a
VZ
97 m_pos = wx_static_cast(size_t, ofs);
98
99 return ofs;
100}
101
4004775e 102wxFileOffset wxStringInputStream::OnSysTell() const
121fa06a 103{
4004775e 104 return wx_static_cast(wxFileOffset, m_pos);
121fa06a
VZ
105}
106
107// ----------------------------------------------------------------------------
108// actual IO
109// ----------------------------------------------------------------------------
110
111size_t wxStringInputStream::OnSysRead(void *buffer, size_t size)
112{
c5d99d7b 113 const size_t sizeMax = m_len - m_pos;
121fa06a
VZ
114
115 if ( size >= sizeMax )
116 {
117 if ( sizeMax == 0 )
118 {
119 m_lasterror = wxSTREAM_EOF;
120 return 0;
121 }
122
123 size = sizeMax;
124 }
125
c5d99d7b 126 memcpy(buffer, m_buf + m_pos, size);
121fa06a
VZ
127 m_pos += size;
128
129 return size;
130}
131
132// ============================================================================
133// wxStringOutputStream implementation
134// ============================================================================
135
a5ea75bc
VZ
136// ----------------------------------------------------------------------------
137// seek/tell
138// ----------------------------------------------------------------------------
139
4004775e 140wxFileOffset wxStringOutputStream::OnSysTell() const
a5ea75bc 141{
4004775e 142 return wx_static_cast(wxFileOffset, m_pos);
a5ea75bc
VZ
143}
144
121fa06a
VZ
145// ----------------------------------------------------------------------------
146// actual IO
147// ----------------------------------------------------------------------------
148
149size_t wxStringOutputStream::OnSysWrite(const void *buffer, size_t size)
150{
c5d99d7b 151 const char *p = wx_static_cast(const char *, buffer);
121fa06a 152
c5d99d7b
RN
153 // append the input buffer (may not be null terminated - thus
154 // the literal length
155 m_str->Append(wxString(p, m_conv, size));
121fa06a
VZ
156
157 // return number of bytes actually written
c5d99d7b 158 m_pos += size;
a5ea75bc 159
c5d99d7b 160 return size;
121fa06a
VZ
161}
162
163#endif // wxUSE_STREAMS
164