]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: 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 license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef _WX_FFILE_H_ | |
13 | #define _WX_FFILE_H_ | |
14 | ||
15 | #ifdef __GNUG__ | |
16 | #pragma interface "ffile.h" | |
17 | #endif | |
18 | ||
19 | #if wxUSE_FILE | |
20 | ||
21 | #ifndef WX_PRECOMP | |
22 | #include "wx/string.h" | |
23 | #include "wx/filefn.h" | |
24 | #endif | |
25 | ||
26 | #include <stdio.h> | |
27 | ||
28 | // ---------------------------------------------------------------------------- | |
29 | // class wxFFile: standard C stream library IO | |
30 | // | |
31 | // NB: for space efficiency this class has no virtual functions, including | |
32 | // dtor which is _not_ virtual, so it shouldn't be used as a base class. | |
33 | // ---------------------------------------------------------------------------- | |
34 | ||
35 | class WXDLLEXPORT wxFFile | |
36 | { | |
37 | public: | |
38 | // ctors | |
39 | // ----- | |
40 | // def ctor | |
41 | wxFFile() { m_fp = NULL; } | |
42 | // open specified file (may fail, use IsOpened()) | |
43 | wxFFile(const wxChar *filename, const char *mode = "r"); | |
44 | // attach to (already opened) file | |
45 | wxFFile(FILE *fp) { m_fp = fp; } | |
46 | ||
47 | // open/close | |
48 | // open a file (existing or not - the mode controls what happens) | |
49 | bool Open(const wxChar *filename, const char *mode = "r"); | |
50 | // closes the opened file (this is a NOP if not opened) | |
51 | bool Close(); | |
52 | ||
53 | // assign an existing file descriptor and get it back from wxFFile object | |
54 | void Attach(FILE *fp, const wxString& name = wxT("")) | |
55 | { Close(); m_fp = fp; m_name = name; } | |
56 | void Detach() { m_fp = NULL; } | |
57 | FILE *fp() const { return m_fp; } | |
58 | ||
59 | // read/write (unbuffered) | |
60 | // read all data from the file into a string (useful for text files) | |
61 | bool ReadAll(wxString *str); | |
62 | // returns number of bytes read - use Eof() and Error() to see if an error | |
63 | // occured or not | |
64 | size_t Read(void *pBuf, size_t nCount); | |
65 | // returns the number of bytes written | |
66 | size_t Write(const void *pBuf, size_t nCount); | |
67 | // returns true on success | |
68 | bool Write(const wxString& s) | |
69 | { | |
70 | size_t size = s.Len()*sizeof(wxChar); | |
71 | return Write(s.c_str(), size) == size; | |
72 | } | |
73 | // flush data not yet written | |
74 | bool Flush(); | |
75 | ||
76 | // file pointer operations (return ofsInvalid on failure) | |
77 | // move ptr ofs bytes related to start/current pos/end of file | |
78 | bool Seek(long ofs, wxSeekMode mode = wxFromStart); | |
79 | // move ptr to ofs bytes before the end | |
80 | bool SeekEnd(long ofs = 0) { return Seek(ofs, wxFromEnd); } | |
81 | // get current position in the file | |
82 | size_t Tell() const; | |
83 | // get current file length | |
84 | size_t Length() const; | |
85 | ||
86 | // simple accessors | |
87 | // is file opened? | |
88 | bool IsOpened() const { return m_fp != NULL; } | |
89 | // is end of file reached? | |
90 | bool Eof() const { return feof(m_fp) != 0; } | |
91 | // is an error occured? | |
92 | bool Error() const { return ferror(m_fp) != 0; } | |
93 | // get the file name | |
94 | const wxString& GetName() const { return m_name; } | |
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_FILE | |
112 | ||
113 | #endif // _WX_FFILE_H_ | |
114 |