Line data Source code
1 : /* File: io_exporter_light.h; Copyright and License: see below */ 2 : 3 : #include "json/json_element_writer.h" 4 : #include "json/json_writer_pass.h" 5 : #include "u8stream/universal_output_stream.h" 6 : #include "u8stream/universal_memory_output_stream.h" 7 : #include <assert.h> 8 : 9 0 : static inline void io_exporter_light_init ( io_exporter_light_t *this_, data_database_reader_t *db_reader ) 10 : { 11 0 : assert( NULL != db_reader ); 12 0 : (*this_).db_reader = db_reader; 13 0 : } 14 : 15 0 : static inline void io_exporter_light_destroy ( io_exporter_light_t *this_ ) 16 : { 17 0 : assert( NULL != (*this_).db_reader ); 18 0 : (*this_).db_reader = NULL; 19 0 : } 20 : 21 0 : static inline u8_error_t io_exporter_light_export_set_to_buf( io_exporter_light_t *this_, 22 : const data_small_set_t *set_to_be_exported, 23 : data_stat_t *io_export_stat, 24 : utf8stringbuf_t out_buf ) 25 : { 26 0 : assert( NULL != set_to_be_exported ); 27 0 : assert( NULL != io_export_stat ); 28 0 : u8_error_t exp_err = U8_ERROR_NONE; 29 : data_stat_t count_just_once; 30 0 : data_stat_init( &count_just_once ); 31 : 32 : /* initialize an output stream */ 33 : universal_memory_output_stream_t memout; 34 0 : universal_memory_output_stream_init( &memout, 35 0 : utf8stringbuf_get_string( out_buf ), 36 : utf8stringbuf_get_size( out_buf ), 37 : UNIVERSAL_MEMORY_OUTPUT_STREAM_0TERM_UTF8 38 : ); 39 : universal_output_stream_t *output; 40 0 : output = universal_memory_output_stream_get_output_stream( &memout ); 41 : 42 : /* initialize an element writer */ 43 : json_element_writer_t json_writer; 44 0 : json_element_writer_init( &json_writer, io_export_stat, output ); 45 : io_element_writer_t *element_writer; 46 0 : element_writer = json_element_writer_get_element_writer( &json_writer ); 47 : 48 : /* initialize a traversal */ 49 0 : io_export_set_traversal_init( &((*this_).temp_set_traversal), 50 : (*this_).db_reader, 51 : &count_just_once, 52 : element_writer 53 : ); 54 : 55 : /* do traversal */ 56 0 : const char *const document_title = ""; 57 0 : exp_err |= io_element_writer_write_header( element_writer, document_title ); 58 0 : json_element_writer_set_mode( &json_writer, JSON_WRITER_PASS_VIEWS ); 59 0 : exp_err |= io_export_set_traversal_export_set( &((*this_).temp_set_traversal), set_to_be_exported ); 60 0 : data_stat_add( io_export_stat, &count_just_once ); /* after the first pass, report the error statistics */ 61 0 : json_element_writer_set_mode( &json_writer, JSON_WRITER_PASS_NODES ); 62 0 : exp_err |= io_export_set_traversal_export_set( &((*this_).temp_set_traversal), set_to_be_exported ); 63 0 : json_element_writer_set_mode( &json_writer, JSON_WRITER_PASS_EDGES ); 64 0 : exp_err |= io_export_set_traversal_export_set( &((*this_).temp_set_traversal), set_to_be_exported ); 65 0 : exp_err |= io_element_writer_write_footer( element_writer ); 66 : 67 : /* de-initialize a traversal */ 68 0 : io_export_set_traversal_destroy ( &((*this_).temp_set_traversal) ); 69 : 70 : /* de-initialize an element writer */ 71 0 : json_element_writer_destroy( &json_writer ); 72 : 73 : /* de-initialize an output stream */ 74 0 : exp_err |= universal_memory_output_stream_flush( &memout ); /* enforces 0-termination */ 75 0 : exp_err |= universal_memory_output_stream_destroy( &memout ); 76 : 77 0 : data_stat_destroy( &count_just_once ); 78 0 : return exp_err; 79 : } 80 : 81 : 82 : /* 83 : Copyright 2021-2024 Andreas Warnke 84 : 85 : Licensed under the Apache License, Version 2.0 (the "License"); 86 : you may not use this file except in compliance with the License. 87 : You may obtain a copy of the License at 88 : 89 : http://www.apache.org/licenses/LICENSE-2.0 90 : 91 : Unless required by applicable law or agreed to in writing, software 92 : distributed under the License is distributed on an "AS IS" BASIS, 93 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 94 : See the License for the specific language governing permissions and 95 : limitations under the License. 96 : */