slatenails

Untitled

Jul 12th, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. const char defineString[] = "#define";
  7.  
  8. inline int isWhitespace (char* c) {
  9.     return (*c == ' ' || *c == '\t');
  10. }
  11.  
  12. int main (int argc, char* argv[]) {
  13.     if (argc != 3) {
  14.         fprintf (stderr, "usage: %s <in> <out>\n", argv[0]);
  15.         return 0;
  16.     }
  17.    
  18.     FILE* fp = fopen (argv[1], "r");
  19.     FILE* fp2 = fopen (argv[2], "w");
  20.     char text[1024];
  21.    
  22.     time_t t = time (NULL);
  23.     fprintf (fp2, "// Auto-generated by %s at %s\n", argv[0], asctime (localtime (&t)));
  24.    
  25.     while (fgets (text, 1024, fp) > 0) {
  26.         char name[64];
  27.         char value[64];
  28.         char* c = &text[0];
  29.         size_t len;
  30.        
  31.         if (strncmp (text, defineString, strlen (defineString)) != 0)
  32.             continue; // not a #define string
  33.        
  34.         c = text + strlen (defineString);
  35.        
  36.         while (*c && isWhitespace (c)) c++;
  37.         if (!*c) continue;
  38.        
  39.         char* nameptr = c;
  40.        
  41.         while (*c && !isWhitespace (c)) c++;
  42.         if (!*c) continue;
  43.        
  44.         len = c - nameptr;
  45.         strncpy (name, nameptr, len);
  46.         name[len] = '\0';
  47.        
  48.         while (*c && isWhitespace (c)) c++;
  49.         if (!*c) continue;
  50.        
  51.         char* valueptr = c;
  52.         while (*c && !isWhitespace (c) && *c != '\n') c++;
  53.         len = c - valueptr;
  54.         strncpy (value, valueptr, len);
  55.         value[len] = '\0';
  56.        
  57.         fprintf (fp2, "const int %s = %d;\n", name, atoi (value));
  58.     }
  59.    
  60.     fclose (fp);
  61.     fclose (fp2);
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment