2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// cFile.h
|
|
|
|
|
|
|
|
// Interfaces to the cFile class providing an OS-independent abstraction of a file.
|
|
|
|
|
|
|
|
/*
|
|
|
|
The object is optimized towards binary reads.
|
|
|
|
The object has no multithreading locks, don't use from multiple threads!
|
|
|
|
Usage:
|
|
|
|
1, Construct a cFile instance (no-param constructor)
|
|
|
|
2, Open a file using Open(), check return value for success
|
|
|
|
3, Read / write
|
|
|
|
4, Destroy the instance
|
|
|
|
|
|
|
|
-- OR --
|
|
|
|
|
|
|
|
1, Construct a cFile instance opening the file (filename-param constructor)
|
|
|
|
2, Check if the file was opened using IsOpen()
|
|
|
|
3, Read / write
|
|
|
|
4, Destroy the instance
|
2014-01-25 09:27:34 -05:00
|
|
|
|
|
|
|
For reading entire files into memory, just use the static cFile::ReadWholeFile()
|
2012-06-14 09:06:06 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#define USE_STDIO_FILE
|
|
|
|
#endif // _WIN32
|
|
|
|
|
|
|
|
// DEBUG:
|
|
|
|
#define USE_STDIO_FILE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-09 03:38:47 -04:00
|
|
|
// tolua_begin
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
class cFile
|
|
|
|
{
|
|
|
|
public:
|
2013-10-09 03:38:47 -04:00
|
|
|
|
|
|
|
// tolua_end
|
|
|
|
|
2012-09-25 04:23:19 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
static const char PathSeparator = '\\';
|
|
|
|
#else
|
|
|
|
static const char PathSeparator = '/';
|
|
|
|
#endif
|
|
|
|
|
2014-01-25 09:27:34 -05:00
|
|
|
/** The mode in which to open the file */
|
2013-02-07 04:15:55 -05:00
|
|
|
enum eMode
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-08-03 07:53:11 -04:00
|
|
|
fmRead, // Read-only. If the file doesn't exist, object will not be valid
|
|
|
|
fmWrite, // Write-only. If the file already exists, it will be overwritten
|
|
|
|
fmReadWrite // Read/write. If the file already exists, it will be left intact; writing will overwrite the data from the beginning
|
2012-06-14 09:06:06 -04:00
|
|
|
} ;
|
|
|
|
|
2014-01-25 09:27:34 -05:00
|
|
|
/** Simple constructor - creates an unopened file object, use Open() to open / create a real file */
|
2012-06-14 09:06:06 -04:00
|
|
|
cFile(void);
|
|
|
|
|
2014-01-25 09:27:34 -05:00
|
|
|
/** Constructs and opens / creates the file specified, use IsOpen() to check for success */
|
2013-02-07 04:15:55 -05:00
|
|
|
cFile(const AString & iFileName, eMode iMode);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-01-25 09:27:34 -05:00
|
|
|
/** Auto-closes the file, if open */
|
2012-06-14 09:06:06 -04:00
|
|
|
~cFile();
|
|
|
|
|
2013-02-07 04:15:55 -05:00
|
|
|
bool Open(const AString & iFileName, eMode iMode);
|
2012-06-14 09:06:06 -04:00
|
|
|
void Close(void);
|
|
|
|
bool IsOpen(void) const;
|
|
|
|
bool IsEOF(void) const;
|
|
|
|
|
2014-01-25 09:27:34 -05:00
|
|
|
/** Reads up to iNumBytes bytes into iBuffer, returns the number of bytes actually read, or -1 on failure; asserts if not open */
|
2014-04-28 13:31:07 -04:00
|
|
|
int Read (void * iBuffer, size_t iNumBytes);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-01-25 09:27:34 -05:00
|
|
|
/** Writes up to iNumBytes bytes from iBuffer, returns the number of bytes actually written, or -1 on failure; asserts if not open */
|
2014-04-28 13:31:07 -04:00
|
|
|
int Write(const void * iBuffer, size_t iNumBytes);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2014-01-25 09:27:34 -05:00
|
|
|
/** Seeks to iPosition bytes from file start, returns old position or -1 for failure; asserts if not open */
|
2012-06-14 09:06:06 -04:00
|
|
|
int Seek (int iPosition);
|
|
|
|
|
2014-01-25 09:27:34 -05:00
|
|
|
/** Returns the current position (bytes from file start) or -1 for failure; asserts if not open */
|
2012-06-14 09:06:06 -04:00
|
|
|
int Tell (void) const;
|
|
|
|
|
2014-01-25 09:27:34 -05:00
|
|
|
/** Returns the size of file, in bytes, or -1 for failure; asserts if not open */
|
2012-06-14 09:06:06 -04:00
|
|
|
int GetSize(void) const;
|
|
|
|
|
2014-01-25 09:27:34 -05:00
|
|
|
/** Reads the file from current position till EOF into an AString; returns the number of bytes read or -1 for error */
|
2012-06-14 09:06:06 -04:00
|
|
|
int ReadRestOfFile(AString & a_Contents);
|
|
|
|
|
2013-10-09 03:38:47 -04:00
|
|
|
// tolua_begin
|
|
|
|
|
2014-01-25 09:27:34 -05:00
|
|
|
/** Returns true if the file specified exists */
|
2012-06-14 09:06:06 -04:00
|
|
|
static bool Exists(const AString & a_FileName);
|
|
|
|
|
2014-01-25 09:27:34 -05:00
|
|
|
/** Deletes a file, returns true if successful */
|
2013-05-01 12:59:36 -04:00
|
|
|
static bool Delete(const AString & a_FileName);
|
|
|
|
|
2014-01-25 09:27:34 -05:00
|
|
|
/** Renames a file or folder, returns true if successful. May fail if dest already exists (libc-dependant)! */
|
2013-10-09 05:31:38 -04:00
|
|
|
static bool Rename(const AString & a_OrigPath, const AString & a_NewPath);
|
2013-05-01 12:59:36 -04:00
|
|
|
|
2014-01-25 09:27:34 -05:00
|
|
|
/** Copies a file, returns true if successful. */
|
2013-10-09 03:38:47 -04:00
|
|
|
static bool Copy(const AString & a_SrcFileName, const AString & a_DstFileName);
|
|
|
|
|
2014-01-25 09:27:34 -05:00
|
|
|
/** Returns true if the specified path is a folder */
|
2013-09-18 12:43:03 -04:00
|
|
|
static bool IsFolder(const AString & a_Path);
|
|
|
|
|
2014-01-25 09:27:34 -05:00
|
|
|
/** Returns true if the specified path is a regular file */
|
2013-10-09 03:38:47 -04:00
|
|
|
static bool IsFile(const AString & a_Path);
|
|
|
|
|
2014-01-25 09:27:34 -05:00
|
|
|
/** Returns the size of the file, or a negative number on error */
|
2013-10-09 03:38:47 -04:00
|
|
|
static int GetSize(const AString & a_FileName);
|
|
|
|
|
2014-01-25 09:27:34 -05:00
|
|
|
/** Creates a new folder with the specified name. Returns true if successful. Path may be relative or absolute */
|
2013-10-09 03:57:48 -04:00
|
|
|
static bool CreateFolder(const AString & a_FolderPath);
|
|
|
|
|
2014-01-25 09:27:34 -05:00
|
|
|
/** Returns the entire contents of the specified file as a string. Returns empty string on error. */
|
2013-11-23 14:26:00 -05:00
|
|
|
static AString ReadWholeFile(const AString & a_FileName);
|
|
|
|
|
2013-10-09 03:38:47 -04:00
|
|
|
// tolua_end
|
|
|
|
|
2014-01-25 09:27:34 -05:00
|
|
|
/** Returns the list of all items in the specified folder (files, folders, nix pipes, whatever's there). */
|
2013-11-22 14:11:24 -05:00
|
|
|
static AStringVector GetFolderContents(const AString & a_Folder); // Exported in ManualBindings.cpp
|
|
|
|
|
2014-03-14 09:11:49 -04:00
|
|
|
int Printf(const char * a_Fmt, ...) FORMATSTRING(2, 3);
|
2012-09-29 09:50:05 -04:00
|
|
|
|
2014-01-25 09:27:34 -05:00
|
|
|
/** Flushes all the bufferef output into the file (only when writing) */
|
|
|
|
void Flush(void);
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
private:
|
|
|
|
#ifdef USE_STDIO_FILE
|
|
|
|
FILE * m_File;
|
|
|
|
#else
|
|
|
|
HANDLE m_File;
|
|
|
|
#endif
|
2013-10-09 03:38:47 -04:00
|
|
|
} ; // tolua_export
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|