]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: wx/ffile.h | |
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> | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef _WX_FFILE_H_ | |
13 | #define _WX_FFILE_H_ | |
14 | ||
15 | #include "wx/defs.h" // for wxUSE_FFILE | |
16 | ||
17 | #if wxUSE_FFILE | |
18 | ||
19 | #include "wx/string.h" | |
20 | #include "wx/filefn.h" | |
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 | ||
31 | class WXDLLIMPEXP_BASE wxFFile | |
32 | { | |
33 | public: | |
34 | // ctors | |
35 | // ----- | |
36 | // def ctor | |
37 | wxFFile() { m_fp = NULL; } | |
38 | // open specified file (may fail, use IsOpened()) | |
39 | wxFFile(const wxChar *filename, const wxChar *mode = _T("r")); | |
40 | // attach to (already opened) file | |
41 | wxFFile(FILE *lfp) { m_fp = lfp; } | |
42 | ||
43 | // open/close | |
44 | // open a file (existing or not - the mode controls what happens) | |
45 | bool Open(const wxChar *filename, const wxChar *mode = _T("r")); | |
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 | |
50 | void Attach(FILE *lfp, const wxString& name = wxEmptyString) | |
51 | { Close(); m_fp = lfp; m_name = name; } | |
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) | |
57 | bool ReadAll(wxString *str, wxMBConv& conv = wxConvUTF8); | |
58 | // returns number of bytes read - use Eof() and Error() to see if an error | |
59 | // occurred or not | |
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 | |
64 | bool Write(const wxString& s, wxMBConv& conv = wxConvUTF8) | |
65 | { | |
66 | const wxWX2MBbuf buf = s.mb_str(conv); | |
67 | size_t size = strlen(buf); | |
68 | return Write((const char *)buf, size) == size; | |
69 | } | |
70 | // flush data not yet written | |
71 | bool Flush(); | |
72 | ||
73 | // file pointer operations (return ofsInvalid on failure) | |
74 | // move ptr ofs bytes related to start/current pos/end of file | |
75 | bool Seek(wxFileOffset ofs, wxSeekMode mode = wxFromStart); | |
76 | // move ptr to ofs bytes before the end | |
77 | bool SeekEnd(wxFileOffset ofs = 0) { return Seek(ofs, wxFromEnd); } | |
78 | // get current position in the file | |
79 | wxFileOffset Tell() const; | |
80 | // get current file length | |
81 | wxFileOffset Length() const; | |
82 | ||
83 | // simple accessors: note that Eof() and Error() may only be called if | |
84 | // IsOpened()! | |
85 | // is file opened? | |
86 | bool IsOpened() const { return m_fp != NULL; } | |
87 | // is end of file reached? | |
88 | bool Eof() const { return feof(m_fp) != 0; } | |
89 | // has an error occurred? | |
90 | bool Error() const { return ferror(m_fp) != 0; } | |
91 | // get the file name | |
92 | const wxString& GetName() const { return m_name; } | |
93 | // type such as disk or pipe | |
94 | wxFileKind GetKind() const { return wxGetFileKind(m_fp); } | |
95 | ||
96 | // dtor closes the file if opened | |
97 | ~wxFFile() { Close(); } | |
98 | ||
99 | private: | |
100 | // copy ctor and assignment operator are private because it doesn't make | |
101 | // sense to copy files this way: attempt to do it will provoke a compile-time | |
102 | // error. | |
103 | wxFFile(const wxFFile&); | |
104 | wxFFile& operator=(const wxFFile&); | |
105 | ||
106 | FILE *m_fp; // IO stream or NULL if not opened | |
107 | ||
108 | wxString m_name; // the name of the file (for diagnostic messages) | |
109 | }; | |
110 | ||
111 | #endif // wxUSE_FFILE | |
112 | ||
113 | #endif // _WX_FFILE_H_ | |
114 |