Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- const char defineString[] = "#define";
- inline int isWhitespace (char* c) {
- return (*c == ' ' || *c == '\t');
- }
- int main (int argc, char* argv[]) {
- if (argc != 3) {
- fprintf (stderr, "usage: %s <in> <out>\n", argv[0]);
- return 0;
- }
- FILE* fp = fopen (argv[1], "r");
- FILE* fp2 = fopen (argv[2], "w");
- char text[1024];
- time_t t = time (NULL);
- fprintf (fp2, "// Auto-generated by %s at %s\n", argv[0], asctime (localtime (&t)));
- while (fgets (text, 1024, fp) > 0) {
- char name[64];
- char value[64];
- char* c = &text[0];
- size_t len;
- if (strncmp (text, defineString, strlen (defineString)) != 0)
- continue; // not a #define string
- c = text + strlen (defineString);
- while (*c && isWhitespace (c)) c++;
- if (!*c) continue;
- char* nameptr = c;
- while (*c && !isWhitespace (c)) c++;
- if (!*c) continue;
- len = c - nameptr;
- strncpy (name, nameptr, len);
- name[len] = '\0';
- while (*c && isWhitespace (c)) c++;
- if (!*c) continue;
- char* valueptr = c;
- while (*c && !isWhitespace (c) && *c != '\n') c++;
- len = c - valueptr;
- strncpy (value, valueptr, len);
- value[len] = '\0';
- fprintf (fp2, "const int %s = %d;\n", name, atoi (value));
- }
- fclose (fp);
- fclose (fp2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment