-
Notifications
You must be signed in to change notification settings - Fork 2
/
comment.c
234 lines (212 loc) · 9.14 KB
/
comment.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
¬°¤*,¸¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸
¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯
__/¯\____ ___/\__ _/\__ _/\_ _/\__ _/\___ ___/\__ __/\_ _/\__
\_ ____/_> ____ \_/ _ \_ \ < /_ \_/ _>> ____ \_ > \_/ _ \_
_> ___/ ¯>__> <<__// __ _/ |> ></ _/> </ ¯ \\__> <<__// /\ // __ _/
_> \7 <__/:. \__/:. \> \_/ L/ _____/. 7> .\_/:. \__/ <_/ </:. \> \_
|:::::::::::::::::::::::/:::::::::::::>::::::::/::::::::::::::::::::::::/:::::|
|¯¯\::::/\:/¯\::::/¯¯¯¯<::::/\::/¯¯\:/¯¯¯¯¯¯\::\::/¯¯\::::/¯¯\::::/¯¯¯¯<::::/¯|
|__ |¯¯| T _ |¯¯¯| ___ |¯¯| |¯| _ T ______ |¯¯¯¯| _ |¯¯¯| _ |¯¯¯| ___ |¯¯| _|
\|__|/\|/ \|___|/ \|__|/\|_|/ \|/ \| |/ \|___|/ \|___|/dNo\|__|/
¬°¤*,¸¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸
¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯
This file is part of Etripator,
copyright (c) 2009--2023 Vincent Cruz.
Etripator is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Etripator is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Etripator. If not, see <http://www.gnu.org/licenses/>.
¬°¤*,¸¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸
¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯
*/
#include "comment.h"
#include "message.h"
#include <jansson.h>
#include "jsonhelpers.h"
#define COMMENT_ARRAY_INC 16
/**
* Comment repository.
*/
struct comment_repository_impl {
size_t size; /**< Size of comment repository */
size_t last; /**< Last element in the repository */
comment_t *comments; /**< Comments */
};
/**
* Get comment index by its address.
* \param [in] repository Coment repository.
* \param [in] logical Logical address.
* \param [in] page Memory page.
* \return comment index or -1 if the label was not found.
*/
static int comment_repository_index(comment_repository_t* repository, uint16_t logical, uint8_t page) {
size_t i;
for(i=0; i<repository->last; i++) {
if( (repository->comments[i].page == page) &&
(repository->comments[i].logical == logical) ) {
return (int)i;
}
}
return -1;
}
/* Create comment repository. */
comment_repository_t* comment_repository_create() {
comment_repository_t *repository;
repository = (comment_repository_t*)malloc(sizeof(comment_repository_t));
if(repository == NULL) {
ERROR_MSG("Failed to create comment repository: %s", strerror(errno));
} else {
repository->last = 0;
repository->comments = NULL;
repository->size = COMMENT_ARRAY_INC;
repository->comments = (comment_t*)malloc(repository->size * sizeof(comment_t));
if(repository->comments == NULL) {
ERROR_MSG("Failed to create comments: %s", strerror(errno));
comment_repository_destroy(repository);
free(repository);
repository = NULL;
}
}
return repository;
}
/* Release comment repository resources. */
void comment_repository_destroy(comment_repository_t* repository) {
repository->size = 0;
repository->last = 0;
if(repository->comments != NULL) {
for(int i=0; i<repository->last; i++) {
if(repository->comments[i].text) {
free(repository->comments[i].text);
}
}
free(repository->comments);
repository->comments = NULL;
}
}
/* Add comment to repository. */
int comment_repository_add(comment_repository_t* repository, uint16_t logical, uint8_t page, const char *text) {
int ret = 1;
int index = comment_repository_index(repository, logical, page);
if(index >= 0) {
WARNING_MSG("Duplicate comment for logical address $%04x in page $%02x", logical, page);
} else {
/* Expand arrays if necessary */
if(repository->last >= repository->size) {
comment_t *ptr;
repository->size += COMMENT_ARRAY_INC;
ptr = (comment_t*)realloc(repository->comments, repository->size * sizeof(comment_t));
if(ptr == NULL) {
comment_repository_destroy(repository);
ret = 0;
} else {
repository->comments = ptr;
}
}
if(ret != 0) {
/* Push addresses & text */
repository->comments[repository->last].logical = logical;
repository->comments[repository->last].page = page;
repository->comments[repository->last].text = strdup(text);
++repository->last;
}
}
return ret;
}
/* Find a comment by its address. */
int comment_repository_find(comment_repository_t* repository, uint16_t logical, uint8_t page, comment_t *out) {
int index = comment_repository_index(repository, logical, page);
if(index < 0) {
memset(out, 0, sizeof(comment_t));
return 0;
}
memcpy(out, &repository->comments[index], sizeof(comment_t));
return 1;
}
/* Get the number of comments stored in the repository. */
int comment_repository_size(comment_repository_t* repository) {
return repository ? (int)repository->last : 0;
}
/* Retrieve the comment at the specified index. */
int comment_repository_get(comment_repository_t* repository, int index, comment_t *out) {
if((repository != NULL) && ((index >= 0) && (index < (int)repository->last))) {
memcpy(out, &repository->comments[index], sizeof(comment_t));
return 1;
} else {
memset(out, 0, sizeof(comment_t));
return 0;
}
}
/* Delete comments. */
int comment_repository_delete(comment_repository_t* repository, uint16_t first, uint16_t end, uint8_t page) {
size_t i;
for(i=0; i<repository->last; i++) {
if( (repository->comments[i].page == page) &&
(repository->comments[i].logical >= first) &&
(repository->comments[i].logical < end) ) {
if(repository->last) {
repository->last--;
if(repository->comments[i].text) {
free(repository->comments[i].text);
}
memcpy(&repository->comments[i], &repository->comments[repository->last], sizeof(comment_t));
i--;
}
}
}
return 1;}
/* Load comments from file. */
int comment_repository_load(const char* filename, comment_repository_t* repository) {
json_t* root;
json_error_t err;
json_t* value;
int ret = 0, index = 0;
root = json_load_file(filename, 0, &err);
if(!root) {
ERROR_MSG("Failed to parse %s:%d:%d: %s", filename, err.line, err.column, err.text);
} else {
if(!json_is_array(root)) {
ERROR_MSG("Array expected.");
} else for (index = 0, ret = 1; ret && (index < json_array_size(root)) && (value = json_array_get(root, index)); index++) {
ret = 0;
if(!json_is_object(value)) {
ERROR_MSG("Expected object.");
} else {
int num;
// logical
json_t *tmp = json_object_get(value, "logical");
if(!json_validate_int(tmp, &num)) {
ERROR_MSG("Invalid or missing logical address.");
} else if((num < 0) || (num > 0xffff)) {
ERROR_MSG("Logical address out of range.");
} else {
uint16_t logical = (uint16_t)num;
// page
tmp = json_object_get(value, "page");
if(!json_validate_int(tmp, &num)) {
ERROR_MSG("Invalid or missing page.");
} else {
// text (same format as section/label description)
char* text = json_load_description(value, "text");
if(text == NULL) {
ERROR_MSG("Invalid or missing text.");
} else if((num < 0) || (num > 0xff)) {
ERROR_MSG("Page value out of range.");
} else if(comment_repository_add(repository, logical, (uint8_t)num, text)) {
ret = 1;
}
free(text);
}
}
}
}
json_decref(root);
}
return ret;
}