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