Skip to content
Snippets Groups Projects
Unverified Commit 205353e8 authored by Jayesh Badwaik's avatar Jayesh Badwaik
Browse files

[wip]: writer: added yaml_writer

parent 1fcfacee
No related branches found
No related tags found
No related merge requests found
Pipeline #52854 failed
//------------------------------------------------------------------------------
// SPDX-License-Identifier: "Apache-2.0 OR MIT"
// Copyright (C) 2020, Jayesh Badwaik <jayesh@badwaik.in>
//------------------------------------------------------------------------------
#ifndef RADLE_DETAIL_YAML_WRITE_DOCUMENT_H
#define RADLE_DETAIL_YAML_WRITE_DOCUMENT_H
#include <radle/detail/yaml_result.h>
#include <radle/detail/yaml_schema.h>
#include <radle/detail/yaml_value.h>
#include <stdio.h>
radle_yaml_result radle_yaml_write_document(
radle_yaml_value const* value_ptr,
FILE* file_handle,
radle_yaml_schema schema);
#endif // RADLE_DETAIL_YAML_WRITE_DOCUMENT_H
//------------------------------------------------------------------------------
// SPDX-License-Identifier: "Apache-2.0 OR MIT"
// Copyright (C) 2020, Jayesh Badwaik <jayesh@badwaik.in>
//------------------------------------------------------------------------------
#ifndef RADLE_DETAIL_YAML_WRITE_FILE_H
#define RADLE_DETAIL_YAML_WRITE_FILE_H
#include <radle/detail/yaml_file.h>
#include <radle/detail/yaml_result.h>
#include <radle/detail/yaml_schema.h>
radle_yaml_result radle_yaml_write_file(
radle_yaml_file const* file_ptr,
char const* filename,
radle_yaml_schema schema);
#endif // RADLE_DETAIL_YAML_WRITE_FILE_H
//------------------------------------------------------------------------------
// SPDX-License-Identifier: "Apache-2.0 OR MIT"
// Copyright (C) 2020, Jayesh Badwaik <jayesh@badwaik.in>
//------------------------------------------------------------------------------
#ifndef RADLE_DETAIL_YAML_WRITE_VALUE_H
#define RADLE_DETAIL_YAML_WRITE_VALUE_H
#endif // RADLE_DETAIL_YAML_WRITE_VALUE_H
......@@ -19,3 +19,5 @@ target_sources(${LIBNAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/yaml_parse_scalar.c)
target_sources(${LIBNAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/yaml_parse_value.c)
target_sources(${LIBNAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/yaml_sequence.c)
target_sources(${LIBNAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/yaml_value.c)
target_sources(${LIBNAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/yaml_write_file.c)
target_sources(${LIBNAME} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/yaml_write_document.c)
//------------------------------------------------------------------------------
// SPDX-License-Identifier: "Apache-2.0 OR MIT"
// Copyright (C) 2020, Jayesh Badwaik <jayesh@badwaik.in>
//------------------------------------------------------------------------------
#include <radle/detail/yaml_write_document.h>
#include <yaml.h>
radle_yaml_result radle_yaml_write_document(
radle_yaml_value const* value_ptr,
FILE* file_handle,
radle_yaml_schema schema)
{
int success = 0;
yaml_emitter_t emitter;
yaml_event_t event;
yaml_emitter_initialize(&emitter);
yaml_emitter_set_output_file(&emitter, file_handle);
yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
success = yaml_emitter_emit(&emitter, &event);
if (!success) {
goto radle_yaml_write_document_error_cleanup_and_exit;
}
yaml_document_end_event_initialize(&event, 0);
success = yaml_emitter_emit(&emitter, &event);
if (!success) {
goto radle_yaml_write_document_error_cleanup_and_exit;
}
yaml_emitter_delete(&emitter);
return radle_yaml_result_success;
radle_yaml_write_document_error_cleanup_and_exit:
yaml_emitter_delete(&emitter);
return radle_yaml_result_unknown_error;
}
//------------------------------------------------------------------------------
// SPDX-License-Identifier: "Apache-2.0 OR MIT"
// Copyright (C) 2020, Jayesh Badwaik <jayesh@badwaik.in>
//------------------------------------------------------------------------------
#include <radle/detail/yaml_write_document.h>
#include <radle/detail/yaml_write_file.h>
#include <stdio.h>
#include <yaml.h>
radle_yaml_result radle_yaml_write_file(
radle_yaml_file const* file_ptr,
char const* filename,
radle_yaml_schema schema)
{
int success = 0;
FILE* file_handle = fopen(filename, "w");
if (!file_handle) {
return radle_yaml_result_unknown_error;
}
yaml_emitter_t emitter;
yaml_event_t event;
yaml_emitter_initialize(&emitter);
yaml_emitter_set_output_file(&emitter, file_handle);
yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
success = yaml_emitter_emit(&emitter, &event);
if (!success) {
goto radle_yaml_write_file_error_cleanup_and_exit;
}
for (int i = 0; i < file_ptr->ndocument; ++i) {
success = radle_yaml_write_document(
file_ptr->document_ptr_array[i], file_handle, schema);
if (!success) {
goto radle_yaml_write_file_error_cleanup_and_exit;
}
}
yaml_stream_end_event_initialize(&event);
success = yaml_emitter_emit(&emitter, &event);
if (!success) {
goto radle_yaml_write_file_error_cleanup_and_exit;
}
yaml_emitter_delete(&emitter);
return radle_yaml_result_success;
radle_yaml_write_file_error_cleanup_and_exit:
yaml_emitter_delete(&emitter);
return radle_yaml_result_unknown_error;
}
......@@ -25,6 +25,13 @@ void test_radle_token_yaml_file_07(__attribute__((unused)) void** state);
void test_radle_yaml_value_01(__attribute__((unused)) void** state);
void test_radle_yaml_value_02(__attribute__((unused)) void** state);
void test_radle_yaml_value_03(__attribute__((unused)) void** state);
void test_radle_yaml_write_file_01(__attribute__((unused)) void** state);
void test_radle_yaml_write_file_02(__attribute__((unused)) void** state);
void test_radle_yaml_write_file_03(__attribute__((unused)) void** state);
void test_radle_yaml_write_file_04(__attribute__((unused)) void** state);
void test_radle_yaml_write_file_05(__attribute__((unused)) void** state);
void test_radle_yaml_write_file_06(__attribute__((unused)) void** state);
void test_radle_yaml_write_file_07(__attribute__((unused)) void** state);
int main(void)
{
......@@ -46,6 +53,9 @@ int main(void)
cmocka_unit_test(test_radle_yaml_value_01),
cmocka_unit_test(test_radle_yaml_value_02),
cmocka_unit_test(test_radle_yaml_value_03),
cmocka_unit_test(test_radle_yaml_write_file_01),
cmocka_unit_test(test_radle_yaml_write_file_02),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
......@@ -11,3 +11,4 @@ target_sources(unit.t PRIVATE ${CMAKE_CURRENT_LIST_DIR}/yaml_end_token.t.c)
target_sources(unit.t PRIVATE ${CMAKE_CURRENT_LIST_DIR}/yaml_kvp_find.t.c)
target_sources(unit.t PRIVATE ${CMAKE_CURRENT_LIST_DIR}/yaml_token_parse_file.t.c)
target_sources(unit.t PRIVATE ${CMAKE_CURRENT_LIST_DIR}/yaml_value.t.c)
target_sources(unit.t PRIVATE ${CMAKE_CURRENT_LIST_DIR}/yaml_write_file.t.c)
//------------------------------------------------------------------------------
// SPDX-License-Identifier: "Apache-2.0 OR MIT"
// Copyright (C) 2020, Jayesh Badwaik <jayesh@badwaik.in>
//------------------------------------------------------------------------------
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
// cmocka.h has to be included after all the above includes
#include <cmocka.h>
#include <radle/detail/string.h>
#include <radle/detail/yaml_token_parse_file.h>
#include <radle/detail/yaml_write_file.h>
#include <stdlib.h>
#include <string.h>
static char const* directory_prefix = "test/share/input/yaml";
void test_radle_yaml_write_file_01(__attribute__((unused)) void** state);
void test_radle_yaml_write_file_02(__attribute__((unused)) void** state);
void test_radle_yaml_write_file_03(__attribute__((unused)) void** state);
void test_radle_yaml_write_file_04(__attribute__((unused)) void** state);
void test_radle_yaml_write_file_05(__attribute__((unused)) void** state);
void test_radle_yaml_write_file_06(__attribute__((unused)) void** state);
void test_radle_yaml_write_file_07(__attribute__((unused)) void** state);
void test_radle_yaml_write_file_01(__attribute__((unused)) void** state)
{
radle_yaml_result result = radle_yaml_result_success;
char* filename = radle_strcat(directory_prefix, "/01.yml");
radle_yaml_file file = radle_yaml_token_parse_file(
filename, &result, radle_yaml_schema_failsafe);
assert_true(result == radle_yaml_result_success);
assert_true(file.ndocument == 1);
radle_yaml_delete_file(&file);
free(filename);
}
void test_radle_yaml_write_file_02(__attribute__((unused)) void** state)
{
radle_yaml_result result = radle_yaml_result_success;
char* filename = radle_strcat(directory_prefix, "/02.yml");
radle_yaml_file file = radle_yaml_token_parse_file(
filename, &result, radle_yaml_schema_failsafe);
assert_true(result == radle_yaml_result_success);
assert_true(file.ndocument == 2);
radle_yaml_delete_file(&file);
free(filename);
}
void test_radle_yaml_write_file_03(__attribute__((unused)) void** state)
{
radle_yaml_result result = radle_yaml_result_success;
char* filename = radle_strcat(directory_prefix, "/03.yml");
radle_yaml_file file = radle_yaml_token_parse_file(
filename, &result, radle_yaml_schema_failsafe);
assert_true(result == radle_yaml_result_success);
assert_true(file.ndocument == 1);
radle_yaml_delete_file(&file);
free(filename);
}
void test_radle_yaml_write_file_04(__attribute__((unused)) void** state)
{
radle_yaml_result result = radle_yaml_result_success;
char* filename = radle_strcat(directory_prefix, "/04.yml");
radle_yaml_file file = radle_yaml_token_parse_file(
filename, &result, radle_yaml_schema_failsafe);
assert_true(result == radle_yaml_result_success);
assert_true(file.ndocument == 1);
radle_yaml_value* document_ptr = file.document_ptr_array[0];
assert_true(document_ptr->category == radle_yaml_string_c);
assert_true(
strcmp(document_ptr->storage.string, "radle_yaml_parse_file_04") == 0);
radle_yaml_delete_file(&file);
free(filename);
}
void test_radle_yaml_write_file_05(__attribute__((unused)) void** state)
{
radle_yaml_result result = radle_yaml_result_success;
char* filename = radle_strcat(directory_prefix, "/05.yml");
radle_yaml_file file = radle_yaml_token_parse_file(
filename, &result, radle_yaml_schema_failsafe);
assert_true(result == radle_yaml_result_success);
assert_true(file.ndocument == 1);
radle_yaml_value* document_ptr = file.document_ptr_array[0];
assert_true(document_ptr->category == radle_yaml_mapping_c);
radle_yaml_mapping* mapping_ptr = &document_ptr->storage.mapping;
assert_true(mapping_ptr->nkvp == 2);
assert_true(strcmp(mapping_ptr->kvp_ptr_array[0]->key, "alpha") == 0);
assert_true(
mapping_ptr->kvp_ptr_array[0]->value_ptr->category == radle_yaml_string_c);
assert_true(
strcmp(mapping_ptr->kvp_ptr_array[0]->value_ptr->storage.string, "beta")
== 0);
assert_true(strcmp(mapping_ptr->kvp_ptr_array[1]->key, "delta") == 0);
assert_true(
mapping_ptr->kvp_ptr_array[1]->value_ptr->category == radle_yaml_string_c);
assert_true(
strcmp(mapping_ptr->kvp_ptr_array[1]->value_ptr->storage.string, "gamma")
== 0);
radle_yaml_delete_file(&file);
free(filename);
}
void test_radle_yaml_write_file_06(__attribute__((unused)) void** state)
{
radle_yaml_result result = radle_yaml_result_success;
char* filename = radle_strcat(directory_prefix, "/06.yml");
radle_yaml_file file = radle_yaml_token_parse_file(
filename, &result, radle_yaml_schema_failsafe);
assert_true(result == radle_yaml_result_success);
assert_true(file.ndocument == 1);
radle_yaml_value* document_ptr = file.document_ptr_array[0];
assert_true(document_ptr->category == radle_yaml_sequence_c);
radle_yaml_sequence* sequence_ptr = &document_ptr->storage.sequence;
assert_true(sequence_ptr->nvalue == 2);
assert_true(
strcmp(sequence_ptr->value_ptr_array[0]->storage.string, "alpha") == 0);
assert_true(
strcmp(sequence_ptr->value_ptr_array[1]->storage.string, "delta") == 0);
radle_yaml_delete_file(&file);
free(filename);
}
void test_radle_yaml_write_file_07(__attribute__((unused)) void** state)
{
radle_yaml_result result = radle_yaml_result_success;
char* filename = radle_strcat(directory_prefix, "/07.yml");
radle_yaml_file file = radle_yaml_token_parse_file(
filename, &result, radle_yaml_schema_failsafe);
assert_true(result == radle_yaml_result_success);
assert_true(file.ndocument == 1);
radle_yaml_value* document_ptr = file.document_ptr_array[0];
assert_true(document_ptr->category == radle_yaml_mapping_c);
assert_true(document_ptr->storage.mapping.nkvp == 1);
assert_true(
strcmp(document_ptr->storage.mapping.kvp_ptr_array[0]->key, "scratch")
== 0);
radle_yaml_delete_file(&file);
free(filename);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment