]>
Commit | Line | Data |
---|---|---|
a1b82138 | 1 | ///////////////////////////////////////////////////////////////////////////// |
0e0a4e64 | 2 | // Name: wx/ffile.h |
a1b82138 VZ |
3 | // Purpose: wxFFile - encapsulates "FILE *" stream |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 14.07.99 | |
a1b82138 | 7 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> |
65571936 | 8 | // Licence: wxWindows licence |
a1b82138 VZ |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | #ifndef _WX_FFILE_H_ | |
12 | #define _WX_FFILE_H_ | |
13 | ||
0e0a4e64 VZ |
14 | #include "wx/defs.h" // for wxUSE_FFILE |
15 | ||
1e6feb95 | 16 | #if wxUSE_FFILE |
a1b82138 | 17 | |
20ceebaa MW |
18 | #include "wx/string.h" |
19 | #include "wx/filefn.h" | |
830f8f11 | 20 | #include "wx/convauto.h" |
a1b82138 VZ |
21 | |
22 | #include <stdio.h> | |
23 | ||
24 | // ---------------------------------------------------------------------------- | |
25 | // class wxFFile: standard C stream library IO | |
26 | // | |
27 | // NB: for space efficiency this class has no virtual functions, including | |
28 | // dtor which is _not_ virtual, so it shouldn't be used as a base class. | |
29 | // ---------------------------------------------------------------------------- | |
30 | ||
bddd7a8d | 31 | class WXDLLIMPEXP_BASE wxFFile |
a1b82138 VZ |
32 | { |
33 | public: | |
34 | // ctors | |
35 | // ----- | |
36 | // def ctor | |
37 | wxFFile() { m_fp = NULL; } | |
38 | // open specified file (may fail, use IsOpened()) | |
9a83f860 | 39 | wxFFile(const wxString& filename, const wxString& mode = wxT("r")); |
a1b82138 | 40 | // attach to (already opened) file |
222702b1 | 41 | wxFFile(FILE *lfp) { m_fp = lfp; } |
a1b82138 VZ |
42 | |
43 | // open/close | |
44 | // open a file (existing or not - the mode controls what happens) | |
9a83f860 | 45 | bool Open(const wxString& filename, const wxString& mode = wxT("r")); |
a1b82138 VZ |
46 | // closes the opened file (this is a NOP if not opened) |
47 | bool Close(); | |
48 | ||
49 | // assign an existing file descriptor and get it back from wxFFile object | |
222702b1 WS |
50 | void Attach(FILE *lfp, const wxString& name = wxEmptyString) |
51 | { Close(); m_fp = lfp; m_name = name; } | |
a1b82138 VZ |
52 | void Detach() { m_fp = NULL; } |
53 | FILE *fp() const { return m_fp; } | |
54 | ||
55 | // read/write (unbuffered) | |
56 | // read all data from the file into a string (useful for text files) | |
830f8f11 | 57 | bool ReadAll(wxString *str, const wxMBConv& conv = wxConvAuto()); |
a1b82138 | 58 | // returns number of bytes read - use Eof() and Error() to see if an error |
3103e8a9 | 59 | // occurred or not |
a1b82138 VZ |
60 | size_t Read(void *pBuf, size_t nCount); |
61 | // returns the number of bytes written | |
62 | size_t Write(const void *pBuf, size_t nCount); | |
63 | // returns true on success | |
b1c67394 | 64 | bool Write(const wxString& s, const wxMBConv& conv = wxConvAuto()); |
a1b82138 VZ |
65 | // flush data not yet written |
66 | bool Flush(); | |
67 | ||
68 | // file pointer operations (return ofsInvalid on failure) | |
69 | // move ptr ofs bytes related to start/current pos/end of file | |
70a7bd90 | 70 | bool Seek(wxFileOffset ofs, wxSeekMode mode = wxFromStart); |
a1b82138 | 71 | // move ptr to ofs bytes before the end |
70a7bd90 | 72 | bool SeekEnd(wxFileOffset ofs = 0) { return Seek(ofs, wxFromEnd); } |
a1b82138 | 73 | // get current position in the file |
70a7bd90 | 74 | wxFileOffset Tell() const; |
a1b82138 | 75 | // get current file length |
70a7bd90 | 76 | wxFileOffset Length() const; |
a1b82138 | 77 | |
de2ce07c VZ |
78 | // simple accessors: note that Eof() and Error() may only be called if |
79 | // IsOpened()! | |
a1b82138 VZ |
80 | // is file opened? |
81 | bool IsOpened() const { return m_fp != NULL; } | |
82 | // is end of file reached? | |
83 | bool Eof() const { return feof(m_fp) != 0; } | |
3103e8a9 | 84 | // has an error occurred? |
a1b82138 VZ |
85 | bool Error() const { return ferror(m_fp) != 0; } |
86 | // get the file name | |
87 | const wxString& GetName() const { return m_name; } | |
3c70014d | 88 | // type such as disk or pipe |
0912690b | 89 | wxFileKind GetKind() const { return wxGetFileKind(m_fp); } |
a1b82138 VZ |
90 | |
91 | // dtor closes the file if opened | |
92 | ~wxFFile() { Close(); } | |
93 | ||
94 | private: | |
95 | // copy ctor and assignment operator are private because it doesn't make | |
96 | // sense to copy files this way: attempt to do it will provoke a compile-time | |
97 | // error. | |
98 | wxFFile(const wxFFile&); | |
99 | wxFFile& operator=(const wxFFile&); | |
100 | ||
101 | FILE *m_fp; // IO stream or NULL if not opened | |
102 | ||
103 | wxString m_name; // the name of the file (for diagnostic messages) | |
104 | }; | |
105 | ||
1e6feb95 | 106 | #endif // wxUSE_FFILE |
a1b82138 VZ |
107 | |
108 | #endif // _WX_FFILE_H_ | |
109 |