]> git.saurik.com Git - wxWidgets.git/blob - include/wx/txtstrm.h
Ensure wxCharTypeBuffer data is NUL-terminated after extend() call.
[wxWidgets.git] / include / wx / txtstrm.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/txtstrm.h
3 // Purpose: Text stream classes
4 // Author: Guilhem Lavaux
5 // Modified by:
6 // Created: 28/06/1998
7 // RCS-ID: $Id$
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_TXTSTREAM_H_
13 #define _WX_TXTSTREAM_H_
14
15 #include "wx/stream.h"
16 #include "wx/convauto.h"
17
18 #if wxUSE_STREAMS
19
20 class WXDLLIMPEXP_FWD_BASE wxTextInputStream;
21 class WXDLLIMPEXP_FWD_BASE wxTextOutputStream;
22
23 typedef wxTextInputStream& (*__wxTextInputManip)(wxTextInputStream&);
24 typedef wxTextOutputStream& (*__wxTextOutputManip)(wxTextOutputStream&);
25
26 WXDLLIMPEXP_BASE wxTextOutputStream &endl( wxTextOutputStream &stream );
27
28
29 #define wxEOT wxT('\4') // the End-Of-Text control code (used only inside wxTextInputStream)
30
31 // If you're scanning through a file using wxTextInputStream, you should check for EOF _before_
32 // reading the next item (word / number), because otherwise the last item may get lost.
33 // You should however be prepared to receive an empty item (empty string / zero number) at the
34 // end of file, especially on Windows systems. This is unavoidable because most (but not all) files end
35 // with whitespace (i.e. usually a newline).
36 class WXDLLIMPEXP_BASE wxTextInputStream
37 {
38 public:
39 #if wxUSE_UNICODE
40 wxTextInputStream(wxInputStream& s,
41 const wxString &sep=wxT(" \t"),
42 const wxMBConv& conv = wxConvAuto());
43 #else
44 wxTextInputStream(wxInputStream& s, const wxString &sep=wxT(" \t"));
45 #endif
46 ~wxTextInputStream();
47
48 const wxInputStream& GetInputStream() const { return m_input; }
49
50 wxUint32 Read32(int base = 10); // base may be between 2 and 36, inclusive, or the special 0 (= C format)
51 wxUint16 Read16(int base = 10);
52 wxUint8 Read8(int base = 10);
53 wxInt32 Read32S(int base = 10);
54 wxInt16 Read16S(int base = 10);
55 wxInt8 Read8S(int base = 10);
56 double ReadDouble();
57 wxString ReadLine();
58 wxString ReadWord();
59 wxChar GetChar() { wxChar c = NextChar(); return (wxChar)(c != wxEOT ? c : 0); }
60
61 wxString GetStringSeparators() const { return m_separators; }
62 void SetStringSeparators(const wxString &c) { m_separators = c; }
63
64 // Operators
65 wxTextInputStream& operator>>(wxString& word);
66 wxTextInputStream& operator>>(char& c);
67 #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
68 wxTextInputStream& operator>>(wchar_t& wc);
69 #endif // wxUSE_UNICODE
70 wxTextInputStream& operator>>(wxInt16& i);
71 wxTextInputStream& operator>>(wxInt32& i);
72 wxTextInputStream& operator>>(wxUint16& i);
73 wxTextInputStream& operator>>(wxUint32& i);
74 wxTextInputStream& operator>>(double& i);
75 wxTextInputStream& operator>>(float& f);
76
77 wxTextInputStream& operator>>( __wxTextInputManip func) { return func(*this); }
78
79 #if WXWIN_COMPATIBILITY_2_6
80 wxDEPRECATED( wxString ReadString() ); // use ReadLine or ReadWord instead
81 #endif // WXWIN_COMPATIBILITY_2_6
82
83 protected:
84 wxInputStream &m_input;
85 wxString m_separators;
86 char m_lastBytes[10]; // stores the bytes that were read for the last character
87
88 #if wxUSE_UNICODE
89 wxMBConv *m_conv;
90 #endif
91
92 bool EatEOL(const wxChar &c);
93 void UngetLast(); // should be used instead of wxInputStream::Ungetch() because of Unicode issues
94 // returns EOT (\4) if there is a stream error, or end of file
95 wxChar NextChar(); // this should be used instead of GetC() because of Unicode issues
96 wxChar NextNonSeparators();
97
98 wxDECLARE_NO_COPY_CLASS(wxTextInputStream);
99 };
100
101 typedef enum
102 {
103 wxEOL_NATIVE,
104 wxEOL_UNIX,
105 wxEOL_MAC,
106 wxEOL_DOS
107 } wxEOL;
108
109 class WXDLLIMPEXP_BASE wxTextOutputStream
110 {
111 public:
112 #if wxUSE_UNICODE
113 wxTextOutputStream(wxOutputStream& s,
114 wxEOL mode = wxEOL_NATIVE,
115 const wxMBConv& conv = wxConvAuto());
116 #else
117 wxTextOutputStream(wxOutputStream& s, wxEOL mode = wxEOL_NATIVE);
118 #endif
119 virtual ~wxTextOutputStream();
120
121 const wxOutputStream& GetOutputStream() const { return m_output; }
122
123 void SetMode( wxEOL mode = wxEOL_NATIVE );
124 wxEOL GetMode() { return m_mode; }
125
126 void Write32(wxUint32 i);
127 void Write16(wxUint16 i);
128 void Write8(wxUint8 i);
129 virtual void WriteDouble(double d);
130 virtual void WriteString(const wxString& string);
131
132 wxTextOutputStream& PutChar(wxChar c);
133
134 void Flush();
135
136 wxTextOutputStream& operator<<(const wxString& string);
137 wxTextOutputStream& operator<<(char c);
138 #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE
139 wxTextOutputStream& operator<<(wchar_t wc);
140 #endif // wxUSE_UNICODE
141 wxTextOutputStream& operator<<(wxInt16 c);
142 wxTextOutputStream& operator<<(wxInt32 c);
143 wxTextOutputStream& operator<<(wxUint16 c);
144 wxTextOutputStream& operator<<(wxUint32 c);
145 wxTextOutputStream& operator<<(double f);
146 wxTextOutputStream& operator<<(float f);
147
148 wxTextOutputStream& operator<<( __wxTextOutputManip func) { return func(*this); }
149
150 protected:
151 wxOutputStream &m_output;
152 wxEOL m_mode;
153
154 #if wxUSE_UNICODE
155 wxMBConv *m_conv;
156 #endif
157
158 wxDECLARE_NO_COPY_CLASS(wxTextOutputStream);
159 };
160
161 #endif
162 // wxUSE_STREAMS
163
164 #endif
165 // _WX_DATSTREAM_H_