[395] | 1 | // Copyright (C) 2014-2015 ChaosForge Ltd
|
---|
[280] | 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
[395] | 4 | // This file is part of Nova libraries.
|
---|
| 5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
[280] | 6 |
|
---|
| 7 | #include "nv/io/string_table.hh"
|
---|
| 8 |
|
---|
| 9 | nv::string_table_creator::string_table_creator()
|
---|
| 10 | {
|
---|
| 11 | insert(""); // 0 always is empty string
|
---|
| 12 | }
|
---|
| 13 |
|
---|
| 14 | nv::string_table_creator::index nv::string_table_creator::insert( const std::string& s )
|
---|
| 15 | {
|
---|
| 16 | auto i = m_map.find( s );
|
---|
| 17 | if ( i != m_map.end() )
|
---|
| 18 | {
|
---|
| 19 | return i->second;
|
---|
| 20 | }
|
---|
| 21 | const char* cs = s.c_str();
|
---|
| 22 | uint32 cs_size = s.size() + 1;
|
---|
| 23 | NV_ASSERT( m_offsets.size() < index(-1), "Too many strings!" );
|
---|
[406] | 24 | index result = index( m_offsets.size() );
|
---|
[392] | 25 | size_t dsize = m_data.size();
|
---|
| 26 | m_offsets.push_back( dsize );
|
---|
| 27 | m_data.resize( dsize + cs_size );
|
---|
| 28 | raw_copy( cs, cs + cs_size, m_data.data() + dsize );
|
---|
[280] | 29 | m_map[ s ] = result;
|
---|
| 30 | return result;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | nv::string_table* nv::string_table_creator::create_table() const
|
---|
| 34 | {
|
---|
| 35 | offset* offsets = new offset[m_offsets.size()];
|
---|
| 36 | char* data = new char [m_data.size()];
|
---|
[392] | 37 | raw_copy( m_offsets.begin(), m_offsets.end(), offsets );
|
---|
| 38 | raw_copy( m_data.begin(), m_data.end(), data );
|
---|
[406] | 39 | return new string_table( data, m_data.size(), offsets, index( m_offsets.size() ) );
|
---|
[280] | 40 | }
|
---|
| 41 |
|
---|
| 42 | void nv::string_table_creator::dump( nv::stream* out ) const
|
---|
| 43 | {
|
---|
[406] | 44 | index count = index( m_offsets.size() );
|
---|
[280] | 45 | uint32 size = m_data.size();
|
---|
| 46 | out->write( &count, sizeof( count ), 1 );
|
---|
| 47 | out->write( &size, sizeof( size ), 1 );
|
---|
| 48 | out->write( m_offsets.data(), sizeof( offset ), count );
|
---|
| 49 | out->write( m_data.data(), sizeof( char ), size );
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | const char* nv::string_table_creator::get( index i ) const
|
---|
| 53 | {
|
---|
| 54 | return i < m_offsets.size() ? m_data.data() + m_offsets[i] : nullptr;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | nv::string_table_creator::index nv::string_table_creator::get( const std::string& s ) const
|
---|
| 58 | {
|
---|
| 59 | auto i = m_map.find( s );
|
---|
| 60 | if ( i != m_map.end() )
|
---|
| 61 | {
|
---|
| 62 | return i->second;
|
---|
| 63 | }
|
---|
| 64 | return 0;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | void nv::string_table_creator::clear()
|
---|
| 68 | {
|
---|
| 69 | m_map.clear();
|
---|
| 70 | m_offsets.clear();
|
---|
| 71 | m_data.clear();
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[281] | 74 | nv::uint32 nv::string_table_creator::dump_size() const
|
---|
| 75 | {
|
---|
| 76 | return sizeof( index ) + sizeof( uint32 ) +
|
---|
| 77 | sizeof( offset ) * m_offsets.size() +
|
---|
| 78 | sizeof( char ) * m_data.size();
|
---|
| 79 | }
|
---|
| 80 |
|
---|