Advertisement
Guest User

Untitled

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