-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.c
91 lines (75 loc) · 1.88 KB
/
utils.c
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
#include "utils.h"
// Função para verificar se um arquivo existe (recebe o path inteiro)
int fileExist(char * filename) {
return !(access(filename, R_OK));
}
void *mallocSafe(size_t s) {
void *p = malloc(s);
if (p)
return p;
fprintf(stderr, "Malloc ERROR!\n");
exit(1);
}
void createFile(const char *path) {
FILE *fp = fopen(path, "ab+");
if (fp) {
fclose(fp);
} else {
fprintf(stderr, "Erro ao abrir arquivo: %s!\n", path);
exit(1);
}
}
FILE *fopenSafe(const char *path, const char *mode) {
FILE *fp = fopen(path, mode);
if (fp) {
return fp;
} else {
fprintf(stderr, "Erro ao abrir arquivo: %s!\n", path);
exit(1);
}
}
void removeFile(char *path) {
if (remove(path)) {
fprintf(stderr, "Erro ao remover arquivo: %s!\n", path);
exit(1);
}
}
void toUpperCase(char *str) {
int i = -1;
while (str[++i] != '\0')
if (str[i] >= 'a' && str[i] <= 'z') str[i] = str[i] - 32;
}
int replaceSpace(char *str, char c) {
int flag = 0;
for(int i = 0; str[i]; i++) {
if (str[i] == ' ') {
str[i] = c;
flag = 1;
}
}
return flag;
}
char *glueString(int n_args, ...) {
char **args = (char **)mallocSafe(n_args * sizeof(char*));
int size = 0;
va_list ap;
va_start(ap, n_args);
for (int i = 0; i < n_args; i++) {
args[i] = va_arg(ap, char *);
for (int j = 0; args[i][j]; j++) {
size++;
}
}
va_end(ap);
size++;
char *r = (char *)mallocSafe(size * sizeof(char));
int k = 0;
for (int i = 0; i < n_args; i++) {
for (int j = 0; args[i][j]; j++) {
r[k++] = args[i][j];
}
}
r[k] = '\0';
free(args);
return r;
}