-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathArchive.cpp
More file actions
320 lines (283 loc) · 8.83 KB
/
Archive.cpp
File metadata and controls
320 lines (283 loc) · 8.83 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/**
* Copyright (c) 2017 HERE Europe B.V.
* See the LICENSE file in the root of this project for license details.
*/
#include <flatdata/Archive.h>
#include <flatdata/internal/ArchiveUtils.h>
#include <cstring>
namespace flatdata
{
constexpr size_t TAB_WIDTH = 4;
Archive::Archive( std::shared_ptr< flatdata::ResourceStorage > storage )
: m_storage( std::move( storage ) )
{
}
Archive::operator bool( ) const
{
return is_open( );
}
bool
Archive::is_open( ) const
{
return m_is_open;
}
bool
Archive::initialize( )
{
if ( !m_storage )
{
return false;
}
auto signature = storage( ).read< flatdata::MemoryDescriptor >(
internal::signature_name( name( ) ).c_str( ), schema( ) );
if ( signature )
{
m_signature = *signature;
}
m_is_open = load_contents( ) && static_cast< bool >( signature );
return m_is_open;
}
flatdata::ResourceStorage&
Archive::storage( )
{
return *m_storage;
}
const flatdata::ResourceStorage&
Archive::storage( ) const
{
return *m_storage;
}
namespace
{
std::vector< std::string >
to_lines( const char* string )
{
std::vector< std::string > result;
if ( string )
{
while ( *string )
{
auto begin = string;
while ( *string && *string != '\n' )
{
string++;
}
result.emplace_back( begin, string );
if ( *string )
{
string++;
}
}
}
return result;
}
std::string
create_context( const std::vector< std::string >& diff )
{
std::string result;
std::vector< bool > result_mask( diff.size( ) );
size_t context_lines = 3;
int lines_to_go = -1;
for ( size_t i = diff.size( ); i > 0; i--, lines_to_go-- )
{
if ( diff[ i - 1 ][ 0 ] != ' ' )
{
lines_to_go = context_lines;
}
result_mask[ i ] = lines_to_go >= 0;
}
lines_to_go = -1;
for ( size_t i = 0; i < diff.size( ); i++, lines_to_go-- )
{
if ( diff[ i ][ 0 ] != ' ' )
{
lines_to_go = context_lines;
}
result_mask[ i ] = result_mask[ i ] || ( lines_to_go >= 0 );
}
bool printed_last = false;
for ( size_t i = diff.size( ); i > 0; i-- )
{
if ( result_mask[ i - 1 ] )
{
result += diff[ i - 1 ] + '\n';
printed_last = true;
}
else if ( printed_last )
{
result += "...\n";
printed_last = false;
}
}
return result;
}
std::string
compute_diff( const char* expected, const char* found )
{
if ( !found )
{
return { };
}
std::vector< std::string > lines_expected = to_lines( expected );
std::vector< std::string > lines_found = to_lines( found );
size_t expected_length = lines_expected.size( ) + 1;
size_t found_length = lines_found.size( ) + 1;
std::vector< size_t > distances;
distances.resize( expected_length * found_length );
auto entry = [ & ]( size_t expected_pos, size_t found_pos ) -> size_t& {
assert( expected_pos < expected_length );
assert( found_pos < found_length );
return distances[ expected_pos + found_pos * expected_length ];
};
for ( size_t i = 0; i < expected_length; i++ )
{
entry( i, 0 ) = i;
}
for ( size_t i = 0; i < found_length; i++ )
{
entry( 0, i ) = i;
}
// fill in distance table
for ( size_t found_pos = 0; found_pos < lines_found.size( ); found_pos++ )
{
for ( size_t expected_pos = 0; expected_pos < lines_expected.size( ); expected_pos++ )
{
size_t min_cost = std::min( entry( expected_pos, found_pos + 1 ) + 1,
entry( expected_pos + 1, found_pos ) + 1 );
if ( lines_expected[ expected_pos ] == lines_found[ found_pos ] )
{
min_cost = std::min( min_cost, entry( expected_pos, found_pos ) );
}
entry( expected_pos + 1, found_pos + 1 ) = min_cost;
}
}
std::vector< std::string > diff;
size_t found_pos = lines_found.size( );
size_t expected_pos = lines_expected.size( );
while ( found_pos != 0 || expected_pos != 0 )
{
size_t next_found_pos = found_pos;
size_t next_expected_pos = expected_pos;
auto check
= [ & ]( size_t new_expected_pos, size_t new_found_pos, size_t transition_cost ) {
size_t cost = entry( new_expected_pos, new_found_pos );
if ( cost + transition_cost == entry( expected_pos, found_pos ) )
{
next_found_pos = new_found_pos;
next_expected_pos = new_expected_pos;
}
};
if ( found_pos != 0 )
{
check( expected_pos, found_pos - 1, 1 );
}
if ( expected_pos != 0 )
{
check( expected_pos - 1, found_pos, 1 );
}
if ( expected_pos != 0 && found_pos != 0
&& lines_expected[ expected_pos - 1 ] == lines_found[ found_pos - 1 ] )
{
check( expected_pos - 1, found_pos - 1, 0 );
}
assert( expected_pos != next_expected_pos || found_pos != next_found_pos );
if ( next_found_pos == found_pos )
{
diff.emplace_back( std::string( "-" ) + "\"" + lines_expected[ next_expected_pos ]
+ "\"" );
}
else if ( next_expected_pos == expected_pos )
{
diff.emplace_back( std::string( "+" ) + "\"" + lines_found[ next_found_pos ] + "\"" );
}
else
{
diff.emplace_back( std::string( " " ) + "\"" + lines_expected[ next_expected_pos ]
+ "\"" );
}
found_pos = next_found_pos;
expected_pos = next_expected_pos;
}
return create_context( diff );
}
} // namespace
std::string
Archive::describe( size_t nest_level ) const
{
const auto newl = std::string( "\n" );
const auto hline = std::string( 80, '=' ) + newl;
const auto empty = std::string( "" );
const bool is_root_node = ( nest_level == 0 );
std::ostringstream result;
if ( !m_storage )
{
if ( is_root_node )
{
result << hline << "FATAL: Resource storage not initialized. Please check archive path."
<< newl << hline;
}
else
{
result << "Uninitialized Archive " << name( ) << newl;
}
return result.str( );
}
if ( !m_signature )
{
result << ( is_root_node ? hline : empty )
<< "FATAL: Archive signature does not match software expectations."
<< ( is_root_node ? newl : empty ) << hline;
result << compute_diff(
schema( ),
m_storage->read_schema( internal::signature_name( name( ) ).c_str( ) ).char_ptr( ) );
return result.str( );
}
if ( !m_is_open )
{
// Error propagated to root and storage is not initialized in respective child. No root
// check needed.
result << hline
<< "FATAL: Archive initialization failed. Failed loading mandatory resources."
<< std::endl;
}
if ( is_root_node )
{
result << hline << "Flatdata Archive: " << name( ) << std::endl
<< hline
<< "Resource Optional Too Large Loaded Details"
<< std::endl
<< hline;
}
else
{
const std::string indent( ( nest_level - 1 ) * TAB_WIDTH, ' ' );
result << newl + indent + std::string( "|" ) + newl + indent + std::string( "|->" )
<< " Flatdata Archive: " << name( ) << newl;
}
describe_resources( result, nest_level );
if ( is_root_node )
{
result << hline;
}
return result.str( );
}
void
Archive::describe_impl( std::ostream& stream,
const char* name,
bool optional,
bool loaded,
const char* details,
bool has_nested_details,
bool too_large,
size_t nest_level )
{
const std::string indent( nest_level * TAB_WIDTH, ' ' );
size_t offset = indent.size( );
stream << indent << std::left << std::setw( 37 - offset ) << std::setfill( ' ' )
<< std::string( name ).substr( 0, 30 ) << std::left << std::setw( 10 )
<< std::setfill( ' ' ) << ( optional ? "YES" : "NO" ) << std::left << std::setw( 11 )
<< std::setfill( ' ' ) << ( too_large ? "YES" : "NO" ) << std::left << std::setw( 10 )
<< std::setfill( ' ' ) << ( static_cast< bool >( loaded ) ? "YES" : "NO" ) << details
<< ( has_nested_details ? "" : "\n" );
}
} // namespace flatdata