-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.c
executable file
·66 lines (55 loc) · 1.66 KB
/
board.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
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "board.h"
#include "ioInterface.h"
#define MAX_BOXES_NUMBER 512
static char *descriptions[MAX_BOXES_NUMBER];
static int totalBoxesNumber;
void initBoard()
{
memset(descriptions, '\0', sizeof(descriptions));
totalBoxesNumber = 0;
}
void addBox(int boxNumber, char *description)
{
int len;
if (boxNumber > totalBoxesNumber)
totalBoxesNumber = boxNumber;
boxNumber--;
if (boxNumber < 0 || boxNumber >= MAX_BOXES_NUMBER) {
printErr("box number %d out of bounds [0; %d]\n", boxNumber,
MAX_BOXES_NUMBER - 1);
exit(EXIT_FAILURE);
}
for (description++; isspace(*description); description++);
if ((len = strlen(description)) == 0) {
printErr("WARINING: empty description\n");
return;
}
if (descriptions[boxNumber] != NULL) {
printErr("readding description for box number %d\n",
boxNumber);
exit(EXIT_FAILURE);
}
if ((descriptions[boxNumber] =
(char *) malloc(sizeof(char) * (len + 1))) == NULL) {
printErr("unable to allocate memory for a descritpion\n");
exit(EXIT_FAILURE);
}
strcpy(descriptions[boxNumber], description);
}
char *getDescription(int boxNumber)
{
if (boxNumber <= 0 || boxNumber > MAX_BOXES_NUMBER) {
printErr("box number out of index\n");
exit(EXIT_FAILURE);
}
return descriptions[boxNumber - 1] ? descriptions[boxNumber -
1] :
"ATTENZIONE: nessuna descrizione associata alla casella\n";
}
int getTotalBoxesNumber()
{
return totalBoxesNumber;
}