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