forked from Joao-Maria-Janeiro/WarmingUp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructs.c
130 lines (106 loc) · 3.9 KB
/
structs.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "structs.h"
#include "lists.h"
void loadCountryFromFile(char _fileNameCountry[], node_t_country **_head, node_t_country **_tail){
dados_country payload;
char linha[300] = {0};
int length = 0;
char aux[400] = {0};
int i=0;
node_t_auxYear *_headAuxYear = NULL;
node_t_auxYear *_tailAuxYear = NULL;
node_t_auxYear *tmp = NULL;
FILE *fp;
fp = fopen(_fileNameCountry, "r");
if( fp == NULL){
printf("Error opening file");
exit(EXIT_FAILURE);
}
//Here we use the while to read each line of the file
while(fgets(linha, 300, fp) != NULL){
if(i!=0) { //We don't want to save the first line of the file because it's the header
if(strstr(linha, ",,") == NULL) { // We do this because we don't want to save lines without all the information
sscanf(linha, "%d-%d-%d,%f,%f,%[^\n]",
&(payload.dt.ano),
&(payload.dt.mes),
&(payload.dt.dia),
&(payload.temperatura),
&(payload.incerteza),
aux);
length = strlen(aux) - 1;
aux[length] = '\0';
strcpy(payload.pais, aux);
insertAuxYear(&_headAuxYear, &_tailAuxYear, payload.dt.ano, payload); //Here we send it to the year's list
}
}
i++;
}
stitching(_headAuxYear, _tailAuxYear); //We summon this function to join all the mini lists into a big list
//Update the head and tail of the big list
*_head = _headAuxYear->head;
*_tail = _tailAuxYear->tail;
tmp = _headAuxYear;
//Free all the years' list
while(tmp != _tailAuxYear){
tmp = tmp->next;
free(tmp->prev);
}
free(_tailAuxYear);
fclose(fp);
}
void loadCityFromFile(char _fileNameCity[], node_t_city **_head, node_t_city **_tail){
dados_city payload;
char linha[300] = {0};
int length = 0;
char* data = NULL;
char* temperatura = NULL;
char* incerteza = NULL;
char* cidade = NULL;
char* pais = NULL;
char* latitude = NULL;
char* longitude = NULL;
int last_c = 0;
int i=0;
FILE *fp;
fp = fopen(_fileNameCity, "r");
if( fp == NULL){
printf("Error opening file");
exit(EXIT_FAILURE);
}
//Here we use the while to read each line of the file
while(fgets(linha, 300, fp) != NULL){
length = strlen(linha) - 1;
linha[length] = '\0';
if(i!=0) { //We don't want to save the first line of the file because it's the header
if(strstr(linha, ",,") == NULL){ // We do this because we don't want to save lines without all the information
data = strtok(linha, ",");
temperatura = strtok(NULL, ",");
incerteza = strtok(NULL, ",");
cidade = strtok(NULL, ",");
pais = strtok(NULL, ",");
latitude = strtok(NULL, ",");
longitude = strtok(NULL, ",");
sscanf(data, "%d-%d-%d",
&(payload.dt.ano),
&(payload.dt.mes),
&(payload.dt.dia)
);
payload.temperatura = atof(temperatura); //Converts string to float
payload.incerteza = atof(incerteza);
strcpy(payload.cidade, cidade);
strcpy(payload.pais, pais);
sscanf(latitude, "%f", &(payload.latitude.angular));
last_c = strlen(latitude);
payload.latitude.direcao = latitude[last_c-1];
sscanf(longitude,"%f", &(payload.longitude.angular));
last_c = strlen(longitude);
payload.longitude.direcao = longitude[last_c-2];
InsertCityList(_head, _tail, payload);
}
}
i++;
}
fclose(fp);
}