FileTools.h

// FileTools.h
//
// Author David Barrett-Lennard
// (C)opyright Cedanet Pty Ltd 2013

#pragma once
#ifndef Ceda_cxUtils_FileTools_H
#define Ceda_cxUtils_FileTools_H

#include "cxUtils.h"
#include "xstring.h"
#include "HPTime.h"

namespace ceda
{
class xostream;

cxUtils_API bool DeleteFile_u8(ConstStringZ path);
inline bool DeleteFile_u8(const xstring& path) { return DeleteFile_u8(path.c_str()); }

cxUtils_API bool DeleteDirectory( ConstStringZ path );

cxUtils_API bool MakeDirectory( ConstStringZ path );
cxUtils_API bool MakeDirectories( ConstStringZ path );
cxUtils_API void CreateDirectories(const xstring& path, bool pathIsADir);

cxUtils_API bool FileExists(ConstStringZ path);

/*
Write an empty file with the given path. If allowRewrite then allow an existing file
to be rewritten as an empty file.

If allowRewrite 
    Returns true if file exists and it was sucessfully rewritten or a new zero length file
    was created.
else
    Returns true if file doesn't exist and a zero size file was successfully created.
*/
cxUtils_API bool CreateEmptyFile(ConstStringZ path, bool allowRewrite);

/*
Update the last modification timestamp on the file with the given path.  
if allowCreate = true
     Returns false if user doesn't have write permission or there was no file
     with the given path and creating a file failed.
else
     Returns false if file doesn't exist or user doesn't have write permissions.
*/
cxUtils_API bool TouchFile(ConstStringZ path, bool allowCreate);

cxUtils_API HPTime GetLastWriteFileTime(ConstStringZ path);

// Returns -1 if file not found
cxUtils_API int64 GetFileSize(ConstStringZ path);

// Returns -1 if file not found
cxUtils_API int64 GetDirectorySize(ConstStringZ path);

cxUtils_API bool CopyFile_u8(ConstStringZ from, ConstStringZ to, bool failIfExists);

// Set the permissions on the file with the given path to be read only or not read only.
cxUtils_API bool SetFileReadOnly(ConstStringZ path, bool readOnly);

cxUtils_API bool GetFullPath(ConstStringZ relativePath, xstring& fullPath);

cxUtils_API void GetFilenamesInDirectory(const xstring& directory, xvector<xstring>& filenames, bool recurse = true);
cxUtils_API void GetDirectoriesInDirectory(const xstring& directory, xvector<xstring>& directories, bool recurse = true);
cxUtils_API bool SetCurrentDirectory(const xstring& directory);

///////////////////////////////////////////////////////////////////////////////////////////////////

/*
Example usage:

    IoCounters c;
        
    Reset(c);
    < do some I/O >
    Set(c);
        
    Tracer() << c << '\n';
*/
struct IoCounters
{
    int64 ReadOperationCount;       // The number of read operations performed.
    int64 WriteOperationCount;      // The number of write operations performed.
    int64 OtherOperationCount;      // The number of I/O operations performed, other than read and write operations.
    int64 ReadTransferCount;        // The number of bytes read.
    int64 WriteTransferCount;       // The number of bytes written.
    int64 OtherTransferCount;       // The number of bytes transferred during operations other than read and write operations.
};

cxUtils_API void Reset(IoCounters& ioc);
cxUtils_API void Set(IoCounters& ioc);
cxUtils_API xostream& operator<<(xostream& os, const IoCounters& ioc);

} // namespace ceda

#endif // include guard