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