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

Last change on this file since 383 was 383, checked in by epyon, 10 years ago
  • more work on stl
  • fully working vectors!
  • copy & copy_n
  • removal of a lot of std code!
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#include <array>
9
10nv::string_table_creator::string_table_creator()
11{
12        insert(""); // 0 always is empty string
13}
14
15nv::string_table_creator::index nv::string_table_creator::insert( const std::string& s )
16{
17        auto i = m_map.find( s );
18        if ( i != m_map.end() )
19        {
20                return i->second;
21        }
22        const char* cs = s.c_str();
23        uint32 cs_size = s.size() + 1;
24        NV_ASSERT( m_offsets.size() < index(-1), "Too many strings!" );
25        index  result  = (index)m_offsets.size();
26        m_offsets.push_back( m_data.size() );
27        std::copy( cs, cs + cs_size, std::back_inserter( m_data ) );
28        m_map[ s ] = result;
29        return result;
30}
31
32nv::string_table* nv::string_table_creator::create_table() const
33{
34        offset* offsets = new offset[m_offsets.size()];
35        char*   data    = new char [m_data.size()];
36        std::copy( m_offsets.begin(), m_offsets.end(), offsets );
37        std::copy( m_data.begin(),    m_data.end(),    data );
38        return new string_table( data, m_data.size(), offsets, (index)m_offsets.size() );
39}
40
41void nv::string_table_creator::dump( nv::stream* out ) const
42{
43        index  count = (index)m_offsets.size();
44        uint32 size  = m_data.size();
45        out->write( &count,  sizeof( count ), 1 );
46        out->write( &size,   sizeof( size ), 1 );
47        out->write( m_offsets.data(), sizeof( offset ), count );
48        out->write( m_data.data(),    sizeof( char ),  size );
49}
50
51const char* nv::string_table_creator::get( index i ) const
52{
53        return i < m_offsets.size() ? m_data.data() + m_offsets[i] : nullptr;
54}
55
56nv::string_table_creator::index nv::string_table_creator::get( const std::string& s ) const
57{
58        auto i = m_map.find( s );
59        if ( i != m_map.end() )
60        {
61                return i->second;
62        }
63        return 0;
64}
65
66void nv::string_table_creator::clear()
67{
68        m_map.clear();
69        m_offsets.clear();
70        m_data.clear();
71}
72
73nv::uint32 nv::string_table_creator::dump_size() const
74{
75        return sizeof( index ) + sizeof( uint32 ) +
76                sizeof( offset ) * m_offsets.size() +
77                sizeof( char ) * m_data.size();
78}
79
Note: See TracBrowser for help on using the repository browser.