-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfe_functions.h
166 lines (133 loc) · 4.02 KB
/
fe_functions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/** Functions for File Explorer
*
* BeXCool 2020
* Petr Pavlik <[email protected]>
*
* [APACHE LICENSE 2.0 - "./LICENSE.md"]
*
*/
#ifndef FE_FUNCTIONS_H_INCLUDED
#define FE_FUNCTIONS_H_INCLUDED
#endif // FE_FUNCTIONS_H_INCLUDED
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <windows.h>
#include "colors.h"
void delay(int number_of_seconds) {
// Converting time into milli_seconds
int milli_seconds = 1000 * number_of_seconds;
// Storing start time
clock_t start_time = clock();
// looping till required time is not achieved
while (clock() < start_time + milli_seconds);
}
int command(char url[1000], char lastDir[1000]) {
if(strcmp(url, "$root") == 0){
strcpy(lastDir, "");
text_color(COLOR_YELLOW);
printf("\nRemoved current URL.");
text_color(COLOR_WHITE);
return(lastDir);
}else if(strcmp(url, "$abort") == 0) {
text_color(COLOR_YELLOW);
printf("\nAborted current action.");
text_color(COLOR_WHITE);
return(1);
} else {
return(0);
}
}
int remove_directory(const char *path) {
DIR *d = opendir(path);
size_t path_len = strlen(path);
int r = -1;
if (d) {
struct dirent *p;
r = 0;
while (!r && (p=readdir(d))) {
int r2 = -1;
char *buf;
size_t len;
/* Skip the names "." and ".." as we don't want to recurse on them. */
if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
continue;
len = path_len + strlen(p->d_name) + 2;
buf = malloc(len);
if (buf) {
struct stat statbuf;
snprintf(buf, len, "%s/%s", path, p->d_name);
if (!stat(buf, &statbuf)) {
if (S_ISDIR(statbuf.st_mode))
r2 = remove_directory(buf);
else
r2 = unlink(buf);
}
free(buf);
}
r = r2;
}
closedir(d);
}
if (!r)
r = rmdir(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;
}