[Add] No longer need Makefile when working in 'trunk', reads from

directories directly.
[Change] Code clean up.
[Add] Documentation.
This commit is contained in:
Allanis 2014-05-18 19:54:36 +01:00
parent cbe4680de8
commit ea6db68433
9 changed files with 214 additions and 67 deletions

View File

@ -331,6 +331,8 @@ int ai_pinit(Pilot* p, char* ai) {
else buf[i] = '\0'; else buf[i] = '\0';
prof = ai_getProfile(buf); prof = ai_getProfile(buf);
if(prof == NULL)
WARN("AI Profile '%s' not found.", buf);
p->ai = prof; p->ai = prof;
L = p->ai->L; L = p->ai->L;
@ -375,24 +377,33 @@ void ai_destroy(Pilot* p) {
* @return 0 on no errors. * @return 0 on no errors.
*/ */
int ai_init(void) { int ai_init(void) {
const char** files; char** files;
uint32_t nfiles, i; uint32_t nfiles, i;
char path[PATH_MAX];
int flen, suflen;
/* Get the file list. */ /* Get the file list. */
files = ldata_list(AI_PREFIX, &nfiles); files = ldata_list(AI_PREFIX, &nfiles);
/* Load the profiles. */ /* Load the profiles. */
for(i = 0; i < nfiles; i++) suflen = strlen(AI_SUFFIX);
if((strncmp(files[i], AI_PREFIX, strlen(AI_PREFIX))==0) && /* Prefixed. */ for(i = 0; i < nfiles; i++) {
(strncmp(files[i] + strlen(AI_PREFIX), AI_INCLUDE, /* Not an include. */ flen = strlen(files[i]);
strlen(AI_INCLUDE)) != 0) && if(strncmp(&files[i][flen-suflen], AI_SUFFIX, suflen)==0) {
(strncmp(files[i] + strlen(files[i]) - strlen(AI_SUFFIX), /* Suffixed. */ snprintf(path, PATH_MAX, AI_PREFIX"%s", files[i]);
AI_SUFFIX, strlen(AI_SUFFIX))==0)) if(ai_loadProfile(path)) /* Load the profile. */
if(ai_loadProfile(files[i])) /* Load the profiles. */ WARN("Error loading AI profile '%s'", path);
WARN("Error loading AI profile '%s'", files[i]); }
/* Clean up. */
free(files[i]);
}
DEBUG("Loaded %d AI profile%c", nprofiles, (nprofiles==1)?' ':'s'); DEBUG("Loaded %d AI profile%c", nprofiles, (nprofiles==1)?' ':'s');
/* More clean up. */
free(files);
return 0; return 0;
} }

View File

@ -541,7 +541,8 @@ void gl_fontInit(glFont* font, const char* fname, const unsigned int h) {
/* Objects that freetype uses to store font info. */ /* Objects that freetype uses to store font info. */
if(FT_New_Memory_Face(library, buf, bufsize, 0, &face)) if(FT_New_Memory_Face(library, buf, bufsize, 0, &face))
WARN("FT_New_Memory_Face failed loading library from %s", fname); WARN("FT_New_Face failed loading library from %s",
(fname != NULL) ? fname : FONT_DEF);
/* Try to resize. */ /* Try to resize. */
if(FT_IS_SCALABLE(face)) { if(FT_IS_SCALABLE(face)) {

View File

@ -32,6 +32,9 @@ static char* ldata_packName = NULL; /**< Name of the ldata module. */
static const char** ldata_fileList = NULL; /**< List of the files in the packfile. */ static const char** ldata_fileList = NULL; /**< List of the files in the packfile. */
static uint32_t ldata_fileNList = 0; /**< Number of files in ldata_fileList. */ static uint32_t ldata_fileNList = 0; /**< Number of files in ldata_fileList. */
static char** filterList(const char** list, int nlist,
const char* path, uint32_t* nfiles);
/** /**
* @brief Check to see if path is a ldata file. * @brief Check to see if path is a ldata file.
* @param path Path to check to see if it's an ldata file. * @param path Path to check to see if it's an ldata file.
@ -44,6 +47,7 @@ int ldata_check(char* path) {
/** /**
* @brief Set the current ldata path to use. * @brief Set the current ldata path to use.
* @param path Path to set. * @param path Path to set.
* @return 0 on success.
*/ */
int ldata_setPath(char* path) { int ldata_setPath(char* path) {
if(ldata_filename != NULL) if(ldata_filename != NULL)
@ -54,6 +58,7 @@ int ldata_setPath(char* path) {
/** /**
* @brief Open a packfile if needed. * @brief Open a packfile if needed.
* @return 0 on success.
*/ */
static int ldata_openPackfile(void) { static int ldata_openPackfile(void) {
int i; int i;
@ -180,30 +185,102 @@ const char* ldata_name(void) {
/** /**
* @brief Read a file from the ldata. * @brief Read a file from the ldata.
* @param filename Name of the file to read.
* @param[out] Stores the size of the file.
* @return The file data or NULL on error.
*/ */
void* ldata_read(const char* filename, uint32_t* filesize) { void* ldata_read(const char* filename, uint32_t* filesize) {
if(ldata_cache == NULL) char* buf;
ldata_openPackfile(); int nbuf;
/* See if needs to load the packfile. */
if(ldata_cache == NULL) {
/* Try to read the file as locally. */
buf = lfile_readFile(&nbuf, filename);
if(buf != NULL) {
*filesize = nbuf;
return buf;
}
/* Load the packfile. */
ldata_openPackfile();
}
/* Get data from packfile. */
return pack_readfileCached(ldata_cache, filename, filesize); return pack_readfileCached(ldata_cache, filename, filesize);
} }
/** /**
* @brief Get the list of files in the ldata. * @brief Filter a file list to match path.
* @param list List to filter.
* @param nlist Members in list.
* @param path Path to filter.
* @param[out] nfiles Files that match.
*/ */
const char** ldata_list(const char* path, uint32_t* nfiles) { static char** filterList(const char** list, int nlist,
const char* path, uint32_t* nfiles) {
char** filtered;
int i, j, k;
int len;
/* Maximum size by default. */
filtered = malloc(sizeof(char*) * nlist);
len = strlen(path);
/* Filter list. */
j = 0;
for(i = 0; i < nlist; i++) {
/* Must match path. */
if(strncmp(list[i], path, len) != 0)
continue;
/* Make sure there are no stray '/'. */
for(k = len; list[i][k] != '\0'; k++)
if(list[i][k] != '/')
break;
if(list[i][k] != '\0')
continue;
/* Copy the file name without the path. */
filtered[j++] = strdup(&list[i][len]);
}
/* Return results. */
*nfiles = j;
return filtered;
}
/**
* @brief Get the list of files in the ldata.
* @param path List files in path.
* @param nfiles Number of files found.
* @return List of files found.
*/
char** ldata_list(const char* path, uint32_t* nfiles) {
(void)path; (void)path;
char** files;
int n;
if(ldata_fileList != NULL) { /* Already loaded the list. */
*nfiles = ldata_fileNList; if(ldata_fileList != NULL)
return ldata_fileList; return filterList(ldata_fileList, ldata_fileNList, path, nfiles);
/* See if we can load from local directory. */
if(ldata_cache == NULL) {
files = lfile_readDir(&n, path);
/* Found locally. */
if(files != NULL) {
*nfiles = n;
return files;
} }
if(ldata_cache == NULL) /* Open packfile. */
ldata_openPackfile(); ldata_openPackfile();
}
ldata_fileList = pack_listfilesCached(ldata_cache, &ldata_fileNList);
*nfiles = ldata_fileNList; /* Load list. */
return ldata_fileList; ldata_fileList = pack_listfilesCached(ldata_cache, &ldata_fileNList);
return filterList(ldata_fileList, ldata_fileNList, path, nfiles);
} }

View File

@ -12,5 +12,5 @@ const char* ldata_name(void);
/* Individual file functions. */ /* Individual file functions. */
void* ldata_read(const char* filename, uint32_t* filesize); void* ldata_read(const char* filename, uint32_t* filesize);
const char** ldata_list(const char* path, uint32_t* nfiles); char** ldata_list(const char* path, uint32_t* nfiles);

View File

@ -283,25 +283,13 @@ int main(int argc, char** argv) {
* @brief Display a loading screen. * @brief Display a loading screen.
*/ */
void loadscreen_load(void) { void loadscreen_load(void) {
int i; unsigned int i;
char file_path[PATH_MAX]; char file_path[PATH_MAX];
const char** loadscreens; char** loadscreens;
const char** files; uint32_t nload;
uint32_t nfiles;
size_t len;
int nload;
/* Count the loading screens. */ /* Count the loading screens. */
files = ldata_list("../gfx/loading/", &nfiles); loadscreens = ldata_list("../gfx/loading/", &nload);
len = strlen("../gfx/loading/");
nload = 0;
loadscreens = malloc(sizeof(char*) * nfiles);
for(i = 0; i < (int)nfiles; i++) {
if(strncmp(files[i], "../gfx/loading/", len)==0) {
loadscreens[nload] = files[i];
nload++;
}
}
/* Must have loading screens. */ /* Must have loading screens. */
if(nload == 0) { if(nload == 0) {
@ -310,10 +298,13 @@ void loadscreen_load(void) {
} }
/* Load the texture. */ /* Load the texture. */
strncpy(file_path, loadscreens[RNG(0, nload-1)], PATH_MAX); snprintf(file_path, PATH_MAX, "../gfx/loading/%s",
loadscreens[RNG_SANE(0, nload-1)]);
loading = gl_newImage(file_path, 0); loading = gl_newImage(file_path, 0);
/* Clean up. */ /* Clean up. */
for(i = 0; i < nload; i++)
free(loadscreens[i]);
free(loadscreens); free(loadscreens);
} }

View File

@ -22,6 +22,8 @@
#include "log.h" #include "log.h"
#include "lfile.h" #include "lfile.h"
#define BLOCK_SIZE 128*1024 /**< 128 kilobytes. */
static char lephisto_base[PATH_MAX] = "\0"; /**< Store Lephisto's base path. */ static char lephisto_base[PATH_MAX] = "\0"; /**< Store Lephisto's base path. */
/** /**
* @fn char* lfile_basePath(void) * @fn char* lfile_basePath(void)
@ -235,6 +237,59 @@ char** lfile_readDir(int* lfiles, const char* path, ...) {
return files; return files;
} }
/**
* @brief Try to read a file.
* @param filesize Stores the size of the file.
* @param path Path of the file.
* @return The file data.
*/
char* lfile_readFile(int* filesize, const char* path, ...) {
char base[PATH_MAX];
char* buf;
FILE* file;
int len, pos;
va_list ap;
if(path == NULL) {
*filesize = 0;
return NULL;
} else { /* Get the message. */
va_start(ap, path);
vsnprintf(base, PATH_MAX, path, ap);
va_end(ap);
}
/* Open file. */
file = fopen(base, "r");
if(file == NULL)
return NULL;
/* Get file size. */
len = fseek(file, 0, SEEK_END);
if(len == -1) {
fclose(file);
return NULL;
}
len = ftell(file);
fseek(file, 0, SEEK_SET);
/* Allocate buffer. */
buf = malloc(len);
if(buf == NULL) {
WARN("Out of memory!");
fclose(file);
return NULL;
}
/* Read the file. */
pos = fread(buf, len, 1, file);
if(pos != 1)
WARN("Error occurred while reading '%s'.", base);
*filesize = len;
return buf;
}
/** /**
* @brief Tries to create the file if it doesn't exist. * @brief Tries to create the file if it doesn't exist.
* @param path Path of the file to create. * @param path Path of the file to create.

View File

@ -1,14 +1,9 @@
#pragma once #pragma once
char* lfile_basePath(void); char* lfile_basePath(void);
/* Create if doesn't exist, 0 success. */
int lfile_dirMakeExist(const char* path, ...); int lfile_dirMakeExist(const char* path, ...);
/* 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, ...);
char* lfile_readFile(int* filesize, const char* path, ...);
int lfile_touch(const char* path, ...); int lfile_touch(const char* path, ...);

View File

@ -164,10 +164,10 @@ static void music_free(void) {
* @return 0 on success. * @return 0 on success.
*/ */
static int music_find(void) { static int music_find(void) {
const char** files; char** files;
uint32_t nfiles, i; uint32_t nfiles, i;
char tmp[64]; char tmp[64];
int len; int len, suflen, flen;
int mem; int mem;
if(music_disabled) return 0; if(music_disabled) return 0;
@ -177,11 +177,10 @@ static int music_find(void) {
/* Load the profiles. */ /* Load the profiles. */
mem = 0; mem = 0;
suflen = strlen(MUSIC_SUFFIX);
for(i = 0; i < nfiles; i++) { for(i = 0; i < nfiles; i++) {
if((strncmp(files[i], MUSIC_PREFIX, strlen(MUSIC_PREFIX))==0) && flen = strlen(files[i]);
(strncmp(files[i] + strlen(files[i]) - strlen(MUSIC_SUFFIX), if(strncmp(&files[i][flen - suflen], MUSIC_SUFFIX, suflen)==0) {
MUSIC_SUFFIX, strlen(MUSIC_SUFFIX))==0)) {
/* Grow the selection size. */ /* Grow the selection size. */
nmusic_selection++; nmusic_selection++;
if(nmusic_selection > mem) { if(nmusic_selection > mem) {
@ -190,17 +189,23 @@ static int music_find(void) {
} }
/* Remove the prefix and suffix. */ /* Remove the prefix and suffix. */
len = strlen(files[i]) - strlen(MUSIC_SUFFIX MUSIC_PREFIX); len = flen - suflen;
strncpy(tmp, files[i] + strlen(MUSIC_PREFIX), len); strncpy(tmp, files[i], len);
tmp[MIN(len, 64-1)] = '\0'; tmp[MIN(len, 64-1)] = '\0';
music_selection[nmusic_selection-1] = strdup(tmp); music_selection[nmusic_selection-1] = strdup(tmp);
} }
/* Clean up. */
free(files[i]);
} }
music_selection = realloc(music_selection, sizeof(char*)*nmusic_selection); music_selection = realloc(music_selection, sizeof(char*)*nmusic_selection);
DEBUG("Loaded %d song%c", nmusic_selection, (nmusic_selection==1)?' ':'s'); DEBUG("Loaded %d song%c", nmusic_selection, (nmusic_selection==1)?' ':'s');
/* More clean up. */
free(files);
return 0; return 0;
} }

View File

@ -425,10 +425,11 @@ int sound_updateListener(double dir, double x, double y) {
* @brief Make the list of available sounds. * @brief Make the list of available sounds.
*/ */
static int sound_makeList(void) { static int sound_makeList(void) {
const char** files; char** files;
uint32_t nfiles, i; uint32_t nfiles, i;
char path[PATH_MAX];
char tmp[64]; char tmp[64];
int len; int len, suflen, flen;
int mem; int mem;
if(sound_disabled) return 0; if(sound_disabled) return 0;
@ -438,10 +439,11 @@ static int sound_makeList(void) {
/* Load the profiles. */ /* Load the profiles. */
mem = 0; mem = 0;
for(i = 0; i < nfiles; i++) suflen = strlen(SOUND_SUFFIX);
if((strncmp(files[i], SOUND_PREFIX, strlen(SOUND_PREFIX))==0) && for(i = 0; i < nfiles; i++) {
(strncmp(files[i] + strlen(files[i]) - strlen(SOUND_SUFFIX), flen = strlen(files[i]);
SOUND_SUFFIX, strlen(SOUND_SUFFIX))==0)) { if(strncmp(&files[i][flen - suflen],
SOUND_SUFFIX, suflen)==0) {
/* Expand the selection size. */ /* Expand the selection size. */
sound_nlist++; sound_nlist++;
@ -450,14 +452,21 @@ static int sound_makeList(void) {
sound_list = realloc(sound_list, mem*sizeof(alSound)); sound_list = realloc(sound_list, mem*sizeof(alSound));
} }
/* Remove the prefix and suffix. */ /* Remove the suffix. */
len = strlen(files[i]) - strlen(SOUND_SUFFIX SOUND_PREFIX); len = flen - suflen;
strncpy(tmp, files[i] + strlen(SOUND_PREFIX), len); strncpy(tmp, files[i], len);
tmp[len] = '\0'; tmp[len] = '\0';
/* give it the new name. */ /* give it the new name. */
sound_list[sound_nlist-1].name = strdup(tmp); sound_list[sound_nlist-1].name = strdup(tmp);
sound_list[sound_nlist-1].buffer = sound_load(files[i]);
/* Load the sound. */
snprintf(path, PATH_MAX, SOUND_PREFIX"%s", files[i]);
sound_list[sound_nlist-1].buffer = sound_load(path);
}
/* Clean up. */
free(files[i]);
} }
/* Shrink to minimum ram usage. */ /* Shrink to minimum ram usage. */
@ -465,6 +474,9 @@ static int sound_makeList(void) {
DEBUG("Loaded %d sound%s", sound_nlist, (sound_nlist==1)?"":"s"); DEBUG("Loaded %d sound%s", sound_nlist, (sound_nlist==1)?"":"s");
/* More clean up. */
free(files);
return 0; return 0;
} }