Advertisement
2607

cipher

Sep 30th, 2021
989
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <dirent.h>
  5.  
  6. void writeFile(char *fPath, char *inputString, int *cc, char *fflag) {
  7.     FILE *tFile;
  8.     tFile = fopen(fPath, "r");
  9.     if (tFile) {
  10.         fclose(tFile);
  11.         tFile = fopen(fPath, fflag);
  12.         fprintf(tFile, "%s", inputString);
  13.     } else {
  14.         *cc = 1;
  15.         printf("n/a\n");
  16.     }
  17.     if (tFile) fclose(tFile);
  18. }
  19.  
  20. void getCountBytesInFile(char *fPath, long *fLength, int *er) {
  21.     FILE * tFile;
  22.     tFile = fopen(fPath, "rb");
  23.     if (!tFile) {
  24.         *er = 1;
  25.     } else {
  26.         fseek(tFile, 0, SEEK_END);
  27.         *fLength = ftell(tFile);
  28.         fclose(tFile);
  29.     }
  30. }
  31.  
  32. void read_n_Print_File(char *fPath, long *fLength) {
  33.     if (fLength != 0) {
  34.         FILE *tFile;
  35.         tFile = fopen(fPath, "rb");
  36.         printf("f");
  37.         if (tFile) {
  38.             int symbol;
  39.             while ( (symbol = fgetc(tFile) ) != EOF ) {
  40.                 putchar(symbol);
  41.             }
  42.             printf("\n");
  43.         }
  44.         if (tFile) fclose(tFile);
  45.     } else {
  46.         printf("n/a");
  47.     }
  48. }
  49.  
  50. char *getString() {
  51.     char *fPath = NULL;
  52.     char c;
  53.     int len = 0;
  54.     fPath = (char*)malloc(sizeof(char));
  55.     while ((c = getchar()) != '\n') {
  56.         fPath[len] = c;
  57.         len++;
  58.         char *tmp = (char*)realloc(fPath, len);
  59.         if (NULL != tmp) {
  60.             fPath = tmp;
  61.         }
  62.     }
  63.     fPath[len] = '\0';
  64.     return fPath;
  65. }
  66.  
  67. char *get_filename_ext(const char *filename) {
  68.     char *dot = strrchr(filename, '.');
  69.     if (!dot || dot == filename) return "";
  70.     return dot + 1;
  71. }
  72.  
  73. void toCaesar(char *concat, int shift) {
  74.     FILE *tFile;
  75.     char aOut[100];
  76.     tFile = fopen(concat, "r");
  77.     int symbol;
  78.     int i = 0; int cc = 0;
  79.     while ( (symbol = fgetc(tFile) ) != EOF ) {
  80.         if (symbol >= 65 && symbol <= 90) {
  81.             symbol = symbol + (shift % 26);
  82.             if (symbol > 90) symbol = 65 + (symbol - 90) - 1;
  83.             aOut[i] = (char)(symbol);
  84.         } else if (symbol >= 97 && symbol <= 122) {
  85.             symbol = symbol + (shift % 26);
  86.             if (symbol > 122) symbol = 97 + (symbol - 122) - 1;
  87.             aOut[i] = (char)(symbol);
  88.         } else if (symbol >= 33 && symbol <= 64) {
  89.             symbol = symbol + (shift % 32);
  90.             if (symbol > 64) symbol = 33 + (symbol - 64) - 1;
  91.             aOut[i] = (char)(symbol);
  92.         } else if (symbol >= 123 && symbol <= 126) {
  93.             symbol = symbol + (shift % 4);
  94.             if (symbol > 126) symbol = 123 + (symbol - 126) - 1;
  95.             aOut[i] = (char)(symbol);
  96.         } else if (symbol >= 192 && symbol <= 223) {
  97.             symbol = symbol + (shift % 32);
  98.             if (symbol > 223) symbol = 192 + (symbol - 223) - 1;
  99.             aOut[i] = (char)(symbol);
  100.         } else if (symbol >= 224 && symbol <= 255) {
  101.             symbol = symbol + (shift % 32);
  102.             if (symbol > 255) symbol = 224 + (symbol - 255) - 1;
  103.             aOut[i] = (char)(symbol);
  104.         } else {
  105.             aOut[i] = (char)(symbol);
  106.         }
  107.         i++;
  108.     }
  109.     fclose(tFile);
  110.     char *fflag = "w";
  111.     writeFile(concat, aOut, &cc, fflag);
  112. }
  113.  
  114. void quest3() {
  115.     DIR *dir;
  116.     struct dirent *de;
  117.     char *fPath = NULL;
  118.  
  119.     fPath = getString();
  120.     dir = opendir(fPath);
  121.     if (dir) {
  122.         int shift;
  123.         scanf("%d", &shift);
  124.         if (getchar() != '\n') {
  125.             printf("n/a\n");
  126.         } else {
  127.             while (dir) {
  128.                 de = readdir(dir);
  129.                 if (!de) break;
  130.                 char *fName = de->d_name;
  131.                 char *ext = get_filename_ext(fName);
  132.                 char concat[512];
  133.                 snprintf(concat, sizeof concat, "%s/%s", fPath, fName);
  134.                 if (strcmp(ext, "h") == 0) {
  135.                     FILE *tFile;
  136.                     tFile = fopen(concat, "w");
  137.                     fclose(tFile);
  138.                 } else if (strcmp(ext, "c") == 0) {
  139.                     toCaesar(concat, shift);
  140.                 }
  141.             }
  142.         }
  143.     closedir(dir);
  144.     } else {
  145.         printf("n/a\n");
  146.     }
  147.     if (NULL != fPath) free(fPath);
  148. }
  149.  
  150. int main() {
  151.     char *fPath = NULL;
  152.     char *fTemp = NULL;
  153.     char *inputString = NULL;
  154.     int er = 0;
  155.     int n;
  156.     char charTest;
  157.     int menu_item = 0;
  158.  
  159.     while (menu_item != -1) {
  160.         clearerr(stdin);
  161.         if ((scanf("%d%c", &n, &charTest) == 2) &&
  162.             (n == -1) && charTest == '\n') {
  163.             menu_item = -1;
  164.         } else if (n == 1 && charTest == '\n') {
  165.             fPath = getString();
  166.             long fLength = 0;
  167.             getCountBytesInFile(fPath, &fLength, &er);
  168.             if ((strlen(fPath) == 0) || (er == 1) || fLength == 0) {
  169.                 printf("n/a\n");
  170.                 er = 0;
  171.             } else {
  172.                 read_n_Print_File(fPath, &fLength);
  173.             }
  174.             fTemp = fPath;
  175.             if (NULL != fPath) free(fPath);
  176.         } else if (n == 2 && charTest == '\n') {
  177.             inputString = getString();
  178.             int cc = 0;
  179.             if (fTemp) {
  180.                 char *fflag = "a";
  181.                 writeFile(fTemp, inputString, &cc, fflag);
  182.             }
  183.             if (inputString) {
  184.                 if (fTemp) {
  185.                     long fLength = 0;
  186.                     getCountBytesInFile(fTemp, &fLength, &er);
  187.                     read_n_Print_File(fTemp, &fLength);
  188.                 } else if (cc != 1) {
  189.                     printf("n/a\n");
  190.                 }
  191.             }
  192.             if (NULL != inputString) free(inputString);
  193.         } else if (n == 3 && charTest == '\n') {
  194.             quest3();
  195.         } else if (EOF) {
  196.             if (NULL != inputString) free(inputString);
  197.             clearerr(stdin);
  198.             fflush(NULL);
  199.         } else {
  200.             printf("n/a\n");
  201.             fflush(NULL);
  202.             if (NULL != fTemp) free(fTemp);
  203.         }
  204.     }
  205.     return 0;
  206. }
  207.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement