*** empty log message ***
git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@1177 e016b1c5-ed68-44aa-a2f7-31cac88cffde
This commit is contained in:
+114
-9
@@ -2,9 +2,16 @@
|
||||
|
||||
#include "cube.h"
|
||||
|
||||
//////////////////////////// pool ///////////////////////////
|
||||
///////////////////////// file system ///////////////////////
|
||||
|
||||
///////////////////////// misc tools ///////////////////////
|
||||
#ifndef WIN32
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#endif
|
||||
|
||||
string homedir = "";
|
||||
vector<char *> packagedirs;
|
||||
|
||||
char *path(char *s)
|
||||
{
|
||||
@@ -29,26 +36,122 @@ char *path(char *s)
|
||||
return s;
|
||||
}
|
||||
|
||||
char *parentdir(char *directory)
|
||||
const char *parentdir(const char *directory)
|
||||
{
|
||||
char *p = strrchr(directory, '/');
|
||||
const char *p = strrchr(directory, '/');
|
||||
if(!p) p = strrchr(directory, '\\');
|
||||
if(!p) p = directory;
|
||||
static string parent;
|
||||
size_t len = p-directory+1;
|
||||
char *parent = new char[len];
|
||||
s_strncpy(parent, directory, len);
|
||||
return parent;
|
||||
}
|
||||
|
||||
char *loadfile(char *fn, int *size)
|
||||
bool fileexists(const char *path, const char *mode)
|
||||
{
|
||||
FILE *f = fopen(fn, "rb");
|
||||
bool exists = true;
|
||||
if(mode[0]=='w' || mode[0]=='a') path = parentdir(path);
|
||||
#ifdef WIN32
|
||||
if(GetFileAttributes(path) == INVALID_FILE_ATTRIBUTES) exists = false;
|
||||
#else
|
||||
if(access(path, R_OK | (mode[0]=='w' || mode[0]=='a' ? W_OK : 0)) == -1) exists = false;
|
||||
#endif
|
||||
return exists;
|
||||
}
|
||||
|
||||
bool createdir(const char *path)
|
||||
{
|
||||
int len = strlen(path);
|
||||
if(path[len-1]==PATHDIV)
|
||||
{
|
||||
static string strip;
|
||||
path = s_strncpy(strip, path, len);
|
||||
}
|
||||
#ifdef WIN32
|
||||
return CreateDirectory(path, NULL)!=0;
|
||||
#else
|
||||
return mkdir(path, 0777)==0;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void fixdir(char *dir)
|
||||
{
|
||||
path(dir);
|
||||
int len = strlen(dir);
|
||||
if(dir[len-1]!=PATHDIV)
|
||||
{
|
||||
dir[len] = PATHDIV;
|
||||
dir[len+1] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
void sethomedir(const char *dir)
|
||||
{
|
||||
fixdir(s_strcpy(homedir, dir));
|
||||
}
|
||||
|
||||
void addpackagedir(const char *dir)
|
||||
{
|
||||
fixdir(packagedirs.add(newstringbuf(dir)));
|
||||
}
|
||||
|
||||
const char *findfile(const char *filename, const char *mode)
|
||||
{
|
||||
static string s;
|
||||
if(homedir[0])
|
||||
{
|
||||
s_sprintf(s)("%s%s", homedir, filename);
|
||||
if(fileexists(s, mode)) return s;
|
||||
if(mode[0]=='w' || mode[0]=='a')
|
||||
{
|
||||
string dirs;
|
||||
s_strcpy(dirs, s);
|
||||
char *dir = strchr(dirs[0]==PATHDIV ? dirs+1 : dirs, PATHDIV);
|
||||
while(dir)
|
||||
{
|
||||
*dir = '\0';
|
||||
if(!fileexists(dirs, "r") && !createdir(dirs)) return s;
|
||||
*dir = PATHDIV;
|
||||
dir = strchr(dir+1, PATHDIV);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
}
|
||||
if(mode[0]=='w' || mode[0]=='a') return filename;
|
||||
loopv(packagedirs)
|
||||
{
|
||||
s_sprintf(s)("%s%s", packagedirs[i], filename);
|
||||
if(fileexists(s, mode)) return s;
|
||||
}
|
||||
return filename;
|
||||
}
|
||||
|
||||
FILE *openfile(const char *filename, const char *mode)
|
||||
{
|
||||
const char *found = findfile(filename, mode);
|
||||
if(!found) return NULL;
|
||||
return fopen(found, mode);
|
||||
}
|
||||
|
||||
#ifndef STANDALONE
|
||||
gzFile opengzfile(const char *filename, const char *mode)
|
||||
{
|
||||
const char *found = findfile(filename, mode);
|
||||
if(!found) return NULL;
|
||||
return gzopen(found, mode);
|
||||
}
|
||||
#endif
|
||||
|
||||
char *loadfile(const char *fn, int *size)
|
||||
{
|
||||
FILE *f = openfile(fn, "rb");
|
||||
if(!f) return NULL;
|
||||
fseek(f, 0, SEEK_END);
|
||||
int len = ftell(f);
|
||||
if(len<=0) { fclose(f); return NULL; };
|
||||
if(len<=0) { fclose(f); return NULL; }
|
||||
fseek(f, 0, SEEK_SET);
|
||||
char *buf = new char[len+1];
|
||||
if(!buf) { fclose(f); return NULL; };
|
||||
if(!buf) { fclose(f); return NULL; }
|
||||
buf[len] = 0;
|
||||
size_t rlen = fread(buf, 1, len, f);
|
||||
fclose(f);
|
||||
@@ -61,6 +164,8 @@ char *loadfile(char *fn, int *size)
|
||||
return buf;
|
||||
}
|
||||
|
||||
///////////////////////// misc tools ///////////////////////
|
||||
|
||||
bool cmpb(void *b, int n, enet_uint32 c)
|
||||
{
|
||||
ENetBuffer buf;
|
||||
|
||||
Reference in New Issue
Block a user