1 #define STB_DEFINE
2 #include "../stb.h"
3 
main(int argc,char ** argv)4 int 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 buffer
9    char *footer = stb_file("README.footer.md", &flen);      // stb_file - read file into malloc()ed buffer
10    char **list  = stb_stringfile("README.list", &listlen);  // stb_stringfile - read file lines into malloced array of strings
11 
12    FILE *f = fopen("../README.md", "wb");
13 
14    fprintf(f, "<!---   THIS FILE IS AUTOMATICALLY GENERATED, DO NOT CHANGE IT BY HAND   --->\n\n");
15    fwrite(header, 1, hlen, f);
16 
17    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 strings
20       int num_lines;
21       char **lines = stb_stringfile(stb_sprintf("../%s", tokens[0]), &num_lines);
22       char *s1, *s2,*s3;
23       s1 = strchr(lines[0], '-');
24       if (!s1) stb_fatal("Couldn't find '-' before version number in %s", tokens[0]); // stb_fatal -- print error message & exit
25       s2 = strchr(s1+2, '-');
26       if (!s2) stb_fatal("Couldn't find '-' after version number in %s", tokens[0]);  // stb_fatal -- print error message & exit
27       *s2 = 0;
28       s1 += 1;
29       s1 = stb_trimwhite(s1);                  // stb_trimwhite -- advance pointer to after whitespace & delete trailing whitespace
30       if (*s1 == 'v') ++s1;
31       s3 = tokens[0];
32       stb_trimwhite(s3);
33       fprintf(f, "**[");
34       if (strlen(s3) < 21) {
35          fprintf(f, "%s", tokens[0]);
36       } else {
37          char buffer[256];
38          strncpy(buffer, s3, 18);
39          buffer[18] = 0;
40          strcat(buffer, "...");
41          fprintf(f, "%s", buffer);
42       }
43       fprintf(f, "](%s)**", tokens[0]);
44       fprintf(f, " | %s", s1);
45       s1 = stb_trimwhite(tokens[1]);           // stb_trimwhite -- advance pointer to after whitespace & delete trailing whitespace
46       s2 = stb_dupreplace(s1, " ", "&nbsp;");  // stb_dupreplace -- search & replace string and malloc result
47       fprintf(f, " | %s", s2);
48       free(s2);
49       fprintf(f, " | %d", num_lines);
50       total_lines += num_lines;
51       for (j=2; j < num; ++j)
52          fprintf(f, " | %s", tokens[j]);
53       fprintf(f, "\n");
54    }
55 
56    fprintf(f, "\n");
57    fprintf(f, "Total libraries: %d  \n", listlen);
58    fprintf(f, "Total lines of C code: %d\n\n", total_lines);
59 
60    fwrite(footer, 1, flen, f);
61    fclose(f);
62 
63    return 0;
64 }
65