-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleitura.c
126 lines (98 loc) · 3.09 KB
/
leitura.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
#define _GNU_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gmodule.h>
#include <glib.h>
#include "leitura.h"
#include "vendas.h"
#include "faturacao.h"
#include "gestaoFilial.h"
#include "catProdutos.h"
#include "catClientes.h"
void getClientes(CAT_CLIENTES c, char * fich){
FILE * fp;
char * niceBuffer, * actualBuffer;
char buffer[10];
if(fopen(fich, "r"))
fp = fopen(fich, "r");
else
fp = fopen("./Ficheiros/Clientes.txt", "r");
while(fgets(buffer, 10, fp)) {
niceBuffer = strtok(buffer, "\r\n");
actualBuffer = strdup(niceBuffer);
insert_Cat_cliente(c, actualBuffer);
}
fclose(fp);
}
void getProdutos(CAT_PRODUTOS p, char * fich){
FILE * fp;
char * niceBuffer, * actualBuffer;
char buffer[10];
if(fopen(fich, "r"))
fp = fopen(fich, "r");
else
fp = fopen("./Ficheiros/Produtos.txt", "r");
while(fgets(buffer, 10, fp)) {
niceBuffer = strtok(buffer, "\r\n");
actualBuffer = strdup(niceBuffer);
insert_Cat_prod(p, actualBuffer);
}
fclose(fp);
}
static int valVendas(VENDA v, CAT_PRODUTOS produtos, CAT_CLIENTES clientes){
int val;
val = 1;
if (!lookup_Cat_prod(produtos, getProduto(v))) val = 0;
if (getPreco(v) < 0 || getPreco(v) >= 1000.0) val = 0;
if (getQuantidade(v) < 1 || getQuantidade(v) > 200) val = 0;
if (getPromo(v) != 'N' && getPromo(v) != 'P') val = 0;
if (!lookup_Cat_cliente(clientes, getCliente(v))) val = 0;
if (getMes(v) < 1 || getMes(v) > 12) val = 0;
if (getFilial(v) < 1 || getFilial(v) > 3) val = 0;
return val;
}
/*esta aqui está a servir de funçao auxiliar mas devia ser uma funçao mesmo*/
void getGestaoFilial(GESTAOFILIAL * gestFilial, VENDA v){
GESTAOCLIENTE gestCliente;
INFOPROD prodActual;
char * prodAux;
prodAux = strdup(getProduto(v));
gestCliente = lookupGestaoFilial(gestFilial[getFilial(v)-1], getCliente(v));
prodActual = newInfoProd();
setQuant(prodActual, getMes(v), getQuantidade(v), getPromo(v));
setPrecoGF(prodActual, getMes(v), getPreco(v), getPromo(v));
addProdutoCliente(gestCliente, prodAux, prodActual);
}
/*a getVendas acho que na verdade só está a servir de funçao que faz a getGestaoFilial e getFaturaçao lol*/
int getVendas(FATGLOBAL fatGlobal, GESTAOFILIAL * gestFilial, CAT_PRODUTOS prod, CAT_CLIENTES client, char * fich){
FILE * fp;
char * lnBuffer;
char buffer[35];
int val, i, numVendas;
VENDA v;
FATURACAO currentFat;
foreach_Cat_prod(prod, initFatGlobal, fatGlobal);
for(i = 0; i < 3; i++)
foreach_Cat_cliente(client, initGestaoFilial, gestFilial[i]);
if(fopen(fich, "r"))
fp = fopen(fich, "r");
else
fp = fopen("./Ficheiros/Vendas_1M.txt", "r");
while(fgets(buffer, 35, fp)) {
lnBuffer = strdup(buffer);
lnBuffer = strtok(lnBuffer, "\r\n");
v = strToVenda(buffer);
val = valVendas(v, prod, client);
currentFat = lookupFatGlobal(fatGlobal, getProduto(v));
free(lnBuffer);
if (val){
numVendas++;
getGestaoFilial(gestFilial, v);
incNVendas(currentFat, getMes(v), getFilial(v), getPromo(v));
somaPrecoTotal(currentFat, getMes(v), getFilial(v), getPreco(v), getPromo(v), getQuantidade(v));
}
destroyVenda(v);
}
return numVendas;
}