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