source: trunk/src/io/string_table.cc @ 374

Last change on this file since 374 was 281, checked in by epyon, 11 years ago
  • animation key raw sizes (the whole system will be dumped though)
  • bugfix for string_table
  • nv::slot is now an enum class
File size: 2.1 KB
Line 
1// Copyright (C) 2014 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of NV Libraries.
5// For conditions of distribution and use, see copyright notice in nv.hh
6
7#include "nv/io/string_table.hh"
8
9nv::string_table_creator::string_table_creator()
10{
11        insert(""); // 0 always is empty string
12}
13
14nv::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!" );
24        index  result  = (index)m_offsets.size();
25        m_offsets.push_back( m_data.size() );
26        std::copy( cs, cs + cs_size, std::back_inserter( m_data ) );
27        m_map[ s ] = result;
28        return result;
29}
30
31nv::string_table* nv::string_table_creator::create_table() const
32{
33        offset* offsets = new offset[m_offsets.size()];
34        char*   data    = new char [m_data.size()];
35        std::copy( m_offsets.begin(), m_offsets.end(), offsets );
36        std::copy( m_data.begin(),    m_data.end(),    data );
37        return new string_table( data, m_data.size(), offsets, (index)m_offsets.size() );
38}
39
40void nv::string_table_creator::dump( nv::stream* out ) const
41{
42        index  count = (index)m_offsets.size();
43        uint32 size  = m_data.size();
44        out->write( &count,  sizeof( count ), 1 );
45        out->write( &size,   sizeof( size ), 1 );
46        out->write( m_offsets.data(), sizeof( offset ), count );
47        out->write( m_data.data(),    sizeof( char ),  size );
48}
49
50const char* nv::string_table_creator::get( index i ) const
51{
52        return i < m_offsets.size() ? m_data.data() + m_offsets[i] : nullptr;
53}
54
55nv::string_table_creator::index nv::string_table_creator::get( const std::string& s ) const
56{
57        auto i = m_map.find( s );
58        if ( i != m_map.end() )
59        {
60                return i->second;
61        }
62        return 0;
63}
64
65void nv::string_table_creator::clear()
66{
67        m_map.clear();
68        m_offsets.clear();
69        m_data.clear();
70}
71
72nv::uint32 nv::string_table_creator::dump_size() const
73{
74        return sizeof( index ) + sizeof( uint32 ) +
75                sizeof( offset ) * m_offsets.size() +
76                sizeof( char ) * m_data.size();
77}
78
Note: See TracBrowser for help on using the repository browser.