Skip to content

Commit 643eab6

Browse files
committed
Added changing file extension on encryption
1 parent 286a055 commit 643eab6

7 files changed

Lines changed: 139 additions & 17 deletions

File tree

FileExplorer.c

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ int main(void) {
6767
printf("\n-----------------------------");
6868
text_color(COLOR_WHITE);
6969

70+
chdir("F:/dokumenty/vyhry2.txt");
71+
72+
printf("%s",getcwd(cwd, 1000));
73+
7074
while(action!=17) {
7175
if(strcmp(lastDir, "") == 0){
7276
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,26 +671,21 @@ int main(void) {
667671

668672
if(command(file_url, lastDir) != 0)goto aborting_encrypting; //Checks for commands
669673

674+
if(file_url[strlen(file_url)-3] != 't' && file_url[strlen(file_url)-2] != 'x' && file_url[strlen(file_url)-1] != 't') {
675+
text_color(COLOR_RED);
676+
printf("\nFile '%s' is not a text file.\n",file_url);
677+
text_color(COLOR_WHITE);
678+
} else {
679+
670680
char fileenstr[5000] = "";
671681
char enstr[5000] = "";
672-
char fileenname[1000] = "";
673682

674683
strcpy(lastDirFile, lastDir);
675684
strcat(lastDirFile, file_url);
676685
strcpy(file_url, lastDirFile); //Copies strings
677686

678687
file = fopen(file_url, "r");
679688

680-
/*
681-
Trying to change file extension
682-
683-
chdir(file_url); //Sets current working dir
684-
//getcwd(cwd, 1000)
685-
686-
strcpy(fileenname, getcwd(cwd, 1000));
687-
strcat
688-
*/
689-
690689
if(file == NULL) { //Error handler
691690
text_color(COLOR_RED);
692691
printf("\nUnable to find file '%s'.\n",file_url);
@@ -709,6 +708,27 @@ int main(void) {
709708
text_color(COLOR_GREEN);
710709
printf("\nFile '%s' has been encrypted.",file_url);
711710
text_color(COLOR_WHITE);
711+
712+
char newFileEnName[1000] = "";
713+
714+
strcpy(newFileEnName, str_replace(file_url, get_filename_from_path(file_url), ""));
715+
strcat(newFileEnName, get_filename_from_path(file_remove_extension(file_url)));
716+
strcat(newFileEnName, ".encf");
717+
718+
renameResult = rename(file_url, newFileEnName); //Changing file name
719+
720+
if(renameResult == 0) {
721+
text_color(COLOR_GREEN);
722+
printf("\nFile has been renamed to '%s'.",get_filename_from_path(newFileEnName));
723+
text_color(COLOR_WHITE);
724+
} else {
725+
text_color(COLOR_RED);
726+
printf("\nFile has not been renamed to '%s'.",get_filename_from_path(newFileEnName));
727+
text_color(COLOR_WHITE);
728+
}
729+
730+
}
731+
712732
}
713733

714734
text_color(COLOR_YELLOW);
@@ -725,7 +745,13 @@ int main(void) {
725745
scanf("%d",&file_url);
726746
gets(file_url);
727747

728-
if(command(file_url, lastDir) != 0)goto aborting_decrypting; //Checks for commands
748+
if(command(file_url, lastDir) != 0)goto aborting_decrypting; //Checks for commands enc
749+
750+
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') {
751+
text_color(COLOR_RED);
752+
printf("\nFile '%s' is not encrypted.\n",file_url);
753+
text_color(COLOR_WHITE);
754+
} else {
729755

730756
char filedestr[5000] = "";
731757
char destr[5000] = "";
@@ -736,6 +762,8 @@ int main(void) {
736762

737763
file = fopen(file_url, "r");
738764

765+
766+
739767
if(file == NULL) { //Error handler
740768
text_color(COLOR_RED);
741769
printf("\nUnable to find file '%s'.\n",file_url);
@@ -756,8 +784,29 @@ int main(void) {
756784
fclose(file);
757785

758786
text_color(COLOR_GREEN);
759-
printf("\nFile '%s' has been encrypted.",file_url);
787+
printf("\nFile '%s' has been decrypted.",file_url);
788+
text_color(COLOR_WHITE);
789+
790+
char newFileEnName[1000] = "";
791+
792+
strcpy(newFileEnName, str_replace(file_url, get_filename_from_path(file_url), ""));
793+
strcat(newFileEnName, get_filename_from_path(file_remove_extension(file_url)));
794+
strcat(newFileEnName, ".txt");
795+
796+
renameResult = rename(file_url, newFileEnName); //Changing file name
797+
798+
if(renameResult == 0) {
799+
text_color(COLOR_GREEN);
800+
printf("\nFile has been renamed to '%s'.",get_filename_from_path(newFileEnName));
801+
text_color(COLOR_WHITE);
802+
} else {
803+
text_color(COLOR_RED);
804+
printf("\nFile has not been renamed to '%s'.",get_filename_from_path(newFileEnName));
760805
text_color(COLOR_WHITE);
806+
}
807+
808+
}
809+
761810
}
762811

763812
text_color(COLOR_YELLOW);
@@ -770,7 +819,7 @@ int main(void) {
770819
break;
771820

772821
case 15: //About
773-
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");
822+
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");
774823
text_color(COLOR_YELLOW);
775824
printf("\n\nPress any key to continue.");
776825
text_color(COLOR_WHITE);

FileExplorer.depend

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# depslib dependency file v1.0
2-
1613038148 source:f:\c projets\fileexplorer\fileexplorer.c
2+
1613151976 source:f:\c projets\fileexplorer\fileexplorer.c
33
<dirent.h>
44
<stdio.h>
55
<string.h>
@@ -10,14 +10,14 @@
1010
<unistd.h>
1111
"fe_functions.h"
1212

13-
1612801954 f:\c projets\fileexplorer\fe_functions.h
13+
1613151339 f:\c projets\fileexplorer\fe_functions.h
1414
<stdio.h>
1515
<time.h>
1616
<string.h>
1717
<windows.h>
1818
"colors.h"
1919

20-
1612780843 f:\c projets\fileexplorer\colors.h
20+
1613136931 f:\c projets\fileexplorer\colors.h
2121
<stdio.h>
2222
<stdlib.h>
2323
<windows.h>

bin/Debug/FileExplorer.exe

3.5 KB
Binary file not shown.

bin/Release/FileExplorer.exe

1.5 KB
Binary file not shown.

fe_functions.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,76 @@ int remove_directory(const char *path) {
9191

9292
return r;
9393
}
94+
95+
char* get_fileextension_from_path(char* filepath) {
96+
97+
char *filename = (char*)calloc(1, sizeof(filepath));
98+
filename = (strrchr(filepath, '.'))+1;
99+
//printf("fileextension: %s\n", filename);
100+
101+
return filename;
102+
103+
}
104+
105+
char* get_filename_from_path(char* filepath) {
106+
107+
char *filename = (char*)calloc(1, sizeof(filepath));
108+
filename = (strrchr(filepath, '/'))+1;
109+
//printf("filename: %s\n", filename);
110+
111+
return filename;
112+
113+
}
114+
115+
char *file_remove_extension(char* myStr) {
116+
char *retStr;
117+
char *lastExt;
118+
if (myStr == NULL) return NULL;
119+
if ((retStr = malloc (strlen (myStr) + 1)) == NULL) return NULL;
120+
strcpy (retStr, myStr);
121+
lastExt = strrchr (retStr, '.');
122+
if (lastExt != NULL)
123+
*lastExt = '\0';
124+
return retStr;
125+
}
126+
127+
char *str_replace(char *orig, char *rep, char *with) {
128+
char *result; // the return string
129+
char *ins; // the next insert point
130+
char *tmp; // varies
131+
int len_rep; // length of rep (the string to remove)
132+
int len_with; // length of with (the string to replace rep with)
133+
int len_front; // distance between rep and end of last rep
134+
int count; // number of replacements
135+
136+
// sanity checks and initialization
137+
if (!orig || !rep)
138+
return NULL;
139+
len_rep = strlen(rep);
140+
if (len_rep == 0)
141+
return NULL; // empty rep causes infinite loop during count
142+
if (!with)
143+
with = "";
144+
len_with = strlen(with);
145+
146+
// count the number of replacements needed
147+
ins = orig;
148+
for (count = 0; tmp = strstr(ins, rep); ++count) {
149+
ins = tmp + len_rep;
150+
}
151+
152+
tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1);
153+
154+
if (!result)
155+
return NULL;
156+
157+
while (count--) {
158+
ins = strstr(orig, rep);
159+
len_front = ins - orig;
160+
tmp = strncpy(tmp, orig, len_front) + len_front;
161+
tmp = strcpy(tmp, with) + len_with;
162+
orig += len_front + len_rep; // move to next "end of rep"
163+
}
164+
strcpy(tmp, orig);
165+
return result;
166+
}

obj/Debug/FileExplorer.o

4.18 KB
Binary file not shown.

obj/Release/FileExplorer.o

2.27 KB
Binary file not shown.

0 commit comments

Comments
 (0)