Added sharedFile library

This commit is contained in:
Anonymous
2014-01-14 00:28:35 -07:00
parent 06c67c31de
commit cae5de9425
53 changed files with 9876 additions and 0 deletions
@@ -0,0 +1,123 @@
// ======================================================================
//
// OsFile.cpp
// Copyright 2002, Sony Online Entertainment Inc.
// All Rights Reserved.
//
// ======================================================================
#include "sharedFile/FirstSharedFile.h"
#include "sharedFile/OsFile.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
// ======================================================================
void OsFile::install()
{
}
// ----------------------------------------------------------------------
bool OsFile::exists(const char *fileName)
{
struct stat statBuffer;
if (stat(fileName, &statBuffer) != 0)
return false;
if (S_ISDIR(statBuffer.st_mode))
return false;
return true;
}
// ----------------------------------------------------------------------
int OsFile::getFileSize(const char *fileName)
{
struct stat statBuffer;
int fileSize = 0;
if (stat(fileName, &statBuffer) == 0)
{
fileSize = static_cast<int>(statBuffer.st_size);
}
return fileSize;
}
// ----------------------------------------------------------------------
OsFile *OsFile::open(const char *fileName, bool randomAccess)
{
UNREF(randomAccess);
if (!exists(fileName))
return 0;
// attempt to open the file
const int handle = ::open(fileName, O_RDONLY);
FATAL(handle < 0, ("OsFile::open failed to open file %s, errno=%d, which does exist.", fileName, errno));
return new OsFile(handle, DuplicateString(fileName));
}
// ----------------------------------------------------------------------
OsFile::OsFile(int handle, char *fileName)
:
m_handle(handle),
m_length(0),
m_offset(0),
m_fileName(fileName)
{
m_length = lseek(m_handle, 0, SEEK_END);
lseek(m_handle, 0, SEEK_SET);
}
// ----------------------------------------------------------------------
OsFile::~OsFile()
{
close(m_handle);
delete [] m_fileName;
}
// ----------------------------------------------------------------------
int OsFile::length() const
{
return m_length;
}
// ----------------------------------------------------------------------
void OsFile::seek(int newFilePosition)
{
if (m_offset != newFilePosition)
{
const int result = lseek(m_handle, newFilePosition, SEEK_SET);
DEBUG_FATAL(result != newFilePosition, ("SetFilePointer failed"));
UNREF(result);
m_offset = newFilePosition;
}
}
// ----------------------------------------------------------------------
int OsFile::read(void *destinationBuffer, int numberOfBytes)
{
int result = 0;
do
{
result = ::read(m_handle, destinationBuffer, numberOfBytes);
DEBUG_FATAL((result < 0 && errno != EAGAIN), ("Read failed for %s: %d %d %s", m_fileName, result, errno, strerror(errno)));
} while (result < 0);
m_offset += result;
return result;
}
// ======================================================================
@@ -0,0 +1,51 @@
// ======================================================================
//
// OsFile.h
// Copyright 2002, Sony Online Entertainment Inc.
// All Rights Reserved.
//
// ======================================================================
#ifndef INCLUDED_OsFile_H
#define INCLUDED_OsFile_H
// ======================================================================
class OsFile
{
public:
static void install();
static bool exists(const char *fileName);
static int getFileSize(const char *fileName);
static OsFile *open(const char *fileName, bool randomAccess=false);
public:
~OsFile();
int length() const;
int tell() const;
void seek(int newFilePosition);
int read(void *destinationBuffer, int numberOfBytes);
private:
OsFile(int handle, char *fileName);
OsFile();
OsFile(const OsFile &);
OsFile &operator =(const OsFile &);
private:
int m_handle;
int m_length;
int m_offset;
char *m_fileName;
};
// ======================================================================
#endif