-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
245 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
¬°¤*,¸¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸ | ||
¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯ | ||
__/¯\____ ___/\__ _/\__ _/\_ _/\__ _/\___ ___/\__ __/\_ _/\__ | ||
\_ ____/_> ____ \_/ _ \_ \ < /_ \_/ _>> ____ \_ > \_/ _ \_ | ||
_> ___/ ¯>__> <<__// __ _/ |> ></ _/> </ ¯ \\__> <<__// /\ // __ _/ | ||
_> \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 <jansson.h> | ||
#include "../message.h" | ||
#include "../jsonhelpers.h" | ||
#include "../comment.h" | ||
|
||
// Load comments from file. | ||
bool comment_repository_load(CommentRepository* repository, const char* filename) { | ||
assert(filename != NULL); | ||
assert(repository != NULL); | ||
|
||
bool ret = false; | ||
|
||
json_t* root; | ||
json_error_t err; | ||
json_t* value; | ||
int 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 = true; ret && (index < json_array_size(root)) && (value = json_array_get(root, index)); index++) { | ||
ret = false; | ||
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 = true; | ||
} | ||
free(text); | ||
} | ||
} | ||
} | ||
} | ||
json_decref(root); | ||
} | ||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
¬°¤*,¸¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸ | ||
¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯¬°¤*,¸_¸,*¤°¬°¤*,¸,*¤°¬¯ | ||
__/¯\____ ___/\__ _/\__ _/\_ _/\__ _/\___ ___/\__ __/\_ _/\__ | ||
\_ ____/_> ____ \_/ _ \_ \ < /_ \_/ _>> ____ \_ > \_/ _ \_ | ||
_> ___/ ¯>__> <<__// __ _/ |> ></ _/> </ ¯ \\__> <<__// /\ // __ _/ | ||
_> \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 <errno.h> | ||
#include <string.h> | ||
|
||
#include "../message.h" | ||
#include "../jsonhelpers.h" | ||
#include "../comment.h" | ||
|
||
// Save comments to file. | ||
bool comment_repository_save(CommentRepository* repository, const char* filename) { | ||
assert(filename != NULL); | ||
assert(repository != NULL); | ||
|
||
bool ret = false; | ||
|
||
int i, count = comment_repository_size(repository); | ||
FILE *stream = fopen(filename, "wb"); | ||
if(stream == NULL) { | ||
ERROR_MSG("Failed to open %s: %s", filename, strerror(errno)); | ||
} else { | ||
fprintf(stream, "[\n"); | ||
for(i=0; i<count; i++) { | ||
Comment entry; | ||
if(comment_repository_get(repository, i, &entry)) { | ||
fprintf(stream, "\t{ \"logical\":\"%04x\", \"page\":\"%02x\", \"text\": ", entry.logical, entry.page); | ||
|
||
bool multiline = false; | ||
if(strchr(entry.text, '\n') != NULL) { | ||
multiline = true; | ||
} | ||
|
||
if(multiline) { | ||
fprintf(stream, "[\n"); | ||
for(char *ptr=entry.text, *next=NULL; ptr!=NULL; ptr=next) { | ||
next = strchr(ptr, '\n'); | ||
if(next != NULL) { | ||
fprintf(stream, "\t\""); | ||
fwrite(ptr, 1, next-ptr, stream); | ||
fprintf(stream, "\",\n"); | ||
} else { | ||
fprintf(stream, "\"%s\"\n", ptr); | ||
} | ||
} | ||
fprintf(stream, "]\n"); | ||
} else { | ||
fprintf(stream, "\"%s\"", entry.text); | ||
} | ||
|
||
fprintf(stream,"}%c\n", (i<(count-1)) ? ',' : ' '); | ||
} | ||
} | ||
fprintf(stream, "]\n"); | ||
fclose(stream); | ||
ret = true; | ||
} | ||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[ | ||
{ "logical": "EABC", "page": "0", "text": "single line comment" }, | ||
{ "logical": "C105", "page": "3", "text": [ | ||
"line 0", | ||
"line 1", | ||
"line 2" | ||
] | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[ | ||
{ "logical": "CAFE", "page": "0a", "text": "hello!" }, | ||
{ "logical": "F00D", "page": "40", "text": { "foo": "bar" } } | ||
] |