| 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 | wxUint32 Read32(int base = 10); // base may be between 2 and 36, inclusive, or the special 0 (= C format) |
| 49 | wxUint16 Read16(int base = 10); |
| 50 | wxUint8 Read8(int base = 10); |
| 51 | wxInt32 Read32S(int base = 10); |
| 52 | wxInt16 Read16S(int base = 10); |
| 53 | wxInt8 Read8S(int base = 10); |
| 54 | double ReadDouble(); |
| 55 | wxString ReadLine(); |
| 56 | wxString ReadWord(); |
| 57 | wxChar GetChar() { wxChar c = NextChar(); return (wxChar)(c != wxEOT ? c : 0); } |
| 58 | |
| 59 | wxString GetStringSeparators() const { return m_separators; } |
| 60 | void SetStringSeparators(const wxString &c) { m_separators = c; } |
| 61 | |
| 62 | // Operators |
| 63 | wxTextInputStream& operator>>(wxString& word); |
| 64 | wxTextInputStream& operator>>(char& c); |
| 65 | #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE |
| 66 | wxTextInputStream& operator>>(wchar_t& wc); |
| 67 | #endif // wxUSE_UNICODE |
| 68 | wxTextInputStream& operator>>(wxInt16& i); |
| 69 | wxTextInputStream& operator>>(wxInt32& i); |
| 70 | wxTextInputStream& operator>>(wxUint16& i); |
| 71 | wxTextInputStream& operator>>(wxUint32& i); |
| 72 | wxTextInputStream& operator>>(double& i); |
| 73 | wxTextInputStream& operator>>(float& f); |
| 74 | |
| 75 | wxTextInputStream& operator>>( __wxTextInputManip func) { return func(*this); } |
| 76 | |
| 77 | #if WXWIN_COMPATIBILITY_2_6 |
| 78 | wxDEPRECATED( wxString ReadString() ); // use ReadLine or ReadWord instead |
| 79 | #endif // WXWIN_COMPATIBILITY_2_6 |
| 80 | |
| 81 | protected: |
| 82 | wxInputStream &m_input; |
| 83 | wxString m_separators; |
| 84 | char m_lastBytes[10]; // stores the bytes that were read for the last character |
| 85 | |
| 86 | #if wxUSE_UNICODE |
| 87 | wxMBConv *m_conv; |
| 88 | #endif |
| 89 | |
| 90 | bool EatEOL(const wxChar &c); |
| 91 | void UngetLast(); // should be used instead of wxInputStream::Ungetch() because of Unicode issues |
| 92 | // returns EOT (\4) if there is a stream error, or end of file |
| 93 | wxChar NextChar(); // this should be used instead of GetC() because of Unicode issues |
| 94 | wxChar NextNonSeparators(); |
| 95 | |
| 96 | wxDECLARE_NO_COPY_CLASS(wxTextInputStream); |
| 97 | }; |
| 98 | |
| 99 | typedef enum |
| 100 | { |
| 101 | wxEOL_NATIVE, |
| 102 | wxEOL_UNIX, |
| 103 | wxEOL_MAC, |
| 104 | wxEOL_DOS |
| 105 | } wxEOL; |
| 106 | |
| 107 | class WXDLLIMPEXP_BASE wxTextOutputStream |
| 108 | { |
| 109 | public: |
| 110 | #if wxUSE_UNICODE |
| 111 | wxTextOutputStream(wxOutputStream& s, |
| 112 | wxEOL mode = wxEOL_NATIVE, |
| 113 | const wxMBConv& conv = wxConvAuto()); |
| 114 | #else |
| 115 | wxTextOutputStream(wxOutputStream& s, wxEOL mode = wxEOL_NATIVE); |
| 116 | #endif |
| 117 | virtual ~wxTextOutputStream(); |
| 118 | |
| 119 | void SetMode( wxEOL mode = wxEOL_NATIVE ); |
| 120 | wxEOL GetMode() { return m_mode; } |
| 121 | |
| 122 | void Write32(wxUint32 i); |
| 123 | void Write16(wxUint16 i); |
| 124 | void Write8(wxUint8 i); |
| 125 | virtual void WriteDouble(double d); |
| 126 | virtual void WriteString(const wxString& string); |
| 127 | |
| 128 | wxTextOutputStream& PutChar(wxChar c); |
| 129 | |
| 130 | void Flush(); |
| 131 | |
| 132 | wxTextOutputStream& operator<<(const wxString& string); |
| 133 | wxTextOutputStream& operator<<(char c); |
| 134 | #if wxUSE_UNICODE && wxWCHAR_T_IS_REAL_TYPE |
| 135 | wxTextOutputStream& operator<<(wchar_t wc); |
| 136 | #endif // wxUSE_UNICODE |
| 137 | wxTextOutputStream& operator<<(wxInt16 c); |
| 138 | wxTextOutputStream& operator<<(wxInt32 c); |
| 139 | wxTextOutputStream& operator<<(wxUint16 c); |
| 140 | wxTextOutputStream& operator<<(wxUint32 c); |
| 141 | wxTextOutputStream& operator<<(double f); |
| 142 | wxTextOutputStream& operator<<(float f); |
| 143 | |
| 144 | wxTextOutputStream& operator<<( __wxTextOutputManip func) { return func(*this); } |
| 145 | |
| 146 | protected: |
| 147 | wxOutputStream &m_output; |
| 148 | wxEOL m_mode; |
| 149 | |
| 150 | #if wxUSE_UNICODE |
| 151 | wxMBConv *m_conv; |
| 152 | #endif |
| 153 | |
| 154 | wxDECLARE_NO_COPY_CLASS(wxTextOutputStream); |
| 155 | }; |
| 156 | |
| 157 | #endif |
| 158 | // wxUSE_STREAMS |
| 159 | |
| 160 | #endif |
| 161 | // _WX_DATSTREAM_H_ |