Skip to content

Commit

Permalink
astyle
Browse files Browse the repository at this point in the history
  • Loading branch information
moxian committed Jan 12, 2025
1 parent e770d13 commit 7ba83fe
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 54 deletions.
3 changes: 2 additions & 1 deletion src/cata_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ class cata_path
}

template<typename T>
static std::enable_if_t < !std::is_same_v<std::decay_t<T>, std::filesystem::path>, std::filesystem::path >
static std::enable_if_t < !std::is_same_v<std::decay_t<T>, std::filesystem::path>,

Check failure on line 157 in src/cata_path.h

View workflow job for this annotation

GitHub Actions / Clang 14, macOS 12, Tiles, Sound, x64 and arm64 Universal Binary

'path' is unavailable: introduced in macOS 10.15
std::filesystem::path >

Check failure on line 158 in src/cata_path.h

View workflow job for this annotation

GitHub Actions / Clang 14, macOS 12, Tiles, Sound, x64 and arm64 Universal Binary

'path' is unavailable: introduced in macOS 10.15
as_fs_path( T &&path ) {
return std::filesystem::u8path( std::forward<T>( path ) );
}
Expand Down
6 changes: 4 additions & 2 deletions src/cata_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ bool write_to_file( const cata_path &path, const std::function<void( std::ostrea
}
}

ofstream_wrapper::ofstream_wrapper( const std::filesystem::path &path, const std::ios::openmode mode )
ofstream_wrapper::ofstream_wrapper( const std::filesystem::path &path,
const std::ios::openmode mode )
: path( path )

{
Expand Down Expand Up @@ -449,7 +450,8 @@ bool read_from_file( const cata_path &path, const std::function<void( std::istre
return read_from_file( path.get_unrelative_path(), reader );
}

bool read_from_file( const std::filesystem::path &path, const std::function<void( std::istream & )> &reader )
bool read_from_file( const std::filesystem::path &path,
const std::function<void( std::istream & )> &reader )
{
std::unique_ptr<std::istream> finp = read_maybe_compressed_file( path );
if( !finp ) {
Expand Down
3 changes: 2 additions & 1 deletion src/cata_utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ void write_to_file( const cata_path &path, const std::function<void( std::ostrea
*/
/**@{*/
bool read_from_file( const std::string &path, const std::function<void( std::istream & )> &reader );
bool read_from_file( const std::filesystem::path &path, const std::function<void( std::istream & )> &reader );
bool read_from_file( const std::filesystem::path &path,
const std::function<void( std::istream & )> &reader );
bool read_from_file( const cata_path &path, const std::function<void( std::istream & )> &reader );
bool read_from_file_json( const cata_path &path,
const std::function<void( const JsonValue & )> &reader );
Expand Down
6 changes: 4 additions & 2 deletions src/debug_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,8 @@ bool _add_dir( tgz_archiver &tgz, std::filesystem::path const &root, f_validate_
return true;
}

bool _trim_mapbuffer( std::filesystem::path const &dep, rdi_t &iter, tripoint_range<tripoint> const &segs,
bool _trim_mapbuffer( std::filesystem::path const &dep, rdi_t &iter,
tripoint_range<tripoint> const &segs,
tripoint_range<tripoint> const &regs )
{
// discard map memory outside of current region and adjacent regions
Expand Down Expand Up @@ -411,7 +412,8 @@ void write_min_archive()

tgz_archiver tgz( ofile );

f_validate_t const mb_validate = [&segs, &regs, &oms]( std::filesystem::path const & dep, rdi_t & iter ) {
f_validate_t const mb_validate = [&segs, &regs, &oms]( std::filesystem::path const & dep,
rdi_t & iter ) {
return _discard_temporary( dep ) && _trim_mapbuffer( dep, iter, segs, regs ) &&
_trim_overmapbuffer( dep, oms );
};
Expand Down
36 changes: 24 additions & 12 deletions src/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ bool file_exist( const std::filesystem::path &path )
bool file_exist( const cata_path &path )
{
const std::filesystem::path unrelative_path = path.get_unrelative_path();
return std::filesystem::exists( unrelative_path ) && !std::filesystem::is_directory( unrelative_path );
return std::filesystem::exists( unrelative_path ) &&
!std::filesystem::is_directory( unrelative_path );
}

std::string as_norm_dir( const std::string &path )
Expand Down Expand Up @@ -292,7 +293,8 @@ bool is_directory( const std::filesystem::directory_entry &entry )
// If at_end is true, returns whether entry's name ends in match.
// Otherwise, returns whether entry's name contains match.
//--------------------------------------------------------------------------------------------------
bool name_contains( const std::filesystem::directory_entry &entry, const std::string &match, const bool at_end,
bool name_contains( const std::filesystem::directory_entry &entry, const std::string &match,
const bool at_end,
const bool match_path )
{
std::string entry_name;
Expand Down Expand Up @@ -436,15 +438,17 @@ std::vector<cata_path> find_file_if_bfs( const cata_path &root_path,
std::vector<std::string> get_files_from_path( const std::string &pattern,
const std::string &root_path, const bool recursive_search, const bool match_extension )
{
return find_file_if_bfs( root_path, recursive_search, [&]( const std::filesystem::directory_entry & entry,
return find_file_if_bfs( root_path,
recursive_search, [&]( const std::filesystem::directory_entry & entry,
bool ) {
return name_contains( entry, pattern, match_extension, false );
} );
}
std::vector<cata_path> get_files_from_path( const std::string &pattern,
const cata_path &root_path, const bool recursive_search, const bool match_extension )
{
return find_file_if_bfs( root_path, recursive_search, [&]( const std::filesystem::directory_entry & entry,
return find_file_if_bfs( root_path,
recursive_search, [&]( const std::filesystem::directory_entry & entry,
bool ) {
return name_contains( entry, pattern, match_extension, false );
} );
Expand All @@ -454,7 +458,8 @@ std::vector<std::string> get_files_from_path_with_path_exclusion( const std::str
const std::string &pattern_clash,
const std::string &root_path, const bool recursive_search, const bool match_extension )
{
return find_file_if_bfs( root_path, recursive_search, [&]( const std::filesystem::directory_entry & entry,
return find_file_if_bfs( root_path,
recursive_search, [&]( const std::filesystem::directory_entry & entry,
bool ) {
return name_contains( entry, pattern, match_extension, false ) &&
!name_contains( entry, pattern_clash, false, true );
Expand All @@ -464,7 +469,8 @@ std::vector<cata_path> get_files_from_path_with_path_exclusion( const std::strin
const std::string &pattern_clash,
const cata_path &root_path, const bool recursive_search, const bool match_extension )
{
return find_file_if_bfs( root_path, recursive_search, [&]( const std::filesystem::directory_entry & entry,
return find_file_if_bfs( root_path,
recursive_search, [&]( const std::filesystem::directory_entry & entry,
bool ) {
return name_contains( entry, pattern, match_extension, false ) &&
!name_contains( entry, pattern_clash, false, true );
Expand All @@ -485,7 +491,8 @@ std::vector<std::string> get_directories_with( const std::string &pattern,
return std::vector<std::string>();
}

auto files = find_file_if_bfs( root_path, recursive_search, [&]( const std::filesystem::directory_entry & entry,
auto files = find_file_if_bfs( root_path,
recursive_search, [&]( const std::filesystem::directory_entry & entry,
bool ) {
return name_contains( entry, pattern, true, false );
} );
Expand All @@ -507,7 +514,8 @@ std::vector<cata_path> get_directories_with( const std::string &pattern,
return {};
}

auto files = find_file_if_bfs( root_path, recursive_search, [&]( const std::filesystem::directory_entry & entry,
auto files = find_file_if_bfs( root_path,
recursive_search, [&]( const std::filesystem::directory_entry & entry,
bool ) {
return name_contains( entry, pattern, true, false );
} );
Expand Down Expand Up @@ -539,7 +547,8 @@ std::vector<std::string> get_directories_with( const std::vector<std::string> &p
const auto ext_beg = std::begin( patterns );
const auto ext_end = std::end( patterns );

auto files = find_file_if_bfs( root_path, recursive_search, [&]( const std::filesystem::directory_entry & entry,
auto files = find_file_if_bfs( root_path,
recursive_search, [&]( const std::filesystem::directory_entry & entry,
bool ) {
return std::any_of( ext_beg, ext_end, [&]( const std::string & ext ) {
return name_contains( entry, ext, true, false );
Expand Down Expand Up @@ -567,7 +576,8 @@ std::vector<cata_path> get_directories_with( const std::vector<std::string> &pat
const auto ext_beg = std::begin( patterns );
const auto ext_end = std::end( patterns );

auto files = find_file_if_bfs( root_path, recursive_search, [&]( const std::filesystem::directory_entry & entry,
auto files = find_file_if_bfs( root_path,
recursive_search, [&]( const std::filesystem::directory_entry & entry,
bool ) {
return std::any_of( ext_beg, ext_end, [&]( const std::string & ext ) {
return name_contains( entry, ext, true, false );
Expand All @@ -594,7 +604,8 @@ std::vector<cata_path> get_directories_with( const std::vector<std::string> &pat
std::vector<std::string> get_directories( const std::string &root_path,
const bool recursive_search )
{
auto files = find_file_if_bfs( root_path, recursive_search, [&]( const std::filesystem::directory_entry & entry,
auto files = find_file_if_bfs( root_path,
recursive_search, [&]( const std::filesystem::directory_entry & entry,
bool ) {
return dir_exist( entry.path() );
} );
Expand All @@ -612,7 +623,8 @@ std::vector<std::string> get_directories( const std::string &root_path,
*/
std::vector<cata_path> get_directories( const cata_path &root_path, const bool recursive_search )
{
auto files = find_file_if_bfs( root_path, recursive_search, [&]( const std::filesystem::directory_entry & entry,
auto files = find_file_if_bfs( root_path,
recursive_search, [&]( const std::filesystem::directory_entry & entry,
bool ) {
return dir_exist( entry.path() );
} );
Expand Down
50 changes: 31 additions & 19 deletions src/flexbuffer_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ void try_find_and_throw_json_error( TextJsonValue &jv )
}
}

std::filesystem::file_time_type get_file_mtime_millis( const std::filesystem::path &path, std::error_code &ec )
std::filesystem::file_time_type get_file_mtime_millis( const std::filesystem::path &path,
std::error_code &ec )
{
std::filesystem::file_time_type ret = std::filesystem::last_write_time( path, ec );
if( ec ) {
return ret;
}
// Truncate to nearest millisecond.
ret = std::filesystem::file_time_type( std::chrono::milliseconds(
std::chrono::duration_cast<std::chrono::milliseconds>( ret.time_since_epoch() ).count() ) );
std::chrono::duration_cast<std::chrono::milliseconds>( ret.time_since_epoch() ).count() ) );
return ret;
}

Expand Down Expand Up @@ -231,7 +232,8 @@ struct string_flexbuffer : parsed_flexbuffer {
class flexbuffer_disk_cache
{
public:
static std::unique_ptr<flexbuffer_disk_cache> init_from_folder( const std::filesystem::path &cache_path,
static std::unique_ptr<flexbuffer_disk_cache> init_from_folder( const std::filesystem::path
&cache_path,
const std::filesystem::path &root_path ) {
// Private constructor, make_unique doesn't have access.
std::unique_ptr<flexbuffer_disk_cache> cache{ new flexbuffer_disk_cache( cache_path, root_path ) };
Expand All @@ -257,13 +259,15 @@ class flexbuffer_disk_cache
}

// Explicit constructor from milliseconds to file_time_type.
std::filesystem::file_time_type cached_mtime = std::filesystem::file_time_type( std::chrono::milliseconds( std::strtoull(
// extension() returns a string with the leading .
mtime_str.c_str() + 1,
nullptr, 0 ) ) );

std::filesystem::path root_relative_json_path = cached_flexbuffer_path.parent_path().lexically_relative(
cache_path ) / original_json_file_name;
std::filesystem::file_time_type cached_mtime = std::filesystem::file_time_type(
std::chrono::milliseconds( std::strtoull(
// extension() returns a string with the leading .
mtime_str.c_str() + 1,
nullptr, 0 ) ) );

std::filesystem::path root_relative_json_path =
cached_flexbuffer_path.parent_path().lexically_relative(
cache_path ) / original_json_file_name;
std::string root_relative_json_path_string = root_relative_json_path.u8string();

// Don't just blindly insert, we may end up in a situation with multiple flexbuffers for the same input json
Expand All @@ -289,7 +293,8 @@ class flexbuffer_disk_cache
return cached_flexbuffers_.count( json_source_path.u8string() ) > 0;
}

std::filesystem::file_time_type cached_mtime_for_json( const std::filesystem::path &json_source_path ) {
std::filesystem::file_time_type cached_mtime_for_json( const std::filesystem::path
&json_source_path ) {
auto it = cached_flexbuffers_.find( json_source_path.u8string() );
if( it != cached_flexbuffers_.end() ) {
return it->second.mtime;
Expand All @@ -301,7 +306,8 @@ class flexbuffer_disk_cache
const std::filesystem::path &lexically_normal_json_source_path ) {
std::shared_ptr<flexbuffer_mmap_storage> storage;

std::filesystem::path root_relative_source_path = lexically_normal_json_source_path.lexically_relative(
std::filesystem::path root_relative_source_path =
lexically_normal_json_source_path.lexically_relative(
root_path_ ).lexically_normal();

// Is there even a potential cached flexbuffer for this file.
Expand All @@ -311,7 +317,8 @@ class flexbuffer_disk_cache
}

std::error_code ec;
std::filesystem::file_time_type source_mtime = get_file_mtime_millis( lexically_normal_json_source_path, ec );
std::filesystem::file_time_type source_mtime = get_file_mtime_millis(
lexically_normal_json_source_path, ec );
if( ec ) {
return storage;
}
Expand Down Expand Up @@ -340,7 +347,8 @@ class flexbuffer_disk_cache
const std::vector<uint8_t> &flexbuffer_binary ) {
std::error_code ec;
std::string json_source_path_string = lexically_normal_json_source_path.u8string();
std::filesystem::file_time_type mtime = get_file_mtime_millis( lexically_normal_json_source_path, ec );
std::filesystem::file_time_type mtime = get_file_mtime_millis( lexically_normal_json_source_path,
ec );
if( ec ) {
return false;
}
Expand All @@ -349,8 +357,9 @@ class flexbuffer_disk_cache
( mtime.time_since_epoch() ).count();

// Doing some variable reuse to avoid copies.
std::filesystem::path flexbuffer_path = ( cache_path_ / lexically_normal_json_source_path.lexically_relative(
root_path_ ) ).remove_filename();
std::filesystem::path flexbuffer_path = ( cache_path_ /
lexically_normal_json_source_path.lexically_relative(
root_path_ ) ).remove_filename();

assure_dir_exist( flexbuffer_path );

Expand All @@ -374,7 +383,8 @@ class flexbuffer_disk_cache
}

private:
explicit flexbuffer_disk_cache( std::filesystem::path cache_path, std::filesystem::path root_path ) : cache_path_{ std::move( cache_path ) },
explicit flexbuffer_disk_cache( std::filesystem::path cache_path,
std::filesystem::path root_path ) : cache_path_{ std::move( cache_path ) },
root_path_{ std::move( root_path ) } {}

std::filesystem::path cache_path_;
Expand Down Expand Up @@ -435,7 +445,8 @@ std::shared_ptr<parsed_flexbuffer> flexbuffer_cache::parse_and_cache(
lexically_normal_json_source_path );
if( cached_storage ) {
std::error_code ec;
std::filesystem::file_time_type mtime = get_file_mtime_millis( lexically_normal_json_source_path, ec );
std::filesystem::file_time_type mtime = get_file_mtime_millis( lexically_normal_json_source_path,
ec );
( void )ec;

return std::make_shared<file_flexbuffer>( std::move( cached_storage ),
Expand All @@ -461,7 +472,8 @@ std::shared_ptr<parsed_flexbuffer> flexbuffer_cache::parse_and_cache(
auto storage = std::make_shared<flexbuffer_vector_storage>( std::move( fb ) );

std::error_code ec;
std::filesystem::file_time_type mtime = std::filesystem::last_write_time( lexically_normal_json_source_path, ec );
std::filesystem::file_time_type mtime = std::filesystem::last_write_time(
lexically_normal_json_source_path, ec );
if( ec ) {
// Whatever.
}
Expand Down
6 changes: 4 additions & 2 deletions src/flexbuffer_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ class flexbuffer_cache
using shared_flexbuffer = std::shared_ptr<parsed_flexbuffer>;

public:
explicit flexbuffer_cache( const std::filesystem::path &cache_directory, const std::filesystem::path &root_directory );
explicit flexbuffer_cache( const std::filesystem::path &cache_directory,
const std::filesystem::path &root_directory );
~flexbuffer_cache();

// Throw exceptions on IO and parse errors.
static shared_flexbuffer parse( std::filesystem::path json_source_path, size_t offset = 0 ) noexcept( false );
static shared_flexbuffer parse( std::filesystem::path json_source_path,
size_t offset = 0 ) noexcept( false );
shared_flexbuffer parse_and_cache( std::filesystem::path lexically_normal_json_source_path,
size_t offset = 0 ) noexcept( false ) ;

Expand Down
6 changes: 4 additions & 2 deletions src/tgz_archiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ tgz_archiver::~tgz_archiver()
finalize();
}

std::string tgz_archiver::_gen_tar_header( std::filesystem::path const &file_name, std::filesystem::path const &prefix,
std::string tgz_archiver::_gen_tar_header( std::filesystem::path const &file_name,
std::filesystem::path const &prefix,
std::filesystem::path const &real_path, std::streamsize size )
{
unsigned const type = std::filesystem::is_directory( real_path ) ? 5 : 0;
Expand Down Expand Up @@ -54,7 +55,8 @@ std::string tgz_archiver::_gen_tar_header( std::filesystem::path const &file_nam
return ss.str();
}

bool tgz_archiver::add_file( std::filesystem::path const &real_path, std::filesystem::path const &archived_path )
bool tgz_archiver::add_file( std::filesystem::path const &real_path,
std::filesystem::path const &archived_path )
{
if( fd == nullptr && ( fd = gzopen( output.c_str(), "wb" ), fd == nullptr ) ) {
return false;
Expand Down
3 changes: 2 additions & 1 deletion src/tgz_archiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class tgz_archiver
private:
static constexpr std::size_t tar_block_size = 512;
using tar_block_t = std::array<char, tar_block_size>;
std::string _gen_tar_header( std::filesystem::path const &file_name, std::filesystem::path const &prefix,
std::string _gen_tar_header( std::filesystem::path const &file_name,
std::filesystem::path const &prefix,
std::filesystem::path const &real_path, std::streamsize size );

gzFile fd = nullptr;
Expand Down
Loading

0 comments on commit 7ba83fe

Please sign in to comment.