LCOV - code coverage report
Current view: top level - io/source - io_importer.c (source / functions) Coverage Total Hit
Test: crystal-facet-uml_v1.63.2_covts Lines: 100.0 % 91 91
Test Date: 2025-05-01 10:10:14 Functions: 100.0 % 5 5

            Line data    Source code
       1              : /* File: io_importer.c; Copyright and License: see below */
       2              : 
       3              : #include "io_importer.h"
       4              : #include "json/json_element_reader.h"
       5              : #include "u8stream/universal_file_input_stream.h"
       6              : #include "u8stream/universal_memory_input_stream.h"
       7              : #include "u8stream/universal_null_output_stream.h"
       8              : #include "u8/u8_error.h"
       9              : #include "utf8stringbuf/utf8string.h"
      10              : #include "u8/u8_trace.h"
      11              : #include <assert.h>
      12              : #include "io_gtk.h"
      13              : #include <stdbool.h>
      14              : 
      15           12 : void io_importer_init ( io_importer_t *this_,
      16              :                         data_database_reader_t *db_reader,
      17              :                         ctrl_controller_t *controller )
      18              : {
      19           12 :     U8_TRACE_BEGIN();
      20           12 :     assert( NULL != db_reader );
      21           12 :     assert( NULL != controller );
      22              : 
      23           12 :     (*this_).db_reader = db_reader;
      24           12 :     (*this_).controller = controller;
      25              : 
      26           12 :     U8_TRACE_END();
      27           12 : }
      28              : 
      29           12 : void io_importer_destroy ( io_importer_t *this_ )
      30              : {
      31           12 :     U8_TRACE_BEGIN();
      32           12 :     assert( NULL != (*this_).db_reader );
      33           12 :     assert( NULL != (*this_).controller );
      34              : 
      35           12 :     (*this_).db_reader = NULL;
      36           12 :     (*this_).controller = NULL;
      37              : 
      38           12 :     U8_TRACE_END();
      39           12 : }
      40              : 
      41           14 : u8_error_t io_importer_import_clipboard( io_importer_t *this_,
      42              :                                          const char *json_text,
      43              :                                          data_row_t diagram_id,
      44              :                                          data_stat_t *io_stat,
      45              :                                          u8_error_info_t *out_err_info )
      46              : {
      47           14 :     U8_TRACE_BEGIN();
      48           14 :     assert( NULL != json_text );
      49           14 :     assert( out_err_info != NULL );
      50           14 :     u8_error_info_init_void( out_err_info );
      51           14 :     u8_error_t result = U8_ERROR_NONE;
      52              : 
      53              :     universal_null_output_stream_t null_out;
      54           14 :     universal_null_output_stream_init( &null_out );
      55              :     utf8stream_writer_t out_writer;
      56           14 :     utf8stream_writer_init( &out_writer, universal_null_output_stream_get_output_stream( &null_out ) );
      57              : 
      58           14 :     io_import_elements_init_for_paste( &((*this_).temp_elements_importer),
      59              :                                        diagram_id,
      60              :                                        (*this_).db_reader,
      61              :                                        (*this_).controller,
      62              :                                        io_stat,
      63              :                                        &out_writer
      64              :                                      );
      65           14 :     json_importer_init( &((*this_).temp_json_importer), &((*this_).temp_elements_importer) );
      66              : 
      67              :     universal_memory_input_stream_t in_mem_stream;
      68           14 :     universal_memory_input_stream_init( &in_mem_stream, json_text, strlen(json_text) );
      69           14 :     universal_input_stream_t* in_stream = universal_memory_input_stream_get_input_stream( &in_mem_stream );
      70              : 
      71           14 :     result = json_importer_import_stream( &((*this_).temp_json_importer),
      72              :                                           in_stream,
      73              :                                           out_err_info
      74              :                                         );
      75              : 
      76           14 :     universal_memory_input_stream_destroy( &in_mem_stream );
      77              : 
      78           14 :     json_importer_destroy( &((*this_).temp_json_importer) );
      79           14 :     io_import_elements_destroy( &((*this_).temp_elements_importer) );
      80              : 
      81           14 :     utf8stream_writer_destroy( &out_writer );
      82           14 :     universal_null_output_stream_destroy( &null_out );
      83              : 
      84           14 :     U8_TRACE_END_ERR( result );
      85           14 :     return result;
      86              : }
      87              : 
      88            2 : u8_error_t io_importer_import_file( io_importer_t *this_,
      89              :                                     io_import_mode_t import_mode,
      90              :                                     const char *import_file_path,
      91              :                                     data_stat_t *io_stat,
      92              :                                     u8_error_info_t *out_err_info,
      93              :                                     utf8stream_writer_t *out_english_report )
      94              : {
      95            2 :     U8_TRACE_BEGIN();
      96            2 :     assert( import_file_path != NULL );
      97            2 :     assert( io_stat != NULL );
      98            2 :     assert( out_english_report != NULL );
      99            2 :     assert( out_err_info != NULL );
     100            2 :     u8_error_info_init_void( out_err_info );
     101            2 :     u8_error_t parse_error = U8_ERROR_NONE;
     102              : 
     103              :     /* open file */
     104              :     universal_file_input_stream_t in_file;
     105            2 :     universal_file_input_stream_init( &in_file );
     106            2 :     parse_error |= universal_file_input_stream_open( &in_file, import_file_path );
     107              : 
     108              :     /* import from stream */
     109            2 :     if ( parse_error == U8_ERROR_NONE )
     110              :     {
     111            2 :         universal_input_stream_t *const in_stream = universal_file_input_stream_get_input_stream( &in_file );
     112            2 :         parse_error = io_importer_import_stream( this_, import_mode, in_stream, io_stat, out_err_info, out_english_report );
     113              :     }
     114              : 
     115              :     /* close file */
     116            2 :     parse_error |= universal_file_input_stream_close( &in_file );
     117            2 :     parse_error |= universal_file_input_stream_destroy( &in_file );
     118              : 
     119            2 :     U8_TRACE_END_ERR( parse_error );
     120            2 :     return parse_error;
     121              : }
     122              : 
     123            3 : u8_error_t io_importer_import_stream( io_importer_t *this_,
     124              :                                       io_import_mode_t import_mode,
     125              :                                       universal_input_stream_t *in_stream,
     126              :                                       data_stat_t *io_stat,
     127              :                                       u8_error_info_t *out_err_info,
     128              :                                       utf8stream_writer_t *out_english_report )
     129              : {
     130            3 :     U8_TRACE_BEGIN();
     131            3 :     assert( in_stream != NULL );
     132            3 :     assert( io_stat != NULL );
     133            3 :     assert( out_english_report != NULL );
     134            3 :     assert( out_err_info != NULL );
     135            3 :     u8_error_info_init_void( out_err_info );
     136            3 :     u8_error_t parse_error = U8_ERROR_NONE;
     137              : 
     138            3 :     io_import_elements_init( &((*this_).temp_elements_importer),
     139              :                              (*this_).db_reader,
     140              :                              (*this_).controller,
     141              :                              io_stat,
     142              :                              out_english_report
     143              :                            );
     144            3 :     json_importer_init( &((*this_).temp_json_importer), &((*this_).temp_elements_importer) );
     145              : 
     146              :     /* check json structure */
     147            3 :     if ( parse_error == U8_ERROR_NONE )
     148              :     {
     149            3 :         U8_TRACE_INFO("scanning file...");
     150              :         static const char *const PASS_CHECK_TITLE
     151              :             = "PASS: Check that the file structure is valid\n      ";
     152            3 :         utf8stream_writer_write_str( out_english_report, PASS_CHECK_TITLE );
     153              : 
     154            3 :         io_import_elements_set_mode( &((*this_).temp_elements_importer), IO_IMPORT_MODE_CHECK );
     155            3 :         parse_error = json_importer_import_stream( &((*this_).temp_json_importer),
     156              :                                                    in_stream,
     157              :                                                    out_err_info
     158              :                                                  );
     159              : 
     160            3 :         utf8stream_writer_write_str( out_english_report, "\n" );
     161              :     }
     162              : 
     163              :     /* reset file */
     164            3 :     if ( parse_error == U8_ERROR_NONE )
     165              :     {
     166            2 :         parse_error |= universal_input_stream_reset( in_stream );
     167              :     }
     168              : 
     169              :     /* start an outer transaction to speed up the creation of objects */
     170            3 :     parse_error |= ctrl_controller_transaction_begin( (*this_).controller );
     171              : 
     172              :     /* import: create elements */
     173            3 :     if (( import_mode != IO_IMPORT_MODE_CHECK )&&( parse_error == U8_ERROR_NONE ))
     174              :     {
     175            2 :         U8_TRACE_INFO("importing file...");
     176              :         static const char *const PASS_CREATE_TITLE
     177              :             = "PASS: Create diagrams, classifiers and features\n      ";
     178            2 :         utf8stream_writer_write_str( out_english_report, PASS_CREATE_TITLE );
     179              : 
     180            2 :         io_import_elements_set_mode( &((*this_).temp_elements_importer), IO_IMPORT_MODE_CREATE );
     181            2 :         parse_error = json_importer_import_stream( &((*this_).temp_json_importer),
     182              :                                                    in_stream,
     183              :                                                    out_err_info
     184              :                                                  );
     185              : 
     186            2 :         utf8stream_writer_write_str( out_english_report, "\n" );
     187              :     }
     188              : 
     189              :     /* reset file once more */
     190            3 :     if ( parse_error == U8_ERROR_NONE )
     191              :     {
     192            2 :         parse_error |= universal_input_stream_reset( in_stream );
     193              :     }
     194              : 
     195              :     /* import: create elements */
     196            3 :     if (( import_mode != IO_IMPORT_MODE_CHECK )&&( parse_error == U8_ERROR_NONE ))
     197              :     {
     198            2 :         U8_TRACE_INFO("importing file...");
     199              :         static const char *const PASS_LINK_TITLE
     200              :             = "PASS: Link diagrams to parents, classifiers to diagrams, create relationships\n      ";
     201            2 :         utf8stream_writer_write_str( out_english_report, PASS_LINK_TITLE );
     202              : 
     203            2 :         io_import_elements_set_mode( &((*this_).temp_elements_importer), IO_IMPORT_MODE_LINK );
     204            2 :         parse_error = json_importer_import_stream( &((*this_).temp_json_importer),
     205              :                                                    in_stream,
     206              :                                                    out_err_info
     207              :                                                  );
     208              : 
     209            2 :         utf8stream_writer_write_str( out_english_report, "\n" );
     210              :     }
     211              : 
     212              :     /* commit the outer transaction */
     213            3 :     parse_error |= ctrl_controller_transaction_commit( (*this_).controller );
     214              : 
     215            3 :     json_importer_destroy( &((*this_).temp_json_importer) );
     216            3 :     io_import_elements_destroy( &((*this_).temp_elements_importer) );
     217              : 
     218            3 :     U8_TRACE_END_ERR( parse_error );
     219            3 :     return parse_error;
     220              : }
     221              : 
     222              : 
     223              : /*
     224              : Copyright 2021-2025 Andreas Warnke
     225              : 
     226              : Licensed under the Apache License, Version 2.0 (the "License");
     227              : you may not use this file except in compliance with the License.
     228              : You may obtain a copy of the License at
     229              : 
     230              :     http://www.apache.org/licenses/LICENSE-2.0
     231              : 
     232              : Unless required by applicable law or agreed to in writing, software
     233              : distributed under the License is distributed on an "AS IS" BASIS,
     234              : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     235              : See the License for the specific language governing permissions and
     236              : limitations under the License.
     237              : */
        

Generated by: LCOV version 2.0-1