Skip to content

Commit

Permalink
Another flood of work...
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Mar 25, 2006
1 parent a112f49 commit c1f0b47
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 21 deletions.
107 changes: 107 additions & 0 deletions fileio.c
Expand Up @@ -46,5 +46,112 @@ boolean mojoInputToPhysicalFile(MojoInput *in, const char *fname)
return true;
} // mojoInputToPhysicalFile


// MojoArchives from directories on the OS filesystem.

// !!! FIXME: abstract the unixy bits into the platform/ dir.
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>

typedef struct
{
DIR *dir;
char *path;
} MojoArchiveDirInstance;

static boolean MojoArchive_dir_enumerate(MojoArchive *ar, const char *path)
{
MojoArchiveDirInstance *inst = (MojoArchiveDirInstance *) ar->opaque;
if (inst->dir != NULL)
closedir(inst->dir);

free(ar->lastEnumInfo.filename);
ar->lastEnumInfo.filename = NULL;
ar->lastEnumInfo.type = MOJOARCHIVE_ENTRY_UNKNOWN;
ar->lastEnumInfo.filesize = 0;

inst->path = xrealloc(inst->path, strlen(path) + 1);
strcpy(inst->path, path);

inst->dir = opendir(path);
return (inst->dir != NULL);
} // MojoArchive_dir_enumerate


static const MojoArchiveEntryInfo *MojoArchive_dir_enumNext(MojoArchive *ar)
{
struct stat statbuf;
char *fullpath = NULL;
struct dirent *dent;
MojoArchiveDirInstance *inst = (MojoArchiveDirInstance *) ar->opaque;
if (inst->dir == NULL)
return NULL;

free(ar->lastEnumInfo.filename);
ar->lastEnumInfo.filename = NULL;
ar->lastEnumInfo.type = MOJOARCHIVE_ENTRY_UNKNOWN;
ar->lastEnumInfo.filesize = 0;

dent = readdir(inst->dir);
if (dent == NULL) // end of dir?
{
closedir(inst->dir);
inst->dir = NULL;
return NULL;
} // if

if ((strcmp(dent->d_name, ".") == 0) || (strcmp(dent->d_name, "..") == 0))
return MojoArchive_dir_enumNext(ar);

fullpath = (char *) alloca(strlen(dent->d_name) + strlen(inst->path) + 2);
sprintf(fullpath, "%s/%s", inst->path, dent->d_name);
if (stat(fullpath, &statbuf) != -1)
{
ar->lastEnumInfo.filesize = statbuf.st_size;
if (S_ISDIR(statbuf.st_mode))
ar->lastEnumInfo.type = MOJOARCHIVE_ENTRY_DIR;
else if (S_ISREG(statbuf.st_mode))
ar->lastEnumInfo.type = MOJOARCHIVE_ENTRY_FILE;
else if (S_ISLNK(statbuf.st_mode))
ar->lastEnumInfo.type = MOJOARCHIVE_ENTRY_SYMLINK;
} // if

ar->lastEnumInfo.filename = xmalloc(strlen(dent->d_name) + 1);
strcpy(ar->lastEnumInfo.filename, dent->d_name);
return &ar->lastEnumInfo;
} // MojoArchive_dir_enumNext


static MojoInput *MojoArchive_dir_openCurrentEntry(MojoArchive *ar)
{
STUBBED("writeme");
return NULL;
} // MojoArchive_dir_openCurrentEntry


static void MojoArchive_dir_close(MojoArchive *ar)
{
MojoArchiveDirInstance *inst = (MojoArchiveDirInstance *) ar->opaque;
if (inst->dir != NULL)
closedir(inst->dir);
free(inst->path);
free(ar->lastEnumInfo.filename);
free(ar->opaque);
free(ar);
} // MojoArchive_dir_close


MojoArchive *MojoArchive_newFromDirectory(const char *dirname)
{
MojoArchive *ar = (MojoArchive *) xmalloc(sizeof (MojoArchive));
ar->enumerate = MojoArchive_dir_enumerate;
ar->enumNext = MojoArchive_dir_enumNext;
ar->openCurrentEntry = MojoArchive_dir_openCurrentEntry;
ar->close = MojoArchive_dir_close;
ar->opaque = xmalloc(sizeof (MojoArchiveDirInstance));
return ar;
} // MojoArchive_newFromDirectory

// end of fileio.c ...

9 changes: 5 additions & 4 deletions fileio.h
Expand Up @@ -38,7 +38,8 @@ MojoInput *MojoInput_newFromMemory(void *mem, size_t bytes);

typedef enum
{
MOJOARCHIVE_ENTRY_FILE = 0,
MOJOARCHIVE_ENTRY_UNKNOWN = 0,
MOJOARCHIVE_ENTRY_FILE,
MOJOARCHIVE_ENTRY_DIR,
MOJOARCHIVE_ENTRY_SYMLINK,
} MojoArchiveEntryType;
Expand All @@ -47,7 +48,7 @@ typedef enum
typedef struct MojoArchiveEntryInfo MojoArchiveEntryInfo;
struct MojoArchiveEntryInfo
{
const char *filename;
char *filename;
MojoArchiveEntryType type;
int64 filesize;
};
Expand All @@ -56,8 +57,8 @@ typedef struct MojoArchive MojoArchive;
struct MojoArchive
{
// public
void (*restartEnumeration)(MojoArchive *ar);
const MojoArchiveEntryInfo* (*enumEntry)(MojoArchive *ar);
boolean (*enumerate)(MojoArchive *ar, const char *path);
const MojoArchiveEntryInfo* (*enumNext)(MojoArchive *ar);
MojoInput* (*openCurrentEntry)(MojoArchive *ar);
void (*close)(MojoArchive *ar);

Expand Down
29 changes: 14 additions & 15 deletions gui.c
Expand Up @@ -53,15 +53,12 @@ static PluginList *loadGuiPlugin(MojoArchive *ar)
entry = (MojoGuiEntryType) dlsym(lib, MOJOGUI_ENTRY_POINT_STR);
if ( (entry != NULL) && ((gui = entry()) != NULL) )
{
retval = malloc(sizeof (PluginList));
if (retval != NULL)
{
strcpy(retval->fname, fname);
retval->lib = lib;
retval->gui = gui;
retval->priority = gui->priority(gui);
retval->next = NULL;
} // if
retval = xmalloc(sizeof (PluginList));
strcpy(retval->fname, fname);
retval->lib = lib;
retval->gui = gui;
retval->priority = gui->priority(gui);
retval->next = NULL;
} // if
} // if
} // if
Expand Down Expand Up @@ -129,18 +126,20 @@ MojoGui *MojoGui_initGuiPlugin(void)
if (dir == NULL)
return false;

dir->restartEnumeration(dir);
while ((entinfo = dir->enumEntry(dir)) != NULL)
if (!dir->enumerate(dir, "gui"))
{
STUBBED("Don't close this when it's a global.");
dir->close(dir);
return false;
} // if

while ((entinfo = dir->enumNext(dir)) != NULL)
{
PluginList *item;

if (entinfo->type != MOJOARCHIVE_ENTRY_FILE)
continue;

// not in the gui dir?
if (strncmp(entinfo->filename, "guiplugins/", 4) != 0)
continue;

item = loadGuiPlugin(dir);
if (item == NULL)
continue;
Expand Down
6 changes: 4 additions & 2 deletions gui.h
Expand Up @@ -62,7 +62,6 @@ struct MojoGui_rev1

typedef MOJOGUI_STRUCT MojoGui;
typedef MOJOGUI_STRUCT * (*MojoGuiEntryType)(void);
__EXPORT__ MOJOGUI_STRUCT *MOJOGUI_ENTRY_POINT(void);

/*
* We do this as a macro so we only have to update one place, and it
Expand All @@ -83,10 +82,13 @@ MOJOGUI_STRUCT *MOJOGUI_ENTRY_POINT(void) \
return &retval; \
} \


#ifndef BUILDING_EXTERNAL_PLUGIN
extern MojoGui *GGui;
MojoGui *MojoGui_initGuiPlugin(void);
void MojoGui_deinitGuiPlugin(void);
#else
__EXPORT__ MOJOGUI_STRUCT *MOJOGUI_ENTRY_POINT(void);
#endif

#ifdef __cplusplus
}
Expand Down
1 change: 1 addition & 0 deletions gui/gui_stdio.c
@@ -1,3 +1,4 @@
#define BUILDING_EXTERNAL_PLUGIN 1
#include "../gui.h"
#include <ctype.h>

Expand Down
1 change: 1 addition & 0 deletions makefile
Expand Up @@ -107,6 +107,7 @@ INCLUDES := \

SRCS := \
mojosetup.c \
misc.c \
gui.c \
fileio.c \

Expand Down
29 changes: 29 additions & 0 deletions misc.c
@@ -0,0 +1,29 @@
#include "universal.h"

static void panic(const char *err)
{
fprintf(stderr, "\n\n\nPANIC: %s\n\n\n", err);
exit(22);
} // panic

#undef malloc
#undef calloc
void *xmalloc(size_t bytes)
{
void *retval = calloc(1, bytes);
if (retval == NULL)
panic("out of memory");
return retval;
} // xmalloc

#undef realloc
void *xrealloc(void *ptr, size_t bytes)
{
void *retval = realloc(ptr, bytes);
if (retval == NULL)
panic("out of memory");
return retval;
} // xrealloc

// end of misc.c ...

12 changes: 12 additions & 0 deletions universal.h
Expand Up @@ -28,9 +28,21 @@ typedef int boolean;
#define true 1
#define false 0

// Command line access outside of main().
extern int GArgc;
extern char **GArgv;

// Malloc replacements that blow up on allocation failure.
void *xmalloc(size_t bytes);
void *xrealloc(void *ptr, size_t bytes);

// External plugins won't link against misc.c ...
#ifndef BUILDING_EXTERNAL_PLUGIN
#define malloc(x) DO_NOT_CALL_MALLOC__USE_XMALLOC_INSTEAD
#define calloc(x,y) DO_NOT_CALL_CALLOC__USE_XMALLOC_INSTEAD
#define realloc(x,y) DO_NOT_CALL_REALLOC__USE_XREALLOC_INSTEAD
#endif

#ifndef DOXYGEN_SHOULD_IGNORE_THIS
#if (defined _MSC_VER)
#define __EXPORT__ __declspec(dllexport)
Expand Down

0 comments on commit c1f0b47

Please sign in to comment.