[Change] If ldata isn't found, try to be a bit more robust in finding it

yah?!
This commit is contained in:
Allanis 2014-03-08 01:13:34 +00:00
parent b73a332952
commit d6e2018e4b
8 changed files with 60 additions and 24 deletions

View File

@ -13,6 +13,7 @@
#include "input.h" #include "input.h"
#include "music.h" #include "music.h"
#include "nebulae.h" #include "nebulae.h"
#include "pack.h"
#include "lfile.h" #include "lfile.h"
#include "conf.h" #include "conf.h"
@ -80,17 +81,36 @@ static void print_usage(char** argv) {
LOG(" -v - Print the version and exit"); LOG(" -v - Print the version and exit");
} }
char dataVersion[PATH_MAX];
/** /**
* @brief Set the default configuration. * @brief Set the default configuration.
*/ */
void conf_setDefaults(void) { void conf_setDefaults(void) {
/* Global. */ int i, nfiles;
char** files;
size_t len;
/* Find data. */
if(lfile_fileExists("%s-%d.%d.%d", DATA_NAME, VMAJOR, VMINOR, VREV)) { if(lfile_fileExists("%s-%d.%d.%d", DATA_NAME, VMAJOR, VMINOR, VREV)) {
snprintf(dataVersion, 128, "%s-%d.%d.%d", DATA_NAME, VMAJOR, VMINOR, VREV); data = malloc(PATH_MAX);
data = dataVersion; snprintf(data, PATH_MAX, "%s-%d.%d.%d", DATA_NAME, VMAJOR, VMINOR, VREV);
} else } else if(lfile_fileExists(DATA_DEF))
data = DATA_DEF; data = DATA_DEF;
else {
files = lfile_readDir(&nfiles, ".");
len = strlen(DATA_NAME);
for(i = 0; i < nfiles; i++) {
if(strncmp(files[i], DATA_NAME, len) == 0) {
/* Must be packfile. */
if(pack_check(files[i]))
continue;
data = strdup(files[i]);
}
}
for(i = 0; i < nfiles; i++)
free(files[i]);
free(files);
}
/* GL. */ /* GL. */
gl_screen.w = 800; gl_screen.w = 800;
gl_screen.h = 600; gl_screen.h = 600;

View File

@ -132,7 +132,7 @@ int main(int argc, char** argv) {
debug_sigInit(); debug_sigInit();
/* Create the home directory if needed. */ /* Create the home directory if needed. */
if(lfile_dirMakeExist(".")) if(lfile_dirMakeExist("%s", lfile_basePath()))
WARN("Unable to create lephisto directory '%s'", lfile_basePath()); WARN("Unable to create lephisto directory '%s'", lfile_basePath());
/* Input must be initialized for config to work. */ /* Input must be initialized for config to work. */

View File

@ -60,18 +60,23 @@ char* lfile_basePath(void) {
* @param path Path to create directory if it doesn't exist. * @param path Path to create directory if it doesn't exist.
* @return 0 on success. * @return 0 on success.
*/ */
int lfile_dirMakeExist(const char* path) { int lfile_dirMakeExist(const char* path, ...) {
char file[PATH_MAX]; char file[PATH_MAX];
va_list ap;
size_t l;
l = 0;
if(path == NULL) return -1;
else { /* Get the message. */
va_start(ap, path);
vsnprintf(file, PATH_MAX-l, path, ap);
l = strlen(file);
va_end(ap);
}
#ifdef LINUX #ifdef LINUX
struct stat buf; struct stat buf;
if(strcmp(path, ".")==0) {
strncpy(file, lfile_basePath(), PATH_MAX);
file[PATH_MAX-1] = '\0';
}
else
snprintf(file, PATH_MAX, "%s%s", lfile_basePath(), path);
stat(file, &buf); stat(file, &buf);
if(!S_ISDIR(buf.st_mode)) if(!S_ISDIR(buf.st_mode))
if(mkdir(file, S_IRWXU | S_IRWXG | S_IRWXO) < 0) { if(mkdir(file, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
@ -127,11 +132,22 @@ int lfile_fileExists(const char* path, ...) {
* @param[out] lfiles Returns how many files there are. * @param[out] lfiles Returns how many files there are.
* @param path Directory to read. * @param path Directory to read.
*/ */
char** lfile_readDir(int* lfiles, const char* path) { char** lfile_readDir(int* lfiles, const char* path, ...) {
char file[PATH_MAX]; char file[PATH_MAX], base[PATH_MAX];
char** files; char** files;
va_list ap;
size_t l;
snprintf(file, PATH_MAX, "%s%s", lfile_basePath(), path); l = 0;
if(path == NULL) {
*lfiles = 0;
return NULL;
} else { /* Get the message. */
va_start(ap, path);
vsnprintf(base, PATH_MAX-l, path, ap);
l = strlen(base);
va_end(ap);
}
#ifdef LINUX #ifdef LINUX
int i, j, k, n; int i, j, k, n;
@ -148,7 +164,7 @@ char** lfile_readDir(int* lfiles, const char* path) {
tfiles = malloc(sizeof(char*)*mfiles); tfiles = malloc(sizeof(char*)*mfiles);
tt = malloc(sizeof(time_t)*mfiles); tt = malloc(sizeof(time_t)*mfiles);
d = opendir(file); d = opendir(base);
if(d == NULL) if(d == NULL)
return NULL; return NULL;
@ -161,7 +177,7 @@ char** lfile_readDir(int* lfiles, const char* path) {
continue; continue;
/* Stat the file. */ /* Stat the file. */
snprintf(file, PATH_MAX, "%s%s/%s", lfile_basePath(), path, name); snprintf(file, PATH_MAX, "%s/%s", base, name);
if(stat(file, &sb) == -1) if(stat(file, &sb) == -1)
continue; /* Unable to stat. */ continue; /* Unable to stat. */

View File

@ -3,10 +3,10 @@
char* lfile_basePath(void); char* lfile_basePath(void);
/* Create if doesn't exist, 0 success. */ /* Create if doesn't exist, 0 success. */
int lfile_dirMakeExist(const char* path); int lfile_dirMakeExist(const char* path, ...);
/* Return 1 on exit. */ /* Return 1 on exit. */
int lfile_fileExists(const char* path, ...); int lfile_fileExists(const char* path, ...);
char** lfile_readDir(int* lfiles, const char* path); char** lfile_readDir(int* lfiles, const char* path, ...);

View File

@ -493,7 +493,7 @@ static int nebu_generate(void) {
/* Generate all the nebulae backgrounds. */ /* Generate all the nebulae backgrounds. */
nebu = noise_genNebulaeMap(w, h, NEBULAE_Z, 5.); nebu = noise_genNebulaeMap(w, h, NEBULAE_Z, 5.);
lfile_dirMakeExist("gen"); lfile_dirMakeExist("%sgen", lfile_basePath());
/* Save each nebulae as an image. */ /* Save each nebulae as an image. */
for(i = 0; i < NEBULAE_Z; i++) { for(i = 0; i < NEBULAE_Z; i++) {

View File

@ -79,7 +79,7 @@ static off_t getfilesize(const char* filename) {
* *
* @brief Check to see if a file is a packfile * @brief Check to see if a file is a packfile
* @param filename Name of the file to check. * @param filename Name of the file to check.
* @return 1 if it is a packfile, 0 if it isn't and -1 on error. * @return 0 if it is a packfile, 1 if it isn't and -1 on error.
*/ */
int pack_check(const char* filename) { int pack_check(const char* filename) {
int ret; int ret;

View File

@ -2042,7 +2042,7 @@ void player_screenshot(void) {
int done; int done;
char filename[PATH_MAX]; char filename[PATH_MAX];
if(lfile_dirMakeExist("screenshots")) { if(lfile_dirMakeExist("%sscreenshots", lfile_basePath())) {
WARN("Aborting screenshots"); WARN("Aborting screenshots");
return; return;
} }

View File

@ -111,7 +111,7 @@ void load_game_menu(void) {
wid = window_create("Load Game", -1, -1, LOAD_WIDTH, LOAD_HEIGHT); wid = window_create("Load Game", -1, -1, LOAD_WIDTH, LOAD_HEIGHT);
/* Load the saves. */ /* Load the saves. */
files = lfile_readDir(&lfiles, "saves"); files = lfile_readDir(&lfiles, "%ssaves", lfile_basePath());
for(i = 0; i < lfiles; i++) { for(i = 0; i < lfiles; i++) {
len = strlen(files[i]); len = strlen(files[i]);