2008-03-08 08:52:38 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: ffile.h
|
2008-03-10 11:24:38 -04:00
|
|
|
// Purpose: interface of wxFFile
|
2008-03-08 08:52:38 -05:00
|
|
|
// Author: wxWidgets team
|
2010-07-13 09:29:13 -04:00
|
|
|
// Licence: wxWindows licence
|
2008-03-08 08:52:38 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2008-03-30 19:34:30 -04:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
|
|
|
@class wxFFile
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-30 19:34:30 -04:00
|
|
|
wxFFile implements buffered file I/O.
|
|
|
|
|
|
|
|
This is a very small class designed to minimize the overhead of using it - in fact,
|
|
|
|
there is hardly any overhead at all, but using it brings you automatic error checking
|
|
|
|
and hides differences between platforms and compilers.
|
|
|
|
|
|
|
|
It wraps inside it a @c FILE * handle used by standard C IO library (also known as @c stdio).
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
@library{wxbase}
|
|
|
|
@category{file}
|
2008-03-08 09:43:31 -05:00
|
|
|
|
2008-03-10 11:24:38 -04:00
|
|
|
@see wxFFile::IsOpened
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-08 09:43:31 -05:00
|
|
|
class wxFFile
|
2008-03-08 08:52:38 -05:00
|
|
|
{
|
|
|
|
public:
|
2008-03-30 19:34:30 -04:00
|
|
|
wxFFile();
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
|
|
|
Opens a file with the given file pointer, which has already been opened.
|
2008-03-20 09:45:17 -04:00
|
|
|
|
2008-03-30 19:34:30 -04:00
|
|
|
@param fp
|
|
|
|
An existing file descriptor, such as stderr.
|
|
|
|
*/
|
|
|
|
wxFFile(FILE* fp);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Opens a file with the given mode.
|
|
|
|
As there is no way to return whether the operation was successful or not from
|
|
|
|
the constructor you should test the return value of IsOpened() to check that it
|
|
|
|
didn't fail.
|
|
|
|
|
2008-03-08 09:43:31 -05:00
|
|
|
@param filename
|
2008-03-09 08:33:59 -04:00
|
|
|
The filename.
|
2008-03-08 09:43:31 -05:00
|
|
|
@param mode
|
2008-03-09 08:33:59 -04:00
|
|
|
The mode in which to open the file using standard C strings.
|
|
|
|
Note that you should use "b" flag if you use binary files under Windows
|
|
|
|
or the results might be unexpected due to automatic newline conversion done
|
|
|
|
for the text files.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-08 09:43:31 -05:00
|
|
|
wxFFile(const wxString& filename, const wxString& mode = "r");
|
2008-03-30 19:34:30 -04:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Destructor will close the file.
|
2008-03-30 19:34:30 -04:00
|
|
|
|
|
|
|
@note it is not virtual so you should @e not derive from wxFFile!
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
~wxFFile();
|
|
|
|
|
|
|
|
/**
|
|
|
|
Attaches an existing file pointer to the wxFFile object.
|
2008-03-30 19:34:30 -04:00
|
|
|
|
|
|
|
The descriptor should be already opened and it will be closed by wxFFile object.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-10-28 11:36:26 -04:00
|
|
|
void Attach(FILE* fp, const wxString& name = wxEmptyString);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Closes the file and returns @true on success.
|
|
|
|
*/
|
|
|
|
bool Close();
|
|
|
|
|
|
|
|
/**
|
|
|
|
Get back a file pointer from wxFFile object -- the caller is responsible for
|
2008-03-30 19:34:30 -04:00
|
|
|
closing the file if this descriptor is opened.
|
|
|
|
|
|
|
|
IsOpened() will return @false after call to Detach().
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
void Detach();
|
|
|
|
|
|
|
|
/**
|
2011-04-03 16:31:32 -04:00
|
|
|
Returns @true if an attempt has been made to read @e past
|
2008-03-08 09:43:31 -05:00
|
|
|
the end of the file.
|
2008-03-30 19:34:30 -04:00
|
|
|
|
|
|
|
Note that the behaviour of the file descriptor based class wxFile is different as
|
|
|
|
wxFile::Eof() will return @true here as soon as the last byte of the file has been read.
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
Also note that this method may only be called for opened files and may crash if
|
|
|
|
the file is not opened.
|
2008-03-20 09:45:17 -04:00
|
|
|
|
2008-03-30 19:34:30 -04:00
|
|
|
@todo THIS METHOD MAY CRASH? DOESN'T SOUND GOOD
|
|
|
|
|
2008-03-09 08:33:59 -04:00
|
|
|
@see IsOpened()
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
bool Eof() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns @true if an error has occurred on this file, similar to the standard
|
|
|
|
@c ferror() function.
|
2008-03-30 19:34:30 -04:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
Please note that this method may only be called for opened files and may crash
|
|
|
|
if the file is not opened.
|
2008-03-20 09:45:17 -04:00
|
|
|
|
2008-03-30 19:34:30 -04:00
|
|
|
@todo THIS METHOD MAY CRASH? DOESN'T SOUND GOOD
|
|
|
|
|
2008-03-09 08:33:59 -04:00
|
|
|
@see IsOpened()
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-30 19:34:30 -04:00
|
|
|
bool Error() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Flushes the file and returns @true on success.
|
|
|
|
*/
|
|
|
|
bool Flush();
|
|
|
|
|
|
|
|
/**
|
2008-03-30 19:34:30 -04:00
|
|
|
Returns the type of the file.
|
|
|
|
|
|
|
|
@see wxFileKind
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
wxFileKind GetKind() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
2012-01-31 14:38:41 -05:00
|
|
|
/**
|
|
|
|
Returns the file name.
|
|
|
|
|
|
|
|
This is the name that was specified when the object was constructed or
|
|
|
|
to the last call to Open(). Notice that it may be empty if Attach() was
|
|
|
|
called without specifying the name.
|
|
|
|
*/
|
|
|
|
const wxString& GetName() const;
|
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
/**
|
2008-03-30 19:34:30 -04:00
|
|
|
Returns @true if the file is opened.
|
|
|
|
|
|
|
|
Most of the methods of this class may only be used for an opened file.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
bool IsOpened() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the length of the file.
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
wxFileOffset Length() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Opens the file, returning @true if successful.
|
2008-03-20 09:45:17 -04:00
|
|
|
|
2008-03-08 09:43:31 -05:00
|
|
|
@param filename
|
2008-03-09 08:33:59 -04:00
|
|
|
The filename.
|
2008-03-08 09:43:31 -05:00
|
|
|
@param mode
|
2008-03-09 08:33:59 -04:00
|
|
|
The mode in which to open the file.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
bool Open(const wxString& filename, const wxString& mode = "r");
|
|
|
|
|
|
|
|
/**
|
2008-03-30 19:34:30 -04:00
|
|
|
Reads the specified number of bytes into a buffer, returning the actual number read.
|
2008-03-20 09:45:17 -04:00
|
|
|
|
2008-03-08 09:43:31 -05:00
|
|
|
@param buffer
|
2008-03-09 08:33:59 -04:00
|
|
|
A buffer to receive the data.
|
2008-03-08 09:43:31 -05:00
|
|
|
@param count
|
2008-03-09 08:33:59 -04:00
|
|
|
The number of bytes to read.
|
2008-03-20 09:45:17 -04:00
|
|
|
|
2008-05-10 21:38:53 -04:00
|
|
|
@return The number of bytes read.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
size_t Read(void* buffer, size_t count);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Reads the entire contents of the file into a string.
|
2008-03-20 09:45:17 -04:00
|
|
|
|
2008-03-08 09:43:31 -05:00
|
|
|
@param str
|
2008-03-09 08:33:59 -04:00
|
|
|
String to read data into.
|
2008-03-08 09:43:31 -05:00
|
|
|
@param conv
|
2008-03-09 08:33:59 -04:00
|
|
|
Conversion object to use in Unicode build; by default supposes
|
|
|
|
that file contents is encoded in UTF-8.
|
2008-03-20 09:45:17 -04:00
|
|
|
|
2008-05-10 21:38:53 -04:00
|
|
|
@return @true if file was read successfully, @false otherwise.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-30 19:34:30 -04:00
|
|
|
bool ReadAll(wxString* str, const wxMBConv& conv = wxConvAuto());
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Seeks to the specified position and returns @true on success.
|
2008-03-20 09:45:17 -04:00
|
|
|
|
2008-03-08 09:43:31 -05:00
|
|
|
@param ofs
|
2008-03-09 08:33:59 -04:00
|
|
|
Offset to seek to.
|
2008-03-08 09:43:31 -05:00
|
|
|
@param mode
|
2008-03-09 08:33:59 -04:00
|
|
|
One of wxFromStart, wxFromEnd, wxFromCurrent.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
bool Seek(wxFileOffset ofs, wxSeekMode mode = wxFromStart);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Moves the file pointer to the specified number of bytes before the end of the
|
2008-03-30 19:34:30 -04:00
|
|
|
file and returns @true on success.
|
2008-03-20 09:45:17 -04:00
|
|
|
|
2008-03-08 09:43:31 -05:00
|
|
|
@param ofs
|
2008-03-09 08:33:59 -04:00
|
|
|
Number of bytes before the end of the file.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
|
|
|
bool SeekEnd(wxFileOffset ofs = 0);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the current position.
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
wxFileOffset Tell() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Writes the contents of the string to the file, returns @true on success.
|
2008-03-30 19:34:30 -04:00
|
|
|
|
2008-03-08 08:52:38 -05:00
|
|
|
The second argument is only meaningful in Unicode build of wxWidgets when
|
2008-03-30 19:34:30 -04:00
|
|
|
@a conv is used to convert @a str to multibyte representation.
|
|
|
|
*/
|
|
|
|
bool Write(const wxString& str, const wxMBConv& conv = wxConvAuto());
|
|
|
|
|
|
|
|
/**
|
|
|
|
Writes the specified number of bytes from a buffer.
|
|
|
|
|
|
|
|
@param buffer
|
|
|
|
A buffer containing the data.
|
|
|
|
@param count
|
|
|
|
The number of bytes to write.
|
|
|
|
|
2008-05-10 21:38:53 -04:00
|
|
|
@return The number of bytes written.
|
2008-03-08 08:52:38 -05:00
|
|
|
*/
|
2008-03-30 19:34:30 -04:00
|
|
|
size_t Write(const void* buffer, size_t count);
|
2008-03-08 08:52:38 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the file pointer associated with the file.
|
|
|
|
*/
|
2008-03-09 12:24:26 -04:00
|
|
|
FILE* fp() const;
|
2008-03-08 08:52:38 -05:00
|
|
|
};
|
2008-03-10 11:24:38 -04:00
|
|
|
|