Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <dirent.h>
- void writeFile(char *fPath, char *inputString, int *cc, char *fflag) {
- FILE *tFile;
- tFile = fopen(fPath, "r");
- if (tFile) {
- fclose(tFile);
- tFile = fopen(fPath, fflag);
- fprintf(tFile, "%s", inputString);
- } else {
- *cc = 1;
- printf("n/a\n");
- }
- if (tFile) fclose(tFile);
- }
- void getCountBytesInFile(char *fPath, long *fLength, int *er) {
- FILE * tFile;
- tFile = fopen(fPath, "rb");
- if (!tFile) {
- *er = 1;
- } else {
- fseek(tFile, 0, SEEK_END);
- *fLength = ftell(tFile);
- fclose(tFile);
- }
- }
- void read_n_Print_File(char *fPath, long *fLength) {
- if (fLength != 0) {
- FILE *tFile;
- tFile = fopen(fPath, "rb");
- printf("f");
- if (tFile) {
- int symbol;
- while ( (symbol = fgetc(tFile) ) != EOF ) {
- putchar(symbol);
- }
- printf("\n");
- }
- if (tFile) fclose(tFile);
- } else {
- printf("n/a");
- }
- }
- char *getString() {
- char *fPath = NULL;
- char c;
- int len = 0;
- fPath = (char*)malloc(sizeof(char));
- while ((c = getchar()) != '\n') {
- fPath[len] = c;
- len++;
- char *tmp = (char*)realloc(fPath, len);
- if (NULL != tmp) {
- fPath = tmp;
- }
- }
- fPath[len] = '\0';
- return fPath;
- }
- char *get_filename_ext(const char *filename) {
- char *dot = strrchr(filename, '.');
- if (!dot || dot == filename) return "";
- return dot + 1;
- }
- void toCaesar(char *concat, int shift) {
- FILE *tFile;
- char aOut[100];
- tFile = fopen(concat, "r");
- int symbol;
- int i = 0; int cc = 0;
- while ( (symbol = fgetc(tFile) ) != EOF ) {
- if (symbol >= 65 && symbol <= 90) {
- symbol = symbol + (shift % 26);
- if (symbol > 90) symbol = 65 + (symbol - 90) - 1;
- aOut[i] = (char)(symbol);
- } else if (symbol >= 97 && symbol <= 122) {
- symbol = symbol + (shift % 26);
- if (symbol > 122) symbol = 97 + (symbol - 122) - 1;
- aOut[i] = (char)(symbol);
- } else if (symbol >= 33 && symbol <= 64) {
- symbol = symbol + (shift % 32);
- if (symbol > 64) symbol = 33 + (symbol - 64) - 1;
- aOut[i] = (char)(symbol);
- } else if (symbol >= 123 && symbol <= 126) {
- symbol = symbol + (shift % 4);
- if (symbol > 126) symbol = 123 + (symbol - 126) - 1;
- aOut[i] = (char)(symbol);
- } else if (symbol >= 192 && symbol <= 223) {
- symbol = symbol + (shift % 32);
- if (symbol > 223) symbol = 192 + (symbol - 223) - 1;
- aOut[i] = (char)(symbol);
- } else if (symbol >= 224 && symbol <= 255) {
- symbol = symbol + (shift % 32);
- if (symbol > 255) symbol = 224 + (symbol - 255) - 1;
- aOut[i] = (char)(symbol);
- } else {
- aOut[i] = (char)(symbol);
- }
- i++;
- }
- fclose(tFile);
- char *fflag = "w";
- writeFile(concat, aOut, &cc, fflag);
- }
- void quest3() {
- DIR *dir;
- struct dirent *de;
- char *fPath = NULL;
- fPath = getString();
- dir = opendir(fPath);
- if (dir) {
- int shift;
- scanf("%d", &shift);
- if (getchar() != '\n') {
- printf("n/a\n");
- } else {
- while (dir) {
- de = readdir(dir);
- if (!de) break;
- char *fName = de->d_name;
- char *ext = get_filename_ext(fName);
- char concat[512];
- snprintf(concat, sizeof concat, "%s/%s", fPath, fName);
- if (strcmp(ext, "h") == 0) {
- FILE *tFile;
- tFile = fopen(concat, "w");
- fclose(tFile);
- } else if (strcmp(ext, "c") == 0) {
- toCaesar(concat, shift);
- }
- }
- }
- closedir(dir);
- } else {
- printf("n/a\n");
- }
- if (NULL != fPath) free(fPath);
- }
- int main() {
- char *fPath = NULL;
- char *fTemp = NULL;
- char *inputString = NULL;
- int er = 0;
- int n;
- char charTest;
- int menu_item = 0;
- while (menu_item != -1) {
- clearerr(stdin);
- if ((scanf("%d%c", &n, &charTest) == 2) &&
- (n == -1) && charTest == '\n') {
- menu_item = -1;
- } else if (n == 1 && charTest == '\n') {
- fPath = getString();
- long fLength = 0;
- getCountBytesInFile(fPath, &fLength, &er);
- if ((strlen(fPath) == 0) || (er == 1) || fLength == 0) {
- printf("n/a\n");
- er = 0;
- } else {
- read_n_Print_File(fPath, &fLength);
- }
- fTemp = fPath;
- if (NULL != fPath) free(fPath);
- } else if (n == 2 && charTest == '\n') {
- inputString = getString();
- int cc = 0;
- if (fTemp) {
- char *fflag = "a";
- writeFile(fTemp, inputString, &cc, fflag);
- }
- if (inputString) {
- if (fTemp) {
- long fLength = 0;
- getCountBytesInFile(fTemp, &fLength, &er);
- read_n_Print_File(fTemp, &fLength);
- } else if (cc != 1) {
- printf("n/a\n");
- }
- }
- if (NULL != inputString) free(inputString);
- } else if (n == 3 && charTest == '\n') {
- quest3();
- } else if (EOF) {
- if (NULL != inputString) free(inputString);
- clearerr(stdin);
- fflush(NULL);
- } else {
- printf("n/a\n");
- fflush(NULL);
- if (NULL != fTemp) free(fTemp);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement