2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* file_access.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2017-08-27 12:16:55 +00:00
/* https://godotengine.org */
2014-02-10 01:10:30 +00:00
/*************************************************************************/
2022-01-03 20:27:34 +00:00
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
2014-02-10 01:10:30 +00:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2018-01-04 23:50:27 +00:00
2014-02-10 01:10:30 +00:00
# ifndef FILE_ACCESS_H
# define FILE_ACCESS_H
2018-09-11 16:13:45 +00:00
# include "core/math/math_defs.h"
2022-03-23 09:08:58 +00:00
# include "core/object/ref_counted.h"
2018-09-11 16:13:45 +00:00
# include "core/os/memory.h"
2020-11-07 22:33:38 +00:00
# include "core/string/ustring.h"
2018-09-11 16:13:45 +00:00
# include "core/typedefs.h"
2014-02-10 01:10:30 +00:00
/**
* Multi - Platform abstraction for accessing to files .
*/
2022-03-23 09:08:58 +00:00
class FileAccess : public RefCounted {
2014-02-10 01:10:30 +00:00
public :
enum AccessType {
ACCESS_RESOURCES ,
ACCESS_USERDATA ,
ACCESS_FILESYSTEM ,
ACCESS_MAX
} ;
2016-06-13 13:10:50 +00:00
typedef void ( * FileCloseFailNotify ) ( const String & ) ;
2022-03-23 09:08:58 +00:00
typedef Ref < FileAccess > ( * CreateFunc ) ( ) ;
2021-05-20 12:58:03 +00:00
bool big_endian = false ;
2020-05-12 15:01:17 +00:00
bool real_is_double = false ;
2014-02-10 01:10:30 +00:00
2019-04-07 18:46:52 +00:00
virtual uint32_t _get_unix_permissions ( const String & p_file ) = 0 ;
virtual Error _set_unix_permissions ( const String & p_file , uint32_t p_permissions ) = 0 ;
2014-02-10 01:10:30 +00:00
protected :
2022-08-24 18:06:03 +00:00
AccessType get_access_type ( ) const ;
2014-02-10 01:10:30 +00:00
String fix_path ( const String & p_path ) const ;
virtual Error _open ( const String & p_path , int p_mode_flags ) = 0 ; ///< open a file
virtual uint64_t _get_modified_time ( const String & p_file ) = 0 ;
2016-06-13 13:10:50 +00:00
static FileCloseFailNotify close_fail_notify ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
private :
2014-02-10 01:10:30 +00:00
static bool backup_save ;
2020-05-12 15:01:17 +00:00
AccessType _access_type = ACCESS_FILESYSTEM ;
2014-02-10 01:10:30 +00:00
static CreateFunc create_func [ ACCESS_MAX ] ; /** default file access creation function for a platform */
template < class T >
2022-03-23 09:08:58 +00:00
static Ref < FileAccess > _create_builtin ( ) {
2014-02-10 01:10:30 +00:00
return memnew ( T ) ;
}
public :
2016-06-13 13:10:50 +00:00
static void set_file_close_fail_notify_callback ( FileCloseFailNotify p_cbk ) { close_fail_notify = p_cbk ; }
2014-02-10 01:10:30 +00:00
virtual void _set_access_type ( AccessType p_access ) ;
enum ModeFlags {
READ = 1 ,
WRITE = 2 ,
READ_WRITE = 3 ,
2016-01-10 18:15:04 +00:00
WRITE_READ = 7 ,
2014-02-10 01:10:30 +00:00
} ;
2016-03-08 23:00:52 +00:00
virtual bool is_open ( ) const = 0 ; ///< true when file is open
2014-02-10 01:10:30 +00:00
2018-03-09 14:45:21 +00:00
virtual String get_path ( ) const { return " " ; } /// returns the path for the current open file
virtual String get_path_absolute ( ) const { return " " ; } /// returns the absolute path for the current open file
2019-03-26 17:51:13 +00:00
virtual void seek ( uint64_t p_position ) = 0 ; ///< seek to a given position
virtual void seek_end ( int64_t p_position = 0 ) = 0 ; ///< seek from the end of file with negative offset
virtual uint64_t get_position ( ) const = 0 ; ///< get position in the file
2021-05-25 06:58:49 +00:00
virtual uint64_t get_length ( ) const = 0 ; ///< get size of the file
2014-02-10 01:10:30 +00:00
2016-03-08 23:00:52 +00:00
virtual bool eof_reached ( ) const = 0 ; ///< reading passed EOF
virtual uint8_t get_8 ( ) const = 0 ; ///< get a byte
virtual uint16_t get_16 ( ) const ; ///< get 16 bits uint
virtual uint32_t get_32 ( ) const ; ///< get 32 bits uint
virtual uint64_t get_64 ( ) const ; ///< get 64 bits uint
2014-02-10 01:10:30 +00:00
virtual float get_float ( ) const ;
virtual double get_double ( ) const ;
virtual real_t get_real ( ) const ;
2019-03-26 17:51:13 +00:00
virtual uint64_t get_buffer ( uint8_t * p_dst , uint64_t p_length ) const ; ///< get an array of bytes
2014-02-10 01:10:30 +00:00
virtual String get_line ( ) const ;
2017-05-29 00:46:48 +00:00
virtual String get_token ( ) const ;
2018-11-13 11:53:24 +00:00
virtual Vector < String > get_csv_line ( const String & p_delim = " , " ) const ;
2022-07-31 16:46:53 +00:00
virtual String get_as_utf8_string ( bool p_skip_cr = false ) const ;
2016-03-08 23:00:52 +00:00
2021-05-20 12:58:03 +00:00
/**
* Use this for files WRITTEN in _big_ endian machines ( ie , amiga / mac )
2014-02-10 01:10:30 +00:00
* It ' s not about the current CPU type but file formats .
2021-05-20 12:58:03 +00:00
* This flag gets reset to ` false ` ( little endian ) on each open .
2014-02-10 01:10:30 +00:00
*/
2021-05-20 12:58:03 +00:00
virtual void set_big_endian ( bool p_big_endian ) { big_endian = p_big_endian ; }
inline bool is_big_endian ( ) const { return big_endian ; }
2014-02-10 01:10:30 +00:00
2016-03-08 23:00:52 +00:00
virtual Error get_error ( ) const = 0 ; ///< get last error
2014-02-10 01:10:30 +00:00
2017-09-22 05:56:02 +00:00
virtual void flush ( ) = 0 ;
2016-03-08 23:00:52 +00:00
virtual void store_8 ( uint8_t p_dest ) = 0 ; ///< store a byte
virtual void store_16 ( uint16_t p_dest ) ; ///< store 16 bits uint
virtual void store_32 ( uint32_t p_dest ) ; ///< store 32 bits uint
virtual void store_64 ( uint64_t p_dest ) ; ///< store 64 bits uint
2014-02-10 01:10:30 +00:00
virtual void store_float ( float p_dest ) ;
virtual void store_double ( double p_dest ) ;
virtual void store_real ( real_t p_real ) ;
virtual void store_string ( const String & p_string ) ;
2017-08-11 19:10:05 +00:00
virtual void store_line ( const String & p_line ) ;
2018-11-13 11:53:24 +00:00
virtual void store_csv_line ( const Vector < String > & p_values , const String & p_delim = " , " ) ;
2014-02-10 01:10:30 +00:00
2014-03-14 01:57:24 +00:00
virtual void store_pascal_string ( const String & p_string ) ;
virtual String get_pascal_string ( ) ;
2019-03-26 17:51:13 +00:00
virtual void store_buffer ( const uint8_t * p_src , uint64_t p_length ) ; ///< store an array of bytes
2016-03-08 23:00:52 +00:00
virtual bool file_exists ( const String & p_name ) = 0 ; ///< return true if a file exists
2014-02-10 01:10:30 +00:00
virtual Error reopen ( const String & p_path , int p_mode_flags ) ; ///< does not change the AccessType
2022-03-23 09:08:58 +00:00
static Ref < FileAccess > create ( AccessType p_access ) ; /// Create a file access (for the current platform) this is the only portable way of accessing files.
static Ref < FileAccess > create_for_path ( const String & p_path ) ;
static Ref < FileAccess > open ( const String & p_path , int p_mode_flags , Error * r_error = nullptr ) ; /// Create a file access (for the current platform) this is the only portable way of accessing files.
2014-02-10 01:10:30 +00:00
static CreateFunc get_create_func ( AccessType p_access ) ;
static bool exists ( const String & p_name ) ; ///< return true if a file exists
static uint64_t get_modified_time ( const String & p_file ) ;
2019-04-07 18:46:52 +00:00
static uint32_t get_unix_permissions ( const String & p_file ) ;
static Error set_unix_permissions ( const String & p_file , uint32_t p_permissions ) ;
2014-02-10 01:10:30 +00:00
static void set_backup_save ( bool p_enable ) { backup_save = p_enable ; } ;
static bool is_backup_save_enabled ( ) { return backup_save ; } ;
static String get_md5 ( const String & p_file ) ;
2016-06-17 07:55:16 +00:00
static String get_sha256 ( const String & p_file ) ;
2017-12-27 18:21:18 +00:00
static String get_multiple_md5 ( const Vector < String > & p_file ) ;
2014-02-10 01:10:30 +00:00
2020-04-01 23:20:12 +00:00
static Vector < uint8_t > get_file_as_array ( const String & p_path , Error * r_error = nullptr ) ;
static String get_file_as_string ( const String & p_path , Error * r_error = nullptr ) ;
2014-02-10 01:10:30 +00:00
template < class T >
static void make_default ( AccessType p_access ) {
create_func [ p_access ] = _create_builtin < T > ;
}
2020-05-12 15:01:17 +00:00
FileAccess ( ) { }
2014-02-10 01:10:30 +00:00
virtual ~ FileAccess ( ) { }
} ;
2020-03-25 10:10:34 +00:00
# endif // FILE_ACCESS_H