-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparset.c
executable file
·155 lines (151 loc) · 3.76 KB
/
parset.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
/**
* chatterbox Progetto del corso di LSO 2017/2018
*
* Dipartimento di Informatica Università di Pisa
* Docenti: Prencipe, Torquati
*
*/
/**
* @file parset.c
* @brief Contiene implementazione funzioni dichiarate in parset.h
*
* @author Nicolo` Maio 544935
*
* Si dichiara che il contenuto di questo file e' in ogni sua parte opera
* originale dell'autore
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "parset.h"
/**
* @function read_str_from_config_line
* @brief legge una stringa da una riga
*
* @param config_line riga da cui leggo la stringa
* @param val stringa in cui salvo caratteri letti
*/
void read_str_from_config_line(char* config_line, char* val) {
char prm_name[MAX_CONFIG_VARIABLE_LEN];
sscanf(config_line, "%s", prm_name);
int i=0;
while(config_line[i]!='=')
{
i++;
}
i++;
int dim = (strlen(config_line)-i)+1;
char stringa[dim];
int j=0;
while(j<dim-1)
{
stringa[j]=config_line[i];
j++;
i++;
}
stringa[dim-1]='\0';
char ret[dim];
sscanf(stringa,"%s",ret);
val[0]='.';
j =1;
for(int h=0;h<dim-1;h++)
{
val[j]=ret[h];
j++;
}
val[dim-1]='\0';
}
/**
* @function read_int_from_config_line
* @brief legge un intero da una riga
*
* @param config_line riga da cui leggo intero
*
* @return intero letto
*/
int read_int_from_config_line(char* config_line) {
// Selezionata la stringa utile per separare stringa e intero
// scorro linearmente dopo aver fatto la prima sscanf
char prm_name[MAX_CONFIG_VARIABLE_LEN];
int i=0;
sscanf(config_line, "%s", prm_name);
while(config_line[i]!='=')
{
i++;
}
i++;
int dim = (strlen(config_line)-i)+1;
char stringa[dim];
int j=0;
while(j<dim-1)
{
stringa[j]=config_line[i];
j++;
i++;
}
stringa[dim-1]='\0';
char val[5];
sscanf(stringa,"%s",val);
return atoi(val);
}
/**
* @function read_config_file
* @brief legge le righe non commentate nel file di configurazione
* e da esse estrae le stringhe e gli interi richiesti
*
* @param config_filename nome del file da cui leggere righe
* @param config struttura da riempire leggendo file di configurazione
*/
void read_config_file(char* config_filename, Config* config) {
FILE *fp;
char buf[CONFIG_LINE_BUFFER_SIZE];
if ((fp=fopen(config_filename, "r")) == NULL)
{
fprintf(stderr, "Failed to open config file %s", config_filename);
exit(EXIT_FAILURE);
}
while(!feof(fp))
{
fgets(buf, CONFIG_LINE_BUFFER_SIZE, fp);
if (buf[0] == '#' || strlen(buf) < 8)
{
continue;
}
if (strstr(buf, "UnixPath"))
{
read_str_from_config_line(buf, config->UnixPath);
}
if (strstr(buf, "DirName"))
{
read_str_from_config_line(buf, config->DirName);
}
if (strstr(buf, "StatFileName"))
{
read_str_from_config_line(buf, config->StatFileName);
}
if (strstr(buf, "MaxConnections"))
{
config->MaxConnections = read_int_from_config_line(buf);
}
if (strstr(buf, "ThreadsInPool"))
{
config->ThreadsInPool = read_int_from_config_line(buf);
}
if (strstr(buf, "MaxMsgSize"))
{
config->MaxMsgSize = read_int_from_config_line(buf);
}
if (strstr(buf, "MaxFileSize"))
{
config->MaxFileSize = read_int_from_config_line(buf);
}
if (strstr(buf, "MaxHistMsgs"))
{
config->MaxHistMsgs = read_int_from_config_line(buf);
}
}
int notused;
SYSCALL(notused,fclose(fp),"fclose file chatty.conf");
}