Formatting cleanup, modified a function to pass by const reference.

This commit is contained in:
wrenczes@gmail.com
2011-03-31 11:08:58 +00:00
parent 64d421b8b8
commit 8add655df7
2 changed files with 99 additions and 92 deletions

View File

@@ -129,7 +129,7 @@ private:
string mZipFileName; string mZipFileName;
char *mPassword; char *mPassword;
bool mZipAvailable; bool mZipAvailable;
void preloadZip(string filename); void preloadZip(const string& filename);
#if defined (WIN32) || defined (LINUX) || defined(IOS) #if defined (WIN32) || defined (LINUX) || defined(IOS)
FILE *mFile; FILE *mFile;
#else #else

View File

@@ -9,7 +9,7 @@
//------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------
#ifdef WIN32 #ifdef WIN32
#pragma warning(disable : 4786) #pragma warning(disable : 4786)
#endif #endif
#include "../include/JGE.h" #include "../include/JGE.h"
@@ -25,17 +25,21 @@
#include <string> #include <string>
JZipCache::JZipCache(){} JZipCache::JZipCache()
{}
JZipCache::~JZipCache(){ JZipCache::~JZipCache()
map<string,unz_file_pos *>::iterator it; {
for (it = dir.begin(); it != dir.end(); ++it){ map<string,unz_file_pos *>::iterator it;
delete(it->second); for (it = dir.begin(); it != dir.end(); ++it)
} {
dir.clear(); delete(it->second);
}
dir.clear();
} }
void JFileSystem::preloadZip(string filename){ void JFileSystem::preloadZip(const string& filename)
{
map<string,JZipCache *>::iterator it = mZipCache.find(filename); map<string,JZipCache *>::iterator it = mZipCache.find(filename);
if (it != mZipCache.end()) return; if (it != mZipCache.end()) return;
@@ -48,10 +52,12 @@ void JFileSystem::preloadZip(string filename){
if (!mZipAvailable || !mZipFile) return; if (!mZipAvailable || !mZipFile) return;
} }
int err = unzGoToFirstFile (mZipFile); int err = unzGoToFirstFile (mZipFile);
while (err == UNZ_OK){ while (err == UNZ_OK)
{
unz_file_pos* filePos = new unz_file_pos(); unz_file_pos* filePos = new unz_file_pos();
char filenameInzip[4096]; char filenameInzip[4096];
if (unzGetCurrentFileInfo(mZipFile, NULL, filenameInzip, sizeof(filenameInzip), NULL, 0, NULL, 0) == UNZ_OK){ if (unzGetCurrentFileInfo(mZipFile, NULL, filenameInzip, sizeof(filenameInzip), NULL, 0, NULL, 0) == UNZ_OK)
{
unzGetFilePos(mZipFile, filePos); unzGetFilePos(mZipFile, filePos);
string name = filenameInzip; string name = filenameInzip;
cache->dir[name] = filePos; cache->dir[name] = filePos;
@@ -64,97 +70,97 @@ JFileSystem* JFileSystem::mInstance = NULL;
JFileSystem* JFileSystem::GetInstance() JFileSystem* JFileSystem::GetInstance()
{ {
if (mInstance == NULL) if (mInstance == NULL)
{ {
mInstance = new JFileSystem(); mInstance = new JFileSystem();
} }
return mInstance; return mInstance;
} }
void JFileSystem::Destroy() void JFileSystem::Destroy()
{ {
if (mInstance) if (mInstance)
{ {
delete mInstance; delete mInstance;
mInstance = NULL; mInstance = NULL;
} }
} }
JFileSystem::JFileSystem() JFileSystem::JFileSystem()
{ {
mZipAvailable = false; mZipAvailable = false;
#if defined (WIN32) || defined (LINUX) || defined (IOS) #if defined (WIN32) || defined (LINUX) || defined (IOS)
mFile = NULL; mFile = NULL;
#else #else
mFile = -1; mFile = -1;
#endif #endif
mPassword = NULL; mPassword = NULL;
mZipFile = NULL; mZipFile = NULL;
mFileSize = 0; mFileSize = 0;
#ifdef RESPATH #ifdef RESPATH
SetResourceRoot(RESPATH"/"); SetResourceRoot(RESPATH"/");
#else #else
SetResourceRoot("Res/"); // default root folder SetResourceRoot("Res/"); // default root folder
#endif #endif
} }
JFileSystem::~JFileSystem() JFileSystem::~JFileSystem()
{ {
DetachZipFile(); DetachZipFile();
map<string,JZipCache *>::iterator it; map<string,JZipCache *>::iterator it;
for (it = mZipCache.begin(); it != mZipCache.end(); ++it){ for (it = mZipCache.begin(); it != mZipCache.end(); ++it){
delete(it->second); delete(it->second);
} }
mZipCache.clear(); mZipCache.clear();
} }
bool JFileSystem::AttachZipFile(const string &zipfile, char *password /* = NULL */) bool JFileSystem::AttachZipFile(const string &zipfile, char *password /* = NULL */)
{ {
if (mZipAvailable && mZipFile != NULL) if (mZipAvailable && mZipFile != NULL)
{ {
if (mZipFileName != zipfile) if (mZipFileName != zipfile)
DetachZipFile(); // close the previous zip file DetachZipFile(); // close the previous zip file
else else
return true; return true;
} }
mZipFileName = zipfile; mZipFileName = zipfile;
mPassword = password; mPassword = password;
mZipFile = unzOpen(mZipFileName.c_str()); mZipFile = unzOpen(mZipFileName.c_str());
if (mZipFile != NULL) if (mZipFile != NULL)
{ {
mZipAvailable = true; mZipAvailable = true;
return true; return true;
} }
return false; return false;
} }
void JFileSystem::DetachZipFile() void JFileSystem::DetachZipFile()
{ {
if (mZipAvailable && mZipFile != NULL) if (mZipAvailable && mZipFile != NULL)
{ {
int error = unzCloseCurrentFile(mZipFile); int error = unzCloseCurrentFile(mZipFile);
if (error < 0 ) if (error < 0 )
JLOG("error calling unzCloseCurrentFile"); JLOG("error calling unzCloseCurrentFile");
error = unzClose(mZipFile); error = unzClose(mZipFile);
if (error < 0) if (error < 0)
JLOG("Error calling unzClose"); JLOG("Error calling unzClose");
} }
mZipFile = NULL; mZipFile = NULL;
mZipAvailable = false; mZipAvailable = false;
} }
@@ -217,63 +223,64 @@ bool JFileSystem::OpenFile(const string &filename)
void JFileSystem::CloseFile() void JFileSystem::CloseFile()
{ {
if (mZipAvailable && mZipFile != NULL){ if (mZipAvailable && mZipFile != NULL)
unzCloseCurrentFile(mZipFile); {
return; unzCloseCurrentFile(mZipFile);
} return;
}
#if defined (WIN32) || defined (LINUX) || defined (IOS) #if defined (WIN32) || defined (LINUX) || defined (IOS)
if (mFile != NULL) if (mFile != NULL)
fclose(mFile); fclose(mFile);
#else #else
if (mFile > 0) if (mFile > 0)
sceIoClose(mFile); sceIoClose(mFile);
#endif #endif
} }
int JFileSystem::ReadFile(void *buffer, int size) int JFileSystem::ReadFile(void *buffer, int size)
{ {
if (mZipAvailable && mZipFile != NULL) if (mZipAvailable && mZipFile != NULL)
{ {
return unzReadCurrentFile(mZipFile, buffer, size); return unzReadCurrentFile(mZipFile, buffer, size);
} }
else else
{ {
#if defined (WIN32) || defined (LINUX) || defined (IOS) #if defined (WIN32) || defined (LINUX) || defined (IOS)
return fread(buffer, 1, size, mFile); return fread(buffer, 1, size, mFile);
#else #else
return sceIoRead(mFile, buffer, size); return sceIoRead(mFile, buffer, size);
#endif #endif
} }
} }
int JFileSystem::GetFileSize() int JFileSystem::GetFileSize()
{ {
return mFileSize; return mFileSize;
} }
void JFileSystem::SetResourceRoot(const string& resourceRoot) void JFileSystem::SetResourceRoot(const string& resourceRoot)
{ {
#ifdef IOS #ifdef IOS
NSString *pathUTF8 = [NSString stringWithUTF8String: resourceRoot.c_str()]; NSString *pathUTF8 = [NSString stringWithUTF8String: resourceRoot.c_str()];
NSString *fullpath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:pathUTF8]; NSString *fullpath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:pathUTF8];
mResourceRoot = [fullpath cStringUsingEncoding:1]; mResourceRoot = [fullpath cStringUsingEncoding:1];
mResourceRoot += "/"; mResourceRoot += "/";
#else #else
mResourceRoot = resourceRoot; mResourceRoot = resourceRoot;
#endif #endif
} }
string JFileSystem::GetResourceRoot() string JFileSystem::GetResourceRoot()
{ {
return mResourceRoot; return mResourceRoot;
} }
string JFileSystem::GetResourceFile(string filename) string JFileSystem::GetResourceFile(string filename)
{ {
string result = mResourceRoot; string result = mResourceRoot;
return result.append(filename); return result.append(filename);
} }