Advertisement
Guest User

Untitled

a guest
Jan 17th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.34 KB | None | 0 0
  1. /*
  2.  
  3.     17 Jan 2020
  4.  
  5.     Usage: spacestotabs <number> <file name>
  6.  
  7.     number: how many spaces will be converted into a tab
  8.  
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. int is_string_numerical(char *str)
  15. {
  16.     int len = strlen(str);
  17.     int i;
  18.     for (i = 0; i < len; ++i) {
  19.         if (!isdigit(str[i])) {
  20.             return 0;
  21.         }
  22.     }
  23.     return 1;
  24. }
  25.  
  26. void backupfile(char *filename)
  27. {
  28.     FILE *file;
  29.     FILE *backupfile;
  30.     char *backupfilename = strcat(filename, "_backup");
  31.  
  32.     file = fopen(filename, "r");
  33.     backupfile = fopen(backupfilename, "w");
  34.  
  35.     char *c = file;
  36.     while (*c != 'EOF') {
  37.         fputc(*c, backupfile);
  38.         ++c;
  39.     }
  40.  
  41.     fclose(backupfile);
  42.     fclose(file);
  43. }
  44.  
  45. int main(int argc, char *argv[])
  46. {
  47.     int howmanyspaces; // how many spaces will be converted into a tab
  48.  
  49.     if (argc < 2) {
  50.         printf("Error: too few arguments. \"spacestotabs -h\" for help.\n");
  51.     }
  52.  
  53.     if (argc > 3) {
  54.         printf("Warning: too many arguments. Arguments after the second one will be ignored. \"spacestotabs -h\" for help.\n");
  55.     }
  56.  
  57.     // Handle first argument
  58.     if (argv[1][0] == '-') {
  59.         switch (argv[1][1])
  60.         {
  61.         case 'h': // Help content
  62.             printf("\nUsage: spacestotabs <number> <file name>\n");
  63.             printf("\n<number> : Number of spaces that will be converted into a tab. Number has to be positive.\n\n");
  64.             return 0;
  65.         default: // Parameter not found, wrong parameter
  66.             printf("Error: wrong arguments. \"spacestotabs -h\" for help.\n");
  67.             return 1;
  68.         }
  69.     } else {
  70.         if (!is_string_numerical(argv[1])) {
  71.             printf("Error: wrong parameter. \"spacestotabs -h\" for help.\n");
  72.             return 1;
  73.         }
  74.  
  75.         howmanyspaces = atoi(argv[1]);
  76.  
  77.         if (howmanyspaces <= 0) {
  78.             printf("Error: number is too small. \"spacestotabs -h\" for help.\n");
  79.             return 1;
  80.         }
  81.     }
  82.  
  83.     const int SIZE = strlen(argv[2]) + 10; // TODO: we need only + 7 or 8
  84.     char filename[SIZE];
  85.     strcpy(filename, argv[2]);
  86.  
  87.     // Backup the file first
  88.     int iscorrectanswer = 0;
  89.     do {
  90.         printf("Would you like to backup your file before the operation? [Y / N] : ");
  91.        
  92.         char c = getchar();
  93.         switch (c)
  94.         {
  95.         case 'n':
  96.         case 'N':
  97.             iscorrectanswer = 1;
  98.             break;
  99.         case 'y':
  100.         case 'Y':
  101.             backupfile(filename);
  102.             printf("Backup is created in the same directory with the name %s_backup\n", filename);
  103.             iscorrectanswer = 1;
  104.             break;
  105.         default:
  106.             printf("Error: unknown input. Use letters y or n to answer.\n");
  107.         }
  108.     } while (!iscorrectanswer);
  109.  
  110.     // TODO : Do the operation on the original file, don't create a new one.   
  111.     char *outfilename = strcat(filename, "_out");
  112.  
  113.     FILE *file;
  114.     FILE *out;
  115.     file = fopen(filename, "r");
  116.     out = fopen(outfilename, "w");
  117.  
  118.     int previousspacecount = 0; // how many spaces has been read in a row
  119.     int writepointercooldown = 4; // TODO: Change this back to 'howmanyspaces' - 1
  120.  
  121.     char *read = file;
  122.     char *write = read - 4; // TODO: Change this back to 'howmanyspaces' - 1
  123.     while (*read != 'EOF') {
  124.         if (*read == ' ') {
  125.             ++previousspacecount;
  126.         } else {
  127.             previousspacecount = 0;
  128.         }
  129.  
  130.         if (!writepointercooldown) {
  131.             fputs(*read, out);
  132.         }
  133.  
  134.         if (previousspacecount == howmanyspaces) {
  135.             fputs('\t', write);
  136.             writepointercooldown = howmanyspaces;
  137.         }
  138.  
  139.         if (!writepointercooldown) {
  140.             ++write;
  141.         } else {
  142.             --writepointercooldown;
  143.         }
  144.  
  145.         ++read;
  146.     }
  147.  
  148.     fclose(file);
  149.     fclose(out);
  150.  
  151.     printf("Spaces have been converted to tabs.\n");
  152.  
  153.     return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement