Added private BPositionIO derived classes BFileIO and BFileDescriptorIO which
wrap a FILE* respectively a file descriptor. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@36324 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2009-2010, Ingo Weinhold, [email protected].
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
#ifndef _FILE_DESCRIPTOR_IO_H
|
||||
#define _FILE_DESCRIPTOR_IO_H
|
||||
|
||||
|
||||
#include <DataIO.h>
|
||||
|
||||
|
||||
class BFileDescriptorIO : public BPositionIO {
|
||||
public:
|
||||
BFileDescriptorIO(int fd,
|
||||
bool takeOverOwnership = false);
|
||||
virtual ~BFileDescriptorIO();
|
||||
|
||||
virtual ssize_t Read(void *buffer, size_t size);
|
||||
virtual ssize_t Write(const void *buffer, size_t size);
|
||||
|
||||
virtual ssize_t ReadAt(off_t position, void *buffer,
|
||||
size_t size);
|
||||
virtual ssize_t WriteAt(off_t position, const void *buffer,
|
||||
size_t size);
|
||||
|
||||
virtual off_t Seek(off_t position, uint32 seekMode);
|
||||
virtual off_t Position() const;
|
||||
|
||||
virtual status_t SetSize(off_t size);
|
||||
virtual status_t GetSize(off_t* size) const;
|
||||
|
||||
private:
|
||||
BFileDescriptorIO(
|
||||
const BFileDescriptorIO& other);
|
||||
BFileDescriptorIO& operator=(const BFileDescriptorIO& other);
|
||||
|
||||
private:
|
||||
int fFD;
|
||||
bool fOwnsFD;
|
||||
};
|
||||
|
||||
|
||||
#endif // _FILE_DESCRIPTOR_IO_H
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2009-2010, Ingo Weinhold, [email protected].
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
#ifndef _FILE_IO_H
|
||||
#define _FILE_IO_H
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <DataIO.h>
|
||||
|
||||
|
||||
class BFileIO : public BPositionIO {
|
||||
public:
|
||||
BFileIO(FILE* file,
|
||||
bool takeOverOwnership = false);
|
||||
virtual ~BFileIO();
|
||||
|
||||
virtual ssize_t Read(void *buffer, size_t size);
|
||||
virtual ssize_t Write(const void *buffer, size_t size);
|
||||
|
||||
virtual ssize_t ReadAt(off_t position, void *buffer,
|
||||
size_t size);
|
||||
virtual ssize_t WriteAt(off_t position, const void *buffer,
|
||||
size_t size);
|
||||
|
||||
virtual off_t Seek(off_t position, uint32 seekMode);
|
||||
virtual off_t Position() const;
|
||||
|
||||
virtual status_t SetSize(off_t size);
|
||||
virtual status_t GetSize(off_t* size) const;
|
||||
|
||||
private:
|
||||
BFileIO(const BFileIO& other);
|
||||
BFileIO& operator=(const BFileIO& other);
|
||||
|
||||
off_t _Seek(off_t position, uint32 seekMode) const;
|
||||
|
||||
private:
|
||||
FILE* fFile;
|
||||
bool fOwnsFile;
|
||||
};
|
||||
|
||||
|
||||
#endif // _FILE_IO_H
|
||||
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright 2009-2010, Ingo Weinhold, [email protected].
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
|
||||
#include <FileDescriptorIO.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
BFileDescriptorIO::BFileDescriptorIO(int fd, bool takeOverOwnership)
|
||||
:
|
||||
fFD(fd),
|
||||
fOwnsFD(takeOverOwnership)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
BFileDescriptorIO::~BFileDescriptorIO()
|
||||
{
|
||||
if (fOwnsFD)
|
||||
close(fFD);
|
||||
}
|
||||
|
||||
|
||||
ssize_t
|
||||
BFileDescriptorIO::Read(void *buffer, size_t size)
|
||||
{
|
||||
ssize_t bytesRead = read(fFD, buffer, size);
|
||||
return bytesRead >= 0 ? bytesRead : errno;
|
||||
}
|
||||
|
||||
|
||||
ssize_t
|
||||
BFileDescriptorIO::Write(const void *buffer, size_t size)
|
||||
{
|
||||
ssize_t bytesWritten = write(fFD, buffer, size);
|
||||
return bytesWritten >= 0 ? bytesWritten : errno;
|
||||
}
|
||||
|
||||
|
||||
ssize_t
|
||||
BFileDescriptorIO::ReadAt(off_t position, void *buffer, size_t size)
|
||||
{
|
||||
ssize_t bytesRead = pread(fFD, buffer, size, position);
|
||||
return bytesRead >= 0 ? bytesRead : errno;
|
||||
}
|
||||
|
||||
|
||||
ssize_t
|
||||
BFileDescriptorIO::WriteAt(off_t position, const void *buffer, size_t size)
|
||||
{
|
||||
ssize_t bytesWritten = pwrite(fFD, buffer, size, position);
|
||||
return bytesWritten >= 0 ? bytesWritten : errno;
|
||||
}
|
||||
|
||||
|
||||
off_t
|
||||
BFileDescriptorIO::Seek(off_t position, uint32 seekMode)
|
||||
{
|
||||
off_t result = lseek(fFD, position, seekMode);
|
||||
return result >= 0 ? result : errno;
|
||||
}
|
||||
|
||||
|
||||
off_t
|
||||
BFileDescriptorIO::Position() const
|
||||
{
|
||||
off_t result = lseek(fFD, 0, SEEK_CUR);
|
||||
return result >= 0 ? result : errno;
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
BFileDescriptorIO::SetSize(off_t size)
|
||||
{
|
||||
return ftruncate(fFD, size) == 0 ? B_OK : errno;
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
BFileDescriptorIO::GetSize(off_t* size) const
|
||||
{
|
||||
struct stat st;
|
||||
if (fstat(fFD, &st) < 0)
|
||||
return errno;
|
||||
|
||||
*size = st.st_size;
|
||||
return B_OK;
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
* Copyright 2009-2010, Ingo Weinhold, [email protected].
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
|
||||
#include <FileIO.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
BFileIO::BFileIO(FILE* file, bool takeOverOwnership)
|
||||
:
|
||||
fFile(file),
|
||||
fOwnsFile(takeOverOwnership)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
BFileIO::~BFileIO()
|
||||
{
|
||||
if (fOwnsFile && fFile != NULL)
|
||||
fclose(fFile);
|
||||
}
|
||||
|
||||
|
||||
ssize_t
|
||||
BFileIO::Read(void *buffer, size_t size)
|
||||
{
|
||||
errno = B_OK;
|
||||
ssize_t bytesRead = fread(buffer, 1, size, fFile);
|
||||
return bytesRead >= 0 ? bytesRead : errno;
|
||||
}
|
||||
|
||||
|
||||
ssize_t
|
||||
BFileIO::Write(const void *buffer, size_t size)
|
||||
{
|
||||
errno = B_OK;
|
||||
ssize_t bytesRead = fwrite(buffer, 1, size, fFile);
|
||||
return bytesRead >= 0 ? bytesRead : errno;
|
||||
}
|
||||
|
||||
|
||||
ssize_t
|
||||
BFileIO::ReadAt(off_t position, void *buffer, size_t size)
|
||||
{
|
||||
// save the old position and seek to the requested one
|
||||
off_t oldPosition = _Seek(position, SEEK_SET);
|
||||
if (oldPosition < 0)
|
||||
return oldPosition;
|
||||
|
||||
// read
|
||||
ssize_t result = BFileIO::Read(buffer, size);
|
||||
|
||||
// seek back
|
||||
fseeko(fFile, oldPosition, SEEK_SET);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
ssize_t
|
||||
BFileIO::WriteAt(off_t position, const void *buffer, size_t size)
|
||||
{
|
||||
// save the old position and seek to the requested one
|
||||
off_t oldPosition = _Seek(position, SEEK_SET);
|
||||
if (oldPosition < 0)
|
||||
return oldPosition;
|
||||
|
||||
// write
|
||||
ssize_t result = BFileIO::Write(buffer, size);
|
||||
|
||||
// seek back
|
||||
fseeko(fFile, oldPosition, SEEK_SET);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
off_t
|
||||
BFileIO::Seek(off_t position, uint32 seekMode)
|
||||
{
|
||||
if (fseeko(fFile, position, seekMode) < 0)
|
||||
return errno;
|
||||
|
||||
return BFileIO::Position();
|
||||
}
|
||||
|
||||
|
||||
off_t
|
||||
BFileIO::Position() const
|
||||
{
|
||||
off_t result = ftello(fFile);
|
||||
return result >= 0 ? result : errno;
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
BFileIO::SetSize(off_t size)
|
||||
{
|
||||
return B_UNSUPPORTED;
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
BFileIO::GetSize(off_t* _size) const
|
||||
{
|
||||
// save the current position and seek to the end
|
||||
off_t position = _Seek(0, SEEK_END);
|
||||
if (position < 0)
|
||||
return position;
|
||||
|
||||
// get the size (position at end) and seek back
|
||||
off_t size = _Seek(position, SEEK_SET);
|
||||
if (size < 0)
|
||||
return size;
|
||||
|
||||
*_size = size;
|
||||
return B_OK;
|
||||
}
|
||||
|
||||
|
||||
off_t
|
||||
BFileIO::_Seek(off_t position, uint32 seekMode) const
|
||||
{
|
||||
// save the current position
|
||||
off_t oldPosition = ftello(fFile);
|
||||
if (oldPosition < 0)
|
||||
return errno;
|
||||
|
||||
// seek to the requested position
|
||||
if (fseeko(fFile, position, seekMode) < 0)
|
||||
return errno;
|
||||
|
||||
return oldPosition;
|
||||
}
|
||||
@@ -21,6 +21,8 @@ MergeObject <libbe>storage_kit.o :
|
||||
Entry.cpp
|
||||
EntryList.cpp
|
||||
File.cpp
|
||||
FileDescriptorIO.cpp
|
||||
FileIO.cpp
|
||||
FindDirectory.cpp
|
||||
Mime.cpp
|
||||
MimeType.cpp
|
||||
|
||||
Reference in New Issue
Block a user