1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
//--------------------------------------------------------------------
// $Id: FileIO.hpp 4621 2005-03-25 20:21:26Z cjm $
//--------------------------------------------------------------------
//
// Visual Binary Diff
// Copyright 1997-2005 by Christopher J. Madsen
//
// Support functions for Posix file I/O
//
//--------------------------------------------------------------------
#ifndef INCLUDED_FILEIO_HPP
#define INCLUDED_FILEIO_HPP
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
typedef int File;
typedef off_t FPos;
typedef ssize_t Size;
const int
SeekEnd = SEEK_END,
SeekPos = SEEK_SET;
const File InvalidFile = -1;
//--------------------------------------------------------------------
inline const char* ErrorMsg()
{
return strerror(errno);
} // end ErrorMsg
//--------------------------------------------------------------------
inline File OpenFile(const char* path, bool writable=false)
{
return open(path, (writable ? O_RDWR : O_RDONLY));
} // end OpenFile
//--------------------------------------------------------------------
inline void CloseFile(File file)
{
close(file);
} // end CloseFile
//--------------------------------------------------------------------
bool WriteFile(File file, const void* buffer, Size count)
{
const char* ptr = reinterpret_cast<const char*>(buffer);
while (count > 0) {
Size bytesWritten = write(file, ptr, count);
if (bytesWritten < 1) {
if (errno == EINTR)
bytesWritten = 0;
else
return false;
}
ptr += bytesWritten;
count -= bytesWritten;
} // end while more to write
return true;
} // end WriteFile
//--------------------------------------------------------------------
inline Size ReadFile(File file, void* buffer, Size count)
{
return read(file, buffer, count);
} // end ReadFile
//--------------------------------------------------------------------
inline FPos SeekFile(File file, FPos position, int whence=SeekPos)
{
return lseek(file, position, whence);
} // end SeekFile
#endif // INCLUDED_FILEIO_HPP
// Local Variables:
// c-file-style: "cjm"
// End:
|