/*
  Copyright (C) 2010-2011  Charles Pence <charles@charlespence.net>
  Copyright (C) 1996-1997  Id Software, Inc.

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef FILESYSTEM_FILE_H__
#define FILESYSTEM_FILE_H__

typedef struct PHYSFS_File PHYSFS_File;


class File
{
public:
    File();
    
    
    // Return true if the given file exists.
    static bool Exists(const std::string &path);
    
    
    // Open file at the given path for reading.
    bool OpenRead(const std::string &path);
    
    // Open file at the given path for writing.  Will delete the file if
    // it does not exist.
    bool OpenWrite(const std::string &path);
    
    // Returns true if the file is open for reading or writing.
    bool open() { return fp != NULL; }
    
    // Close file (whether opened for read or write).
    void Close();
    
    
    // Return the file size.  Returns UINT64_MAX on error.
    uint64_t Size();
    
    // Seek to a given offset in the file.
    bool Seek(uint64_t position);
    
    // Return the current file position.  Returns UINT64_MAX if an error
    // occurred.
    uint64_t Tell();
    
    // Return true if the read position is at end-of-file.
    bool eof();
    
    
    // Read the entire file contents.  Returns NULL if the read fails.
    // Call Size() to determine how much data is in this buffer.  To
    // remain compatible with Quake, this buffer will be NUL-terminated.
    // The returned buffer should be deleted with delete[].
    uint8_t *Read();
    
    // Read the given number of bytes from the file, at the current file
    // position.  Returns NULL if the read fails.  The returned buffer should
    // be deleted with delete[].
    uint8_t *Read(uint64_t len);
    
    // Read the given number of bytes from the file, into the given buffer.
    // Returns false if the read fails.
    bool Read(void *buffer, uint64_t len);
    
    
    // Read a string at the current file position.  Stop either at EOF or
    // a newline character.
    std::string GetLine();
    
    // Do a formatted print into the file.
    bool Printf(const char *str, ...);
            
    
    // Write the given data to the file, at the current file position.
    bool Write(const void *buffer, uint64_t len);
    
    // Flush all pending writes to the file.  Returns false if the file
    // is not opened for reading.
    bool Flush();

private:
    PHYSFS_File *fp;
};

#endif /* FILESYSTEM_FILE_H__ */
