Permalink
Browse files

Reformat chunk_file. Don't usually reformat whole files, but this was…

… too ugly to work on :P
  • Loading branch information...
hrydgard committed Dec 4, 2017
1 parent ca6865b commit 14a263d5b629ce39c1a924cc4acd91c2d6d585df
Showing with 123 additions and 143 deletions.
  1. +108 −125 ext/native/file/chunk_file.cpp
  2. +15 −18 ext/native/file/chunk_file.h
@@ -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);
}
ChunkFile::ChunkFile(const char *filename, bool readMode) {
data_ = 0;
filename_ = filename;
depth_ = 0;
readMode_ = readMode;
pos_ = 0;
didFail_ = false;
fastMode = read ? true : 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<eof) {
pos += 4;
if (data_ && pos_ < eof_) {
pos_ += 4;
if (fastMode)
return *(int *)(data + pos - 4);
return *(int *)(data_ + pos_ - 4);
else {
int i;
if (fread(&i, 1, 4, file) == 4) {
@@ -72,66 +76,65 @@ int ChunkFile::readInt() {
void ChunkFile::writeInt(int i) {
fwrite(&i, 1, 4, file);
pos += 4;
pos_ += 4;
}
//let's get into the business
// let's get into the business
bool ChunkFile::descend(uint32_t id) {
if (numLevels > 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<eof) {
stack[numLevels].ID = readInt();
if (firstID == 0) firstID=stack[numLevels].ID|1;
stack[numLevels].length = readInt();
stack[numLevels].startLocation = pos;
// let's search through children..
while (pos_ < eof_) {
stack[depth_].ID = readInt();
if (firstID == 0) firstID = stack[depth_].ID | 1;
stack[depth_].length = readInt();
stack[depth_].startLocation = pos_;
if (stack[numLevels].ID == id)
{
if (stack[depth_].ID == id) {
found = true;
break;
} else {
seekTo(pos + stack[numLevels].length); //try next block
seekTo(pos_ + stack[depth_].length); // try next block
}
}
}
//if we found nothing, return false so the caller can skip this
// 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);
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;
}
Oops, something went wrong.

0 comments on commit 14a263d

Please sign in to comment.