]> git.saurik.com Git - wxWidgets.git/blob - src/common/sstream.cpp
Fix some bugs
[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 wxFileOffset wxStringInputStream::OnSysSeek(wxFileOffset ofs, wxSeekMode mode)
40 {
41 const size_t ofsMax = m_str.length()*sizeof(wxChar);
42
43 switch ( mode )
44 {
45 case wxFromStart:
46 // nothing to do, ofs already ok
47 break;
48
49 case wxFromEnd:
50 ofs += ofsMax;
51 break;
52
53 case wxFromCurrent:
54 ofs += m_pos;
55 break;
56
57 default:
58 wxFAIL_MSG( _T("invalid seek mode") );
59 return wxInvalidOffset;
60 }
61
62 if ( ofs < 0 || wx_static_cast(size_t, ofs) >= ofsMax )
63 return wxInvalidOffset;
64
65 m_pos = wx_static_cast(size_t, ofs);
66
67 return ofs;
68 }
69
70 wxFileOffset wxStringInputStream::OnSysTell() const
71 {
72 return wx_static_cast(wxFileOffset, m_pos);
73 }
74
75 // ----------------------------------------------------------------------------
76 // actual IO
77 // ----------------------------------------------------------------------------
78
79 size_t wxStringInputStream::OnSysRead(void *buffer, size_t size)
80 {
81 const size_t sizeMax = m_str.length()*sizeof(wxChar) - m_pos;
82
83 if ( size >= sizeMax )
84 {
85 if ( sizeMax == 0 )
86 {
87 m_lasterror = wxSTREAM_EOF;
88 return 0;
89 }
90
91 size = sizeMax;
92 }
93
94 memcpy(buffer, m_str.data() + m_pos, size);
95 m_pos += size;
96
97 return size;
98 }
99
100 // ============================================================================
101 // wxStringOutputStream implementation
102 // ============================================================================
103
104 // ----------------------------------------------------------------------------
105 // seek/tell
106 // ----------------------------------------------------------------------------
107
108 wxFileOffset wxStringOutputStream::OnSysTell() const
109 {
110 return wx_static_cast(wxFileOffset, m_pos);
111 }
112
113 // ----------------------------------------------------------------------------
114 // actual IO
115 // ----------------------------------------------------------------------------
116
117 size_t wxStringOutputStream::OnSysWrite(const void *buffer, size_t size)
118 {
119 // in Unicode mode we might not be able to write the last byte
120 size_t len = size / sizeof(wxChar);
121
122 const wxChar *p = wx_static_cast(const wxChar *, buffer);
123
124 m_str->Append(wxString(p, p + len));
125
126 // return number of bytes actually written
127 len *= sizeof(wxChar);
128 m_pos += len;
129
130 return len;
131 }
132
133 #endif // wxUSE_STREAMS
134