Last active
December 4, 2015 12:44
-
-
Save X4/53475c229ce8db113e2f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* C-Standard-Libraries */ | |
#ifndef LIBRARIES | |
#define LIBRARIES | |
#include <stdio.h> /* stdio.h — standard buffered input/output */ | |
#include <stdlib.h> /* stdlib.h — standard library definitions */ | |
#include <string.h> /* string.h — string operations */ | |
#include <stdarg.h> /* stdarg.h — handle variable argument list */ | |
#include <stdbool.h> /* stdbool.h — boolean type and values */ | |
#include <stddef.h> /* stddef.h — standard type definitions */ | |
#include <ctype.h> /* ctype.h — character types */ | |
#include <sys/types.h> /* stat.h – stat, fstat, lstat - get file status */ | |
#include <sys/stat.h> | |
#include <unistd.h> /* unistd.h — standard symbolic constants and types */ | |
#include <errno.h> /* errno.h — system error numbers */ | |
#include <setjmp.h> /* setjmp.h — stack environment declarations */ | |
#endif /*====LIBRARIES================================================*/ | |
#ifndef MACROS | |
#define MACROS | |
/* I/O Errorcodes */ | |
#define OPEN_ERROR 100 | |
#define READ_ERROR 101 | |
#define CLOSE_ERROR 102 | |
#define VERSION_ERROR 103 | |
#define INVALID_ERROR 104 | |
#define PROGRAM_MEMORY_ERROR 105 | |
#define DATA_MEMORY_ERROR 106 | |
/* CommandLine Errorcodes */ | |
#define SHOW_VERSION 200 | |
#define SHOW_HELP 201 | |
#define UNKNOWN_OPTION 202 | |
#define INPUT_OVERFLOW 203 | |
#define NO_INPUT 204 | |
#endif /*====MACROS===================================================*/ | |
#ifndef MAKRO_FUNCTIONS | |
#define MAKRO_FUNCTIONS | |
/* Try/Catch Emulation */ | |
#define Try do { jmp_buf ex_buf__; switch( setjmp(ex_buf__) ) { case 0: while(1) { | |
#define Catch(x) break; case x: | |
#define Finally break; } default: { | |
#define ETry break; } } }while(0) | |
#define Throw(x) longjmp(ex_buf__, x) | |
#endif /*====MAKRO_FUNCTIONS========================================*/ | |
void *allocate(unsigned size) { | |
void *p; | |
p = malloc(size); | |
if(p == NULL) | |
error("out of memory"); | |
return(p); | |
} | |
void release(void *p) { | |
if(p == NULL) | |
error("NULL pointer detected"); | |
free(p); | |
} | |
void error(char *fmt, ...) { | |
va_list ap; | |
va_start(ap, fmt); | |
printf("Error: "); | |
vprintf(fmt, ap); | |
printf("\n"); | |
va_end(ap); | |
exit(EXIT_FAILURE); | |
} | |
int loadCode(const char *codeFileName) { | |
unsigned int *codeData = NULL; | |
int codeSize = 0; | |
FILE *codeFile; | |
file_t fileInfo; | |
Try { | |
if(codeFileName == NULL) | |
Throw( NO_INPUT ); | |
codeFile = fopen(codeFileName, "r"); | |
if(codeFile == NULL) | |
Throw( OPEN_ERROR ); | |
codeData = allocate(codeSize); | |
if(codeData == NULL) | |
Throw( DATA_MEMORY_ERROR ); | |
fileInfo = getFileInfo(codeFile); | |
if(strcmp(fileInfo.magicBit, "NJBF") != 0) | |
Throw( INVALID_ERROR ); | |
if(fileInfo.version != NJVM_VERSION) | |
Throw( VERSION_ERROR ); | |
} | |
Catch( NO_INPUT ) { | |
error("no code file specified"); | |
} | |
Catch( OPEN_ERROR ) { | |
error("cannot open code file '%s'", codeFileName); | |
} | |
Catch( READ_ERROR ) { | |
error("cannot read code file '%s'", codeFileName); | |
} | |
Catch( CLOSE_ERROR ) { | |
error("cannot close code file '%s'", codeFileName); | |
} | |
Catch( VERSION_ERROR ) { | |
error("file '%s' has wrong version number", codeFileName); | |
} | |
Catch( INVALID_ERROR ) { | |
error("file '%s' is not a Ninja binary", codeFileName); | |
} | |
Catch( PROGRAM_MEMORY_ERROR ) { | |
error("cannot allocate program memory"); | |
} | |
Catch( DATA_MEMORY_ERROR ) { | |
error("cannot allocate data memory"); | |
} | |
ETry; | |
return(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment