Skip to content

Commit

Permalink
Revise 'vfs' code.
Browse files Browse the repository at this point in the history
Switch from struct to class, add encapsulation.
Fix cFILE::fseek() logic.
Fix #14
  • Loading branch information
viewizard committed Oct 19, 2018
1 parent c957c4c commit 276f6b9
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 80 deletions.
14 changes: 7 additions & 7 deletions src/core/audio/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ constexpr unsigned DYNBUF_SIZE{16384}; // (stream) buffer size

struct sStreamBuffer {
std::array<ALuint, NUM_OF_DYNBUF> Buffers{};
std::unique_ptr<sFILE> File{};
std::unique_ptr<cFILE> File{};
OggVorbis_File mVF{};
vorbis_info *mInfo{nullptr};
};
Expand All @@ -64,12 +64,12 @@ std::unordered_map<std::string, sStreamBuffer> StreamBuffersMap;
*/
static size_t VorbisRead(void *ptr, size_t byteSize, size_t sizeToRead, void *datasource)
{
sFILE *vorbisData = static_cast<sFILE *>(datasource);
cFILE *vorbisData = static_cast<cFILE *>(datasource);
return vorbisData->fread(ptr, byteSize, sizeToRead);
}
static int VorbisSeek(void *datasource, ogg_int64_t offset, int whence)
{
sFILE *vorbisData = static_cast<sFILE *>(datasource);
cFILE *vorbisData = static_cast<cFILE *>(datasource);
return vorbisData->fseek(offset, whence);
}
static int VorbisClose(void *UNUSED(datasource))
Expand All @@ -78,7 +78,7 @@ static int VorbisClose(void *UNUSED(datasource))
}
static long VorbisTell(void *datasource)
{
sFILE *vorbisData = static_cast<sFILE *>(datasource);
cFILE *vorbisData = static_cast<cFILE *>(datasource);
return vorbisData->ftell();
}

Expand Down Expand Up @@ -372,7 +372,7 @@ ALuint vw_CreateSoundBufferFromOGG(const std::string &Name)
if (Buffer)
return Buffer;

std::unique_ptr<sFILE> file = vw_fopen(Name);
std::unique_ptr<cFILE> file = vw_fopen(Name);
if (!file)
return 0;

Expand Down Expand Up @@ -438,11 +438,11 @@ ALuint vw_CreateSoundBufferFromWAV(const std::string &Name)
if (Buffer)
return Buffer;

std::unique_ptr<sFILE> file = vw_fopen(Name);
std::unique_ptr<cFILE> file = vw_fopen(Name);
if (!file)
return 0;

Buffer = alutCreateBufferFromFileImage(file->Data.get(), file->Size);
Buffer = alutCreateBufferFromFileImage(file->GetData(), static_cast<ALsizei>(file->GetSize()));
if (!CheckALUTError(__func__))
return 0;

Expand Down
2 changes: 1 addition & 1 deletion src/core/font/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ int vw_InitFont(const std::string &FontName)
if (InternalFontBuffer.get())
InternalFontBuffer.reset();

std::unique_ptr<sFILE> FontFile = vw_fopen(FontName);
std::unique_ptr<cFILE> FontFile = vw_fopen(FontName);
if (!FontFile) {
std::cerr << __func__ << "(): " << "Can't open font file: " << FontName << "\n";
return ERR_FILE_NOT_FOUND;
Expand Down
12 changes: 6 additions & 6 deletions src/core/graphics/gl_glsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,16 +236,16 @@ std::weak_ptr<cGLSL> vw_CreateShader(const std::string &ShaderName,
// create empty object
ShadersMap[ShaderName]->VertexShader = pfn_glCreateShader(GL_VERTEX_SHADER);

std::unique_ptr<sFILE> VertexFile = vw_fopen(VertexShaderFileName);
std::unique_ptr<cFILE> VertexFile = vw_fopen(VertexShaderFileName);

if (!VertexFile) {
ShadersMap.erase(ShaderName);
std::cerr << __func__ << "(): " << "Can't find file " << VertexShaderFileName << "\n";
return std::weak_ptr<cGLSL>{};
}

const GLchar *TmpGLchar = (const GLchar *)VertexFile->Data.get();
GLint TmpGLint = (GLint)VertexFile->Size;
const GLchar *TmpGLchar = (const GLchar *)VertexFile->GetData();
GLint TmpGLint = (GLint)VertexFile->GetSize();
pfn_glShaderSource(ShadersMap[ShaderName]->VertexShader, 1, &TmpGLchar, &TmpGLint);
vw_fclose(VertexFile);
}
Expand All @@ -254,16 +254,16 @@ std::weak_ptr<cGLSL> vw_CreateShader(const std::string &ShaderName,
// create empty object
ShadersMap[ShaderName]->FragmentShader = pfn_glCreateShader(GL_FRAGMENT_SHADER);

std::unique_ptr<sFILE> FragmentFile = vw_fopen(FragmentShaderFileName);
std::unique_ptr<cFILE> FragmentFile = vw_fopen(FragmentShaderFileName);

if (!FragmentFile) {
ShadersMap.erase(ShaderName);
std::cerr << __func__ << "(): " << "Can't find file " << FragmentShaderFileName << "\n";
return std::weak_ptr<cGLSL>{};
}

const GLchar *TmpGLchar = (const GLchar *)FragmentFile->Data.get();
GLint TmpGLint = (GLint)FragmentFile->Size;
const GLchar *TmpGLchar = (const GLchar *)FragmentFile->GetData();
GLint TmpGLint = (GLint)FragmentFile->GetSize();
pfn_glShaderSource(ShadersMap[ShaderName]->FragmentShader, 1, &TmpGLchar, &TmpGLint);
vw_fclose(FragmentFile);
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/model3d/model3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ bool cModel3DWrapper::LoadVW3D(const std::string &FileName)
if (FileName.empty())
return false;

std::unique_ptr<sFILE> File = vw_fopen(FileName);
std::unique_ptr<cFILE> File = vw_fopen(FileName);
if (!File)
return false;

Expand Down
14 changes: 7 additions & 7 deletions src/core/text/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ static void CreateTextTableUTF32()
/*
* Parse each row's block, separated by 1.SymbolSeparator, 2.SymbolEndOfLine, 3.EOF
*/
static int GetRowTextBlock(std::string &CurrentTextBlock, uint8_t *Data, unsigned DataSize, unsigned &i,
static int GetRowTextBlock(std::string &CurrentTextBlock, uint8_t *Data, long DataSize, long &i,
const char SymbolSeparator, const char SymbolEndOfLine)
{
constexpr char SymbolQuotes{'\"'};
Expand Down Expand Up @@ -159,7 +159,7 @@ int vw_InitText(const char *FileName, const char SymbolSeparator, const char Sym
vw_ReleaseText();

// open and don't call vw_fclose(), use tmpFile->Data directly
std::unique_ptr<sFILE> tmpFile = vw_fopen(FileName);
std::unique_ptr<cFILE> tmpFile = vw_fopen(FileName);
if (!tmpFile)
return ERR_FILE_NOT_FOUND;
std::cout << "Load and parse .csv file " << FileName << "\n";
Expand All @@ -171,15 +171,15 @@ int vw_InitText(const char *FileName, const char SymbolSeparator, const char Sym
std::string CurrentRowCode;
unsigned int CurrentColumnNumber{0};
unsigned int LineNumber{1}; // line number for error message
for (unsigned int i = 0; i < tmpFile->Size; i++) {
for (long i = 0; i < tmpFile->GetSize(); i++) {
// parse each row
for (; (tmpFile->Data[i] != SymbolEndOfLine) && (i < tmpFile->Size); i++) {
for (; (tmpFile->GetData()[i] != SymbolEndOfLine) && (i < tmpFile->GetSize()); i++) {
// read text block in line, .csv line looks like:
// text_block;text_block;...;text_blockSymbolEndOfLine
// if text braced by quotes:
// "text_block";"text_block";...;"text_block"SymbolEndOfLine
std::string CurrentRowTextBlock{};
if (GetRowTextBlock(CurrentRowTextBlock, tmpFile->Data.get(), tmpFile->Size, i,
if (GetRowTextBlock(CurrentRowTextBlock, tmpFile->GetData(), tmpFile->GetSize(), i,
SymbolSeparator, SymbolEndOfLine)) {
std::cerr << __func__ << "(): " << "file corrupted.";
vw_ReleaseText();
Expand All @@ -198,10 +198,10 @@ int vw_InitText(const char *FileName, const char SymbolSeparator, const char Sym
if (isElementPresentInTable(TextTable, CurrentColumnNumber, CurrentRowCode)) {
std::cerr << __func__ << "(): " << "* Duplicate line detected, line number "
<< LineNumber << "\n";
for (; (tmpFile->Data[i] != SymbolEndOfLine) && (i < tmpFile->Size); i++) {}
for (; (tmpFile->GetData()[i] != SymbolEndOfLine) && (i < tmpFile->GetSize()); i++) {}
}
// we found SymbolEndOfLine in previous cycle, in order to prevent "i" changes, break cycle
if (tmpFile->Data[i] == SymbolEndOfLine)
if (tmpFile->GetData()[i] == SymbolEndOfLine)
break;
}
// move to next row
Expand Down
4 changes: 2 additions & 2 deletions src/core/texture/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ void vw_ConvertImageToVW2D(const std::string &SrcName, const std::string &DestNa
std::unique_ptr<uint8_t[]> tmpPixelsArray{};
eLoadTextureAs LoadAs{eLoadTextureAs::TGA};

std::unique_ptr<sFILE> pFile = vw_fopen(SrcName);
std::unique_ptr<cFILE> pFile = vw_fopen(SrcName);
if (pFile == nullptr) {
std::cerr << __func__ << "(): " << "Unable to found " << SrcName << "\n";
return;
Expand Down Expand Up @@ -396,7 +396,7 @@ GLtexture vw_LoadTexture(const std::string &TextureName, eTextureCompressionType
// don't use std::vector here, since it allocates AND value-initializes
std::unique_ptr<uint8_t[]> tmpPixelsArray{};

std::unique_ptr<sFILE> pFile = vw_fopen(TextureName);
std::unique_ptr<cFILE> pFile = vw_fopen(TextureName);
if (!pFile) {
std::cerr << __func__ << "(): " << "Unable to found " << TextureName << "\n";
return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/core/texture/texture_tga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

namespace viewizard {

int ReadTGA(std::unique_ptr<uint8_t[]> &PixelsArray, sFILE *pFile, int &DWidth, int &DHeight, int &DChanels)
int ReadTGA(std::unique_ptr<uint8_t[]> &PixelsArray, cFILE *pFile, int &DWidth, int &DHeight, int &DChanels)
{
constexpr uint8_t TGA_RGB{2}; // normal RGB (BGR) file
constexpr uint8_t TGA_RLE{10}; // RLE file
Expand Down
2 changes: 1 addition & 1 deletion src/core/texture/texture_tga.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

namespace viewizard {

int ReadTGA(std::unique_ptr<uint8_t[]> &PixelsArray, sFILE *pFile, int &DWidth, int &DHeight, int &DChanels);
int ReadTGA(std::unique_ptr<uint8_t[]> &PixelsArray, cFILE *pFile, int &DWidth, int &DHeight, int &DChanels);

} // viewizard namespace

Expand Down
71 changes: 35 additions & 36 deletions src/core/vfs/vfs.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
On VFS file open, VFS entries list generated with all available in this VFS
files data. Could be opened multiple VFS files, in this case VFS entries list
will contain all available in all opened VFS files data.
On sFILE open, all requested data will be copied into memory buffer (sFILE->Data).
Opened sFILE is not connected to VFS file or VFS entries list in any way.
On cFILE open, all requested data will be copied into memory buffer (cFILE->Data_).
Opened cFILE is not connected to VFS file or VFS entries list in any way.
Caller should hold sFILE open as long, as it need memory buffer (sFILE->Data).
In order to code simplicity, read and write direct access to sFILE data allowed.
Caller could reset() memory buffer with different size (sFILE->Data), but should
care about sFILE->Size and sFILE->Pos field (access by fseek()).
Caller should hold cFILE open as long, as it need memory buffer (cFILE->Data_).
In order to code simplicity, read and write direct access to cFILE data allowed.
Caller could reset() memory buffer with different size (cFILE->Data), but should
care about cFILE->Size_ and cFILE->Pos_ field (access by fseek()).
Game data VFS v1.6 structure.
Expand Down Expand Up @@ -85,7 +85,7 @@ struct sVFS {

struct sVFS_Entry {
uint32_t Offset{0};
uint32_t Size{0};
uint32_t Size{0}; // NOTE cFILE::Size_ is long (32/64 depending from platform)
std::weak_ptr<sVFS> Parent{};
};

Expand Down Expand Up @@ -225,11 +225,12 @@ int vw_CreateVFS(const std::string &Name, unsigned int BuildNumber,

// copy all files from pack into new VFS
for (const auto &tmpVFSEntry : VFSEntriesMap) {
std::unique_ptr<sFILE> tmpFile = vw_fopen(tmpVFSEntry.first);
std::unique_ptr<cFILE> tmpFile = vw_fopen(tmpVFSEntry.first);
if (!tmpFile)
return ERR_FILE_NOT_FOUND;
err = WriteIntoVFSfromMemory(TempVFS, tmpVFSEntry.first, tmpFile->Data.get(),
tmpFile->Size, FileTableOffset, WritableVFSEntriesMap);
err = WriteIntoVFSfromMemory(TempVFS, tmpVFSEntry.first, tmpFile->GetData(),
static_cast<uint32_t>(tmpFile->GetSize()),
FileTableOffset, WritableVFSEntriesMap);
if (err) {
std::cerr << __func__ << "(): " << "VFS compilation process aborted!\n";
return err;
Expand Down Expand Up @@ -364,7 +365,7 @@ void vw_ShutdownVFS()
* Open the sFILE.
* Return std::unique_ptr, provide smart pointer connected to caller's scope.
*/
std::unique_ptr<sFILE> vw_fopen(const std::string &FileName)
std::unique_ptr<cFILE> vw_fopen(const std::string &FileName)
{
if (FileName.empty())
return nullptr;
Expand All @@ -377,12 +378,12 @@ std::unique_ptr<sFILE> vw_fopen(const std::string &FileName)
return nullptr;
}

std::unique_ptr<sFILE> File(new sFILE(0, 0));
std::unique_ptr<cFILE> File(new cFILE(0, 0));

File->Size = FileInVFS->second.Size;
File->Size_ = static_cast<long>(FileInVFS->second.Size);
sharedParent->File.seekg(FileInVFS->second.Offset, std::ios::beg);
File->Data.reset(new uint8_t[File->Size]);
sharedParent->File.read(reinterpret_cast<char*>(File->Data.get()), File->Size);
File->Data_.reset(new uint8_t[File->Size_]);
sharedParent->File.read(reinterpret_cast<char*>(File->Data_.get()), File->Size_);

return File;
}
Expand All @@ -395,11 +396,11 @@ std::unique_ptr<sFILE> vw_fopen(const std::string &FileName)
return nullptr;
fsFile.seekg(0, std::ios::beg);

std::unique_ptr<sFILE> File(new sFILE(0, 0));
std::unique_ptr<cFILE> File(new cFILE(0, 0));

File->Size = static_cast<uint32_t>(tmpSize);
File->Data.reset(new uint8_t[File->Size]);
fsFile.read(reinterpret_cast<char*>(File->Data.get()), File->Size);
File->Size_ = static_cast<long>(tmpSize);
File->Data_.reset(new uint8_t[File->Size_]);
fsFile.read(reinterpret_cast<char*>(File->Data_.get()), File->Size_);

return File;
}
Expand All @@ -411,7 +412,7 @@ std::unique_ptr<sFILE> vw_fopen(const std::string &FileName)
* You could call vw fclose() if you should release memory in particular
* part of code. Otherwise, it will be released automatically (see. unique_ptr).
*/
int vw_fclose(std::unique_ptr<sFILE> &stream)
int vw_fclose(std::unique_ptr<cFILE> &stream)
{
if (!stream.get())
return ERR_PARAMETERS;
Expand All @@ -426,15 +427,15 @@ int vw_fclose(std::unique_ptr<sFILE> &stream)
* Reads an array of 'count' elements, each one with a size of 'size' bytes,
* from the stream and stores them in the block of memory specified by 'buffer'.
*/
size_t sFILE::fread(void *buffer, size_t size, size_t count)
size_t cFILE::fread(void *buffer, size_t size, size_t count)
{
if (!buffer || !Data)
if (!buffer || !Data_)
return ERR_PARAMETERS;

size_t CopyCount{0};
for (; (CopyCount < count) && (Size >= static_cast<uint32_t>(Pos + size)); CopyCount++) {
memcpy(static_cast<uint8_t *>(buffer) + CopyCount * size, Data.get() + Pos, size);
Pos += size;
for (; (CopyCount < count) && (Size_ >= static_cast<long>(Pos_ + size)); CopyCount++) {
memcpy(static_cast<uint8_t *>(buffer) + CopyCount * size, Data_.get() + Pos_, size);
Pos_ += size;
}

return CopyCount;
Expand All @@ -443,27 +444,25 @@ size_t sFILE::fread(void *buffer, size_t size, size_t count)
/*
* Sets the position indicator associated with the stream to a new position.
*/
int sFILE::fseek(long offset, int origin)
int cFILE::fseek(long offset, int origin)
{
// FIXME offset could be positive or negative, fix logic and use proper types (don't allow signed/unsigned comparison)

switch (origin) {
case SEEK_CUR:
if ((Pos + offset) > Size)
if ((Pos_ + offset > Size_) || (Pos_ + offset < 0))
return ERR_PARAMETERS;
Pos += offset;
Pos_ += offset;
break;

case SEEK_END:
if (offset > Size)
if ((offset > 0) || (Size_ + offset < 0))
return ERR_PARAMETERS;
Pos = Size - offset;
Pos_ = Size_ + offset;
break;

case SEEK_SET:
if ((offset < 0) || (offset > Size))
if ((offset < 0) || (offset > Size_))
return ERR_PARAMETERS;
Pos = offset;
Pos_ = offset;
break;

default:
Expand All @@ -477,9 +476,9 @@ int sFILE::fseek(long offset, int origin)
/*
* Returns the current value of the position indicator of the stream.
*/
long sFILE::ftell()
long cFILE::ftell()
{
return Pos;
return Pos_;
}

} // viewizard namespace
Loading

0 comments on commit 276f6b9

Please sign in to comment.