]>
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "ffile.h"
19 #include "wx/defs.h" // for wxUSE_FFILE
24 #include "wx/string.h"
25 #include "wx/filefn.h"
30 // ----------------------------------------------------------------------------
31 // class wxFFile: standard C stream library IO
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 // ----------------------------------------------------------------------------
37 class WXDLLIMPEXP_BASE wxFFile
43 wxFFile() { m_fp
= NULL
; }
44 // open specified file (may fail, use IsOpened())
45 wxFFile(const wxChar
*filename
, const wxChar
*mode
= _T("r"));
46 // attach to (already opened) file
47 wxFFile(FILE *fp
) { m_fp
= fp
; }
50 // open a file (existing or not - the mode controls what happens)
51 bool Open(const wxChar
*filename
, const wxChar
*mode
= _T("r"));
52 // closes the opened file (this is a NOP if not opened)
55 // assign an existing file descriptor and get it back from wxFFile object
56 void Attach(FILE *fp
, const wxString
& name
= wxEmptyString
)
57 { Close(); m_fp
= fp
; m_name
= name
; }
58 void Detach() { m_fp
= NULL
; }
59 FILE *fp() const { return m_fp
; }
61 // read/write (unbuffered)
62 // read all data from the file into a string (useful for text files)
63 bool ReadAll(wxString
*str
);
64 // returns number of bytes read - use Eof() and Error() to see if an error
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
70 bool Write(const wxString
& s
, wxMBConv
& conv
= wxConvUTF8
)
72 const wxWX2MBbuf buf
= s
.mb_str(conv
);
73 size_t size
= strlen(buf
);
74 return Write((const char *)buf
, size
) == size
;
76 // flush data not yet written
79 // file pointer operations (return ofsInvalid on failure)
80 // move ptr ofs bytes related to start/current pos/end of file
81 bool Seek(long ofs
, wxSeekMode mode
= wxFromStart
);
82 // move ptr to ofs bytes before the end
83 bool SeekEnd(long ofs
= 0) { return Seek(ofs
, wxFromEnd
); }
84 // get current position in the file
86 // get current file length
87 size_t Length() const;
91 bool IsOpened() const { return m_fp
!= NULL
; }
92 // is end of file reached?
93 bool Eof() const { return feof(m_fp
) != 0; }
94 // is an error occured?
95 bool Error() const { return ferror(m_fp
) != 0; }
97 const wxString
& GetName() const { return m_name
; }
99 // dtor closes the file if opened
100 ~wxFFile() { Close(); }
103 // copy ctor and assignment operator are private because it doesn't make
104 // sense to copy files this way: attempt to do it will provoke a compile-time
106 wxFFile(const wxFFile
&);
107 wxFFile
& operator=(const wxFFile
&);
109 FILE *m_fp
; // IO stream or NULL if not opened
111 wxString m_name
; // the name of the file (for diagnostic messages)
114 #endif // wxUSE_FFILE
116 #endif // _WX_FFILE_H_