]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/ffile.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFFile - encapsulates "FILE *" stream
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 #include "wx/defs.h" // for wxUSE_FFILE
19 #include "wx/string.h"
20 #include "wx/filefn.h"
21 #include "wx/convauto.h"
25 // ----------------------------------------------------------------------------
26 // class wxFFile: standard C stream library IO
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 // ----------------------------------------------------------------------------
32 class WXDLLIMPEXP_BASE wxFFile
38 wxFFile() { m_fp
= NULL
; }
39 // open specified file (may fail, use IsOpened())
40 wxFFile(const wxString
& filename
, const wxString
& mode
= _T("r"));
41 // attach to (already opened) file
42 wxFFile(FILE *lfp
) { m_fp
= lfp
; }
45 // open a file (existing or not - the mode controls what happens)
46 bool Open(const wxString
& filename
, const wxString
& mode
= _T("r"));
47 // closes the opened file (this is a NOP if not opened)
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
; }
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
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 // flush data not yet written
69 // file pointer operations (return ofsInvalid on failure)
70 // move ptr ofs bytes related to start/current pos/end of file
71 bool Seek(wxFileOffset ofs
, wxSeekMode mode
= wxFromStart
);
72 // move ptr to ofs bytes before the end
73 bool SeekEnd(wxFileOffset ofs
= 0) { return Seek(ofs
, wxFromEnd
); }
74 // get current position in the file
75 wxFileOffset
Tell() const;
76 // get current file length
77 wxFileOffset
Length() const;
79 // simple accessors: note that Eof() and Error() may only be called if
82 bool IsOpened() const { return m_fp
!= NULL
; }
83 // is end of file reached?
84 bool Eof() const { return feof(m_fp
) != 0; }
85 // has an error occurred?
86 bool Error() const { return ferror(m_fp
) != 0; }
88 const wxString
& GetName() const { return m_name
; }
89 // type such as disk or pipe
90 wxFileKind
GetKind() const { return wxGetFileKind(m_fp
); }
92 // dtor closes the file if opened
93 ~wxFFile() { Close(); }
96 // copy ctor and assignment operator are private because it doesn't make
97 // sense to copy files this way: attempt to do it will provoke a compile-time
99 wxFFile(const wxFFile
&);
100 wxFFile
& operator=(const wxFFile
&);
102 FILE *m_fp
; // IO stream or NULL if not opened
104 wxString m_name
; // the name of the file (for diagnostic messages)
107 #endif // wxUSE_FFILE
109 #endif // _WX_FFILE_H_