Skip to content
Myr

stb_include.h

This file

Path

stb_include.h

295 lines · 9.0 KiB · C

Last changed by

Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a

History of this file

Take it

1// stb_include.h - v0.02 - parse and process #include directives - public domain2//3// To build this, in one source file that includes this file do4//      #define STB_INCLUDE_IMPLEMENTATION5//6// This program parses a string and replaces lines of the form7//         #include "foo"8// with the contents of a file named "foo". It also embeds the9// appropriate #line directives. Note that all include files must10// reside in the location specified in the path passed to the API;11// it does not check multiple directories.12//13// If the string contains a line of the form14//         #inject15// then it will be replaced with the contents of the string 'inject' passed to the API.16//17// Options:18//19//      Define STB_INCLUDE_LINE_GLSL to get GLSL-style #line directives20//      which use numbers instead of filenames.21//22//      Define STB_INCLUDE_LINE_NONE to disable output of #line directives.23//24// Standard libraries:25//26//      stdio.h     FILE, fopen, fclose, fseek, ftell27//      stdlib.h    malloc, realloc, free28//      string.h    strcpy, strncmp, memcpy29//30// Credits:31//32// Written by Sean Barrett.33//34// Fixes:35//  Michal Klos3637#ifndef STB_INCLUDE_STB_INCLUDE_H38#define STB_INCLUDE_STB_INCLUDE_H3940// Do include-processing on the string 'str'. To free the return value, pass it to free()41char *stb_include_string(char *str, char *inject, char *path_to_includes, char *filename_for_line_directive, char error[256]);4243// Concatenate the strings 'strs' and do include-processing on the result. To free the return value, pass it to free()44char *stb_include_strings(char **strs, int count, char *inject, char *path_to_includes, char *filename_for_line_directive, char error[256]);4546// Load the file 'filename' and do include-processing on the string therein. note that47// 'filename' is opened directly; 'path_to_includes' is not used. To free the return value, pass it to free()48char *stb_include_file(char *filename, char *inject, char *path_to_includes, char error[256]);4950#endif515253#ifdef STB_INCLUDE_IMPLEMENTATION5455#include <stdio.h>56#include <stdlib.h>57#include <string.h>5859static char *stb_include_load_file(char *filename, size_t *plen)60{61   char *text;62   size_t len;63   FILE *f = fopen(filename, "rb");64   if (f == 0) return 0;65   fseek(f, 0, SEEK_END);66   len = (size_t) ftell(f);67   if (plen) *plen = len;68   text = (char *) malloc(len+1);69   if (text == 0) return 0;70   fseek(f, 0, SEEK_SET);71   fread(text, 1, len, f);72   fclose(f);73   text[len] = 0;74   return text;75}7677typedef struct78{79   int offset;80   int end;81   char *filename;82   int next_line_after;83} include_info;8485static include_info *stb_include_append_include(include_info *array, int len, int offset, int end, char *filename, int next_line)86{87   include_info *z = (include_info *) realloc(array, sizeof(*z) * (len+1));88   z[len].offset   = offset;89   z[len].end      = end;90   z[len].filename = filename;91   z[len].next_line_after = next_line;92   return z;93}9495static void stb_include_free_includes(include_info *array, int len)96{97   int i;98   for (i=0; i < len; ++i)99      free(array[i].filename);100   free(array);101}102103static int stb_include_isspace(int ch)104{105   return (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n');106}107108// find location of all #include and #inject109static int stb_include_find_includes(char *text, include_info **plist)110{111   int line_count = 1;112   int inc_count = 0;113   char *s = text, *start;114   include_info *list = NULL;115   while (*s) {116      // parse is always at start of line when we reach here117      start = s;118      while (*s == ' ' || *s == '\t')119         ++s;120      if (*s == '#') {121         ++s;122         while (*s == ' ' || *s == '\t')123            ++s;124         if (0==strncmp(s, "include", 7) && stb_include_isspace(s[7])) {125            s += 7;126            while (*s == ' ' || *s == '\t')127               ++s;128            if (*s == '"') {129               char *t = ++s;130               while (*t != '"' && *t != '\n' && *t != '\r' && *t != 0)131                  ++t;132               if (*t == '"') {133                  char *filename = (char *) malloc(t-s+1);134                  memcpy(filename, s, t-s);135                  filename[t-s] = 0;136                  s=t;137                  while (*s != '\r' && *s != '\n' && *s != 0)138                     ++s;139                  // s points to the newline, so s-start is everything except the newline140                  list = stb_include_append_include(list, inc_count++, start-text, s-text, filename, line_count+1);141               }142            }143         } else if (0==strncmp(s, "inject", 6) && (stb_include_isspace(s[6]) || s[6]==0)) {144            while (*s != '\r' && *s != '\n' && *s != 0)145               ++s;146            list = stb_include_append_include(list, inc_count++, start-text, s-text, NULL, line_count+1);147         }148      }149      while (*s != '\r' && *s != '\n' && *s != 0)150         ++s;151      if (*s == '\r' || *s == '\n') {152         s = s + (s[0] + s[1] == '\r' + '\n' ? 2 : 1);153      }154      ++line_count;155   }156   *plist = list;157   return inc_count;158}159160// avoid dependency on sprintf()161static void stb_include_itoa(char str[9], int n)162{163   int i;164   for (i=0; i < 8; ++i)165      str[i] = ' ';166   str[i] = 0;167168   for (i=1; i < 8; ++i) {169      str[7-i] = '0' + (n % 10);170      n /= 10;171      if (n == 0)172         break;173   }174}175176static char *stb_include_append(char *str, size_t *curlen, char *addstr, size_t addlen)177{178   str = (char *) realloc(str, *curlen + addlen);179   memcpy(str + *curlen, addstr, addlen);180   *curlen += addlen;181   return str;182}183184char *stb_include_string(char *str, char *inject, char *path_to_includes, char *filename, char error[256])185{186   char temp[4096];187   include_info *inc_list;188   int i, num = stb_include_find_includes(str, &inc_list);189   size_t source_len = strlen(str);190   char *text=0;191   size_t textlen=0, last=0;192   for (i=0; i < num; ++i) {193      text = stb_include_append(text, &textlen, str+last, inc_list[i].offset - last);194      // write out line directive for the include195      #ifndef STB_INCLUDE_LINE_NONE196      #ifdef STB_INCLUDE_LINE_GLSL197      if (textlen != 0)  // GLSL #version must appear first, so don't put a #line at the top198      #endif199      {200         strcpy(temp, "#line ");201         stb_include_itoa(temp+6, 1);202         strcat(temp, " ");203         #ifdef STB_INCLUDE_LINE_GLSL204         stb_include_itoa(temp+15, i+1);205         #else206         strcat(temp, "\"");207         if (inc_list[i].filename == 0)208            strcmp(temp, "INJECT");209         else210            strcat(temp, inc_list[i].filename);211         strcat(temp, "\"");212         #endif213         strcat(temp, "\n");214         text = stb_include_append(text, &textlen, temp, strlen(temp));215      }216      #endif217      if (inc_list[i].filename == 0) {218         if (inject != 0)219            text = stb_include_append(text, &textlen, inject, strlen(inject));220      } else {221         char *inc;222         strcpy(temp, path_to_includes);223         strcat(temp, "/");224         strcat(temp, inc_list[i].filename);225         inc = stb_include_file(temp, inject, path_to_includes, error);226         if (inc == NULL) {227            stb_include_free_includes(inc_list, num);228            return NULL;229         }230         text = stb_include_append(text, &textlen, inc, strlen(inc));231         free(inc);232      }233      // write out line directive234      #ifndef STB_INCLUDE_LINE_NONE235      strcpy(temp, "\n#line ");236      stb_include_itoa(temp+6, inc_list[i].next_line_after);237      strcat(temp, " ");238      #ifdef STB_INCLUDE_LINE_GLSL239      stb_include_itoa(temp+15, 0);240      #else241      strcat(temp, filename != 0 ? filename : "source-file");242      #endif243      text = stb_include_append(text, &textlen, temp, strlen(temp));244      // no newlines, because we kept the #include newlines, which will get appended next245      #endif246      last = inc_list[i].end;247   }248   text = stb_include_append(text, &textlen, str+last, source_len - last + 1); // append '\0'249   stb_include_free_includes(inc_list, num);250   return text;251}252253char *stb_include_strings(char **strs, int count, char *inject, char *path_to_includes, char *filename, char error[256])254{255   char *text;256   char *result;257   int i;258   size_t length=0;259   for (i=0; i < count; ++i)260      length += strlen(strs[i]);261   text = (char *) malloc(length+1);262   length = 0;263   for (i=0; i < count; ++i) {264      strcpy(text + length, strs[i]);265      length += strlen(strs[i]);266   }267   result = stb_include_string(text, inject, path_to_includes, filename, error);268   free(text);269   return result;270}271272char *stb_include_file(char *filename, char *inject, char *path_to_includes, char error[256])273{274   size_t len;275   char *result;276   char *text = stb_include_load_file(filename, &len);277   if (text == NULL) {278      strcpy(error, "Error: couldn't load '");279      strcat(error, filename);280      strcat(error, "'");281      return 0;282   }283   result = stb_include_string(text, inject, path_to_includes, filename, error);284   free(text);285   return result;286}287288#if 0 // @TODO, GL_ARB_shader_language_include-style system that doesn't touch filesystem289char *stb_include_preloaded(char *str, char *inject, char *includes[][2], char error[256])290{291292}293#endif294295#endif // STB_INCLUDE_IMPLEMENTATION