LCOV - code coverage report
Current view: top level - io/source - io_importer.c (source / functions) Hit Total Coverage
Test: crystal-facet-uml_v1.61.0_covts Lines: 91 91 100.0 %
Date: 2024-10-26 21:44:38 Functions: 5 5 100.0 %

          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 <gtk/gtk.h>
      13             : #include <stdbool.h>
      14             : 
      15          11 : void io_importer_init ( io_importer_t *this_,
      16             :                         data_database_reader_t *db_reader,
      17             :                         ctrl_controller_t *controller )
      18             : {
      19          11 :     U8_TRACE_BEGIN();
      20          11 :     assert( NULL != db_reader );
      21          11 :     assert( NULL != controller );
      22             : 
      23          11 :     (*this_).db_reader = db_reader;
      24          11 :     (*this_).controller = controller;
      25             : 
      26          11 :     U8_TRACE_END();
      27          11 : }
      28             : 
      29          11 : void io_importer_destroy ( io_importer_t *this_ )
      30             : {
      31          11 :     U8_TRACE_BEGIN();
      32          11 :     assert( NULL != (*this_).db_reader );
      33          11 :     assert( NULL != (*this_).controller );
      34             : 
      35          11 :     (*this_).db_reader = NULL;
      36          11 :     (*this_).controller = NULL;
      37             : 
      38          11 :     U8_TRACE_END();
      39          11 : }
      40             : 
      41          14 : u8_error_t io_importer_import_clipboard( io_importer_t *this_,
      42             :                                          const char *json_text,
      43             :                                          data_row_id_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           1 : 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           1 :     U8_TRACE_BEGIN();
      96           1 :     assert( import_file_path != NULL );
      97           1 :     assert( io_stat != NULL );
      98           1 :     assert( out_english_report != NULL );
      99           1 :     assert( out_err_info != NULL );
     100           1 :     u8_error_info_init_void( out_err_info );
     101           1 :     u8_error_t parse_error = U8_ERROR_NONE;
     102             : 
     103             :     /* open file */
     104             :     universal_file_input_stream_t in_file;
     105           1 :     universal_file_input_stream_init( &in_file );
     106           1 :     parse_error |= universal_file_input_stream_open( &in_file, import_file_path );
     107             : 
     108             :     /* import from stream */
     109           1 :     if ( parse_error == U8_ERROR_NONE )
     110             :     {
     111           1 :         universal_input_stream_t *const in_stream = universal_file_input_stream_get_input_stream( &in_file );
     112           1 :         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           1 :     parse_error |= universal_file_input_stream_close( &in_file );
     117           1 :     parse_error |= universal_file_input_stream_destroy( &in_file );
     118             : 
     119           1 :     U8_TRACE_END_ERR( parse_error );
     120           1 :     return parse_error;
     121             : }
     122             : 
     123           2 : 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           2 :     U8_TRACE_BEGIN();
     131           2 :     assert( in_stream != NULL );
     132           2 :     assert( io_stat != NULL );
     133           2 :     assert( out_english_report != NULL );
     134           2 :     assert( out_err_info != NULL );
     135           2 :     u8_error_info_init_void( out_err_info );
     136           2 :     u8_error_t parse_error = U8_ERROR_NONE;
     137             : 
     138           2 :     io_import_elements_init( &((*this_).temp_elements_importer),
     139             :                              (*this_).db_reader,
     140             :                              (*this_).controller,
     141             :                              io_stat,
     142             :                              out_english_report
     143             :                            );
     144           2 :     json_importer_init( &((*this_).temp_json_importer), &((*this_).temp_elements_importer) );
     145             : 
     146             :     /* check json structure */
     147           2 :     if ( parse_error == U8_ERROR_NONE )
     148             :     {
     149           2 :         U8_TRACE_INFO("scanning file...");
     150             :         static const char *const PASS_CHECK_TITLE
     151             :             = "PASS: Check that the file structure is valid\n      ";
     152           2 :         utf8stream_writer_write_str( out_english_report, PASS_CHECK_TITLE );
     153             : 
     154           2 :         io_import_elements_set_mode( &((*this_).temp_elements_importer), IO_IMPORT_MODE_CHECK );
     155           2 :         parse_error = json_importer_import_stream( &((*this_).temp_json_importer),
     156             :                                                    in_stream,
     157             :                                                    out_err_info
     158             :                                                  );
     159             : 
     160           2 :         utf8stream_writer_write_str( out_english_report, "\n" );
     161             :     }
     162             : 
     163             :     /* reset file */
     164           2 :     if ( parse_error == U8_ERROR_NONE )
     165             :     {
     166           1 :         parse_error |= universal_input_stream_reset( in_stream );
     167             :     }
     168             : 
     169             :     /* start an outer transaction to speed up the creation of objects */
     170           2 :     parse_error |= ctrl_controller_transaction_begin( (*this_).controller );
     171             : 
     172             :     /* import: create elements */
     173           2 :     if (( import_mode != IO_IMPORT_MODE_CHECK )&&( parse_error == U8_ERROR_NONE ))
     174             :     {
     175           1 :         U8_TRACE_INFO("importing file...");
     176             :         static const char *const PASS_CREATE_TITLE
     177             :             = "PASS: Create diagrams, classifiers and features\n      ";
     178           1 :         utf8stream_writer_write_str( out_english_report, PASS_CREATE_TITLE );
     179             : 
     180           1 :         io_import_elements_set_mode( &((*this_).temp_elements_importer), IO_IMPORT_MODE_CREATE );
     181           1 :         parse_error = json_importer_import_stream( &((*this_).temp_json_importer),
     182             :                                                    in_stream,
     183             :                                                    out_err_info
     184             :                                                  );
     185             : 
     186           1 :         utf8stream_writer_write_str( out_english_report, "\n" );
     187             :     }
     188             : 
     189             :     /* reset file once more */
     190           2 :     if ( parse_error == U8_ERROR_NONE )
     191             :     {
     192           1 :         parse_error |= universal_input_stream_reset( in_stream );
     193             :     }
     194             : 
     195             :     /* import: create elements */
     196           2 :     if (( import_mode != IO_IMPORT_MODE_CHECK )&&( parse_error == U8_ERROR_NONE ))
     197             :     {
     198           1 :         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           1 :         utf8stream_writer_write_str( out_english_report, PASS_LINK_TITLE );
     202             : 
     203           1 :         io_import_elements_set_mode( &((*this_).temp_elements_importer), IO_IMPORT_MODE_LINK );
     204           1 :         parse_error = json_importer_import_stream( &((*this_).temp_json_importer),
     205             :                                                    in_stream,
     206             :                                                    out_err_info
     207             :                                                  );
     208             : 
     209           1 :         utf8stream_writer_write_str( out_english_report, "\n" );
     210             :     }
     211             : 
     212             :     /* commit the outer transaction */
     213           2 :     parse_error |= ctrl_controller_transaction_commit( (*this_).controller );
     214             : 
     215           2 :     json_importer_destroy( &((*this_).temp_json_importer) );
     216           2 :     io_import_elements_destroy( &((*this_).temp_elements_importer) );
     217             : 
     218           2 :     U8_TRACE_END_ERR( parse_error );
     219           2 :     return parse_error;
     220             : }
     221             : 
     222             : 
     223             : /*
     224             : Copyright 2021-2024 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 1.16