]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: common/sstream.cpp | |
3 | // Purpose: string-based streams implementation | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: Ryan Norton (UTF8 UNICODE) | |
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 | // construction/destruction | |
37 | // ---------------------------------------------------------------------------- | |
38 | ||
39 | wxStringInputStream::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 | ||
52 | wxStringInputStream::~wxStringInputStream() | |
53 | { | |
54 | #if wxUSE_UNICODE | |
55 | // Note: wx[W]CharBuffer uses malloc()/free() | |
56 | free(m_buf); | |
57 | #endif | |
58 | } | |
59 | ||
60 | // ---------------------------------------------------------------------------- | |
61 | // getlength | |
62 | // ---------------------------------------------------------------------------- | |
63 | ||
64 | wxFileOffset wxStringInputStream::GetLength() const | |
65 | { | |
66 | return m_len; | |
67 | } | |
68 | ||
69 | // ---------------------------------------------------------------------------- | |
70 | // seek/tell | |
71 | // ---------------------------------------------------------------------------- | |
72 | ||
73 | wxFileOffset wxStringInputStream::OnSysSeek(wxFileOffset ofs, wxSeekMode mode) | |
74 | { | |
75 | switch ( mode ) | |
76 | { | |
77 | case wxFromStart: | |
78 | // nothing to do, ofs already ok | |
79 | break; | |
80 | ||
81 | case wxFromEnd: | |
82 | ofs += m_len; | |
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 | ||
94 | if ( ofs < 0 || wx_static_cast(size_t, ofs) >= m_len ) | |
95 | return wxInvalidOffset; | |
96 | ||
97 | m_pos = wx_static_cast(size_t, ofs); | |
98 | ||
99 | return ofs; | |
100 | } | |
101 | ||
102 | wxFileOffset wxStringInputStream::OnSysTell() const | |
103 | { | |
104 | return wx_static_cast(wxFileOffset, m_pos); | |
105 | } | |
106 | ||
107 | // ---------------------------------------------------------------------------- | |
108 | // actual IO | |
109 | // ---------------------------------------------------------------------------- | |
110 | ||
111 | size_t wxStringInputStream::OnSysRead(void *buffer, size_t size) | |
112 | { | |
113 | const size_t sizeMax = m_len - m_pos; | |
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 | ||
126 | memcpy(buffer, m_buf + m_pos, size); | |
127 | m_pos += size; | |
128 | ||
129 | return size; | |
130 | } | |
131 | ||
132 | // ============================================================================ | |
133 | // wxStringOutputStream implementation | |
134 | // ============================================================================ | |
135 | ||
136 | // ---------------------------------------------------------------------------- | |
137 | // seek/tell | |
138 | // ---------------------------------------------------------------------------- | |
139 | ||
140 | wxFileOffset wxStringOutputStream::OnSysTell() const | |
141 | { | |
142 | return wx_static_cast(wxFileOffset, m_pos); | |
143 | } | |
144 | ||
145 | // ---------------------------------------------------------------------------- | |
146 | // actual IO | |
147 | // ---------------------------------------------------------------------------- | |
148 | ||
149 | size_t wxStringOutputStream::OnSysWrite(const void *buffer, size_t size) | |
150 | { | |
151 | const char *p = wx_static_cast(const char *, buffer); | |
152 | ||
153 | // append the input buffer (may not be null terminated - thus | |
154 | // the literal length | |
155 | m_str->Append(wxString(p, m_conv, size)); | |
156 | ||
157 | // return number of bytes actually written | |
158 | m_pos += size; | |
159 | ||
160 | return size; | |
161 | } | |
162 | ||
163 | #endif // wxUSE_STREAMS | |
164 |