-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasicUtilities.c
More file actions
143 lines (125 loc) · 3.81 KB
/
basicUtilities.c
File metadata and controls
143 lines (125 loc) · 3.81 KB
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
//
// Created by Francesco on 21/02/2023.
//
#include "basicUtilities.h"
#include <ctype.h>
#include <dirent.h>
#include <errno.h> /* serve per errno */
#include <fcntl.h>
#include <math.h>
#include <stdio.h>
#include <stdio.h> /* serve per la perror */
#include <stdlib.h> /* serve per la exit */
#include <string.h> /* serve per strlen */
#include <sys/stat.h>
#include <unistd.h> /* serve per la write */
#define BUFFER_SIZE 1024
void SYSERROR(int result, const char *msg) {
if (result == -1) {
fprintf(stderr, "%s: %s\n", msg, strerror(errno));
exit(EXIT_FAILURE);
}
}
char *strstrip(char *s) {
size_t size;
char *end;
size = strlen(s);
if (!size)
return s;
end = s + size - 1;
while (end >= s && isspace(*end))
end--;
*(end + 1) = '\0';
while (*s && isspace(*s))
s++;
return s;
}
int readFile(char *file, double **values) {
int values_n = 0;
FILE *fp;
char line[256];
fp = fopen(file, "r");
if (fp == NULL) {
printf("Failed to open the file.\n");
exit(1);
}
while (fgets(line, sizeof(line), fp)) {
char *strippedLine = strstrip(line);
if(strcmp(strippedLine,"") == 0) break;
double new_element = atof(strippedLine);
values_n++;
size_t new_size = values_n * sizeof(double);
*values = (double *)realloc(*values, new_size);
(*values)[values_n - 1] = new_element;
}
fclose(fp);
return values_n;
} /*
int readFile(char *file, double **values) {
// printf("Leggendo il file:%s", file);
int fd; //File Descriptor
char buffer[BUFFER_SIZE];
int bytes_read;
int values_n = 0;
char fileName[256];
IFERROR(fd = open(file, O_RDWR ^ O_APPEND), "Apro il file...");
while ((bytes_read = read(fd, buffer, BUFFER_SIZE)) > 0) {
int line_lenght = 0; //Dimensione della riga che sto leggendo ( inizia
da 0 ) char *line = calloc(1, sizeof(char)); //Linea che sto leggendo for (int
i = 0; i < bytes_read + 1; i++) { if (buffer[i] == '\n') { if (strcmp(line, "")
!= 0) { char *strippedLine = strstrip(line); if (strcmp(strippedLine, "") != 0)
{ double new_element = atof(strippedLine); values_n++; size_t new_size =
values_n * sizeof(double); *values = (double *) realloc(*values, new_size);
(*values)[values_n - 1] = new_element;
}
free(line);
line = calloc(1, sizeof(char));
line_lenght = 0;
}
} else {
line_lenght++;
char *new_line = realloc(line, line_lenght * sizeof(char));
if (new_line == NULL) {
exit(1);
}
line = new_line;
line[line_lenght - 1] = buffer[i];
}
}
free(line);
}
close(fd);
return values_n;
}
*/
double calcolaMedia(double *values, int values_n) {
double sum = 0.0;
for (int i = 0; i < values_n; i++) {
sum += values[i];
}
return sum / (double)values_n;
}
double calcolaVariazioneStandard(double *values, int values_n) {
double media = calcolaMedia(values, values_n);
double sum = 0.0;
for (int i = 0; i < values_n; i++) {
sum += pow((values[i] - media), 2);
}
sum = sum / values_n;
return sqrt(sum);
}
fileElaborato elaboraFile(char *path) {
fileElaborato f;
double *values = NULL;
f.nomeFile = strdup(path);
int num_values = readFile(path, &values);
f.dimensione = num_values;
f.media = calcolaMedia(values, num_values);
f.variazioneStandard = calcolaVariazioneStandard(values, num_values);
free(values);
return f;
}
char *basename(char *path) {
char *base = strrchr(path, '/');
return base == NULL ? path : base + 1;
}