Fix major overstep with last commit
[vg.git] / dep / stb / stb_include.h
index c5db20138a23b0a760252960966c6720f5f05153..6a90097144265a958bfa25b9fcb8dadf8bfa3019 100644 (file)
@@ -53,9 +53,27 @@ char *stb_include_file(char *filename, char *inject, char *path_to_includes, cha
 #ifdef STB_INCLUDE_IMPLEMENTATION
 
 #include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
 
+#ifndef STB_MALLOC
+  #define STB_STDLIB
+  #define STB_MALLOC malloc
+#endif
+
+#ifndef STB_FREE
+  #define STB_STDLIB
+  #define STB_FREE free
+#endif
+
+#ifndef STB_REALLOC
+  #define STB_STDLIB
+  #define STB_REALLOC realloc
+#endif
+
+#ifdef STB_STDLIB
+  #include <stdlib.h>
+#endif
+
 static char *stb_include_load_file(char *filename, size_t *plen)
 {
    char *text;
@@ -65,7 +83,7 @@ static char *stb_include_load_file(char *filename, size_t *plen)
    fseek(f, 0, SEEK_END);
    len = (size_t) ftell(f);
    if (plen) *plen = len;
-   text = (char *) malloc(len+1);
+   text = (char *) STB_MALLOC(len+1);
    if (text == 0) return 0;
    fseek(f, 0, SEEK_SET);
    fread(text, 1, len, f);
@@ -84,7 +102,7 @@ typedef struct
 
 static include_info *stb_include_append_include(include_info *array, int len, int offset, int end, char *filename, int next_line)
 {
-   include_info *z = (include_info *) realloc(array, sizeof(*z) * (len+1));
+   include_info *z = (include_info *)STB_REALLOC(array, sizeof(*z) * (len+1));
    z[len].offset   = offset;
    z[len].end      = end;
    z[len].filename = filename;
@@ -96,8 +114,8 @@ static void stb_include_free_includes(include_info *array, int len)
 {
    int i;
    for (i=0; i < len; ++i)
-      free(array[i].filename);
-   free(array);
+      STB_FREE(array[i].filename);
+   STB_FREE(array);
 }
 
 static int stb_include_isspace(int ch)
@@ -130,7 +148,7 @@ static int stb_include_find_includes(char *text, include_info **plist)
                while (*t != '"' && *t != '\n' && *t != '\r' && *t != 0)
                   ++t;
                if (*t == '"') {
-                  char *filename = (char *) malloc(t-s+1);
+                  char *filename = (char *) STB_MALLOC(t-s+1);
                   memcpy(filename, s, t-s);
                   filename[t-s] = 0;
                   s=t;
@@ -175,7 +193,7 @@ static void stb_include_itoa(char str[9], int n)
 
 static char *stb_include_append(char *str, size_t *curlen, char *addstr, size_t addlen)
 {
-   str = (char *) realloc(str, *curlen + addlen);
+   str = (char *)STB_REALLOC(str, *curlen + addlen);
    memcpy(str + *curlen, addstr, addlen);
    *curlen += addlen;
    return str;
@@ -228,7 +246,7 @@ char *stb_include_string(char *str, char *inject, char *path_to_includes, char *
             return NULL;
          }
          text = stb_include_append(text, &textlen, inc, strlen(inc));
-         free(inc);
+         STB_FREE(inc);
       }
       // write out line directive
       #ifndef STB_INCLUDE_LINE_NONE
@@ -258,14 +276,14 @@ char *stb_include_strings(char **strs, int count, char *inject, char *path_to_in
    size_t length=0;
    for (i=0; i < count; ++i)
       length += strlen(strs[i]);
-   text = (char *) malloc(length+1);
+   text = (char *) STB_MALLOC(length+1);
    length = 0;
    for (i=0; i < count; ++i) {
       strcpy(text + length, strs[i]);
       length += strlen(strs[i]);
    }
    result = stb_include_string(text, inject, path_to_includes, filename, error);
-   free(text);
+   STB_FREE(text);
    return result;
 }
 
@@ -281,7 +299,7 @@ char *stb_include_file(char *filename, char *inject, char *path_to_includes, cha
       return 0;
    }
    result = stb_include_string(text, inject, path_to_includes, filename, error);
-   free(text);
+   STB_FREE(text);
    return result;
 }