forked from heremaps/flatdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TarFileResourceStorage.h
119 lines (98 loc) · 3.5 KB
/
TarFileResourceStorage.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* Copyright (c) 2021 HERE Europe B.V.
* See the LICENSE file in the root of this project for license details.
*/
#pragma once
#include "MemoryMappedTarFileStorage.h"
#include "ResourceStorage.h"
#include <boost/filesystem.hpp>
#include <fstream>
namespace flatdata
{
/**
* @brief Read-only resource storage for reading flatdata archives inside a TAR file.
*/
class TarFileResourceStorage : public ResourceStorage
{
public:
/**
* @brief Create resource storage for a TAR file
* @param tar_path The path to the TAR file
* @param tar_path The path inside the TAR file
* @throws std::runtime_error if the reading of the TAR file fails
* @return TarFileResourceStorage or nullptr on error
*/
static std::unique_ptr< TarFileResourceStorage > create( const char* tar_path,
const char* sub_path = "" );
std::unique_ptr< ResourceStorage > create_directory( const char* key ) override;
std::unique_ptr< ResourceStorage > directory( const char* key ) override;
bool exists( const char* key ) override;
protected:
std::shared_ptr< std::ostream > create_output_stream( const char* key ) override;
MemoryDescriptor read_resource( const char* key ) override;
private:
TarFileResourceStorage( std::shared_ptr< const MemoryMappedTarFileStorage > storage,
const std::string& tar_path,
const std::string& sub_path );
std::string get_path( const char* key ) const;
private:
std::shared_ptr< const MemoryMappedTarFileStorage > m_storage;
std::string m_tar_path;
std::string m_sub_path;
};
// -------------------------------------------------------------------------------------------------
inline std::unique_ptr< TarFileResourceStorage >
TarFileResourceStorage::create( const char* tar_path, const char* sub_path )
{
std::shared_ptr< const MemoryMappedTarFileStorage > storage(
new MemoryMappedTarFileStorage( tar_path ) );
return std::unique_ptr< TarFileResourceStorage >(
new TarFileResourceStorage( storage, tar_path, sub_path ) );
}
inline std::shared_ptr< std::ostream >
TarFileResourceStorage::create_output_stream( const char* )
{
// Writing to TAR files is not supported
return nullptr;
}
inline TarFileResourceStorage::TarFileResourceStorage(
std::shared_ptr< const MemoryMappedTarFileStorage > storage,
const std::string& tar_path,
const std::string& sub_path )
: m_storage( std::move( storage ) )
, m_tar_path( tar_path )
, m_sub_path( sub_path )
{
}
inline std::string
TarFileResourceStorage::get_path( const char* key ) const
{
const char TAR_PATH_SEPARATOR = '/';
return m_sub_path.empty( ) ? std::string( key ) : m_sub_path + TAR_PATH_SEPARATOR + key;
}
inline MemoryDescriptor
TarFileResourceStorage::read_resource( const char* key )
{
if ( !exists( key ) )
{
return MemoryDescriptor( );
}
return m_storage->read( get_path( key ).c_str( ) );
}
inline std::unique_ptr< ResourceStorage >
TarFileResourceStorage::create_directory( const char* key )
{
return directory( key );
}
inline std::unique_ptr< ResourceStorage >
TarFileResourceStorage::directory( const char* key )
{
return std::unique_ptr< TarFileResourceStorage >(
new TarFileResourceStorage( m_storage, m_tar_path, get_path( key ) ) );
}
inline bool
TarFileResourceStorage::exists( const char* key )
{
return m_storage->read( get_path( key ).c_str( ) ).data( ) != nullptr;
}
} // namespace flatdata