-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.c
More file actions
156 lines (142 loc) · 3.87 KB
/
data.c
File metadata and controls
156 lines (142 loc) · 3.87 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
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "data.h"
// Data generation/free
struct Data* data_generate(uint16_t data_number)
{
// Allocates memory for the struct
struct Data* d = malloc(sizeof(struct Data));
if (!d)
{
#ifdef DEBUG
error("ERROR : data_generate : Dynamic allocation not possible for the data structure");
#endif // DEBUG
#ifdef __AVR__
while(1){}
#else
system("PAUSE>NUL");
exit(EXIT_FAILURE);
#endif
}
// Allocates memory for the array
uint16_t n = (data_number - 1) / 8 + 1; // Number of byte needed
d->data_array = calloc(n,sizeof(uint8_t)); // Automatically sets the bits to zero
if (d->data_array == NULL)
{
#ifdef DEBUG
error("ERROR : data_generate : Dynamic allocation not possible for the array");
#endif // DEBUG
#ifdef __AVR__
while(1){}
#else
system("PAUSE>NUL");
exit(EXIT_FAILURE);
#endif
}
// Sets the data_number field
d->data_number = data_number;
return d;
}
void data_free(struct Data* d)
{
if(d != NULL)
{
free(d->data_array);
free(d);
}
}
// Data operations
uint8_t data_get(uint16_t n, struct Data* d)
{
if(n < d->data_number)
return (d->data_array[n/8] >> ( n - 8*(n/8) ) ) & 1;
else
{
#ifdef DEBUG
error("ERROR: Incorect data number. Function data_get");
#endif // DEBUG
#ifdef __AVR__
while(1){}
#else
system("PAUSE>NUL");
exit(EXIT_FAILURE);
#endif
}
return -1;
}
void data_set(uint16_t n, uint8_t data, struct Data* d)
{
if(n < d->data_number)
if (data)
d->data_array[n / 8] |= (1 << ( n - 8*(n/8) ));
else
d->data_array[n / 8] &= ~(1 << ( n - 8*(n/8) ));
else
{
#ifdef DEBUG
error("ERROR: Incorect data number. Function data_set.");
#endif // DEBUG
#ifdef __AVR__
while(1){}
#else
system("PAUSE>NUL");
exit(EXIT_FAILURE);
#endif
}
}
void data_delete(uint16_t n, struct Data* d)
{
if (0 <= n && n < d->data_number)
{
// Move the data, starting from the data we want to delete.
for(uint16_t i = n; i < d->data_number - 1; i++)
data_set(i, data_get(i + 1, d), d);
// Determines whether or not it is necessary to allocate a new block of memory.
uint16_t old_byte_number_needed = (d->data_number - 1)/8 + 1;;
uint16_t new_byte_number_needed = d->data_number/8 + 1;
if(new_byte_number_needed < old_byte_number_needed)
d->data_array = realloc(d->data_array, new_byte_number_needed);
else
data_set(d->data_number - 1, 0, d);
d->data_number -= 1;
}
else
{
#ifdef DEBUG
error("ERROR: Wrong block. Function data_delete.");
#endif // DEBUG
#ifdef __AVR__
while(1){}
#else
system("PAUSE>NUL");
exit(EXIT_FAILURE);
#endif
}
}
#ifdef DEBUG
void data_show(struct Data* d)
{
#ifdef __AVR__
if(d == NULL)
uart_tx_str("data == NULL\r\n");
else
{
for(uint16_t i = 0; i < d->data_number; i++)
{
char str[5];
sprintf(str, "%d", data_get(i, d));
uart_tx_str(str);
}
uart_newline();
}
#else
if(d == NULL)
printf("\ndata == NULL\n");
else
{
printf("(");
for(uint16_t i = 0; i < d->data_number; i++)
printf("%d ", data_get(i, d));
printf(")\n\n");
}
#endif // __AVR__
}
#endif