Skip to content
Myr

make_readme.c

This file

Path

tools/make_readme.c

65 lines · 2.5 KiB · C

Last changed by

Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d

History of this file

Take it

1#define STB_DEFINE2#include "../stb.h"34int main(int argc, char  **argv)5{6   int i;7   int hlen, flen, listlen, total_lines = 0;8   char *header = stb_file("README.header.md", &hlen);      // stb_file - read file into malloc()ed buffer9   char *footer = stb_file("README.footer.md", &flen);      // stb_file - read file into malloc()ed buffer10   char **list  = stb_stringfile("README.list", &listlen);  // stb_stringfile - read file lines into malloced array of strings1112   FILE *f = fopen("../README.md", "wb");1314   fprintf(f, "<!---   THIS FILE IS AUTOMATICALLY GENERATED, DO NOT CHANGE IT BY HAND   --->\r\n\r\n");15   fwrite(header, 1, hlen, f);1617   for (i=0; i < listlen; ++i) {18      int num,j;19      char **tokens = stb_tokens_stripwhite(list[i], "|", &num);  // stb_tokens -- tokenize string into malloced array of strings20      int num_lines;21      char **lines = stb_stringfile(stb_sprintf("../%s", tokens[0]), &num_lines);22      char *s1, *s2,*s3;23      if (lines == NULL) stb_fatal("Couldn't open '%s'", tokens[0]);24      s1 = strchr(lines[0], '-');25      if (!s1) stb_fatal("Couldn't find '-' before version number in %s", tokens[0]); // stb_fatal -- print error message & exit26      s2 = strchr(s1+2, '-');27      if (!s2) stb_fatal("Couldn't find '-' after version number in %s", tokens[0]);  // stb_fatal -- print error message & exit28      *s2 = 0;29      s1 += 1;30      s1 = stb_trimwhite(s1);                  // stb_trimwhite -- advance pointer to after whitespace & delete trailing whitespace31      if (*s1 == 'v') ++s1;32      s3 = tokens[0];33      stb_trimwhite(s3);34      fprintf(f, "**[");35      if (strlen(s3) < 21) {36         fprintf(f, "%s", tokens[0]);37      } else {38         char buffer[256];39         strncpy(buffer, s3, 18);40         buffer[18] = 0;   41         strcat(buffer, "...");42         fprintf(f, "%s", buffer);43      }44      fprintf(f, "](%s)**", tokens[0]);45      fprintf(f, " | %s", s1);46      s1 = stb_trimwhite(tokens[1]);           // stb_trimwhite -- advance pointer to after whitespace & delete trailing whitespace47      s2 = stb_dupreplace(s1, " ", "&nbsp;");  // stb_dupreplace -- search & replace string and malloc result48      fprintf(f, " | %s", s2);49      free(s2);50      fprintf(f, " | %d", num_lines);51      total_lines += num_lines;52      for (j=2; j < num; ++j)53         fprintf(f, " | %s", tokens[j]);54      fprintf(f, "\r\n");55   }5657   fprintf(f, "\r\n");58   fprintf(f, "Total libraries: %d\r\n", listlen);59   fprintf(f, "Total lines of C code: %d\r\n\r\n", total_lines);6061   fwrite(footer, 1, flen, f);62   fclose(f);6364   return 0;65}