Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 1 | #include <stdio.h> |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 2 | #include <string.h> |
Lawrence Tang | 368e0b4 | 2022-07-07 14:31:06 +0100 | [diff] [blame] | 3 | #include "../cper-parse.h" |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 4 | #include "json.h" |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 5 | #include "../json-schema.h" |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 6 | |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 7 | void test_ir_to_cper(int argc, char* argv[]); |
| 8 | void test_cper_to_ir(int argc, char* argv[]); |
Lawrence Tang | f0f9557 | 2022-07-07 16:56:22 +0100 | [diff] [blame] | 9 | |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 10 | int main(int argc, char* argv[]) |
| 11 | { |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 12 | if (!strcmp(argv[1], "convert-to-json")) |
| 13 | test_cper_to_ir(argc, argv); |
| 14 | else if (!strcmp(argv[1], "convert-to-cper")) |
| 15 | test_ir_to_cper(argc, argv); |
| 16 | else |
| 17 | { |
| 18 | printf("Invalid command provided. Must be one of 'convert-to-json' or 'convert-to-cper'.\n"); |
| 19 | } |
| 20 | |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 21 | return 0; |
| 22 | } |
| 23 | |
| 24 | void test_ir_to_cper(int argc, char* argv[]) |
| 25 | { |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 26 | if (argc < 4) |
| 27 | { |
| 28 | printf("Insufficient number of arguments.\n"); |
| 29 | return; |
| 30 | } |
| 31 | |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 32 | //Read JSON IR from file. |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 33 | json_object* ir = json_object_from_file(argv[2]); |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 34 | if (ir == NULL) |
| 35 | { |
| 36 | printf("Could not read IR JSON, import returned null."); |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | //Open a read for the output file. |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 41 | FILE* cper_file = fopen(argv[3], "w"); |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 42 | if (cper_file == NULL) { |
| 43 | printf("Could not open output file, file handle returned null."); |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | //Run the converter. |
| 48 | ir_to_cper(ir, cper_file); |
| 49 | fclose(cper_file); |
| 50 | printf("Conversion finished!\n"); |
| 51 | } |
| 52 | |
| 53 | void test_cper_to_ir(int argc, char* argv[]) |
| 54 | { |
Lawrence Tang | f0f9557 | 2022-07-07 16:56:22 +0100 | [diff] [blame] | 55 | //Get a handle for the log file. |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 56 | FILE* cper_file = fopen(argv[2], "r"); |
Lawrence Tang | f0f9557 | 2022-07-07 16:56:22 +0100 | [diff] [blame] | 57 | if (cper_file == NULL) { |
| 58 | printf("Could not open CPER record, file handle returned null."); |
Lawrence Tang | b44314c | 2022-07-13 11:45:22 +0100 | [diff] [blame] | 59 | return; |
Lawrence Tang | f0f9557 | 2022-07-07 16:56:22 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | json_object* ir = cper_to_ir(cper_file); |
| 63 | fclose(cper_file); |
| 64 | |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 65 | const char* json_output = json_object_to_json_string(ir); |
| 66 | printf("\n%s\n", json_output); |
| 67 | |
| 68 | //Test JSON validator. |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 69 | if (argc >= 4) |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 70 | { |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 71 | printf("Validating output with specification %s...\n", argv[3]); |
Lawrence Tang | 8f793ac | 2022-07-13 10:17:09 +0100 | [diff] [blame] | 72 | validate_schema_debug_enable(); |
| 73 | char* error_message = malloc(JSON_ERROR_MSG_MAX_LEN); |
Lawrence Tang | 0cb3379 | 2022-07-13 13:51:39 +0100 | [diff] [blame] | 74 | if (!validate_schema_from_file(argv[3], ir, error_message)) |
Lawrence Tang | 8a2d737 | 2022-07-12 16:44:49 +0100 | [diff] [blame] | 75 | { |
| 76 | printf("Validation failed: %s\n", error_message); |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | printf("Validation passed!\n"); |
| 81 | } |
| 82 | } |
Lawrence Tang | 1b0b00e | 2022-07-05 10:33:10 +0100 | [diff] [blame] | 83 | } |