Line data Source code
1 : /* File: io_data_file.inl; Copyright and License: see below */
2 :
3 : #include "u8/u8_log.h"
4 : #include <assert.h>
5 :
6 4 : static inline u8_error_t io_data_file_open_writeable ( io_data_file_t *this_,
7 : const char* requested_file_path,
8 : data_stat_t *io_stat,
9 : u8_error_info_t *out_err_info )
10 : {
11 4 : return io_data_file_open( this_, requested_file_path, false, io_stat, out_err_info );
12 : }
13 :
14 0 : static inline u8_error_t io_data_file_open_read_only ( io_data_file_t *this_,
15 : const char* requested_file_path,
16 : data_stat_t *io_stat,
17 : u8_error_info_t *out_err_info )
18 : {
19 0 : return io_data_file_open( this_, requested_file_path, true, io_stat, out_err_info );
20 : }
21 :
22 0 : static inline data_database_t *io_data_file_get_database_ptr ( io_data_file_t *this_ )
23 : {
24 0 : return &((*this_).database);
25 : }
26 :
27 0 : static inline ctrl_controller_t *io_data_file_get_controller_ptr ( io_data_file_t *this_ )
28 : {
29 0 : return &((*this_).controller);
30 : }
31 :
32 : static inline data_revision_t io_data_file_get_sync_revision ( io_data_file_t *this_ )
33 : {
34 : return (*this_).sync_revision;
35 : }
36 :
37 2 : static inline bool io_data_file_is_in_sync ( io_data_file_t *this_ )
38 : {
39 2 : const bool is_open = data_database_is_open( &((*this_).database) );
40 2 : const data_revision_t db_revision = data_database_get_revision( &((*this_).database) );
41 2 : return ( ! is_open ) || ( db_revision == (*this_).sync_revision );
42 : }
43 :
44 2 : static inline const char *io_data_file_get_filename_const ( const io_data_file_t *this_ )
45 : {
46 2 : return utf8stringbuf_get_string( &((*this_).json_file_name) );
47 : }
48 :
49 11 : static inline bool io_data_file_is_open( io_data_file_t *this_ )
50 : {
51 11 : return data_database_is_open( &((*this_).database) );
52 : }
53 :
54 4 : static inline void io_data_file_private_split_path( const io_data_file_t *this_,
55 : const utf8stringview_t *path,
56 : utf8stringview_t *out_parent,
57 : utf8stringview_t *out_filename )
58 : {
59 4 : assert( path != NULL );
60 4 : assert( out_parent != NULL );
61 4 : assert( out_filename != NULL );
62 : utf8stringview_t before_winpath_sep;
63 : utf8stringview_t after_winpath_sep;
64 4 : const utf8error_t err_w = utf8stringview_split_at_last_str( path, "\\", &before_winpath_sep, &after_winpath_sep );
65 4 : if ( err_w == UTF8ERROR_SUCCESS )
66 : {
67 : /* add the separator to before_winpath_sep */
68 0 : before_winpath_sep = UTF8STRINGVIEW( utf8stringview_get_start( &before_winpath_sep ),
69 : utf8stringview_get_length( &before_winpath_sep ) + sizeof(char)
70 : );
71 : }
72 : utf8stringview_t before_unixpath_sep;
73 : utf8stringview_t after_unixpath_sep;
74 4 : const utf8error_t err_u = utf8stringview_split_at_last_str( path, "/", &before_unixpath_sep, &after_unixpath_sep );
75 4 : if ( err_u == UTF8ERROR_SUCCESS )
76 : {
77 : /* add the separator to before_unixpath_sep */
78 0 : before_unixpath_sep = UTF8STRINGVIEW( utf8stringview_get_start( &before_unixpath_sep ),
79 : utf8stringview_get_length( &before_unixpath_sep ) + sizeof(char)
80 : );
81 : }
82 4 : if ( err_w == UTF8ERROR_SUCCESS )
83 : {
84 0 : if ( err_u == UTF8ERROR_SUCCESS )
85 : {
86 : /* There is a win and a unix separator, take the shorter filename */
87 0 : if ( utf8stringview_get_length( &after_winpath_sep ) < utf8stringview_get_length( &after_unixpath_sep ) )
88 : {
89 0 : *out_parent = before_winpath_sep;
90 0 : *out_filename = after_winpath_sep;
91 : }
92 : else
93 : {
94 0 : *out_parent = before_unixpath_sep;
95 0 : *out_filename = after_unixpath_sep;
96 : }
97 : }
98 : else
99 : {
100 : /* There is a win separator */
101 0 : *out_parent = before_winpath_sep;
102 0 : *out_filename = after_winpath_sep;
103 : }
104 : }
105 : else
106 : {
107 4 : if ( err_u == UTF8ERROR_SUCCESS )
108 : {
109 : /* There is a unix separator */
110 0 : *out_parent = before_unixpath_sep;
111 0 : *out_filename = after_unixpath_sep;
112 : }
113 : else
114 : {
115 : /* There is neither a win nor a unix separator */
116 4 : *out_parent = UTF8STRINGVIEW_EMPTY;
117 4 : *out_filename = *path;
118 : }
119 : }
120 4 : }
121 :
122 4 : static inline void io_data_file_private_split_extension( const io_data_file_t *this_,
123 : const utf8stringview_t *filename,
124 : utf8stringview_t *out_basename,
125 : utf8stringview_t *out_extension )
126 : {
127 4 : assert( filename != NULL );
128 4 : assert( out_basename != NULL );
129 4 : assert( out_extension != NULL );
130 : utf8stringview_t before_dot;
131 : utf8stringview_t after_dot;
132 4 : const utf8error_t err = utf8stringview_split_at_last_str( filename, ".", &before_dot, &after_dot );
133 4 : if ( ( err != UTF8ERROR_SUCCESS )
134 4 : || ( utf8stringview_get_length( &before_dot ) == 0 )
135 4 : || ( utf8stringview_get_length( &after_dot ) == 0 ) )
136 0 : {
137 : /* either no dot found or the filename begins with dot or the filename ends on dot */
138 0 : *out_basename = *filename;
139 0 : *out_extension = UTF8STRINGVIEW_EMPTY;
140 : }
141 : else
142 : {
143 4 : *out_basename = before_dot;
144 4 : *out_extension = after_dot;
145 : }
146 4 : }
147 :
148 :
149 : /*
150 : Copyright 2022-2026 Andreas Warnke
151 :
152 : Licensed under the Apache License, Version 2.0 (the "License");
153 : you may not use this file except in compliance with the License.
154 : You may obtain a copy of the License at
155 :
156 : http://www.apache.org/licenses/LICENSE-2.0
157 :
158 : Unless required by applicable law or agreed to in writing, software
159 : distributed under the License is distributed on an "AS IS" BASIS,
160 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
161 : See the License for the specific language governing permissions and
162 : limitations under the License.
163 : */
|