]>
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
20 #include "wx/string.h"
21 #include "wx/filefn.h"
26 // ----------------------------------------------------------------------------
27 // class wxFFile: standard C stream library IO
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 // ----------------------------------------------------------------------------
33 class WXDLLIMPEXP_BASE wxFFile
39 wxFFile() { m_fp
= NULL
; }
40 // open specified file (may fail, use IsOpened())
41 wxFFile(const wxChar
*filename
, const wxChar
*mode
= _T("r"));
42 // attach to (already opened) file
43 wxFFile(FILE *lfp
) { m_fp
= lfp
; }
46 // open a file (existing or not - the mode controls what happens)
47 bool Open(const wxChar
*filename
, const wxChar
*mode
= _T("r"));
48 // closes the opened file (this is a NOP if not opened)
51 // assign an existing file descriptor and get it back from wxFFile object
52 void Attach(FILE *lfp
, const wxString
& name
= wxEmptyString
)
53 { Close(); m_fp
= lfp
; m_name
= name
; }
54 void Detach() { m_fp
= NULL
; }
55 FILE *fp() const { return m_fp
; }
57 // read/write (unbuffered)
58 // read all data from the file into a string (useful for text files)
59 bool ReadAll(wxString
*str
, wxMBConv
& conv
= wxConvUTF8
);
60 // returns number of bytes read - use Eof() and Error() to see if an error
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
66 bool Write(const wxString
& s
, wxMBConv
& conv
= wxConvUTF8
)
68 const wxWX2MBbuf buf
= s
.mb_str(conv
);
69 size_t size
= strlen(buf
);
70 return Write((const char *)buf
, size
) == size
;
72 // flush data not yet written
75 // file pointer operations (return ofsInvalid on failure)
76 // move ptr ofs bytes related to start/current pos/end of file
77 bool Seek(wxFileOffset ofs
, wxSeekMode mode
= wxFromStart
);
78 // move ptr to ofs bytes before the end
79 bool SeekEnd(wxFileOffset ofs
= 0) { return Seek(ofs
, wxFromEnd
); }
80 // get current position in the file
81 wxFileOffset
Tell() const;
82 // get current file length
83 wxFileOffset
Length() const;
85 // simple accessors: note that Eof() and Error() may only be called if
88 bool IsOpened() const { return m_fp
!= NULL
; }
89 // is end of file reached?
90 bool Eof() const { return feof(m_fp
) != 0; }
91 // has an error occurred?
92 bool Error() const { return ferror(m_fp
) != 0; }
94 const wxString
& GetName() const { return m_name
; }
95 // type such as disk or pipe
96 wxFileKind
GetKind() const { return wxGetFileKind(m_fp
); }
98 // dtor closes the file if opened
99 ~wxFFile() { Close(); }
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
105 wxFFile(const wxFFile
&);
106 wxFFile
& operator=(const wxFFile
&);
108 FILE *m_fp
; // IO stream or NULL if not opened
110 wxString m_name
; // the name of the file (for diagnostic messages)
113 #endif // wxUSE_FFILE
115 #endif // _WX_FFILE_H_