Advertisement
BenTibnam

Generate Header

Sep 6th, 2023 (edited)
751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.34 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #define YEAR_START 1900
  6.  
  7. #ifdef _WIN32
  8.         #define WINDOWS 1
  9. #else
  10.         #define WINDOWS 0
  11. #endif
  12.  
  13. typedef char* string;
  14.  
  15. int main(int argc, string argv[])
  16. {
  17.         // getting the current date
  18.         time_t t = time(NULL);
  19.         struct tm date = *localtime(&t);
  20.  
  21.         string months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
  22.         string month = months[date.tm_mon];
  23.         int day = date.tm_mday;
  24.         int year = date.tm_year + YEAR_START;
  25.         char cd = '*';
  26.  
  27.                 // checking to make sure usage is correct
  28.         if (argc <  2 && argc > 3) {
  29.                 printf("Usage: ./genheader filename.c <-c or -m, default=-c>\n");
  30.                 return 1;
  31.         }
  32.  
  33.         // selecting the right comment delimeter for the file type
  34.         if (argc == 3 && strlen(argv[2]) == 2)
  35.         {
  36.                 char user_option = argv[2][1];
  37.  
  38.                 switch (user_option)
  39.                 {
  40.                         case 'c':
  41.                                 cd = '*';
  42.                                 break;
  43.                         case 'm':
  44.                                 cd = '#';
  45.                                 break;
  46.                         default:
  47.                                 break;
  48.                 }
  49.  
  50.         }
  51.  
  52.         string file_name = argv[1];
  53.         FILE *fout = fopen(file_name, "w");
  54.  
  55.         fprintf(fout, "/%c                         %s\n %c                           by Name\n %c                             %s %i, %i\n %c\n %c   Program Assignment: P##\n %c   Due Date:   \n %c   Program Description\n %c/\n\n#include <stdio.h>\n\n/%c\n %c\t\tMain Function\n %c/\nint main(void)\n{\n\n\treturn 0;\n}\n",
  56.                 cd, file_name, cd, cd,  month, day, year, cd, cd, cd, cd, cd, cd, cd, cd);
  57.  
  58.         fclose(fout);
  59.  
  60.         // if we're not running Windows creating a makefile, we use head and cut utilities to trim our file
  61.         if (!WINDOWS && cd == '#')
  62.         {
  63.                 char cmd[128];
  64.                 snprintf(cmd, 128," head -3 %s | cut -c 2- > %s", file_name, file_name);
  65.                 printf("Running: %s\n", cmd);
  66.                 system(cmd);
  67.         }
  68.  
  69.  
  70.  
  71.  
  72.         return 0;
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement