Reindent native/file

This commit is contained in:
Henrik Rydgård 2012-10-31 13:23:16 +01:00
parent ad001f7e16
commit 58e9b9ffe8
9 changed files with 547 additions and 547 deletions

View File

@ -5,278 +5,278 @@
//#define CHUNKDEBUG
ChunkFile::ChunkFile(const char *filename, bool _read) {
data=0;
fn = filename;
fastMode=false;
numLevels=0;
read=_read;
pos=0;
didFail=false;
data=0;
fn = filename;
fastMode=false;
numLevels=0;
read=_read;
pos=0;
didFail=false;
fastMode = read ? true : false;
fastMode = read ? true : false;
if (fastMode) {
size_t size;
data = (uint8_t *)VFSReadFile(filename, &size);
if (!data) {
ELOG("Chunkfile fail: %s", filename);
didFail = true;
return;
}
eof = size;
return;
}
if (fastMode) {
size_t size;
data = (uint8_t *)VFSReadFile(filename, &size);
if (!data) {
ELOG("Chunkfile fail: %s", filename);
didFail = true;
return;
}
eof = size;
return;
}
if (file.open(filename, FILE_WRITE)) {
didFail=false;
eof=file.fileSize();
} else {
didFail=true;
return;
}
if (file.open(filename, FILE_WRITE)) {
didFail=false;
eof=file.fileSize();
} else {
didFail=true;
return;
}
}
ChunkFile::~ChunkFile() {
if (fastMode && data)
delete [] data;
else
file.close();
if (fastMode && data)
delete [] data;
else
file.close();
}
int ChunkFile::readInt() {
if (pos<eof) {
/*
int temp = *(int *)(data+pos);
pos+=4;
*/
pos+=4;
if (fastMode)
return *(int *)(data+pos-4);
else
return file.readInt();
} else {
return 0;
}
if (pos<eof) {
/*
int temp = *(int *)(data+pos);
pos+=4;
*/
pos+=4;
if (fastMode)
return *(int *)(data+pos-4);
else
return file.readInt();
} else {
return 0;
}
}
void ChunkFile::writeInt(int i) {
/*
*(int *)(data+pos) = i;
pos+=4;
*/
#ifndef DEMO_VERSION //if this is missing.. heheh
file.writeInt(i);
pos+=4;
/*
*(int *)(data+pos) = i;
pos+=4;
*/
#ifndef DEMO_VERSION //if this is missing.. heheh
file.writeInt(i);
pos+=4;
#endif
}
//let's get into the business
bool ChunkFile::descend(uint32 id) {
id=flipID(id);
if (read) {
bool found = false;
id=flipID(id);
if (read) {
bool found = false;
//save information to restore after the next Ascend
stack[numLevels].parentStartLocation = pos;
stack[numLevels].parentEOF = eof;
//save information to restore after the next Ascend
stack[numLevels].parentStartLocation = pos;
stack[numLevels].parentEOF = eof;
ChunkInfo temp = stack[numLevels];
ChunkInfo temp = stack[numLevels];
int firstID = 0;
//let's search through children..
while(pos<eof) {
stack[numLevels].ID = readInt();
if (firstID == 0) firstID=stack[numLevels].ID|1;
stack[numLevels].length = readInt();
stack[numLevels].startLocation = pos;
int firstID = 0;
//let's search through children..
while(pos<eof) {
stack[numLevels].ID = readInt();
if (firstID == 0) firstID=stack[numLevels].ID|1;
stack[numLevels].length = readInt();
stack[numLevels].startLocation = pos;
if (stack[numLevels].ID == id)
{
found = true;
break;
} else {
seekTo(pos + stack[numLevels].length); //try next block
}
}
if (stack[numLevels].ID == id)
{
found = true;
break;
} else {
seekTo(pos + stack[numLevels].length); //try next block
}
}
//if we found nothing, return false so the caller can skip this
if (!found) {
//if we found nothing, return false so the caller can skip this
if (!found) {
#ifdef CHUNKDEBUG
ILOG("Couldn't find %c%c%c%c", id, id>>8, id>>16, id>>24);
ILOG("Couldn't find %c%c%c%c", id, id>>8, id>>16, id>>24);
#endif
stack[numLevels]=temp;
seekTo(stack[numLevels].parentStartLocation);
return false;
}
stack[numLevels]=temp;
seekTo(stack[numLevels].parentStartLocation);
return false;
}
//descend into it
//pos was set inside the loop above
eof = stack[numLevels].startLocation + stack[numLevels].length;
numLevels++;
//descend into it
//pos was set inside the loop above
eof = stack[numLevels].startLocation + stack[numLevels].length;
numLevels++;
#ifdef CHUNKDEBUG
ILOG("Descended into %c%c%c%c", id, id>>8, id>>16, id>>24);
ILOG("Descended into %c%c%c%c", id, id>>8, id>>16, id>>24);
#endif
return true;
} else {
#ifndef DEMO_VERSION //if this is missing.. heheh
//write a chunk id, and prepare for filling in length later
writeInt(id);
writeInt(0); //will be filled in by Ascend
stack[numLevels].startLocation=pos;
numLevels++;
return true;
return true;
} else {
#ifndef DEMO_VERSION //if this is missing.. heheh
//write a chunk id, and prepare for filling in length later
writeInt(id);
writeInt(0); //will be filled in by Ascend
stack[numLevels].startLocation=pos;
numLevels++;
return true;
#else
return true;
return true;
#endif
}
}
}
void ChunkFile::seekTo(int _pos) {
if (!fastMode)
file.seekBeg(_pos);
pos=_pos;
if (!fastMode)
file.seekBeg(_pos);
pos=_pos;
}
//let's ascend out
void ChunkFile::ascend() {
if (read) {
//ascend, and restore information
numLevels--;
seekTo(stack[numLevels].parentStartLocation);
eof = stack[numLevels].parentEOF;
if (read) {
//ascend, and restore information
numLevels--;
seekTo(stack[numLevels].parentStartLocation);
eof = stack[numLevels].parentEOF;
#ifdef CHUNKDEBUG
int id = stack[numLevels].ID;
ILOG("Ascended out of %c%c%c%c", id, id>>8, id>>16, id>>24);
int id = stack[numLevels].ID;
ILOG("Ascended out of %c%c%c%c", id, id>>8, id>>16, id>>24);
#endif
} else {
numLevels--;
//now fill in the written length automatically
int posNow = pos;
seekTo(stack[numLevels].startLocation - 4);
writeInt(posNow-stack[numLevels].startLocation);
seekTo(posNow);
}
} else {
numLevels--;
//now fill in the written length automatically
int posNow = pos;
seekTo(stack[numLevels].startLocation - 4);
writeInt(posNow-stack[numLevels].startLocation);
seekTo(posNow);
}
}
//read a block
void ChunkFile::readData(void *what, int count) {
if (fastMode)
memcpy(what, data + pos, count);
else
file.read(what,count);
if (fastMode)
memcpy(what, data + pos, count);
else
file.read(what,count);
pos+=count;
char temp[4]; //discarded
count &= 3;
if (count) {
count=4-count;
if (!fastMode)
file.read(temp,count);
pos+=count;
}
pos+=count;
char temp[4]; //discarded
count &= 3;
if (count) {
count=4-count;
if (!fastMode)
file.read(temp,count);
pos+=count;
}
}
//write a block
void ChunkFile::writeData(const void *what, int count) {
file.write(what, count);
pos+=count;
char temp[5]={0,0,0,0,0};
count &= 3;
if (count)
{
count=4-count;
file.write(temp,count);
pos+=count;
}
file.write(what, count);
pos+=count;
char temp[5]={0,0,0,0,0};
count &= 3;
if (count)
{
count=4-count;
file.write(temp,count);
pos+=count;
}
}
/*
void ChunkFile::writeWString(String str) {
wchar_t *text;
int len=str.length();
wchar_t *text;
int len=str.length();
#ifdef UNICODE
#error
text = str.getPointer();
text = str.getPointer();
#else
text = new wchar_t[len+1];
str.toUnicode(text);
text = new wchar_t[len+1];
str.toUnicode(text);
#endif
writeInt(len);
writeData((char *)text, len * sizeof(wchar_t));
writeInt(len);
writeData((char *)text, len * sizeof(wchar_t));
#ifndef UNICODE
delete [] text;
delete [] text;
#endif
}
*/
void ChunkFile::writeWString(const std::string &str) {
unsigned short *text;
int len=str.length();
unsigned short *text;
int len=str.length();
#ifdef UNICODE
#error
text = str.c_str();
text = str.c_str();
#else
text = new unsigned short[len+1];
for (int i=0; i<len; i++)
text[i]=str[i];
text[len]=0;
text = new unsigned short[len+1];
for (int i=0; i<len; i++)
text[i]=str[i];
text[len]=0;
#endif
writeInt(len);
writeData((char *)text, len * sizeof(unsigned short));
writeInt(len);
writeData((char *)text, len * sizeof(unsigned short));
#ifndef UNICODE
delete [] text;
delete [] text;
#endif
}
static void toUnicode(const std::string &str, uint16 *t) {
for (int i=0; i<(int)str.size(); i++) {
*t++ = str[i];
}
*t++ = '\0';
for (int i=0; i<(int)str.size(); i++) {
*t++ = str[i];
}
*t++ = '\0';
}
static std::string fromUnicode(const uint16_t *src, int len) {
std::string str;
str.resize(len);
for (int i=0; i<len; i++) {
str[i] = src[i] > 255 ? ' ' : src[i];
}
return str;
std::string str;
str.resize(len);
for (int i=0; i<len; i++) {
str[i] = src[i] > 255 ? ' ' : src[i];
}
return str;
}
std::string ChunkFile::readWString() {
int len=readInt();
uint16_t *text = new uint16_t[len+1];
readData((char *)text, len*sizeof(uint16_t));
text[len] = 0;
std::string temp = fromUnicode(text, len);
delete [] text;
return temp;
int len=readInt();
uint16_t *text = new uint16_t[len+1];
readData((char *)text, len*sizeof(uint16_t));
text[len] = 0;
std::string temp = fromUnicode(text, len);
delete [] text;
return temp;
}
void ChunkFile::writeString(const std::string &str) {
uint16_t *text;
int len = str.size();
text=new uint16_t[len+1];
toUnicode(str, text);
writeInt(len);
writeData((char *)text,len*sizeof(uint16_t));
delete [] text;
uint16_t *text;
int len = str.size();
text=new uint16_t[len+1];
toUnicode(str, text);
writeInt(len);
writeData((char *)text,len*sizeof(uint16_t));
delete [] text;
}
std::string ChunkFile::readString() {
int len=readInt();
uint16_t *text = new uint16_t[len+1];
readData((char *)text,len*sizeof(uint16_t));
text[len]=0;
std::string temp = fromUnicode(text, len);
delete [] text;
return temp;
int len=readInt();
uint16_t *text = new uint16_t[len+1];
readData((char *)text,len*sizeof(uint16_t));
text[len]=0;
std::string temp = fromUnicode(text, len);
delete [] text;
return temp;
}
int ChunkFile::getCurrentChunkSize() {
if (numLevels)
return stack[numLevels-1].length;
else
return 0;
if (numLevels)
return stack[numLevels-1].length;
else
return 0;
}

View File

@ -14,54 +14,54 @@
#include "file/easy_file.h"
inline uint32 flipID(uint32 id) {
return ((id>>24)&0xFF) | ((id>>8)&0xFF00) | ((id<<8)&0xFF0000) | ((id<<24)&0xFF000000);
return ((id>>24)&0xFF) | ((id>>8)&0xFF00) | ((id<<8)&0xFF0000) | ((id<<24)&0xFF000000);
}
class ChunkFile {
public:
ChunkFile(const char *filename, bool _read);
~ChunkFile();
ChunkFile(const char *filename, bool _read);
~ChunkFile();
bool descend(uint32 id);
void ascend();
bool descend(uint32 id);
void ascend();
int readInt();
void readInt(int &i) {i = readInt();}
void readData(void *data, int count);
// String readWString();
std::string readWString();
int readInt();
void readInt(int &i) {i = readInt();}
void readData(void *data, int count);
// String readWString();
std::string readWString();
void writeString(const std::string &str);
std::string readString();
void writeString(const std::string &str);
std::string readString();
void writeInt(int i);
//void writeWString(String str);
void writeWString(const std::string &str);
void writeData(const void *data, int count);
void writeInt(int i);
//void writeWString(String str);
void writeWString(const std::string &str);
void writeData(const void *data, int count);
int getCurrentChunkSize();
bool failed() const { return didFail; }
std::string filename() const { return fn; }
int getCurrentChunkSize();
bool failed() const { return didFail; }
std::string filename() const { return fn; }
private:
std::string fn;
LAMEFile file;
struct ChunkInfo {
int startLocation;
int parentStartLocation;
int parentEOF;
unsigned int ID;
int length;
};
ChunkInfo stack[8];
int numLevels;
uint8_t *data;
int pos, eof;
bool fastMode;
bool read;
bool didFail;
void seekTo(int _pos);
int getPos() const {return pos;}
std::string fn;
LAMEFile file;
struct ChunkInfo {
int startLocation;
int parentStartLocation;
int parentEOF;
unsigned int ID;
int length;
};
ChunkInfo stack[8];
int numLevels;
uint8_t *data;
int pos, eof;
bool fastMode;
bool read;
bool didFail;
void seekTo(int _pos);
int getPos() const {return pos;}
};

View File

@ -10,55 +10,55 @@
// An false returned means cancel;
bool OpenFileDialog(const char *title, const char *extension, std::string *filename)
{
OPENFILENAME of;
memset(&of, 0, sizeof(of));
char buffer[512] = {0};
of.lStructSize = sizeof(OPENFILENAME);
of.hInstance = 0;
of.hwndOwner = GetActiveWindow();
OPENFILENAME of;
memset(&of, 0, sizeof(of));
char buffer[512] = {0};
of.lStructSize = sizeof(OPENFILENAME);
of.hInstance = 0;
of.hwndOwner = GetActiveWindow();
// These weird strings with zeroes in them can't be dealt with using normal string
// functions, so here we go - evil hackery.
char filter[256] = "XXX files\0*.XXX\0\0";
memcpy(filter, extension, 3);
memcpy(filter + 12, extension, 3);
of.lpstrFilter = filter;
// These weird strings with zeroes in them can't be dealt with using normal string
// functions, so here we go - evil hackery.
char filter[256] = "XXX files\0*.XXX\0\0";
memcpy(filter, extension, 3);
memcpy(filter + 12, extension, 3);
of.lpstrFilter = filter;
of.lpstrDefExt = extension;
of.lpstrFile = buffer;
of.nMaxFile = 511;
of.lpstrDefExt = extension;
of.lpstrFile = buffer;
of.nMaxFile = 511;
of.Flags = OFN_FILEMUSTEXIST;
if (!GetOpenFileName(&of)) return false;
*filename = of.lpstrFile;
return true;
of.Flags = OFN_FILEMUSTEXIST;
if (!GetOpenFileName(&of)) return false;
*filename = of.lpstrFile;
return true;
}
bool SaveFileDialog(const char *title, const char *extension, std::string *filename)
{
OPENFILENAME of;
memset(&of, 0, sizeof(of));
char buffer[512] = {0};
of.lStructSize = sizeof(OPENFILENAME);
of.hInstance = 0;
of.hwndOwner = GetActiveWindow();
OPENFILENAME of;
memset(&of, 0, sizeof(of));
char buffer[512] = {0};
of.lStructSize = sizeof(OPENFILENAME);
of.hInstance = 0;
of.hwndOwner = GetActiveWindow();
// These weird strings with zeroes in them can't be dealt with using normal string
// functions, so here we go - evil hackery.
char filter[256] = "XXX files\0*.XXX\0\0";
memcpy(filter, extension, 3);
memcpy(filter + 12, extension, 3);
of.lpstrFilter = filter;
// These weird strings with zeroes in them can't be dealt with using normal string
// functions, so here we go - evil hackery.
char filter[256] = "XXX files\0*.XXX\0\0";
memcpy(filter, extension, 3);
memcpy(filter + 12, extension, 3);
of.lpstrFilter = filter;
of.lpstrDefExt = extension;
of.lpstrFile = buffer;
of.nMaxFile = 511;
of.lpstrDefExt = extension;
of.lpstrFile = buffer;
of.nMaxFile = 511;
of.Flags = OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY;
if (!GetSaveFileName(&of))
return false;
*filename = of.lpstrFile;
return true;
of.Flags = OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY;
if (!GetSaveFileName(&of))
return false;
*filename = of.lpstrFile;
return true;
}
#else
@ -69,14 +69,14 @@ bool SaveFileDialog(const char *title, const char *extension, std::string *filen
bool OpenFileDialog(const char *title, const char *extension, std::string *filename)
{
ELOG("Asked for OpenFileDialog, not present on this platform.");
return false;
ELOG("Asked for OpenFileDialog, not present on this platform.");
return false;
}
bool SaveFileDialog(const char *title, const char *extension, std::string *filename)
{
ELOG("Asked for SaveFileDialog, not present on this platform.");
return false;
ELOG("Asked for SaveFileDialog, not present on this platform.");
return false;
}
#endif
#endif

View File

@ -4,87 +4,87 @@
#include "file/easy_file.h"
LAMEFile::LAMEFile() : file_(NULL) {
isOpen = false;
isOpen = false;
}
LAMEFile::~LAMEFile() { }
bool LAMEFile::open(const char *filename, eFileMode mode) {
file_ = fopen(filename, mode == FILE_READ ? "rb" : "wb");
file_ = fopen(filename, mode == FILE_READ ? "rb" : "wb");
if (!file_) {
isOpen = false;
} else {
isOpen = true;
if (mode == FILE_READ) {
fseek(file_, 0, SEEK_END);
size_ = ftell(file_);
fseek(file_, 0, SEEK_SET);
}
}
return isOpen;
if (!file_) {
isOpen = false;
} else {
isOpen = true;
if (mode == FILE_READ) {
fseek(file_, 0, SEEK_END);
size_ = ftell(file_);
fseek(file_, 0, SEEK_SET);
}
}
return isOpen;
}
void LAMEFile::close() {
if (isOpen) {
//close the file and reset variables
fclose(file_);
file_ = NULL;
isOpen=false;
}
if (isOpen) {
//close the file and reset variables
fclose(file_);
file_ = NULL;
isOpen=false;
}
}
int LAMEFile::fileSize() {
if (!isOpen) //of course
return 0;
else
return size_;
if (!isOpen) //of course
return 0;
else
return size_;
}
std::string LAMEFile::readAll() {
std::string s;
size_t size = fileSize();
s.resize(size);
read(&s[0], size);
return s;
std::string s;
size_t size = fileSize();
s.resize(size);
read(&s[0], size);
return s;
}
int LAMEFile::write(const void *data, int size) {
if (isOpen) {
return fwrite(data, 1, size, file_); //we return the number of bytes that actually got written
} else {
return 0;
}
if (isOpen) {
return fwrite(data, 1, size, file_); //we return the number of bytes that actually got written
} else {
return 0;
}
}
int LAMEFile::read(void *data, int size) {
if (isOpen) {
return fread(data, 1, size, file_);
} else {
return 0;
}
if (isOpen) {
return fread(data, 1, size, file_);
} else {
return 0;
}
}
int LAMEFile::readInt() {
int temp;
if (read(&temp, sizeof(int)))
return temp;
else
return 0;
int temp;
if (read(&temp, sizeof(int)))
return temp;
else
return 0;
}
void LAMEFile::writeInt(int i) {
write(&i, sizeof(int));
write(&i, sizeof(int));
}
char LAMEFile::readChar() {
char temp;
if (read(&temp, sizeof(char)))
return temp;
else
return 0;
char temp;
if (read(&temp, sizeof(char)))
return temp;
else
return 0;
}
void LAMEFile::writeChar(char i) {
write(&i,sizeof(char));
write(&i,sizeof(char));
}

View File

@ -11,48 +11,48 @@
// Raw file paths, does not go through VFS.
enum eFileMode {
FILE_READ=5,
FILE_WRITE=6
FILE_READ=5,
FILE_WRITE=6
};
// TODO: Rename.
class LAMEFile {
public:
LAMEFile();
virtual ~LAMEFile();
LAMEFile();
virtual ~LAMEFile();
bool open(const char *filename, eFileMode mode);
bool open(std::string filename, eFileMode mode) {
return open(filename.c_str(), mode);
}
void close();
bool open(const char *filename, eFileMode mode);
bool open(std::string filename, eFileMode mode) {
return open(filename.c_str(), mode);
}
void close();
void writeInt(int i);
void writeChar(char i);
int write(const void *data, int size);
void write(const std::string &str) {
write((void *)str.data(), str.size());
}
void writeInt(int i);
void writeChar(char i);
int write(const void *data, int size);
void write(const std::string &str) {
write((void *)str.data(), str.size());
}
int readInt();
char readChar();
int read(void *data, int size);
int readInt();
char readChar();
int read(void *data, int size);
std::string readAll();
std::string readAll();
int fileSize();
int fileSize();
void seekBeg(int pos) {
if (isOpen) fseek(file_,pos,SEEK_SET);
}
void seekEnd(int pos) {
if (isOpen) fseek(file_,pos,SEEK_END);
}
void seekCurrent(int pos) {
if (isOpen) fseek(file_,pos,SEEK_CUR);
}
void seekBeg(int pos) {
if (isOpen) fseek(file_,pos,SEEK_SET);
}
void seekEnd(int pos) {
if (isOpen) fseek(file_,pos,SEEK_END);
}
void seekCurrent(int pos) {
if (isOpen) fseek(file_,pos,SEEK_CUR);
}
private:
FILE *file_;
bool isOpen;
int size_;
FILE *file_;
bool isOpen;
int size_;
};

View File

@ -14,47 +14,47 @@
bool writeStringToFile(bool text_file, const std::string &str, const char *filename)
{
FILE *f = fopen(filename, text_file ? "w" : "wb");
if (!f)
return false;
size_t len = str.size();
if (len != fwrite(str.data(), 1, str.size(), f))
{
fclose(f);
return false;
}
fclose(f);
return true;
FILE *f = fopen(filename, text_file ? "w" : "wb");
if (!f)
return false;
size_t len = str.size();
if (len != fwrite(str.data(), 1, str.size(), f))
{
fclose(f);
return false;
}
fclose(f);
return true;
}
uint64_t GetSize(FILE *f)
{
// can't use off_t here because it can be 32-bit
uint64_t pos = ftell(f);
if (fseek(f, 0, SEEK_END) != 0) {
return 0;
}
uint64_t size = ftell(f);
// Reset the seek position to where it was when we started.
if ((size != pos) && (fseek(f, pos, SEEK_SET) != 0)) {
// Should error here
return 0;
}
return size;
// can't use off_t here because it can be 32-bit
uint64_t pos = ftell(f);
if (fseek(f, 0, SEEK_END) != 0) {
return 0;
}
uint64_t size = ftell(f);
// Reset the seek position to where it was when we started.
if ((size != pos) && (fseek(f, pos, SEEK_SET) != 0)) {
// Should error here
return 0;
}
return size;
}
bool ReadFileToString(bool text_file, const char *filename, std::string &str)
{
FILE *f = fopen(filename, text_file ? "r" : "rb");
if (!f)
return false;
size_t len = (size_t)GetSize(f);
char *buf = new char[len + 1];
buf[fread(buf, 1, len, f)] = 0;
str = std::string(buf, len);
fclose(f);
delete [] buf;
return true;
FILE *f = fopen(filename, text_file ? "r" : "rb");
if (!f)
return false;
size_t len = (size_t)GetSize(f);
char *buf = new char[len + 1];
buf[fread(buf, 1, len, f)] = 0;
str = std::string(buf, len);
fclose(f);
delete [] buf;
return true;
}
#define DIR_SEP "/"
@ -115,70 +115,70 @@ bool isDirectory(const std::string &filename)
}
size_t getFilesInDir(const char *directory, std::vector<FileInfo> *files) {
size_t foundEntries = 0;
size_t foundEntries = 0;
#ifdef _WIN32
// Find the first file in the directory.
WIN32_FIND_DATA ffd;
// Find the first file in the directory.
WIN32_FIND_DATA ffd;
#ifdef UNICODE
HANDLE hFind = FindFirstFile((std::wstring(directory) + "\\*").c_str(), &ffd);
HANDLE hFind = FindFirstFile((std::wstring(directory) + "\\*").c_str(), &ffd);
#else
HANDLE hFind = FindFirstFile((std::string(directory) + "\\*").c_str(), &ffd);
HANDLE hFind = FindFirstFile((std::string(directory) + "\\*").c_str(), &ffd);
#endif
if (hFind == INVALID_HANDLE_VALUE) {
FindClose(hFind);
return 0;
}
// windows loop
do
{
const std::string virtualName(ffd.cFileName);
if (hFind == INVALID_HANDLE_VALUE) {
FindClose(hFind);
return 0;
}
// windows loop
do
{
const std::string virtualName(ffd.cFileName);
#else
struct dirent dirent, *result = NULL;
struct dirent dirent, *result = NULL;
DIR *dirp = opendir(directory);
if (!dirp)
return 0;
DIR *dirp = opendir(directory);
if (!dirp)
return 0;
// non windows loop
while (!readdir_r(dirp, &dirent, &result) && result)
{
const std::string virtualName(result->d_name);
// non windows loop
while (!readdir_r(dirp, &dirent, &result) && result)
{
const std::string virtualName(result->d_name);
#endif
// check for "." and ".."
if (((virtualName[0] == '.') && (virtualName[1] == '\0')) ||
((virtualName[0] == '.') && (virtualName[1] == '.') &&
(virtualName[2] == '\0')))
continue;
// check for "." and ".."
if (((virtualName[0] == '.') && (virtualName[1] == '\0')) ||
((virtualName[0] == '.') && (virtualName[1] == '.') &&
(virtualName[2] == '\0')))
continue;
// Remove dotfiles (should be made optional?)
if (virtualName[0] == '.')
continue;
FileInfo info;
info.name = virtualName;
info.fullName = std::string(directory) + "/" + virtualName;
info.isDirectory = isDirectory(info.fullName);
files->push_back(info);
FileInfo info;
info.name = virtualName;
info.fullName = std::string(directory) + "/" + virtualName;
info.isDirectory = isDirectory(info.fullName);
files->push_back(info);
#ifdef _WIN32
} while (FindNextFile(hFind, &ffd) != 0);
FindClose(hFind);
} while (FindNextFile(hFind, &ffd) != 0);
FindClose(hFind);
#else
}
closedir(dirp);
}
closedir(dirp);
#endif
return foundEntries;
return foundEntries;
}
void deleteFile(const char *file)
{
#ifdef _WIN32
if (!DeleteFile(file)) {
ELOG("Error deleting %s: %i", file, GetLastError());
}
if (!DeleteFile(file)) {
ELOG("Error deleting %s: %i", file, GetLastError());
}
#else
int err = unlink(file);
if (err) {
ELOG("Error unlinking %s: %i", file, err);
}
int err = unlink(file);
if (err) {
ELOG("Error unlinking %s: %i", file, err);
}
#endif
}
#endif
@ -187,18 +187,18 @@ std::string getDir(const std::string &path)
{
if (path == "/")
return path;
int n = path.size() - 1;
while (n >= 0 && path[n] != '\\' && path[n] != '/')
n--;
std::string cutpath = path.substr(0, n);
for (size_t i = 0; i < cutpath.size(); i++)
{
if (cutpath[i] == '\\') cutpath[i] = '/';
}
int n = path.size() - 1;
while (n >= 0 && path[n] != '\\' && path[n] != '/')
n--;
std::string cutpath = path.substr(0, n);
for (size_t i = 0; i < cutpath.size(); i++)
{
if (cutpath[i] == '\\') cutpath[i] = '/';
}
#ifndef _WIN32
if (!cutpath.size()) {
return "/";
}
#endif
return cutpath;
return cutpath;
}

View File

@ -11,9 +11,9 @@ bool readFileToString(bool text_file, const char *filename, std::string &str);
struct FileInfo
{
std::string name;
std::string name;
std::string fullName;
bool isDirectory;
bool isDirectory;
};
size_t getFilesInDir(const char *directory, std::vector<FileInfo> *files);

View File

@ -10,128 +10,128 @@
#ifndef _WIN32
uint8_t *ReadFromZip(zip *archive, const char* filename, size_t *size) {
// Figure out the file size first.
struct zip_stat zstat;
zip_stat(archive, filename, ZIP_FL_NOCASE, &zstat);
// Figure out the file size first.
struct zip_stat zstat;
zip_stat(archive, filename, ZIP_FL_NOCASE, &zstat);
uint8_t *contents = new uint8_t[zstat.size + 1];
uint8_t *contents = new uint8_t[zstat.size + 1];
zip_file *file = zip_fopen(archive, filename, 0);
if (!file) {
ELOG("Error opening %s from ZIP", filename);
delete [] contents;
return 0;
}
zip_fread(file, contents, zstat.size);
zip_fclose(file);
contents[zstat.size] = 0;
zip_file *file = zip_fopen(archive, filename, 0);
if (!file) {
ELOG("Error opening %s from ZIP", filename);
delete [] contents;
return 0;
}
zip_fread(file, contents, zstat.size);
zip_fclose(file);
contents[zstat.size] = 0;
*size = zstat.size;
return contents;
*size = zstat.size;
return contents;
}
#endif
// The return is non-const because - why not?
uint8_t *ReadLocalFile(const char *filename, size_t *size) {
FILE *file = fopen(filename, "rb");
if (!file) {
return 0;
}
fseek(file, 0, SEEK_END);
size_t f_size = ftell(file);
fseek(file, 0, SEEK_SET);
uint8_t *contents = new uint8_t[f_size+1];
fread(contents, 1, f_size, file);
fclose(file);
contents[f_size] = 0;
*size = f_size;
return contents;
FILE *file = fopen(filename, "rb");
if (!file) {
return 0;
}
fseek(file, 0, SEEK_END);
size_t f_size = ftell(file);
fseek(file, 0, SEEK_SET);
uint8_t *contents = new uint8_t[f_size+1];
fread(contents, 1, f_size, file);
fclose(file);
contents[f_size] = 0;
*size = f_size;
return contents;
}
#ifndef _WIN32
ZipAssetReader::ZipAssetReader(const char *zip_file, const char *in_zip_path) {
zip_file_ = zip_open(zip_file, 0, NULL);
strcpy(in_zip_path_, in_zip_path);
if (!zip_file_) {
ELOG("Failed to open %s as a zip file", zip_file);
}
// This is not really necessary.
int numFiles = zip_get_num_files(zip_file_);
for (int i = 0; i < numFiles; i++) {
const char* name = zip_get_name(zip_file_, i, 0);
if (name == NULL) {
ELOG("Error reading zip file name at index %i : %s", i, zip_strerror(zip_file_));
return;
}
// ILOG("File %i : %s\n", i, name);
}
zip_file_ = zip_open(zip_file, 0, NULL);
strcpy(in_zip_path_, in_zip_path);
if (!zip_file_) {
ELOG("Failed to open %s as a zip file", zip_file);
}
// This is not really necessary.
int numFiles = zip_get_num_files(zip_file_);
for (int i = 0; i < numFiles; i++) {
const char* name = zip_get_name(zip_file_, i, 0);
if (name == NULL) {
ELOG("Error reading zip file name at index %i : %s", i, zip_strerror(zip_file_));
return;
}
// ILOG("File %i : %s\n", i, name);
}
}
ZipAssetReader::~ZipAssetReader() {
zip_close(zip_file_);
zip_close(zip_file_);
}
uint8_t *ZipAssetReader::ReadAsset(const char *path, size_t *size) {
char temp_path[256];
strcpy(temp_path, in_zip_path_);
strcat(temp_path, path);
return ReadFromZip(zip_file_, temp_path, size);
char temp_path[256];
strcpy(temp_path, in_zip_path_);
strcat(temp_path, path);
return ReadFromZip(zip_file_, temp_path, size);
}
#endif
uint8_t *DirectoryAssetReader::ReadAsset(const char *path, size_t *size) {
char new_path[256] = {0};
// Check if it already contains the path
if (strlen(path) > strlen(path_) && 0 == memcmp(path, path_, strlen(path_))) {
}
else {
strcpy(new_path, path_);
}
strcat(new_path, path);
// ILOG("New path: %s", new_path);
return ReadLocalFile(new_path, size);
char new_path[256] = {0};
// Check if it already contains the path
if (strlen(path) > strlen(path_) && 0 == memcmp(path, path_, strlen(path_))) {
}
else {
strcpy(new_path, path_);
}
strcat(new_path, path);
// ILOG("New path: %s", new_path);
return ReadLocalFile(new_path, size);
}
struct VFSEntry {
const char *prefix;
AssetReader *reader;
const char *prefix;
AssetReader *reader;
};
static VFSEntry entries[16];
static int num_entries = 0;
void VFSRegister(const char *prefix, AssetReader *reader) {
entries[num_entries].prefix = prefix;
entries[num_entries].reader = reader;
ILOG("Registered VFS for prefix %s: %s", prefix, reader->toString().c_str());
num_entries++;
entries[num_entries].prefix = prefix;
entries[num_entries].reader = reader;
ILOG("Registered VFS for prefix %s: %s", prefix, reader->toString().c_str());
num_entries++;
}
void VFSShutdown() {
for (int i = 0; i < num_entries; i++) {
delete entries[i].reader;
}
num_entries = 0;
for (int i = 0; i < num_entries; i++) {
delete entries[i].reader;
}
num_entries = 0;
}
uint8_t *VFSReadFile(const char *filename, size_t *size) {
int fn_len = strlen(filename);
for (int i = 0; i < num_entries; i++) {
int prefix_len = strlen(entries[i].prefix);
if (prefix_len >= fn_len) continue;
if (0 == memcmp(filename, entries[i].prefix, prefix_len)) {
// ILOG("Prefix match: %s (%s) -> %s", entries[i].prefix, filename, filename + prefix_len);
uint8_t *data = entries[i].reader->ReadAsset(filename + prefix_len, size);
if (data)
return data;
else
continue;
// Else try the other registered file systems.
}
}
ELOG("Missing filesystem for %s", filename);
return 0;
int fn_len = strlen(filename);
for (int i = 0; i < num_entries; i++) {
int prefix_len = strlen(entries[i].prefix);
if (prefix_len >= fn_len) continue;
if (0 == memcmp(filename, entries[i].prefix, prefix_len)) {
// ILOG("Prefix match: %s (%s) -> %s", entries[i].prefix, filename, filename + prefix_len);
uint8_t *data = entries[i].reader->ReadAsset(filename + prefix_len, size);
if (data)
return data;
else
continue;
// Else try the other registered file systems.
}
}
ELOG("Missing filesystem for %s", filename);
return 0;
}

View File

@ -15,42 +15,42 @@ uint8_t *ReadLocalFile(const char *filename, size_t *size);
class AssetReader {
public:
virtual ~AssetReader() {}
// use delete[]
virtual uint8_t *ReadAsset(const char *path, size_t *size) = 0;
virtual std::string toString() const = 0;
virtual ~AssetReader() {}
// use delete[]
virtual uint8_t *ReadAsset(const char *path, size_t *size) = 0;
virtual std::string toString() const = 0;
};
#ifndef _WIN32
uint8_t *ReadFromZip(zip *archive, const char* filename, size_t *size);
class ZipAssetReader : public AssetReader {
public:
ZipAssetReader(const char *zip_file, const char *in_zip_path);
~ZipAssetReader();
// use delete[]
virtual uint8_t *ReadAsset(const char *path, size_t *size);
virtual std::string toString() const {
return in_zip_path_;
}
ZipAssetReader(const char *zip_file, const char *in_zip_path);
~ZipAssetReader();
// use delete[]
virtual uint8_t *ReadAsset(const char *path, size_t *size);
virtual std::string toString() const {
return in_zip_path_;
}
private:
zip *zip_file_;
char in_zip_path_[256];
zip *zip_file_;
char in_zip_path_[256];
};
#endif
class DirectoryAssetReader : public AssetReader {
public:
DirectoryAssetReader(const char *path) {
strcpy(path_, path);
}
// use delete[]
virtual uint8_t *ReadAsset(const char *path, size_t *size);
virtual std::string toString() const {
return path_;
}
DirectoryAssetReader(const char *path) {
strcpy(path_, path);
}
// use delete[]
virtual uint8_t *ReadAsset(const char *path, size_t *size);
virtual std::string toString() const {
return path_;
}
private:
char path_[512];
char path_[512];
};