From 12a864abf5a5cc79f0a8f11535c26bb2009c0ad9 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Fri, 11 Oct 2013 03:02:38 -0400 Subject: [PATCH] Some more platform / compiler specific code refactoring. --- Makefile | 24 +++--- ansi.c | 58 +++++++++++++- ast.c | 1 + exec.c | 5 +- fold.c | 1 + fs.c | 229 +++++++---------------------------------------------- ftepp.c | 3 +- gmqcc.h | 14 ---- intrin.c | 2 + ir.c | 1 + lexer.c | 2 + opts.c | 1 + pak.c | 1 + parser.c | 2 + platform.h | 64 +++++++++++++++ test.c | 3 +- 16 files changed, 176 insertions(+), 235 deletions(-) create mode 100644 platform.h diff --git a/Makefile b/Makefile index f7255d4..341be42 100644 --- a/Makefile +++ b/Makefile @@ -148,22 +148,22 @@ install-doc: # DO NOT DELETE -pak.o: gmqcc.h opts.def -ansi.o: gmqcc.h opts.def +pak.o: gmqcc.h opts.def platform.h +ansi.o: platform.h util.o: gmqcc.h opts.def stat.o: gmqcc.h opts.def -fs.o: gmqcc.h opts.def +fs.o: gmqcc.h opts.def platform.h conout.o: gmqcc.h opts.def -opts.o: gmqcc.h opts.def -test.o: gmqcc.h opts.def +opts.o: gmqcc.h opts.def platform.h +test.o: gmqcc.h opts.def platform.h main.o: gmqcc.h opts.def lexer.h -lexer.o: gmqcc.h opts.def lexer.h -parser.o: parser.h gmqcc.h opts.def lexer.h ast.h ir.h +lexer.o: gmqcc.h opts.def lexer.h platform.h +parser.o: parser.h gmqcc.h opts.def lexer.h ast.h ir.h platform.h code.o: gmqcc.h opts.def -ast.o: gmqcc.h opts.def ast.h ir.h parser.h lexer.h -ir.o: gmqcc.h opts.def ir.h -ftepp.o: gmqcc.h opts.def lexer.h +ast.o: gmqcc.h opts.def ast.h ir.h parser.h lexer.h platform.h +ir.o: gmqcc.h opts.def ir.h platform.h +ftepp.o: gmqcc.h opts.def lexer.h platform.h utf8.o: gmqcc.h opts.def correct.o: gmqcc.h opts.def -fold.o: ast.h ir.h gmqcc.h opts.def parser.h lexer.h -intrin.o: parser.h gmqcc.h opts.def lexer.h ast.h ir.h +fold.o: ast.h ir.h gmqcc.h opts.def parser.h lexer.h platform.h +intrin.o: parser.h gmqcc.h opts.def lexer.h ast.h ir.h platform.h diff --git a/ansi.c b/ansi.c index adc7907..d470e97 100644 --- a/ansi.c +++ b/ansi.c @@ -23,7 +23,7 @@ #include #include -#include "gmqcc.h" +#include "platform.h" int platform_vsnprintf(char *buffer, size_t bytes, const char *format, va_list arg) { return vsnprintf(buffer, bytes, format, arg); @@ -82,3 +82,59 @@ char *platform_strncpy(char *dest, const char *src, size_t num) { const char *platform_strerror(int err) { return strerror(err); } + +FILE *platform_fopen(const char *filename, const char *mode) { + return fopen(filename, mode); +} + +size_t platform_fread(void *ptr, size_t size, size_t count, FILE *stream) { + return fread(ptr, size, count, stream); +} + +size_t platform_fwrite(const void *ptr, size_t size, size_t count, FILE *stream) { + return fwrite(ptr, size, count, stream); +} + +int platform_vfprintf(FILE *stream, const char *format, va_list arg) { + return vfprintf(stream, format, arg); +} + +int platform_fclose(FILE *stream) { + return fclose(stream); +} + +int platform_ferror(FILE *stream) { + return ferror(stream); +} + +int platform_fgetc(FILE *stream) { + return fgetc(stream); +} + +int platform_fputs(const char *str, FILE *stream) { + return fputs(str, stream); +} + +int platform_fseek(FILE *stream, long offset, int origin) { + return fseek(stream, offset, origin); +} + +long platform_ftell(FILE *stream) { + return ftell(stream); +} + +int platform_mkdir(const char *path, int mode) { + return mkdir(path, mode); +} + +DIR *platform_opendir(const char *path) { + return opendir(path); +} + +int platform_closedir(DIR *dir) { + return closedir(dir); +} + +struct dirent *platform_readdir(DIR *dir) { + return readdir(dir); +} diff --git a/ast.c b/ast.c index 56dc1e5..66b78eb 100644 --- a/ast.c +++ b/ast.c @@ -27,6 +27,7 @@ #include "gmqcc.h" #include "ast.h" #include "parser.h" +#include "platform.h" #define ast_instantiate(T, ctx, destroyfn) \ T* self = (T*)mem_a(sizeof(T)); \ diff --git a/exec.c b/exec.c index d614ebf..8f68d5c 100644 --- a/exec.c +++ b/exec.c @@ -23,12 +23,11 @@ */ #ifndef QCVM_LOOP #include -#include -#include -#include #include +#include #include "gmqcc.h" +#include "platform.h" static void loaderror(const char *fmt, ...) { diff --git a/fold.c b/fold.c index d485ed2..6208a8a 100644 --- a/fold.c +++ b/fold.c @@ -25,6 +25,7 @@ #include "ast.h" #include "parser.h" +#include "platform.h" #define FOLD_STRING_UNTRANSLATE_HTSIZE 1024 #define FOLD_STRING_DOTRANSLATE_HTSIZE 1024 diff --git a/fs.c b/fs.c index 7844703..beb4819 100644 --- a/fs.c +++ b/fs.c @@ -21,119 +21,28 @@ * SOFTWARE. */ #include "gmqcc.h" +#include "platform.h" -/* - * This is essentially a "wrapper" interface around standard C's IO - * library. There is two reason we implement this, 1) visual studio - * hearts for "secure" varations, as part of it's "Security Enhancements - * in the CRT" (http://msdn.microsoft.com/en-us/library/8ef0s5kh.aspx). - * 2) But one of the greater reasons is for the possibility of large file - * support in the future. I don't expect to reach the 2GB limit any - * time soon (mainly because that would be insane). But when it comes - * to adding support for some other larger IO tasks (in the test-suite, - * or even the QCVM we'll need it). There is also a third possibility of - * building .dat files directly from zip files (which would be very cool - * at least I think so). - */ -#ifdef _MSC_VER -#include /* _CrtSetReportMode, _CRT_ASSERT */ -/* {{{ */ - /* - * Visual Studio has security CRT features which I actually want to support - * if we ever port to Windows 8, and want GMQCC to be API safe. - * - * We handle them here, for all file-operations. - */ - - static void file_exception ( - const wchar_t *expression, - const wchar_t *function, - const wchar_t *file, - unsigned int line, - uintptr_t reserved - ) { - wprintf(L"Invalid parameter dectected %s:%d %s [%s]\n", file, line, function, expression); - wprintf(L"Aborting ...\n"); - exit(EXIT_FAILURE); - } - - static void file_init() { - static bool init = false; - - if (init) - return; - - _set_invalid_parameter_handler(&file_exception); - - /* - * Turnoff the message box for CRT asserations otherwise - * we don't get the error reported to the console as we should - * otherwise get. - */ - _CrtSetReportMode(_CRT_ASSERT, 0); - init = !init; - } - - - FILE *fs_file_open(const char *filename, const char *mode) { - FILE *handle = NULL; - file_init(); - - return (fopen_s(&handle, filename, mode) != 0) ? NULL : handle; - } - - size_t fs_file_read(void *buffer, size_t size, size_t count, FILE *fp) { - file_init(); - return fread_s(buffer, size*count, size, count, fp); - } - - int fs_file_printf(FILE *fp, const char *format, ...) { - int rt; - va_list va; - va_start(va, format); - - file_init(); - rt = vfprintf_s(fp, format, va); - va_end (va); - - return rt; - } - -/* }}} */ -#else -/* {{{ */ - /* - * All other compilers/platforms that don't restrict insane policies on - * IO for no aparent reason. - */ - FILE *fs_file_open(const char *filename, const char *mode) { - return fopen(filename, mode); - } - - size_t fs_file_read(void *buffer, size_t size, size_t count, FILE *fp) { - return fread(buffer, size, count, fp); - } +FILE *fs_file_open(const char *filename, const char *mode) { + return platform_fopen(filename, mode); +} - int fs_file_printf(FILE *fp, const char *format, ...) { - int rt; - va_list va; - va_start(va, format); - rt = vfprintf(fp, format, va); - va_end (va); +size_t fs_file_read(void *buffer, size_t size, size_t count, FILE *fp) { + return platform_fread(buffer, size, count, fp); +} - return rt; - } +int fs_file_printf(FILE *fp, const char *format, ...) { + int rt; + va_list va; + va_start(va, format); + rt = platform_vfprintf(fp, format, va); + va_end (va); -/* }}} */ -#endif + return rt; +} -/* - * These are implemented as just generic wrappers to keep consistency in - * the API. Not as macros though - */ void fs_file_close(FILE *fp) { - /* Invokes file_exception on windows if fp is null */ - fclose (fp); + platform_fclose (fp); } size_t fs_file_write ( @@ -142,33 +51,27 @@ size_t fs_file_write ( size_t count, FILE *fp ) { - /* Invokes file_exception on windows if fp is null */ - return fwrite(buffer, size, count, fp); + return platform_fwrite(buffer, size, count, fp); } int fs_file_error(FILE *fp) { - /* Invokes file_exception on windows if fp is null */ - return ferror(fp); + return platform_ferror(fp); } int fs_file_getc(FILE *fp) { - /* Invokes file_exception on windows if fp is null */ - return fgetc(fp); + return platform_fgetc(fp); } int fs_file_puts(FILE *fp, const char *str) { - /* Invokes file_exception on windows if fp is null */ - return fputs(str, fp); + return platform_fputs(str, fp); } int fs_file_seek(FILE *fp, long int off, int whence) { - /* Invokes file_exception on windows if fp is null */ - return fseek(fp, off, whence); + return platform_fseek(fp, off, whence); } long int fs_file_tell(FILE *fp) { - /* Invokes file_exception on windows if fp is null */ - return ftell(fp); + return platform_ftell(fp); } /* @@ -219,94 +122,18 @@ int fs_file_getline(char **lineptr, size_t *n, FILE *stream) { return (ret = pos - *lineptr); } -/* - * Now we implement some directory functionality. Windows lacks dirent.h - * this is such a pisss off, we implement it here. - */ -#if defined(_WIN32) && !defined(__MINGW32__) - DIR *fs_dir_open(const char *name) { - DIR *dir = (DIR*)mem_a(sizeof(DIR) + strlen(name)); - if (!dir) - return NULL; - - platform_strncpy(dir->dd_name, name, strlen(name)); - return dir; - } - - int fs_dir_close(DIR *dir) { - FindClose((HANDLE)dir->dd_handle); - mem_d ((void*)dir); - return 0; - } - - struct dirent *fs_dir_read(DIR *dir) { - WIN32_FIND_DATA info; - struct dirent *data; - int rets; - - if (!dir->dd_handle) { - char *dirname; - if (*dir->dd_name) { - size_t n = strlen(dir->dd_name); - if ((dirname = (char*)mem_a(n + 5) /* 4 + 1 */)) { - platform_strncpy(dirname, dir->dd_name, n); - platform_strncpy(dirname + n, "\\*.*", 4); /* 4 + 1 */ - } - } else { - if (!(dirname = util_strdup("\\*.*"))) - return NULL; - } - - dir->dd_handle = (long)FindFirstFile(dirname, &info); - mem_d(dirname); - rets = !(!dir->dd_handle); - } else if (dir->dd_handle != -11) { - rets = FindNextFile ((HANDLE)dir->dd_handle, &info); - } else { - rets = 0; - } - - if (!rets) - return NULL; - - if ((data = (struct dirent*)mem_a(sizeof(struct dirent)))) { - platform_strncpy(data->d_name, info.cFileName, FILENAME_MAX - 1); - data->d_name[FILENAME_MAX - 1] = '\0'; /* terminate */ - data->d_namlen = strlen(data->d_name); - } - return data; - } - - int fs_dir_change(const char *path) { - return !SetCurrentDirectory(path); - } - - int fs_dir_make(const char *path) { - return !CreateDirectory(path, NULL); - } -#else -# if !defined(__MINGW32__) -# include /* mkdir */ - - int fs_dir_make(const char *path) { - return mkdir(path, 0700); - } -# else - int fs_dir_make(const char *path) { - return mkdir(path); - } -# endif /*! !defined(__MINGW32__) */ +int fs_dir_make(const char *path) { + return platform_mkdir(path, 0700); +} DIR *fs_dir_open(const char *name) { - return opendir(name); + return platform_opendir(name); } int fs_dir_close(DIR *dir) { - return closedir(dir); + return platform_closedir(dir); } struct dirent *fs_dir_read(DIR *dir) { - return readdir(dir); + return platform_readdir(dir); } - -#endif /*! defined(_WIN32) && !defined(__MINGW32__) */ diff --git a/ftepp.c b/ftepp.c index f6c850c..b07fa97 100644 --- a/ftepp.c +++ b/ftepp.c @@ -21,13 +21,12 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#include #include #include -#include #include "gmqcc.h" #include "lexer.h" +#include "platform.h" #define HT_MACROS 1024 typedef struct { diff --git a/gmqcc.h b/gmqcc.h index 75f70a5..373232f 100644 --- a/gmqcc.h +++ b/gmqcc.h @@ -24,7 +24,6 @@ #ifndef GMQCC_HDR #define GMQCC_HDR #include -#include /* TODO: remove this? */ #include /* TODO: remove this */ /* @@ -460,19 +459,6 @@ DIR *fs_dir_open (const char *); int fs_dir_close (DIR *); struct dirent *fs_dir_read (DIR *); - -int platform_vsnprintf(char *buffer, size_t bytes, const char *format, va_list arg); -int platform_sscanf(const char *str, const char *format, ...); -const struct tm *platform_localtime(const time_t *timer); -const char *platform_ctime(const time_t *timer); -char *platform_strncat(char *dest, const char *src, size_t num); -const char *platform_tmpnam(char *str); -const char *platform_getenv(char *var); -int platform_snprintf(char *src, size_t bytes, const char *format, ...); -char *platform_strcat(char *dest, const char *src); -char *platform_strncpy(char *dest, const char *src, size_t num); -const char *platform_strerror(int err); - /*===================================================================*/ /*=========================== correct.c =============================*/ /*===================================================================*/ diff --git a/intrin.c b/intrin.c index 49de952..ae6edaa 100644 --- a/intrin.c +++ b/intrin.c @@ -21,7 +21,9 @@ * SOFTWARE. */ #include + #include "parser.h" +#include "platform.h" /* * Provides all the "intrinsics" / "builtins" for GMQCC. These can do diff --git a/ir.c b/ir.c index 1da5d7c..8fa8214 100644 --- a/ir.c +++ b/ir.c @@ -26,6 +26,7 @@ #include "gmqcc.h" #include "ir.h" +#include "platform.h" /*********************************************************************** * Type sizes used at multiple points in the IR codegen diff --git a/lexer.c b/lexer.c index 2d36afb..03c27d1 100644 --- a/lexer.c +++ b/lexer.c @@ -25,6 +25,8 @@ #include "gmqcc.h" #include "lexer.h" +#include "platform.h" + /* * List of Keywords */ diff --git a/opts.c b/opts.c index 570b578..fd10233 100644 --- a/opts.c +++ b/opts.c @@ -25,6 +25,7 @@ #include #include "gmqcc.h" +#include "platform.h" const unsigned int opts_opt_oflag[COUNT_OPTIMIZATIONS+1] = { # define GMQCC_TYPE_OPTIMIZATIONS diff --git a/pak.c b/pak.c index fbc2262..ba820ef 100644 --- a/pak.c +++ b/pak.c @@ -24,6 +24,7 @@ #include #include "gmqcc.h" +#include "platform.h" /* * The PAK format uses a FOURCC concept for storing the magic ident within diff --git a/parser.c b/parser.c index 47563f3..d913567 100644 --- a/parser.c +++ b/parser.c @@ -23,7 +23,9 @@ */ #include #include + #include "parser.h" +#include "platform.h" #define PARSER_HT_LOCALS 2 #define PARSER_HT_SIZE 512 diff --git a/platform.h b/platform.h new file mode 100644 index 0000000..db76edd --- /dev/null +++ b/platform.h @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2012, 2013 + * Dale Weiler + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef GMQCC_PLATFORM_HDR +#define GMQCC_PLATFORM_HDR +#include +#include +#include +#include + +#ifndef _MSC_VER +# include +# include +# include +#endif + +int platform_vsnprintf(char *buffer, size_t bytes, const char *format, va_list arg); +int platform_sscanf(const char *str, const char *format, ...); +const struct tm *platform_localtime(const time_t *timer); +const char *platform_ctime(const time_t *timer); +char *platform_strncat(char *dest, const char *src, size_t num); +const char *platform_tmpnam(char *str); +const char *platform_getenv(char *var); +int platform_snprintf(char *src, size_t bytes, const char *format, ...); +char *platform_strcat(char *dest, const char *src); +char *platform_strncpy(char *dest, const char *src, size_t num); +const char *platform_strerror(int err); +FILE *platform_fopen(const char *filename, const char *mode); +size_t platform_fread(void *ptr, size_t size, size_t count, FILE *stream); +size_t platform_fwrite(const void *ptr, size_t size, size_t count, FILE *stream); +int platform_vfprintf(FILE *stream, const char *format, va_list arg); +int platform_fclose(FILE *stream); +int platform_ferror(FILE *stream); +int platform_fgetc(FILE *stream); +int platform_fputs(const char *str, FILE *stream); +int platform_fseek(FILE *stream, long offset, int origin); +long platform_ftell(FILE *stream); +int platform_mkdir(const char *path, int mode); +DIR *platform_opendir(const char *path); +int platform_closedir(DIR *dir); +struct dirent *platform_readdir(DIR *dir); + + +#endif diff --git a/test.c b/test.c index 07ca9ca..a0484be 100644 --- a/test.c +++ b/test.c @@ -22,10 +22,9 @@ */ #include #include -#include -#include #include "gmqcc.h" +#include "platform.h" static const char *task_bins[] = { "./gmqcc", -- 2.39.2