]> git.saurik.com Git - wxWidgets.git/blob - src/common/sstream.cpp
added wxStringStream classes
[wxWidgets.git] / src / common / sstream.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: common/sstream.cpp
3 // Purpose: string-based streams implementation
4 // Author: Vadim Zeitlin
5 // Modified by:
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
35 // ----------------------------------------------------------------------------
36 // seek/tell
37 // ----------------------------------------------------------------------------
38
39 off_t wxStringInputStream::OnSysSeek(off_t ofs, wxSeekMode mode)
40 {
41 switch ( mode )
42 {
43 case wxFromStart:
44 // nothing to do, ofs already ok
45 break;
46
47 case wxFromEnd:
48 ofs += m_str.length()*sizeof(wxChar);
49 break;
50
51 case wxFromCurrent:
52 ofs += m_pos;
53 break;
54
55 default:
56 wxFAIL_MSG( _T("invalid seek mode") );
57 return wxInvalidOffset;
58 }
59
60 m_pos = wx_static_cast(size_t, ofs);
61
62 return ofs;
63 }
64
65 off_t wxStringInputStream::OnSysTell() const
66 {
67 return wx_static_cast(off_t, m_pos);
68 }
69
70 // ----------------------------------------------------------------------------
71 // actual IO
72 // ----------------------------------------------------------------------------
73
74 size_t wxStringInputStream::OnSysRead(void *buffer, size_t size)
75 {
76 const size_t sizeMax = m_str.length()*sizeof(wxChar) - m_pos;
77
78 if ( size >= sizeMax )
79 {
80 if ( sizeMax == 0 )
81 {
82 m_lasterror = wxSTREAM_EOF;
83 return 0;
84 }
85
86 size = sizeMax;
87 }
88
89 memcpy(buffer, m_str.data() + m_pos, size);
90 m_pos += size;
91
92 return size;
93 }
94
95 // ============================================================================
96 // wxStringOutputStream implementation
97 // ============================================================================
98
99 // ----------------------------------------------------------------------------
100 // actual IO
101 // ----------------------------------------------------------------------------
102
103 size_t wxStringOutputStream::OnSysWrite(const void *buffer, size_t size)
104 {
105 // in Unicode mode we might not be able to write the last byte
106 size_t len = size / sizeof(wxChar);
107
108 const wxChar *p = wx_static_cast(const wxChar *, buffer);
109
110 m_str->Append(wxString(p, p + len + 1));
111
112 // return number of bytes actually written
113 return len*sizeof(wxChar);
114 }
115
116 #endif // wxUSE_STREAMS
117