-
Notifications
You must be signed in to change notification settings - Fork 0
/
rwfile.c
179 lines (127 loc) · 4.6 KB
/
rwfile.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
//
// Created by Edoardo Papa on 2019-09-12.
//
#include "rwfile.h"
#include "string.h"
#include <stdlib.h>
#include "general.h"
#include "manage_sha.h"
/**
* Questa fuonzione permette di scrivere all'interno del file selezionato le informazioni da aggiungere al repository.
* @param arg struttura contenente i parametri :
* - enum TYPE_OF_OPTION * type; < Tipo di istruzione da eseguire
* - enum TYPE_SHA * sha; < Tipo di SHA da eseguire
* - char * output_file; < PATH del file di output dove andare a prendere e a cercare i dati
* - char * analize_file; < PATH del file da analizzare
* - char * type_command;
* @param digest DIGEST da aggiungere
* @param len lunghezza del DIGEST
*/
void write_new_element_to_file(struct digit_arguments arg,char * digest, unsigned long len)
{
FILE *file = fopen(arg.output_file, "a+"); // Apro il file in append mode
for(unsigned long i = 0; i < strlen(arg.analize_file);i++)
{
fprintf(file,"%c",arg.analize_file[i]);
}
fprintf(file,"\n");
char * string_of_sha = get_string_of_sha(arg.sha);
fprintf(file,"%s\n",string_of_sha);
for(unsigned long i = 0; i < len;i++)
{
if((digest[i]&0xFF)<= 0xF)
{
fprintf(file,"0%x",digest[i]&0xFF);
}
else
{
fprintf(file,"%x",digest[i]&0xFF);
}
}
fprintf(file,"\n");
fclose(file);
}
/**
* Leggo tutto il file contenuto al PATH passato come parametro formale e restituisco il puntatore di dove sono contenuti i dati del file.
* @param path PATH del file
* @return puntatore contenente i dati del file
*/
char * ReadFile(char *path) {
char read;
unsigned short counter_charatter = 0;
FILE *file;
file = fopen(path, "r+"); // Apro il file in lettura
if (file == NULL)
{
printf("\nFILE NON PRESENTE\n");
fclose(file);
return 0;
}
while ((read = fgetc(file)) != EOF) { // Leggo fino alla file del file (EOF)
counter_charatter++;
}
fseek(file,0,0);
char *data= malloc(sizeof(char*) * counter_charatter);
if(data == NULL)
{
printf("Memoria esaurita\n");
fclose(file);
exit(1);
}
while ((read = fgetc(file)) != EOF) { // Leggo fino alla file del file (EOF)
*data = read;
data++;
}
*data = '\0';
fclose(file);
data -= counter_charatter;
return data;
// char data[counter_charatter];// = malloc((sizeof(char) * counter_charatter) );
//
// if(data == NULL)
// {
// printf("Memoria esaurita\n");
// fclose(file);
// exit(1);
// }
// int i = 0;
// while ((read = fgetc(file)) != EOF) { // Leggo fino alla file del file (EOF)
//// *data = read;
//// data++;
// data[i] = read;
// i++;
// }
// //*data = '\0';
// //data[i] = '\0';
//
// fclose(file);
//
// //data -= counter_charatter;
//
// return &data[0];
}
/************************************************ PER TEST ************************************************/
void test_all_function_from__RWFILE_H(char * path_to_analize, char* path)
{
/********************************** WRITE NEW ELEMENT TO FILE **********************************/
printf("Hai appena eseguito write_new_element_to_file()&\n"
" ReadFile functions and the test is: ");
struct digit_arguments arg;
path_to_analize[strlen(path_to_analize) - 24] = '\0';
strcat(&path_to_analize[0],"analize_file_for_test.in");
arg.analize_file = malloc(sizeof(arg.analize_file));
strcpy(arg.analize_file,path_to_analize);
path[strlen(path) - 24] = '\0';
strcat(&path[0],"shadb_for_test.out");
arg.sha = SHA1;
arg.output_file = path;
write_new_element_to_file(arg,"12345678912345678912",20);
/********************************** READ FILE **********************************/
char * ret = strstr(ReadFile(path),"3132333435363738393132333435363738393132");
ret[41] = '\0';
if(strcmp(ret,"3132333435363738393132333435363738393132\n") != 0)
{
printf(" failed\n");
}
printf(" passed\n");
}