-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneral.c
214 lines (173 loc) · 5.51 KB
/
general.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//
// Created by Edoardo Papa on 2019-09-09.
//
#include "general.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <stdbool.h>
#include "stdlib.h"
#include "limits.h"
#include "manage_sha.h"
/**
* Partendo da un PATH verifico se quel determinato file esiste. In caso negativo, lo creo.
* @param path PATH del file.
*/
void exist_file(char * path)
{
FILE *file;
file = fopen(path, "r"); // Apro il file in lettura
if (file == NULL) {
file = fopen(path, "w");
fclose(file);
}
fclose(file);
}
/**
* Partendo da un PATH analizzo quel determinato file in maniera tale da riempire la struttura (@FILE_OUT_INFO) con tutti i dati del file.
*
* @param path PATH del file
* @return la struttura contenete tutte le informazioni del file.
*/
FILE_OUT_INFO analizeFile(char * path)
{
FILE *file;
char next = 0;
char *line = NULL;
ssize_t read;
size_t len;
//exist_file(path);
file = fopen(path, "r"); // Apro il file in lettura
if (file == NULL)
{
printf("\nFILE NON PRESENTE : %s \n",path);
fclose(file);
}
volatile FILE_OUT_INFO file_list;
file_list.absolute_path = NULL;
file_list.sha = 0;
file_list.digest = NULL;
file_list.next_element = NULL;
FILE_OUT_INFO *element = malloc(sizeof(FILE_OUT_INFO));
element->absolute_path = NULL;
element->digest = 0;
element->sha = 0;
element->next_element = NULL;
while ((read = getline(&line, &len, file)) != -1)
{
char *token = strtok(line, "\r\n");
if (!element) {
printf("ERRORRE!!!!!!");
}
while (token != NULL)
{
if (strcspn(token, "/") == 0)
{
element->absolute_path = malloc(strlen(token) + 1);
if (element->absolute_path == NULL)
{
printf("ERRORRE");
}
strcpy(element->absolute_path, token);
}
else if (strcspn(token, "S") == 0)
{
for (int i = 0; i < sizeof(map) / sizeof(map[0]); i++)
{
if (strcmp(token, map[i].s) == 0)
{
element->sha = map[i].sha;
break;
}
}
}
else
{
element->digest = malloc(strlen(token) + 1);
if (element->digest == NULL)
{
printf("ERRORRE");
}
strcpy(element->digest, token);
next = 1;
}
token = strtok(NULL, "\r\n");
}
if(next)
{
next = 0;
if (file_list.absolute_path != NULL)
{
for (FILE_OUT_INFO *ptr = &file_list; ptr != NULL; ptr = ptr->next_element)
{
if (ptr->next_element == NULL)
{
ptr->next_element = malloc(sizeof(FILE_OUT_INFO));
ptr->next_element->absolute_path = element->absolute_path;
ptr->next_element->sha = element->sha;
ptr->next_element->digest = element->digest;
ptr->next_element->next_element = element->next_element;
break;
}
}
}
else
{
file_list.absolute_path = malloc(sizeof(char *));
file_list.absolute_path = element->absolute_path;
file_list.sha = element->sha;
file_list.digest = element->digest;
file_list.next_element = element->next_element;
}
FILE_OUT_INFO *element = malloc(sizeof(FILE_OUT_INFO));
}
}
// FILE_OUT_INFO *ptr = &file_list;
// while (ptr != NULL) {
// FILE_OUT_INFO *next = ptr->next_element;
// free(ptr->absolute_path);
// //free(ptr);
// ptr = next;
// }
fclose(file);
free(element);
return file_list;
}
/**
* Mi estrapolo la lunghezza del file passato come parametro formale
* @param data dati del file
* @return quantità dei dati
*/
unsigned long get_len_of_data(const char * data)
{
unsigned long counter_charatter = 0;
while(*data != '\0')
{
counter_charatter++;
data++;
}
data -= counter_charatter;
return counter_charatter;
}
/************************************************ PER TEST ************************************************/
void test_all_function_from__GENERAL_H(char * path)
{
/********************************** ANALIZE FILE **********************************/
printf("Hai appena eseguito analizeFile() function and the test is: ");
char * out_temp;
strcat(&out_temp,&path);
out_temp[strlen(out_temp) - 6] = '\0';
strcat(&out_temp[0],"/shadb_for_test.out");
if(analizeFile(out_temp).digest == NULL)
{
printf(" failed\n");
}
printf(" passed\n");
/********************************** GET LEN OF DATA *******************************/
printf("Hai appena eseguito get_len_of_data() function and the test is: ");
if(get_len_of_data("CIAO") != 4)
{
printf(" failed\n");
}
printf(" passed\n");
}