diff --git a/ext/native/file/chunk_file.cpp b/ext/native/file/chunk_file.cpp index 0544417a33..0c3f2fb5c2 100644 --- a/ext/native/file/chunk_file.cpp +++ b/ext/native/file/chunk_file.cpp @@ -3,63 +3,67 @@ #include "file/zip_read.h" #include "file/file_util.h" -//#define CHUNKDEBUG +// #define CHUNKDEBUG -ChunkFile::ChunkFile(const char *filename, bool _read) { - data=0; - fn = filename; - numLevels=0; - read=_read; - pos=0; - didFail=false; +inline uint32_t flipID(uint32_t id) { + return ((id >> 24) & 0xFF) | ((id >> 8) & 0xFF00) | ((id << 8) & 0xFF0000) | ((id << 24) & 0xFF000000); +} - fastMode = read ? true : false; +ChunkFile::ChunkFile(const char *filename, bool readMode) { + data_ = 0; + filename_ = filename; + depth_ = 0; + readMode_ = readMode; + pos_ = 0; + didFail_ = false; + + fastMode = readMode_ ? true : false; if (fastMode) { size_t size; - data = (uint8_t *)VFSReadFile(filename, &size); - if (!data) { + data_ = (uint8_t *)VFSReadFile(filename, &size); + if (!data_) { ELOG("Chunkfile fail: %s", filename); - didFail = true; + didFail_ = true; return; } - eof = (int)size; + eof_ = (int)size; return; } file = openCFile(filename, "wb"); if (file) { - didFail = false; - eof = 0; - } else { - didFail = true; + didFail_ = false; + eof_ = 0; + } else { + didFail_ = true; } } -ChunkFile::ChunkFile(const uint8_t *read_data, int data_size) { - data = new uint8_t[data_size]; - memcpy(data, read_data, data_size); +ChunkFile::ChunkFile(const uint8_t *data, int dataSize) { + data_ = new uint8_t[dataSize]; + memcpy(data_, data, dataSize); fastMode = true; - numLevels = 0; - read = true; - pos = 0; - didFail = false; - eof = data_size; + depth_ = 0; + readMode_ = true; + pos_ = 0; + didFail_ = false; + eof_ = dataSize; } ChunkFile::~ChunkFile() { if (fastMode) { - delete[] data; + delete[] data_; } else { fclose(file); } } int ChunkFile::readInt() { - if (data && pos 30) + if (depth_ > 30) return false; id = flipID(id); - if (read) { + if (readMode_) { 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[depth_].parentStartLocation = pos_; + stack[depth_].parentEOF = eof_; - ChunkInfo temp = stack[numLevels]; + ChunkInfo temp = stack[depth_]; int firstID = 0; - //let's search through children.. - while(pos>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); + stack[depth_] = temp; + seekTo(stack[depth_].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[depth_].startLocation + stack[depth_].length; + depth_++; #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 +#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++; + writeInt(0); // will be filled in by Ascend + stack[depth_].startLocation = pos_; + depth_++; return true; #else return true; @@ -143,97 +146,77 @@ void ChunkFile::seekTo(int _pos) { if (!fastMode) { fseek(file, 0, SEEK_SET); } - pos = _pos; + pos_ = _pos; } -//let's ascend out +// let's ascend out void ChunkFile::ascend() { - if (read) { - //ascend, and restore information - numLevels--; - seekTo(stack[numLevels].parentStartLocation); - eof = stack[numLevels].parentEOF; + if (readMode_) { + // ascend, and restore information + depth_--; + seekTo(stack[depth_].parentStartLocation); + eof_ = stack[depth_].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[depth_].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); + } else { + depth_--; + // now fill in the written length automatically + int posNow = pos_; + seekTo(stack[depth_].startLocation - 4); + writeInt(posNow - stack[depth_].startLocation); seekTo(posNow); } } -//read a block +// read a block void ChunkFile::readData(void *what, int count) { if (fastMode) { - memcpy(what, data + pos, count); + memcpy(what, data_ + pos_, count); } else { if (fread(what, 1, count, file) != (size_t)count) { ELOG("Failed to read complete %d bytes", count); } } - pos+=count; + pos_ += count; count &= 3; if (count) { - count=4-count; + count = 4 - count; if (!fastMode) { if (fseek(file, count, SEEK_CUR) != 0) { ELOG("Missing padding"); } } - pos+=count; + pos_ += count; } } -//write a block +// write a block void ChunkFile::writeData(const void *what, int count) { fwrite(what, 1, count, file); - pos+=count; - char temp[5]={0,0,0,0,0}; - count &= 3; - if (count) - { - count=4-count; + pos_ += count; + char temp[5] = { 0,0,0,0,0 }; + count &= 3; + if (count) { + count = 4 - count; fwrite(temp, 1, count, file); - pos+=count; + pos_ += count; } } -/* -void ChunkFile::writeWString(String str) { - wchar_t *text; - int len=str.length(); -#ifdef UNICODE -#error - text = str.getPointer(); -#else - text = new wchar_t[len+1]; - str.toUnicode(text); -#endif - writeInt(len); - writeData((char *)text, len * sizeof(wchar_t)); -#ifndef UNICODE - delete [] text; -#endif -} -*/ - // Takes utf-8 void ChunkFile::writeWString(const std::string &str) { unsigned short *text; int len = (int)str.length(); - text = new unsigned short[len+1]; + text = new unsigned short[len + 1]; for (int i = 0; i < len; i++) text[i] = str[i]; - text[len]=0; + text[len] = 0; writeInt(len); writeData((char *)text, len * sizeof(unsigned short)); - delete [] text; + delete[] text; } static void toUnicode(const std::string &str, uint16_t *t) { @@ -253,37 +236,37 @@ static std::string fromUnicode(const uint16_t *src, int len) { } std::string ChunkFile::readWString() { - int len=readInt(); - uint16_t *text = new uint16_t[len+1]; - readData((char *)text, len*sizeof(uint16_t)); + 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; + delete[] text; return temp; } void ChunkFile::writeString(const std::string &str) { uint16_t *text; int len = (int)str.size(); - text=new uint16_t[len+1]; + text = new uint16_t[len + 1]; toUnicode(str, text); writeInt(len); - writeData((char *)text,len*sizeof(uint16_t)); - delete [] text; + 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; + 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; + delete[] text; return temp; } int ChunkFile::getCurrentChunkSize() { - if (numLevels) - return stack[numLevels-1].length; + if (depth_) + return stack[depth_ - 1].length; else return 0; } diff --git a/ext/native/file/chunk_file.h b/ext/native/file/chunk_file.h index f1e9d8f3ee..ae46f2b6b9 100644 --- a/ext/native/file/chunk_file.h +++ b/ext/native/file/chunk_file.h @@ -10,18 +10,14 @@ // otherwise the scheme breaks. #include -#include +#include #include "base/basictypes.h" -inline uint32_t flipID(uint32_t id) { - return ((id>>24)&0xFF) | ((id>>8)&0xFF00) | ((id<<8)&0xFF0000) | ((id<<24)&0xFF000000); -} - class ChunkFile { public: - ChunkFile(const char *filename, bool _read); - ChunkFile(const uint8_t *read_data, int data_size); + ChunkFile(const char *filename, bool readMode); + ChunkFile(const uint8_t *data, int dataSize); ~ChunkFile(); @@ -43,12 +39,10 @@ public: void writeData(const void *data, int count); int getCurrentChunkSize(); - bool failed() const { return didFail; } - std::string filename() const { return fn; } + bool failed() const { return didFail_; } + std::string filename() const { return filename_; } private: - std::string fn; - FILE *file; struct ChunkInfo { int startLocation; int parentStartLocation; @@ -57,15 +51,18 @@ private: int length; }; ChunkInfo stack[32]; - int numLevels; + int depth_ = 0; - uint8_t *data; - int pos; - int eof; + uint8_t *data_; + int pos_ = 0; + int eof_ = 0; bool fastMode; - bool read; - bool didFail; + bool readMode_; + bool didFail_ = false; + + std::string filename_; + FILE *file = nullptr; void seekTo(int _pos); - int getPos() const {return pos;} + int getPos() const {return pos_;} };