/* libzz
 *
 * This program is distributed under the GNU General Public License, version 2.
 * A copy of this license is included with this source.
 *
 * Copyright 2000-2006, Toni Thomsson <toni@tonjac.org> 
*/

#ifndef zz_file_h
#define zz_file_h

#include <zz_export.h>
#include <stdio.h>

#ifdef _WIN32
    #include <io.h>
    #include <conio.h>
    #include <sys\types.h>
    #include <sys\stat.h>       /* _S_ constant definitions */
    #include <windows.h>
#else
    #include <sys/types.h>
    #include <sys/stat.h>       /* _S_ constant definitions */
    #include <unistd.h>
#endif

#include <time.h>
#include <stdio.h>
#include <fcntl.h>          /* _O_ constant definitions */
#include <malloc.h>
#include <errno.h>
#include <zz_string.h>
#include <zz_exception.h>

/** Read buffer size */
#define BUFFLEN 16*1024

/**
    File

    @author Toni Thomsson, toni@tonjac.org  
*/
class ZZ_API CzzFile
{

public:

    /**
        Create a wrapper object for a file
        @param  path    Full path to file
    */
    CzzFile( const char* path );

    ~CzzFile();

    /** File size */
    size_t Size( void );

    /** File modify time */
    time_t Time( void );

    /** 
        Open file
        @param  mode    mode (a,w,r + b) see fopen()
    */
    void Open( const char* mode );

    /** 
        Read a block from the file
        @param  p       Pointer to a memoryblock to receive data read
        @param  size    Size of p
    */
    size_t Read( void* p, size_t size );

    
    /** 
        Write a block to the file
        @param  p       Pointer to a memoryblock to be written
        @param  size    Size of p
    */
    size_t Write( void* p,
                    size_t size );

    /** Close file */
    void Close( void );

    /** Does the file exist? */
    bool Exist( void );

    /**
        Copy file
        @param  path    Path to copy file to
    */
    void CopyTo( const char* path );

    /** File handle */
    FILE* GetHandle( void );

    /** 
        Rename file 
        @param  newname New file name including path
    */
    void Rename( const char* newname );
    
    /** Delete file */
    void Delete( void );
    
    /** Path */
    CzzString& GetPath( void );

    /** Change path */
    void SetPath( const char* path );
    
protected:

    /** Handle */
    FILE* m_Handle;

    /** Path */
    CzzString m_Path;

    /** Mode */
    CzzString m_Mode;

    /** Open? */
    bool m_Open;

    /** Size */
    size_t m_Size;

    /** Time */
    time_t m_Time;

private:
    int Copy( const char *source, const char *target );
};

#endif // zz_file_h