Added the linked list struct, LList, along with two functions for initializing and freeing a single LList. Also added the fio.c/.h files that will contain all functions pertaining to more complicated file or directory access. At the moment it provides two functions: dirToLList, which populates a linked list with the contents of the directory (as explained in the source), and fileExists, a simple function that checks if the file exists.
parent
1ebb045fef
commit
f9f65cd376
|
@ -1,7 +1,7 @@
|
||||||
CC = gcc
|
CC = gcc
|
||||||
PREFIX = ./
|
PREFIX = ./
|
||||||
BINARY=timesynk
|
BINARY=timesynk
|
||||||
OBJS = main.o c_extra.o state_menu.o ui/elements.o sdl/timer.o sdl/r_soft.o sdl/r_gl.o sdl/interface.o sdl/sdl_extra.o sdl/spritesheets.o sdl/font.o resources/badfont_large_png.o resources/badfont_medium_png.o resources/ui_png.o resources/menu_bg_png.o data.o
|
OBJS = main.o c_extra.o state_menu.o ui/elements.o sdl/timer.o sdl/r_soft.o sdl/r_gl.o sdl/interface.o sdl/sdl_extra.o sdl/spritesheets.o sdl/font.o resources/badfont_large_png.o resources/badfont_medium_png.o resources/ui_png.o resources/menu_bg_png.o data.o llist.o fio.o
|
||||||
DEBUG = -g
|
DEBUG = -g
|
||||||
CFLAGS = -Wall -c $(DEBUG)
|
CFLAGS = -Wall -c $(DEBUG)
|
||||||
SDL_LFLAGS = -lGL -lSDL -lSDL_image
|
SDL_LFLAGS = -lGL -lSDL -lSDL_image
|
||||||
|
|
|
@ -0,0 +1,71 @@
|
||||||
|
#include "fio.h"
|
||||||
|
#include <dirent.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
================================
|
||||||
|
struct LList *dirToLList(const char *dir_name, int opts)
|
||||||
|
|
||||||
|
This function takes a given directory path and attempts to populate a linked list(LList) with the contents of the directory (excluding ".." and "."). The opts argument must be provided with F_DIRS, F_FILES, or both F_DIRS|F_FILES. This limits the items added to the linked list to only directories, only files, or both.
|
||||||
|
|
||||||
|
================================
|
||||||
|
*/
|
||||||
|
struct LList *dirToLList(const char *dir_name, int opts) {
|
||||||
|
struct dirent *entry;
|
||||||
|
DIR *dir = opendir(dir_name);
|
||||||
|
if (dir == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
struct LList *start = NULL;
|
||||||
|
struct LList *llist = NULL;
|
||||||
|
struct LList *llist_last = NULL;
|
||||||
|
while ((entry = readdir(dir)) != NULL) {
|
||||||
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
||||||
|
continue;
|
||||||
|
int entry_size = strlen(entry->d_name)+1;
|
||||||
|
// ugly
|
||||||
|
char file_path[strlen(dir_name)+entry_size];
|
||||||
|
strcpy(file_path, dir_name);
|
||||||
|
file_path[strlen(dir_name)] = '/';
|
||||||
|
memcpy(file_path+strlen(dir_name)+1, entry->d_name, entry_size);
|
||||||
|
// ok, let's get file properties and see if we want it
|
||||||
|
struct stat file_stat;
|
||||||
|
stat(file_path, &file_stat);
|
||||||
|
switch(file_stat.st_mode & S_IFMT) {
|
||||||
|
case S_IFDIR:
|
||||||
|
if (~opts & F_DIRS)
|
||||||
|
continue;
|
||||||
|
break;
|
||||||
|
case S_IFREG:
|
||||||
|
if (~opts & F_FILES)
|
||||||
|
continue;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// if we got here, then the entry is valid
|
||||||
|
llist = newLList();
|
||||||
|
llist->data = malloc(entry_size);
|
||||||
|
llist->size = entry_size;
|
||||||
|
memcpy(llist->data, entry->d_name, entry_size);
|
||||||
|
if (start == NULL)
|
||||||
|
start = llist;
|
||||||
|
if (llist_last != NULL)
|
||||||
|
llist_last->next = llist;
|
||||||
|
llist_last = llist;
|
||||||
|
}
|
||||||
|
closedir(dir);
|
||||||
|
return start;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
================================
|
||||||
|
int fileExists(const char *filename)
|
||||||
|
|
||||||
|
A simple function that checks if the given filename exists.
|
||||||
|
================================
|
||||||
|
*/
|
||||||
|
int fileExists(const char *filename) {
|
||||||
|
struct stat buffer;
|
||||||
|
return (stat (filename, &buffer) == 0);
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef FIO_H
|
||||||
|
#define FIO_H
|
||||||
|
#include "llist.h"
|
||||||
|
#define F_DIRS 1
|
||||||
|
#define F_FILES 2
|
||||||
|
|
||||||
|
struct LList *dirToLList(const char *dir_name, int opts);
|
||||||
|
int fileExists(const char *filename);
|
||||||
|
#endif
|
|
@ -3,11 +3,14 @@
|
||||||
#include "ts_event.h"
|
#include "ts_event.h"
|
||||||
#include "state_menu.h"
|
#include "state_menu.h"
|
||||||
#include "../data.h"
|
#include "../data.h"
|
||||||
|
#include "llist.h"
|
||||||
|
|
||||||
struct Table *g_settings;
|
struct Table *g_settings;
|
||||||
|
|
||||||
int g_running;
|
int g_running;
|
||||||
|
|
||||||
|
struct LList *g_modules_list;
|
||||||
|
|
||||||
/* video-related globals */
|
/* video-related globals */
|
||||||
void *g_screen; // void pointer for SDL_Surface or others, set by interface
|
void *g_screen; // void pointer for SDL_Surface or others, set by interface
|
||||||
int g_video_width;
|
int g_video_width;
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
/****** llist.c
|
||||||
|
This file contains the functions for accessing and manipulating the linked list struct, LList.
|
||||||
|
******/
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "llist.h"
|
||||||
|
|
||||||
|
struct LList *newLList() {
|
||||||
|
struct LList *llist = malloc(sizeof(struct LList));
|
||||||
|
llist->size = 0;
|
||||||
|
llist->next = NULL;
|
||||||
|
llist->data = NULL;
|
||||||
|
return llist;
|
||||||
|
}
|
||||||
|
|
||||||
|
int freeLList(struct LList *llist) {
|
||||||
|
if (llist->data != NULL)
|
||||||
|
free(llist->data);
|
||||||
|
free(llist);
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
#ifndef LLIST_H
|
||||||
|
#define LLIST_H
|
||||||
|
struct LList {
|
||||||
|
int size;
|
||||||
|
struct LList *next;
|
||||||
|
void *data;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct LList *newLList();
|
||||||
|
int freeLList(struct LList *llist);
|
||||||
|
#endif
|
|
@ -1,5 +1,6 @@
|
||||||
#include "globals.h"
|
#include "globals.h"
|
||||||
#include "interfaces.h"
|
#include "interfaces.h"
|
||||||
|
#include "fio.h"
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
// let's load in our config file
|
// let's load in our config file
|
||||||
|
@ -19,6 +20,18 @@ int main(int argc, char *argv[]) {
|
||||||
addTablePairInt(g_settings, "v_framecap", 0);
|
addTablePairInt(g_settings, "v_framecap", 0);
|
||||||
addTablePairInt(g_settings, "tickrate", 40000000);
|
addTablePairInt(g_settings, "tickrate", 40000000);
|
||||||
}
|
}
|
||||||
|
// let's search for modules!
|
||||||
|
if ((g_modules_list = dirToLList("modules", F_DIRS)) == NULL)
|
||||||
|
printf("ERR: no modules found\n");
|
||||||
|
printf("modules:\n");
|
||||||
|
struct LList *llist = g_modules_list;
|
||||||
|
while (llist) {
|
||||||
|
if (strcmp((char*)llist->data, getTablePairValueString(g_settings, "module")) == 0)
|
||||||
|
printf("* ");
|
||||||
|
printf("%s\n", (char*)llist->data);
|
||||||
|
llist = llist->next;
|
||||||
|
}
|
||||||
|
|
||||||
// initialize our interface system (ncurses, SDL, etc.)
|
// initialize our interface system (ncurses, SDL, etc.)
|
||||||
if (interfaceInit() == 1) {
|
if (interfaceInit() == 1) {
|
||||||
return 1;
|
return 1;
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
;; module options
|
||||||
|
; name of last module loaded
|
||||||
|
module xibalba
|
||||||
;; video options
|
;; video options
|
||||||
v_width 1024
|
v_width 1024
|
||||||
v_height 768
|
v_height 768
|
||||||
|
|
Loading…
Reference in New Issue