[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';
prof = ai_getProfile(buf);
if(prof == NULL)
WARN("AI Profile '%s' not found.", buf);
p->ai = prof;
L = p->ai->L;
@ -375,24 +377,33 @@ void ai_destroy(Pilot* p) {
* @return 0 on no errors.
*/
int ai_init(void) {
const char** files;
char** files;
uint32_t nfiles, i;
char path[PATH_MAX];
int flen, suflen;
/* Get the file list. */
files = ldata_list(AI_PREFIX, &nfiles);
/* Load the profiles. */
for(i = 0; i < nfiles; i++)
if((strncmp(files[i], AI_PREFIX, strlen(AI_PREFIX))==0) && /* Prefixed. */
(strncmp(files[i] + strlen(AI_PREFIX), AI_INCLUDE, /* Not an include. */
strlen(AI_INCLUDE)) != 0) &&
(strncmp(files[i] + strlen(files[i]) - strlen(AI_SUFFIX), /* Suffixed. */
AI_SUFFIX, strlen(AI_SUFFIX))==0))
if(ai_loadProfile(files[i])) /* Load the profiles. */
WARN("Error loading AI profile '%s'", files[i]);
suflen = strlen(AI_SUFFIX);
for(i = 0; i < nfiles; i++) {
flen = strlen(files[i]);
if(strncmp(&files[i][flen-suflen], AI_SUFFIX, suflen)==0) {
snprintf(path, PATH_MAX, AI_PREFIX"%s", files[i]);
if(ai_loadProfile(path)) /* Load the profile. */
WARN("Error loading AI profile '%s'", path);
}
/* Clean up. */
free(files[i]);
}
DEBUG("Loaded %d AI profile%c", nprofiles, (nprofiles==1)?' ':'s');
/* More clean up. */
free(files);
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. */
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. */
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 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.
* @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.
* @param path Path to set.
* @return 0 on success.
*/
int ldata_setPath(char* path) {
if(ldata_filename != NULL)
@ -54,6 +58,7 @@ int ldata_setPath(char* path) {
/**
* @brief Open a packfile if needed.
* @return 0 on success.
*/
static int ldata_openPackfile(void) {
int i;
@ -180,30 +185,102 @@ const char* ldata_name(void) {
/**
* @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) {
if(ldata_cache == NULL)
ldata_openPackfile();
char* buf;
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);
}
/**
* @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) {
(void)path;
static char** filterList(const char** list, int nlist,
const char* path, uint32_t* nfiles) {
if(ldata_fileList != NULL) {
*nfiles = ldata_fileNList;
return ldata_fileList;
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]);
}
if(ldata_cache == NULL)
ldata_openPackfile();
ldata_fileList = pack_listfilesCached(ldata_cache, &ldata_fileNList);
*nfiles = ldata_fileNList;
return ldata_fileList;
/* 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;
char** files;
int n;
/* Already loaded the list. */
if(ldata_fileList != NULL)
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;
}
/* Open packfile. */
ldata_openPackfile();
}
/* Load list. */
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. */
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.
*/
void loadscreen_load(void) {
int i;
unsigned int i;
char file_path[PATH_MAX];
const char** loadscreens;
const char** files;
uint32_t nfiles;
size_t len;
int nload;
char** loadscreens;
uint32_t nload;
/* Count the loading screens. */
files = ldata_list("../gfx/loading/", &nfiles);
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++;
}
}
loadscreens = ldata_list("../gfx/loading/", &nload);
/* Must have loading screens. */
if(nload == 0) {
@ -310,10 +298,13 @@ void loadscreen_load(void) {
}
/* 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);
/* Clean up. */
for(i = 0; i < nload; i++)
free(loadscreens[i]);
free(loadscreens);
}

View File

@ -22,6 +22,8 @@
#include "log.h"
#include "lfile.h"
#define BLOCK_SIZE 128*1024 /**< 128 kilobytes. */
static char lephisto_base[PATH_MAX] = "\0"; /**< Store Lephisto's base path. */
/**
* @fn char* lfile_basePath(void)
@ -235,6 +237,59 @@ char** lfile_readDir(int* lfiles, const char* path, ...) {
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.
* @param path Path of the file to create.

View File

@ -1,14 +1,9 @@
#pragma once
char* lfile_basePath(void);
/* Create if doesn't exist, 0 success. */
int lfile_dirMakeExist(const char* path, ...);
/* Return 1 on exit. */
int lfile_fileExists(const char* path, ...);
char** lfile_readDir(int* lfiles, const char* path, ...);
int lfile_touch(const char* path, ...);
char* lfile_readFile(int* filesize, const char* path, ...);
int lfile_touch(const char* path, ...);

View File

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

View File

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