Skip to content

Commit

Permalink
More mangling.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Mar 25, 2006
1 parent 7ebf81e commit 346bacc
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 10 deletions.
42 changes: 39 additions & 3 deletions fileio.c
@@ -1,4 +1,5 @@
#include "fileio.h"
#include "platform.h"

// !!! FIXME: don't have this here. (need unlink for now).
#include <unistd.h>
Expand All @@ -7,10 +8,10 @@ boolean mojoInputToPhysicalFile(MojoInput *in, const char *fname)
{
FILE *out = NULL;
boolean iofailure = false;
char buf[1024];
uint32 br;

STUBBED("mkdir first?");
STUBBED("file permissions?");

if (in == NULL)
return false;
Expand All @@ -23,15 +24,15 @@ boolean mojoInputToPhysicalFile(MojoInput *in, const char *fname)

while (!iofailure)
{
br = in->read(in, buf, sizeof (buf));
br = in->read(in, scratchbuf_128k, sizeof (scratchbuf_128k));
STUBBED("how to detect read failures?");
if (br == 0) // we're done!
break;
else if (br < 0)
iofailure = true;
else
{
if (fwrite(buf, br, 1, out) != 1)
if (fwrite(scratchbuf_128k, br, 1, out) != 1)
iofailure = true;
} // else
} // while
Expand Down Expand Up @@ -282,5 +283,40 @@ MojoArchive *MojoArchive_newFromDirectory(const char *dirname)
return ar;
} // MojoArchive_newFromDirectory



MojoArchive *GBaseArchive = NULL;

MojoArchive *MojoArchive_initBaseArchive(void)
{
if (GBaseArchive != NULL)
return GBaseArchive;
else
{
const char *basepath = MojoPlatform_appBinaryPath();
MojoInput *io = MojoInput_newFromFile(basepath);

STUBBED("chdir to path of binary");

if (io != NULL)
GBaseArchive = MojoArchive_newFromInput(io, basepath);

if (GBaseArchive == NULL)
GBaseArchive = MojoArchive_newFromDirectory(".");
} // else

return GBaseArchive;
} // MojoArchive_initBaseArchive


void MojoArchive_deinitBaseArchive(void)
{
if (GBaseArchive != NULL)
{
GBaseArchive->close(GBaseArchive);
GBaseArchive = NULL;
} // if
} // MojoArchive_deinitBaseArchive

// end of fileio.c ...

4 changes: 4 additions & 0 deletions fileio.h
Expand Up @@ -71,6 +71,10 @@ struct MojoArchive
MojoArchive *MojoArchive_newFromDirectory(const char *dirname);
MojoArchive *MojoArchive_newFromInput(MojoInput *io, const char *origfname);

extern MojoArchive *GBaseArchive;
MojoArchive *MojoArchive_initBaseArchive(void);
void MojoArchive_deinitBaseArchive(void);

boolean mojoInputToPhysicalFile(MojoInput *in, const char *fname);

#ifdef __cplusplus
Expand Down
4 changes: 4 additions & 0 deletions misc.c
@@ -1,5 +1,9 @@
#include "universal.h"

uint8 scratchbuf_128k[128 * 1024];
int GArgc = 0;
char **GArgv = NULL;

static void panic(const char *err)
{
fprintf(stderr, "\n\n\nPANIC: %s\n\n\n", err);
Expand Down
28 changes: 21 additions & 7 deletions mojosetup.c
Expand Up @@ -4,8 +4,25 @@
#include "fileio.h"
#include "gui.h"

int GArgc = 0;
char **GArgv = NULL;
static boolean initEverything(void)
{
STUBBED("Init logging functionality.");

if (!MojoArchive_initBaseArchive())
return false;

if (!MojoGui_initGuiPlugin())
return false;

return true;
} // initEverything


static void deinitEverything(void)
{
MojoGui_deinitGuiPlugin();
STUBBED("Deinit logging functionality.");
} // deinitEverything


// This is called from main()/WinMain()/whatever.
Expand All @@ -14,13 +31,10 @@ int MojoSetup_main(int argc, char **argv)
GArgc = argc;
GArgv = argv;

if (!MojoGui_initGuiPlugin())
if (!initEverything())
return 1;

dbgprintf("Using GUI plugin '%s'\n", GGui->name(GGui));
GGui->msgbox(GGui, "Title", "text goes here.");

MojoGui_deinitGuiPlugin();
deinitEverything();
return 0;
} // MojoSetup_main

Expand Down
2 changes: 2 additions & 0 deletions platform.h
Expand Up @@ -10,6 +10,8 @@ extern "C" {
// this is called by your mainline.
int MojoSetup_main(int argc, char **argv);

const char *MojoPlatform_appBinaryPath(void);

#ifdef __cplusplus
}
#endif
Expand Down
23 changes: 23 additions & 0 deletions platform/unix.c
@@ -1,9 +1,32 @@
#include "../platform.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>

int main(int argc, char **argv)
{
return MojoSetup_main(argc, argv);
} // main


const char *MojoPlatform_appBinaryPath(void)
{
char resolved[PATH_MAX];

static char *retval = NULL;
if (retval != NULL)
return retval;

if (realpath("/proc/self/exe", resolved) != NULL)
retval = xstrdup(resolved); // fast path for Linux.
else if ( (strchr(GArgv[0], '/')) && (realpath(GArgv[0], resolved)) )
retval = xstrdup(resolved); // argv[0] contains a path
else
STUBBED("search path");

return retval;
} // MojoPlatform_appBinaryPath

// end of unix.c ...

3 changes: 3 additions & 0 deletions universal.h
Expand Up @@ -32,6 +32,9 @@ typedef int boolean;
extern int GArgc;
extern char **GArgv;

// Static, non-stack memory for scratch work...not thread safe!
extern uint8 scratchbuf_128k[128 * 1024];

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

0 comments on commit 346bacc

Please sign in to comment.