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