File: main_commands.c

package info (click to toggle)
crystal-facet-uml 1.63.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 21,640 kB
  • sloc: ansic: 109,610; xml: 3,085; makefile: 138; sh: 113
file content (379 lines) | stat: -rw-r--r-- 14,775 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* File: main_commands.c; Copyright and License: see below */

#include "main_commands.h"
#include "gui_main.h"
#include "gui_error_info_printer.h"
#include "io_exporter.h"
#include "io_importer.h"
#include "ctrl_controller.h"
#include "u8/u8_trace.h"
#include "u8/u8_log.h"
#include <gtk/gtk.h>
#include <stdbool.h>
#include <assert.h>

static io_data_file_t single_big_data_file;  /*!< a data_file struct, placed in the data segment due to its >5MB size */

u8_error_t main_commands_init ( main_commands_t *this_, bool start_gui, int argc, char **argv )
{
    U8_TRACE_BEGIN();
    u8_error_t result = U8_ERROR_NONE;

    (*this_).argc = argc;
    (*this_).argv = argv;

    /* initialize the base libraries: gobject, gio, glib, gdk and gtk */
    if ( start_gui )
    {
        gtk_init();
        /* if this program was not terminated, gtk init was successful. */
    }
    else
    {
        const gboolean success = gtk_init_check();
        if ( ! success )
        {
            U8_LOG_WARNING("gtk could not be initialized.");
            /* no error here, if no gui requested - test fail otherwise */
        }
    }

    U8_TRACE_INFO("starting DB and its controller...");
    U8_TRACE_INFO_INT("sizeof(io_data_file_t)/B:",sizeof(io_data_file_t));
    (*this_).data_file = &single_big_data_file;
    io_data_file_init( (*this_).data_file );

    U8_TRACE_END_ERR( result );
    return result;
}

void main_commands_destroy ( main_commands_t *this_ )
{
    U8_TRACE_BEGIN();

    U8_TRACE_INFO("stopping DB and its controller...");
    io_data_file_destroy( (*this_).data_file );

    U8_TRACE_END();
}

u8_error_t main_commands_repair ( main_commands_t *this_,
                                  const char *data_file_path,
                                  bool check_only,
                                  utf8stream_writer_t *out_english_report )
{
    U8_TRACE_BEGIN();
    assert( data_file_path != NULL );
    const bool do_repair = ( ! check_only );
    u8_error_t result = U8_ERROR_NONE;

    U8_TRACE_INFO("opening DB...");
    u8_error_info_t err_info;
    result |= io_data_file_open( (*this_).data_file, data_file_path, check_only, &err_info );
    if ( result != U8_ERROR_NONE )
    {
        utf8stream_writer_write_str( out_english_report, "error opening database_file " );
        utf8stream_writer_write_str( out_english_report, data_file_path );
        utf8stream_writer_write_str( out_english_report, "\n" );
    }
    result |= main_commands_private_report_error_info( this_, &err_info, out_english_report );

    U8_TRACE_INFO("reparing/testing...");
    utf8stream_writer_write_str( out_english_report, "\n\n" );
    result |= ctrl_controller_repair_database( io_data_file_get_controller_ptr( (*this_).data_file ),
                                               do_repair,
                                               NULL,
                                               NULL,
                                               out_english_report
                                             );
    utf8stream_writer_write_str( out_english_report, "\n" );
    U8_TRACE_INFO("reparing/testing finished.");

    U8_TRACE_INFO("closing DB...");
    io_data_file_close( (*this_).data_file );

    U8_TRACE_END_ERR( result );
    return result;
}

u8_error_t main_commands_start_gui ( main_commands_t *this_,
                                     const char *data_file_path,
                                     utf8stream_writer_t *out_english_report )
{
    U8_TRACE_BEGIN();
    u8_error_t result = U8_ERROR_NONE;

    U8_TRACE_TIMESTAMP();
    U8_TRACE_INFO("opening DB...");
    if ( NULL != data_file_path )
    {
        u8_error_info_t err_info;
        result |= io_data_file_open_writeable( (*this_).data_file, data_file_path, &err_info );
        if ( result != U8_ERROR_NONE )
        {
            utf8stream_writer_write_str( out_english_report, "error opening database_file " );
            utf8stream_writer_write_str( out_english_report, data_file_path );
            utf8stream_writer_write_str( out_english_report, "\n" );
        }
        result |= main_commands_private_report_error_info( this_, &err_info, out_english_report );
    }

    U8_TRACE_TIMESTAMP();
    U8_TRACE_INFO("running GUI...");
    gui_main( (*this_).data_file, (*this_).argc, (*this_).argv );
    U8_TRACE_INFO("GUI stopped.");

    U8_TRACE_TIMESTAMP();
    U8_TRACE_INFO("closing DB...");
    io_data_file_close( (*this_).data_file );

    U8_TRACE_END_ERR( result );
    return result;
}

u8_error_t main_commands_export ( main_commands_t *this_,
                                  const char *data_file_path,
                                  io_file_format_t export_format,
                                  const char *export_directory,
                                  utf8stream_writer_t *out_english_report )
{
    U8_TRACE_BEGIN();
    assert( data_file_path != NULL );
    assert( export_directory != NULL );
    u8_error_t export_err = U8_ERROR_NONE;

    U8_TRACE_INFO("opening DB...");
    u8_error_info_t err_info;
    export_err |= io_data_file_open_read_only( (*this_).data_file, data_file_path, &err_info );
    if ( export_err != U8_ERROR_NONE )
    {
        utf8stream_writer_write_str( out_english_report, "error opening database_file " );
        utf8stream_writer_write_str( out_english_report, data_file_path );
        utf8stream_writer_write_str( out_english_report, "\n" );
    }
    export_err |= main_commands_private_report_error_info( this_, &err_info, out_english_report );

    U8_TRACE_INFO("exporting DB...");
    U8_TRACE_INFO_STR( "chosen folder:", export_directory );
    const char *document_filename = io_data_file_get_filename_const( (*this_).data_file );
    if ( io_data_file_is_open( (*this_).data_file ) )
    {
        static data_database_reader_t db_reader;
        data_database_reader_init( &db_reader, io_data_file_get_database_ptr( (*this_).data_file ) );
        static io_exporter_t exporter;
        io_exporter_init( &exporter, &db_reader );
        {
            data_stat_t export_stat;
            data_stat_init ( &export_stat );
            export_err = io_exporter_export_files( &exporter, export_format, export_directory, document_filename, &export_stat );
            export_err |= main_commands_private_report_stat( this_, &export_stat, "exported", out_english_report );
            data_stat_trace( &export_stat );
            data_stat_destroy ( &export_stat );
        }
        io_exporter_destroy( &exporter );
        data_database_reader_destroy( &db_reader );
    }
    else
    {
        export_err = U8_ERROR_NO_DB;
    }

    U8_TRACE_INFO("closing DB...");
    io_data_file_close( (*this_).data_file );

    U8_TRACE_END_ERR( export_err );
    return export_err;
}

u8_error_t main_commands_import ( main_commands_t *this_,
                                  const char *data_file_path,
                                  io_import_mode_t import_mode,
                                  const char *import_file_path,
                                  utf8stream_writer_t *out_english_report )
{
    U8_TRACE_BEGIN();
    assert( data_file_path != NULL );
    assert( import_file_path != NULL );
    u8_error_t import_err = U8_ERROR_NONE;

    U8_TRACE_INFO("opening DB...");
    u8_error_info_t err_info;
    import_err |= io_data_file_open_writeable( (*this_).data_file, data_file_path, &err_info );
    if ( import_err != U8_ERROR_NONE )
    {
        utf8stream_writer_write_str( out_english_report, "error opening database_file " );
        utf8stream_writer_write_str( out_english_report, data_file_path );
        utf8stream_writer_write_str( out_english_report, "\n" );
    }
    import_err |= main_commands_private_report_error_info( this_, &err_info, out_english_report );

    U8_TRACE_INFO("importing data...");
    U8_TRACE_INFO_STR( "chosen data:", import_file_path );
    if ( io_data_file_is_open( (*this_).data_file ) )
    {
        static data_database_reader_t db_reader;
        data_database_reader_init( &db_reader, io_data_file_get_database_ptr( (*this_).data_file ) );
        static io_importer_t importer;
        io_importer_init( &importer, &db_reader, io_data_file_get_controller_ptr( (*this_).data_file ));
        {
            data_stat_t import_stat;
            data_stat_init ( &import_stat );
            u8_error_info_t err_info;
            import_err = io_importer_import_file( &importer, import_mode, import_file_path, &import_stat, &err_info, out_english_report );
            import_err |= main_commands_private_report_stat( this_, &import_stat, "imported", out_english_report );
            import_err |= main_commands_private_report_error_info( this_, &err_info, out_english_report );
            data_stat_trace( &import_stat );
            data_stat_destroy ( &import_stat );
        }
        io_importer_destroy( &importer );
        data_database_reader_destroy( &db_reader );
    }
    else
    {
        import_err = U8_ERROR_NO_DB;
    }

    U8_TRACE_INFO("closing DB...");
    io_data_file_close( (*this_).data_file );

    {
        utf8stream_writer_write_str( out_english_report, "\nplease test the integrity of the database_file:\n" );
        utf8stream_writer_write_str( out_english_report, "crystal-facet-uml -t " );
        utf8stream_writer_write_str( out_english_report, data_file_path );
        utf8stream_writer_write_str( out_english_report, "\n\n" );
    }

    U8_TRACE_END_ERR( import_err );
    return import_err;
}

static const char *const series[DATA_STAT_SERIES_MAX] = {
    [DATA_STAT_SERIES_CREATED]  = " new/exp",
    [DATA_STAT_SERIES_MODIFIED] = "modified",
    [DATA_STAT_SERIES_DELETED]  = " deleted",
    [DATA_STAT_SERIES_IGNORED]  = " ignored",
    [DATA_STAT_SERIES_WARNING]  = "warnings",
    [DATA_STAT_SERIES_ERROR]    = "  errors",
};

static const char *const tables[DATA_STAT_TABLE_MAX] = {
    [DATA_TABLE_VOID] = "life",
    [DATA_TABLE_CLASSIFIER] = "clas",
    [DATA_TABLE_FEATURE] = "feat",
    [DATA_TABLE_RELATIONSHIP] = "rel",
    [DATA_TABLE_DIAGRAMELEMENT] = "d_ele",
    [DATA_TABLE_DIAGRAM] = "diag",
};

u8_error_t main_commands_private_report_stat ( main_commands_t *this_,
                                               const data_stat_t *stat,
                                               const char* mode_name,
                                               utf8stream_writer_t *out_english_report )
{
    U8_TRACE_BEGIN();
    assert( stat != NULL );
    assert( mode_name != NULL );
    assert( out_english_report != NULL );
    u8_error_t write_err = U8_ERROR_NONE;

    /* HEADLINE */
    write_err |= utf8stream_writer_write_str( out_english_report, "\n\t" );

    for ( int tables_idx = 0; tables_idx < DATA_STAT_TABLE_MAX; tables_idx ++ )
    {
        write_err |= utf8stream_writer_write_str( out_english_report, "\t" );
        write_err |= utf8stream_writer_write_str( out_english_report, tables[tables_idx] );
    }
    write_err |= utf8stream_writer_write_str( out_english_report, "\n" );

    /* TABLE */
    for ( int series_idx = 0; series_idx < DATA_STAT_SERIES_MAX; series_idx ++ )
    {
        const uint_fast32_t s_cnt = data_stat_get_series_count( stat, series_idx );

        if ( series_idx == DATA_STAT_SERIES_CREATED )
        {
            write_err |= utf8stream_writer_write_str( out_english_report, mode_name );
        }
        else
        {
            write_err |= utf8stream_writer_write_str( out_english_report, series[series_idx] );
        }
        write_err |= utf8stream_writer_write_str( out_english_report, ": " );

        for ( int tables_idx = 0; tables_idx < DATA_STAT_TABLE_MAX; tables_idx ++ )
        {
            write_err |= utf8stream_writer_write_str( out_english_report, "\t" );
            const uint_fast32_t cnt = data_stat_get_count( stat, tables_idx, series_idx );
            if ( cnt != 0 )
            {
                write_err |= utf8stream_writer_write_int( out_english_report, cnt );
            }
        }

        write_err |= utf8stream_writer_write_str( out_english_report, "\t: " );
        write_err |= utf8stream_writer_write_int( out_english_report, s_cnt );
        write_err |= utf8stream_writer_write_str( out_english_report, "\n" );
    }

    /* ROW OF SUMS */
    write_err |= utf8stream_writer_write_str( out_english_report, "     SUM:" );
    for ( int tables_idx = 0; tables_idx < DATA_STAT_TABLE_MAX; tables_idx ++ )
    {
        const uint_fast32_t t_cnt = data_stat_get_table_count( stat, tables_idx );
        write_err |= utf8stream_writer_write_str( out_english_report, "\t" );
        write_err |= utf8stream_writer_write_int( out_english_report, t_cnt );
    }
    const uint_fast32_t total = data_stat_get_total_count( stat );
    write_err |= utf8stream_writer_write_str( out_english_report, "\t: " );
    write_err |= utf8stream_writer_write_int( out_english_report, total );
    write_err |= utf8stream_writer_write_str( out_english_report, "\n" );

    U8_TRACE_END_ERR( write_err );
    return write_err;
}

u8_error_t main_commands_private_report_error_info ( main_commands_t *this_,
                                                     const u8_error_info_t *error_info,
                                                     utf8stream_writer_t *out_english_report )
{
    U8_TRACE_BEGIN();
    assert( error_info != NULL );
    assert( out_english_report != NULL );
    u8_error_t write_err = U8_ERROR_NONE;

    if ( u8_error_info_is_error( error_info ) )
    {
        char info_string_buf[80];
        utf8stringbuf_t info_string = UTF8STRINGBUF(info_string_buf);
        utf8stringbuf_clear( &info_string );
        {
            gui_error_info_printer_t my_err_info_printer;
            gui_error_info_printer_init( &my_err_info_printer );
            write_err |= gui_error_info_printer_show_error_info( &my_err_info_printer, error_info, info_string );
            gui_error_info_printer_destroy( &my_err_info_printer );
        }
        write_err |= utf8stream_writer_write_str( out_english_report, utf8stringbuf_get_string( &info_string ) );
        write_err |= utf8stream_writer_write_str( out_english_report, "\n" );
    }

    U8_TRACE_END_ERR( write_err );
    return write_err;
}


/*
Copyright 2016-2025 Andreas Warnke

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/