]> de.git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
Some more platform / compiler specific code refactoring.
authorDale Weiler <killfieldengine@gmail.com>
Fri, 11 Oct 2013 07:02:38 +0000 (03:02 -0400)
committerDale Weiler <killfieldengine@gmail.com>
Fri, 11 Oct 2013 07:02:38 +0000 (03:02 -0400)
16 files changed:
Makefile
ansi.c
ast.c
exec.c
fold.c
fs.c
ftepp.c
gmqcc.h
intrin.c
ir.c
lexer.c
opts.c
pak.c
parser.c
platform.h [new file with mode: 0644]
test.c

index f7255d405fb5190cfdd52f7fd3335c3af93e1a87..341be42b723ca95f595a659c4871c5b2c4ae7310 100644 (file)
--- 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 adc7907ca1be15bbaaba6bbdfd87f84fb589fd83..d470e979eebdae521a00abb0974696ff72064059 100644 (file)
--- a/ansi.c
+++ b/ansi.c
@@ -23,7 +23,7 @@
 #include <string.h>
 #include <stdlib.h>
 
-#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 56dc1e5cf3aedac4dd5ca73dd7c858ec27489c14..66b78ebd1440d04ec27243268592d9c692711413 100644 (file)
--- 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 d614ebf58305351868c69f0c0dba0ad39a2960ba..8f68d5c3c0629421d207a1da59f512c048446497 100644 (file)
--- a/exec.c
+++ b/exec.c
  */
 #ifndef QCVM_LOOP
 #include <errno.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdarg.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "gmqcc.h"
+#include "platform.h"
 
 static void loaderror(const char *fmt, ...)
 {
diff --git a/fold.c b/fold.c
index d485ed23f1dba4c9957f4635773983b8467fc75b..6208a8a273e638cdc409fb487663a585a9a5073b 100644 (file)
--- 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 7844703e62a818bd9f7d1ebaa85464b4e4d95315..beb48192f85d3f36cb3fc64677452a6608e3b21b 100644 (file)
--- a/fs.c
+++ b/fs.c
  * 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 <crtdbg.h> /* _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 <sys/stat.h> /* 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 f6c850c80a037d885eb6851caada693bd5f513f6..b07fa974364555145fc3020a5b844be7688c20bb 100644 (file)
--- a/ftepp.c
+++ b/ftepp.c
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
-#include <time.h>
 #include <string.h>
 #include <stdlib.h>
-#include <sys/stat.h>
 
 #include "gmqcc.h"
 #include "lexer.h"
+#include "platform.h"
 
 #define HT_MACROS 1024
 typedef struct {
diff --git a/gmqcc.h b/gmqcc.h
index 75f70a5b7a1896004f048b051998999e6a65f80c..373232f7d0e4b5cb5f54a8f83594c0c689e39d77 100644 (file)
--- a/gmqcc.h
+++ b/gmqcc.h
@@ -24,7 +24,6 @@
 #ifndef GMQCC_HDR
 #define GMQCC_HDR
 #include <stdarg.h>
-#include <time.h>  /* TODO: remove this? */
 #include <stdio.h> /* 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 =============================*/
 /*===================================================================*/
index 49de9529fd514b64053b16e93522ce8d94b60fe3..ae6edaa4df446cb837147e0505518c48ecb8cd64 100644 (file)
--- a/intrin.c
+++ b/intrin.c
@@ -21,7 +21,9 @@
  * SOFTWARE.
  */
 #include <string.h>
+
 #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 1da5d7cd6e87f32b3e97b92e3930bb2c522bae86..8fa821410c9287b45aaea22feea295d1d2516b68 100644 (file)
--- 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 2d36afbc8f639d83cb52202f16f9dd9320701841..03c27d1aea8f4e9620cb2219c500d65e426344a2 100644 (file)
--- 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 570b5789116f68cc9b435e5e0d30d5b9c809e880..fd10233ef0e1d2f2a5812dfff332cf7f252ed53b 100644 (file)
--- a/opts.c
+++ b/opts.c
@@ -25,6 +25,7 @@
 #include <stdlib.h>
 
 #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 fbc22629a51424444fe24bac99e2abaacd42f83b..ba820ef2f98b9853a47d3a1c4fc81084bee1279a 100644 (file)
--- a/pak.c
+++ b/pak.c
@@ -24,6 +24,7 @@
 #include <stdlib.h>
 
 #include "gmqcc.h"
+#include "platform.h"
 
 /*
  * The PAK format uses a FOURCC concept for storing the magic ident within
index 47563f3b3d2b2c19b0ee5dc5a82fa9d2f983d869..d913567d5e6db25d78cf1407bf00b9ee96f4255a 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -23,7 +23,9 @@
  */
 #include <string.h>
 #include <math.h>
+
 #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 (file)
index 0000000..db76edd
--- /dev/null
@@ -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 <stddef.h>
+#include <stdarg.h>
+#include <time.h>
+#include <stdio.h>
+
+#ifndef _MSC_VER
+#   include <sys/types.h>
+#   include <sys/stat.h>
+#   include <dirent.h>
+#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 07ca9caf25400ef6ebcc55ec5fd2caea9c77364f..a0484befecb788ddd5f7507af9384e26f3e85945 100644 (file)
--- a/test.c
+++ b/test.c
  */
 #include <stdlib.h>
 #include <string.h>
-#include <sys/types.h>
-#include <sys/stat.h>
 
 #include "gmqcc.h"
+#include "platform.h"
 
 static const char *task_bins[] = {
     "./gmqcc",