]> git.saurik.com Git - wxWidgets.git/blob - include/wx/ffile.h
1a4c3c0de8f31dfe95486f930a3f2f2fe1cc1f84
[wxWidgets.git] / include / wx / ffile.h
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 #include "wx/convauto.h"
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
32 class WXDLLIMPEXP_BASE wxFFile
33 {
34 public:
35 // ctors
36 // -----
37 // def ctor
38 wxFFile() { m_fp = NULL; }
39 // open specified file (may fail, use IsOpened())
40 wxFFile(const wxChar *filename, const wxChar *mode = _T("r"));
41 // attach to (already opened) file
42 wxFFile(FILE *lfp) { m_fp = lfp; }
43
44 // open/close
45 // open a file (existing or not - the mode controls what happens)
46 bool Open(const wxChar *filename, const wxChar *mode = _T("r"));
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
51 void Attach(FILE *lfp, const wxString& name = wxEmptyString)
52 { Close(); m_fp = lfp; m_name = name; }
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)
58 bool ReadAll(wxString *str, const wxMBConv& conv = wxConvAuto());
59 // returns number of bytes read - use Eof() and Error() to see if an error
60 // occurred or not
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
65 bool Write(const wxString& s, const wxMBConv& conv = wxConvAuto())
66 {
67 const wxWX2MBbuf buf = s.mb_str(conv);
68 if (buf)
69 {
70 size_t size = strlen(buf);
71 return Write((const char *)buf, size) == size;
72 }
73 else
74 return false;
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
81 bool Seek(wxFileOffset ofs, wxSeekMode mode = wxFromStart);
82 // move ptr to ofs bytes before the end
83 bool SeekEnd(wxFileOffset ofs = 0) { return Seek(ofs, wxFromEnd); }
84 // get current position in the file
85 wxFileOffset Tell() const;
86 // get current file length
87 wxFileOffset Length() const;
88
89 // simple accessors: note that Eof() and Error() may only be called if
90 // IsOpened()!
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; }
95 // has an error occurred?
96 bool Error() const { return ferror(m_fp) != 0; }
97 // get the file name
98 const wxString& GetName() const { return m_name; }
99 // type such as disk or pipe
100 wxFileKind GetKind() const { return wxGetFileKind(m_fp); }
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
117 #endif // wxUSE_FFILE
118
119 #endif // _WX_FFILE_H_
120