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