diff --git a/FileExplorer.c b/FileExplorer.c index 92599b3..8254b31 100644 --- a/FileExplorer.c +++ b/FileExplorer.c @@ -67,6 +67,10 @@ int main(void) { printf("\n-----------------------------"); text_color(COLOR_WHITE); + chdir("F:/dokumenty/vyhry2.txt"); + + printf("%s",getcwd(cwd, 1000)); + while(action!=17) { if(strcmp(lastDir, "") == 0){ printf("\n\n\nLast opened directory: You do not have any opened directory.\n\nWhat do you want to do?\n\n\t1. Open directory\n\t2. Create directory\n\t3. Copy directory (coming soon)\n\t4. Delete directory\n\t5. Open file\n\t6. Create file\n\t7. Copy file\n\t"); @@ -667,9 +671,14 @@ int main(void) { if(command(file_url, lastDir) != 0)goto aborting_encrypting; //Checks for commands + if(file_url[strlen(file_url)-3] != 't' && file_url[strlen(file_url)-2] != 'x' && file_url[strlen(file_url)-1] != 't') { + text_color(COLOR_RED); + printf("\nFile '%s' is not a text file.\n",file_url); + text_color(COLOR_WHITE); + } else { + char fileenstr[5000] = ""; char enstr[5000] = ""; - char fileenname[1000] = ""; strcpy(lastDirFile, lastDir); strcat(lastDirFile, file_url); @@ -677,16 +686,6 @@ int main(void) { file = fopen(file_url, "r"); - /* - Trying to change file extension - - chdir(file_url); //Sets current working dir - //getcwd(cwd, 1000) - - strcpy(fileenname, getcwd(cwd, 1000)); - strcat - */ - if(file == NULL) { //Error handler text_color(COLOR_RED); printf("\nUnable to find file '%s'.\n",file_url); @@ -709,6 +708,27 @@ int main(void) { text_color(COLOR_GREEN); printf("\nFile '%s' has been encrypted.",file_url); text_color(COLOR_WHITE); + + char newFileEnName[1000] = ""; + + strcpy(newFileEnName, str_replace(file_url, get_filename_from_path(file_url), "")); + strcat(newFileEnName, get_filename_from_path(file_remove_extension(file_url))); + strcat(newFileEnName, ".encf"); + + renameResult = rename(file_url, newFileEnName); //Changing file name + + if(renameResult == 0) { + text_color(COLOR_GREEN); + printf("\nFile has been renamed to '%s'.",get_filename_from_path(newFileEnName)); + text_color(COLOR_WHITE); + } else { + text_color(COLOR_RED); + printf("\nFile has not been renamed to '%s'.",get_filename_from_path(newFileEnName)); + text_color(COLOR_WHITE); + } + + } + } text_color(COLOR_YELLOW); @@ -725,7 +745,13 @@ int main(void) { scanf("%d",&file_url); gets(file_url); - if(command(file_url, lastDir) != 0)goto aborting_decrypting; //Checks for commands + if(command(file_url, lastDir) != 0)goto aborting_decrypting; //Checks for commands enc + + if(file_url[strlen(file_url)-4] != 'e' && file_url[strlen(file_url)-3] != 'n' && file_url[strlen(file_url)-2] != 'c' && file_url[strlen(file_url)-1] != 'f') { + text_color(COLOR_RED); + printf("\nFile '%s' is not encrypted.\n",file_url); + text_color(COLOR_WHITE); + } else { char filedestr[5000] = ""; char destr[5000] = ""; @@ -736,6 +762,8 @@ int main(void) { file = fopen(file_url, "r"); + + if(file == NULL) { //Error handler text_color(COLOR_RED); printf("\nUnable to find file '%s'.\n",file_url); @@ -756,8 +784,29 @@ int main(void) { fclose(file); text_color(COLOR_GREEN); - printf("\nFile '%s' has been encrypted.",file_url); + printf("\nFile '%s' has been decrypted.",file_url); + text_color(COLOR_WHITE); + + char newFileEnName[1000] = ""; + + strcpy(newFileEnName, str_replace(file_url, get_filename_from_path(file_url), "")); + strcat(newFileEnName, get_filename_from_path(file_remove_extension(file_url))); + strcat(newFileEnName, ".txt"); + + renameResult = rename(file_url, newFileEnName); //Changing file name + + if(renameResult == 0) { + text_color(COLOR_GREEN); + printf("\nFile has been renamed to '%s'.",get_filename_from_path(newFileEnName)); + text_color(COLOR_WHITE); + } else { + text_color(COLOR_RED); + printf("\nFile has not been renamed to '%s'.",get_filename_from_path(newFileEnName)); text_color(COLOR_WHITE); + } + + } + } text_color(COLOR_YELLOW); @@ -770,7 +819,7 @@ int main(void) { break; case 15: //About - printf("\n\n\nFile explorer 1.1.0\nThis application is under APACHE LICENSE 2.0 - \"./LICENSE.md\"\nPetr Pavlik 2021 - BeXCool\n\nWeb: bexcool.eu\nEmail: bxc@post.cz"); + printf("\n\n\nFile explorer 1.1.1\nThis application is under APACHE LICENSE 2.0 - \"./LICENSE.md\"\nPetr Pavlik 2021 - BeXCool\n\nWeb: bexcool.eu\nEmail: bxc@post.cz"); text_color(COLOR_YELLOW); printf("\n\nPress any key to continue."); text_color(COLOR_WHITE); diff --git a/FileExplorer.depend b/FileExplorer.depend index 7afa5a0..783e1cf 100644 --- a/FileExplorer.depend +++ b/FileExplorer.depend @@ -1,5 +1,5 @@ # depslib dependency file v1.0 -1613038148 source:f:\c projets\fileexplorer\fileexplorer.c +1613151976 source:f:\c projets\fileexplorer\fileexplorer.c @@ -10,14 +10,14 @@ "fe_functions.h" -1612801954 f:\c projets\fileexplorer\fe_functions.h +1613151339 f:\c projets\fileexplorer\fe_functions.h "colors.h" -1612780843 f:\c projets\fileexplorer\colors.h +1613136931 f:\c projets\fileexplorer\colors.h diff --git a/bin/Debug/FileExplorer.exe b/bin/Debug/FileExplorer.exe index e3241f2..a5a91bc 100644 Binary files a/bin/Debug/FileExplorer.exe and b/bin/Debug/FileExplorer.exe differ diff --git a/bin/Release/FileExplorer.exe b/bin/Release/FileExplorer.exe index 502efe5..537dcbd 100644 Binary files a/bin/Release/FileExplorer.exe and b/bin/Release/FileExplorer.exe differ diff --git a/fe_functions.h b/fe_functions.h index 651ca91..26305b6 100644 --- a/fe_functions.h +++ b/fe_functions.h @@ -91,3 +91,76 @@ int remove_directory(const char *path) { return r; } + +char* get_fileextension_from_path(char* filepath) { + + char *filename = (char*)calloc(1, sizeof(filepath)); + filename = (strrchr(filepath, '.'))+1; + //printf("fileextension: %s\n", filename); + + return filename; + +} + +char* get_filename_from_path(char* filepath) { + + char *filename = (char*)calloc(1, sizeof(filepath)); + filename = (strrchr(filepath, '/'))+1; + //printf("filename: %s\n", filename); + + return filename; + +} + +char *file_remove_extension(char* myStr) { + char *retStr; + char *lastExt; + if (myStr == NULL) return NULL; + if ((retStr = malloc (strlen (myStr) + 1)) == NULL) return NULL; + strcpy (retStr, myStr); + lastExt = strrchr (retStr, '.'); + if (lastExt != NULL) + *lastExt = '\0'; + return retStr; +} + +char *str_replace(char *orig, char *rep, char *with) { + char *result; // the return string + char *ins; // the next insert point + char *tmp; // varies + int len_rep; // length of rep (the string to remove) + int len_with; // length of with (the string to replace rep with) + int len_front; // distance between rep and end of last rep + int count; // number of replacements + + // sanity checks and initialization + if (!orig || !rep) + return NULL; + len_rep = strlen(rep); + if (len_rep == 0) + return NULL; // empty rep causes infinite loop during count + if (!with) + with = ""; + len_with = strlen(with); + + // count the number of replacements needed + ins = orig; + for (count = 0; tmp = strstr(ins, rep); ++count) { + ins = tmp + len_rep; + } + + tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1); + + if (!result) + return NULL; + + while (count--) { + ins = strstr(orig, rep); + len_front = ins - orig; + tmp = strncpy(tmp, orig, len_front) + len_front; + tmp = strcpy(tmp, with) + len_with; + orig += len_front + len_rep; // move to next "end of rep" + } + strcpy(tmp, orig); + return result; +} diff --git a/obj/Debug/FileExplorer.o b/obj/Debug/FileExplorer.o index aad1540..be99d3e 100644 Binary files a/obj/Debug/FileExplorer.o and b/obj/Debug/FileExplorer.o differ diff --git a/obj/Release/FileExplorer.o b/obj/Release/FileExplorer.o index ace120a..3b9f70d 100644 Binary files a/obj/Release/FileExplorer.o and b/obj/Release/FileExplorer.o differ