Annotation of elwix/tools/oldlzma/SRC/Common/C_FileIO.cpp, revision 1.1

1.1     ! misho       1: // Common/C_FileIO.h
        !             2: 
        !             3: #include "C_FileIO.h"
        !             4: 
        !             5: #include <fcntl.h>
        !             6: #include <unistd.h>
        !             7: 
        !             8: namespace NC {
        !             9: namespace NFile {
        !            10: namespace NIO {
        !            11: 
        !            12: bool CFileBase::OpenBinary(const char *name, int flags)
        !            13: {
        !            14:   #ifdef O_BINARY
        !            15:   flags |= O_BINARY;
        !            16:   #endif
        !            17:   Close();
        !            18:   _handle = ::open(name, flags, 0666);
        !            19:   return _handle != -1;
        !            20: }
        !            21: 
        !            22: bool CFileBase::Close()
        !            23: {
        !            24:   if(_handle == -1)
        !            25:     return true;
        !            26:   if (close(_handle) != 0)
        !            27:     return false;
        !            28:   _handle = -1;
        !            29:   return true;
        !            30: }
        !            31: 
        !            32: bool CFileBase::GetLength(UInt64 &length) const
        !            33: {
        !            34:   off_t curPos = Seek(0, SEEK_CUR);
        !            35:   off_t lengthTemp = Seek(0, SEEK_END);
        !            36:   Seek(curPos, SEEK_SET);
        !            37:   length = (UInt64)lengthTemp;
        !            38:   return true;
        !            39: }
        !            40: 
        !            41: off_t CFileBase::Seek(off_t distanceToMove, int moveMethod) const
        !            42: {
        !            43:   return ::lseek(_handle, distanceToMove, moveMethod);
        !            44: }
        !            45: 
        !            46: /////////////////////////
        !            47: // CInFile
        !            48: 
        !            49: bool CInFile::Open(const char *name)
        !            50: {
        !            51:   return CFileBase::OpenBinary(name, O_RDONLY);
        !            52: }
        !            53: 
        !            54: ssize_t CInFile::Read(void *data, size_t size)
        !            55: {
        !            56:   return read(_handle, data, size);
        !            57: }
        !            58: 
        !            59: /////////////////////////
        !            60: // COutFile
        !            61: 
        !            62: bool COutFile::Create(const char *name, bool createAlways)
        !            63: {
        !            64:   if (createAlways)
        !            65:   {
        !            66:     Close();
        !            67:     _handle = ::creat(name, 0666);
        !            68:     return _handle != -1;
        !            69:   }
        !            70:   return OpenBinary(name, O_CREAT | O_EXCL | O_WRONLY);
        !            71: }
        !            72: 
        !            73: ssize_t COutFile::Write(const void *data, size_t size)
        !            74: {
        !            75:   return write(_handle, data, size);
        !            76: }
        !            77: 
        !            78: }}}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>