-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshafa.c
181 lines (152 loc) · 4.23 KB
/
shafa.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
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "shafa.h"
#include "modulo-d.h"
#include "Modulo_f/moduloF.h"
#include "modulo-t.h"
//#include "modulo-c.h"
#ifdef __linux__
#include <time.h>
#endif
#ifdef _WIN32
#include <windows.h>
#endif
int initOpts(Options *opts){
if(!opts) return 0;
opts->fileIN = NULL;
opts->fileOUT[0] = '\0';
opts->fileCOD[0] = '\0';
opts->fileRLE[0] = '\0';
opts->fileFREQ[0] = '\0';
opts->modT = 0;
opts->modD = 0;
opts->modF = 0;
opts->modC = 0;
opts->optB = 0;
opts->optD = 0;
opts->optC = 0;
return 1;
}
Options *getOpts(int argc, char *argv[]){
int i = 1, nameFlag = 0;
Options *opts = malloc(sizeof(Options));
initOpts(opts);
while(i < argc){
if(argv[i][0] == '-'){
i++;
switch(argv[i-1][1]){
case 'm':
switch(argv[i][0]){
case 't': opts->modT = 1; break;
case 'd': opts->modD = 1; break;
case 'f': opts->modF = 1; break;
case 'c': opts->modC = 1; break;
}; break;
case 'b': opts->optB = argv[i][0]; break;
case 'c': opts->optC = argv[i][0]; break;
case 'd': opts->optD = argv[i][0]; break;
case 'o': strcpy(opts->fileOUT, argv[i]); break;
default : fprintf(stdout, "ERRO!!\nA opção '-%c' não existe!\n", argv[i-1][1]);
}
}
else if(!nameFlag){
opts->fileIN = argv[i];
nameFlag = 1;
}
i++;
}
return opts;
}
void data() {
#ifdef __linux__
time_t t = time(NULL);
struct tm tm = *localtime(&t);
printf("%02d-%02d-%d %02d:%02d:%02d\n", tm.tm_mday, tm.tm_mon + 1, tm.tm_year + 1900, tm.tm_hour, tm.tm_min, tm.tm_hour);
#endif
#ifdef _WIN32
SYSTEMTIME t = {0};
GetLocalTime(&t);
printf("%02d-%02d-%d %02d:%02d:%02d\n", t.wDay, t.wMonth, t.wYear, t.wHour, t.wMinute, t.wSecond);
#endif
}
void errorOpenFile (char *fileName, Flag flag, FILE *fpSF, FILE *fpRLE, FILE *fpCOD, FILE *fpFREQ, FILE *fpOut){
if(flag == READ)
fprintf(stderr, "ERRO!!!\nNão foi possível abrir o ficheiro %s! -.-\n", fileName);
else
fprintf(stderr, "ERRO!!!\nNão foi possível criar o ficheiro %s! -.-\n", fileName);
if(fpSF) fclose(fpSF);
if(fpRLE) fclose(fpRLE);
if(fpCOD) fclose(fpCOD);
if(fpFREQ) fclose(fpFREQ);
if(fpOut) fclose(fpOut);
exit(1);
}
FILE *getFile(char *dest, char *fileName, char * mode, char *sufx){
FILE *fp;
if(dest[0] == '\0'){
removeSufix(dest, fileName);
strcat(dest, sufx);
}
fp = fopen(dest, mode);
return fp;
}
char *removeSufix(char *dest, char *src){
int i;
strcpy(dest, src);
for(i = 0; src[i+1] != '\0'; i++);
for(; dest[i] != '.'; i--);
dest[i] = '\0';
return dest;
}
void addFilesCreated(FileCreated **fileCreated, char *newFile){
while(*fileCreated != NULL)
fileCreated = &((*fileCreated)->next);
*fileCreated = malloc(sizeof(FileCreated));
strcpy((*fileCreated)->fileName, newFile);
(*fileCreated)->next = NULL;
}
int main(int argc, char *argv[]){
Options *opts;
FileCreated *list = NULL, *tmp = NULL;
int elapsed;
#ifdef __linux__
struct timespec begin, end;
clock_gettime(CLOCK_MONOTONIC, &begin);
#endif
#ifdef _WIN32
LARGE_INTEGER frequency;
LARGE_INTEGER begin;
LARGE_INTEGER end;
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&begin);
#endif
if(argc == 1) fprintf(stdout, HELP);
opts = getOpts(argc, argv);
if(opts->modD) moduleDMain(opts, &list);
if(opts->modF) moduleFMain(opts, &list);
#ifdef __linux__
if(opts->modT) moduleTMain(opts, &list);
#endif
/*if(opts->modC) moduleCMain(opts, &list);*/
#ifdef __linux__
clock_gettime(CLOCK_MONOTONIC, &end);
elapsed = ((end.tv_sec - begin.tv_sec) + (end.tv_nsec - begin.tv_nsec) / BILLION) * 1000;
#endif
#ifdef _WIN32
QueryPerformanceCounter(&end);
elapsed = ((1000LL * end.QuadPart) - (1000LL * begin.QuadPart)) / frequency.QuadPart;
#endif
fprintf(stdout, "Tempo de execução do módulo (milissegundos): %d\n", elapsed);
fprintf(stdout, "Ficheiro gerado(s):");
while(list != NULL){
fprintf(stdout, " %s", list->fileName);
tmp = list;
list = list->next;
free(tmp);
}
fprintf(stdout, "\n");
if(opts)
free(opts);
return 0;
}