Skip to content

Commit

Permalink
Added changing file extension on encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
Pietro228 committed Feb 12, 2021
1 parent 286a055 commit 643eab6
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 17 deletions.
77 changes: 63 additions & 14 deletions FileExplorer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -667,26 +671,21 @@ 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);
strcpy(file_url, lastDirFile); //Copies strings

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);
Expand All @@ -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);
Expand All @@ -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] = "";
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions FileExplorer.depend
Original file line number Diff line number Diff line change
@@ -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
<dirent.h>
<stdio.h>
<string.h>
Expand All @@ -10,14 +10,14 @@
<unistd.h>
"fe_functions.h"

1612801954 f:\c projets\fileexplorer\fe_functions.h
1613151339 f:\c projets\fileexplorer\fe_functions.h
<stdio.h>
<time.h>
<string.h>
<windows.h>
"colors.h"

1612780843 f:\c projets\fileexplorer\colors.h
1613136931 f:\c projets\fileexplorer\colors.h
<stdio.h>
<stdlib.h>
<windows.h>
Expand Down
Binary file modified bin/Debug/FileExplorer.exe
Binary file not shown.
Binary file modified bin/Release/FileExplorer.exe
Binary file not shown.
73 changes: 73 additions & 0 deletions fe_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Binary file modified obj/Debug/FileExplorer.o
Binary file not shown.
Binary file modified obj/Release/FileExplorer.o
Binary file not shown.

0 comments on commit 643eab6

Please sign in to comment.