-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathStringTable.cpp
More file actions
151 lines (130 loc) · 3.02 KB
/
Copy pathStringTable.cpp
File metadata and controls
151 lines (130 loc) · 3.02 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
// Copyright © 2023 CCP ehf.
#include "stdafx.h"
#include "StringTable.h"
const StringReference INVALID_REFERENCE = StringTable::GetInvalidReference();
StringTable::StringTable() :
m_sorted( true )
{
}
StringTable::~StringTable()
{
}
StringReference StringTable::AddString( const char* string )
{
return AddString( string, strlen( string ) + 1 );
}
StringReference StringTable::AddString( const void* string, size_t length )
{
Blob blob( string, length );
std::lock_guard scope( m_CS );
auto it = m_table.find( &blob );
if( it != m_table.end() )
{
StringReference result = it->second;
return result;
}
StringReference index;
index.reference = uint32_t( m_size );
m_size += blob.m_size;
auto blobPtr = new Blob( blob );
m_table[blobPtr] = index;
m_revTable[index] = std::make_pair( blobPtr, 0 );
m_sorted = false;
return index;
}
StringReference StringTable::AddString( const InlineString& string )
{
auto length = string.end - string.start;
std::unique_ptr<char[]> str( new char[length + 1] );
memcpy( str.get(), string.start, length );
str.get()[length] = 0;
return AddString( str.get(), length + 1 );
}
uint32_t StringTable::GetOffset( StringReference ref )
{
if( ref.reference == uint32_t( -1 ) )
{
return 0xffffffff;
}
std::lock_guard scope( m_CS );
if( !m_sorted )
{
Sort();
}
auto it = m_revTable.find( ref );
if( it == m_revTable.end() )
{
return 0xffffffff;
}
return uint32_t( it->second.second );
}
const char* StringTable::GetString( StringReference ref )
{
std::lock_guard scope( m_CS );
auto it = m_revTable.find( ref );
if( it == m_revTable.end() )
{
return nullptr;
}
return static_cast<const char*>( it->second.first->m_data );
}
size_t StringTable::GetSize() const
{
return m_size + sizeof( uint32_t );
}
bool StringTable::ValueCompare( std::pair<Blob*, size_t>* a, std::pair<Blob*, size_t>* b )
{
return *a->first < *b->first;
}
void StringTable::Sort()
{
std::vector<std::pair<Blob*, size_t>*> values;
values.reserve( m_revTable.size() );
for( auto it = begin( m_revTable ); it != end( m_revTable ); ++it )
{
values.push_back( &it->second );
}
std::sort( begin( values ), end( values ), ValueCompare );
size_t offset = 0;
for( auto it = begin( values ); it != end( values ); ++it )
{
( *it )->second = offset;
offset += ( *it )->first->m_size;
}
m_sorted = true;
}
bool StringTable::Write( FILE* file )
{
std::lock_guard scope( m_CS );
if( !m_sorted )
{
Sort();
}
uint32_t size = uint32_t( m_size );
if( fwrite( &size, 1, sizeof( size ), file ) != sizeof( size ) )
{
return false;
}
if( m_size == 0 )
{
return true;
}
char* buffer = new char[m_size];
for( auto it = begin( m_revTable ); it != end( m_revTable ); ++it )
{
memcpy( buffer + it->second.second, it->second.first->m_data, it->second.first->m_size );
}
if( fwrite( buffer, 1, m_size, file ) != m_size )
{
delete[] buffer;
return false;
}
delete[] buffer;
return true;
}
StringReference StringTable::GetInvalidReference()
{
StringReference ref;
ref.reference = 0xffffffff;
return ref;
}