-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
201 lines (145 loc) · 3.46 KB
/
utils.c
File metadata and controls
201 lines (145 loc) · 3.46 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
int power(int base, int p){
int result = 1;
if(p){
for(int i = p; i > 0; i--){
result *= base;
}
return result;
}
else{
return result;
}
}
int Strlen(const char* string){
int len = 0;
while (*string != '\0'){
len++;
string++;
}
return len;
}
int delete(char*data, int index, int length){
int i;
for (i = index; i < (length)-1; i++){
*(data+i) = *(data+i+1);
}
*(data+i) = '\0';
length--;
return length;
}
void reverse(char*str){
int len = 0;
char*ptr = str;
while(*ptr != '\0'){
len++;
ptr++;
}
int i = 0, j = len-1;
while(i < j){
int temp = *(str+i);
*(str+i) = *(str+j);
*(str+j) = temp;
++i;
--j;
}
}
int insert(char*data, int index, int element, int uSize, int tSize){
if(uSize >= tSize)
return -1;
for(int i = uSize-1; i >= index; i--)
*(data+i+1) = *(data+i);
*(data+index) = element;
uSize++;
*(data+(uSize)) = '\0';
return uSize;
}
int binToDec(char* binary){
int i = 0, j, k;
int dec = 0;
while(*(binary+i) != '\0')
++i;
for(j = i-1, k = 0; j >= 0; --j, ++k){
dec += (*(binary+k)-48)*power(2,j);
}
return dec;
}
char* decToBin(int n){
unsigned int dec = n;
char binary[20];
int i = 0;
while(dec){
int remain = dec % 2;
binary[i++] = remain+48;
dec = dec/2;
}
binary[i] = '\0';
reverse(binary);
return strdup(binary);
}
int charValidate(char ch) {
if(ch < 0 || ch > 126)
return -1;
return 0;
}
int base64Validate(char b64ec) {
if(b64ec < 43 || b64ec > 126 || (b64ec >= 44 && b64ec <47))
return -1;
if((b64ec > 57 && b64ec < 65) || (b64ec > 90 && b64ec < 97))
return -1;
if(b64ec > 122 && b64ec < 126)
return -1;
return 0;
}
int fcheck(const char * filename) {
FILE *file;
if((file = fopen(filename, "r")) != NULL) {
fclose(file);
return 1;
}
return 0;
}
char* retrieve(char*file) {
if(!fcheck(file)) {
fprintf(stderr, "FileError: can't open %s file.\n", file);
exit(1);
}
int buffsize = 512;
FILE * fptr = fopen(file, "rb");
char* buffer = (char*)malloc(buffsize);
if(buffer == NULL) exit(EXIT_FAILURE);
int ch, cursor = 0;
while((ch=fgetc(fptr)) != EOF) {
// if ch is not the end of file, buffer is appended with ch char value
if(isprint(ch) || ch == '\t' || ch == '\n')
buffer[cursor++] = (char)ch;
/* if cursor crosses current buffer size
it's doubled in size and new size gets reallocated */
if(cursor >= buffsize - 1) {
buffsize <<=1;
buffer = (char*)realloc(buffer, buffsize);
if(buffer == NULL) exit(EXIT_FAILURE);
}
} // while ch is not end of the file
fclose(fptr); //always close the file pointer BAKA ^_^
// puts(buffer);
return buffer;
}
char *basename(char const *path, char slash) {
char *s = strrchr(path, slash);
if(!s)
return strdup(path);
else
return strdup(s + 1);
}
void delspace(char* s) {
char* d = s;
do {
while (*d == ' ') {
++d;
}
} while ((*s++ = *d++));
}