summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-serialize-types.cpp
blob: ca3bde6084eaab85ecd97b1ae72b9c3ddc277ac8 (plain)
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
// slang-serialize-types.cpp
#include "slang-serialize-types.h"

#include "../core/slang-byte-encode-util.h"
#include "../core/slang-math.h"
#include "../core/slang-text-io.h"

namespace Slang
{

// Needed for linkage with some compilers
/* static */ const SerialStringData::StringIndex SerialStringData::kNullStringIndex;
/* static */ const SerialStringData::StringIndex SerialStringData::kEmptyStringIndex;

namespace
{ // anonymous

struct ByteReader
{
    Byte operator()() const { return Byte(*m_pos++); }
    ByteReader(const char* pos)
        : m_pos(pos)
    {
    }
    mutable const char* m_pos;
};

} // namespace


// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! SerialStringTableUtil !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

/* static */ void SerialStringTableUtil::encodeStringTable(
    const StringSlicePool& pool,
    List<char>& stringTable)
{
    // Skip the default handles -> nothing is encoded via them
    return encodeStringTable(pool.getAdded(), stringTable);
}

/* static */ void SerialStringTableUtil::encodeStringTable(
    const ConstArrayView<UnownedStringSlice>& slices,
    List<char>& stringTable)
{
    stringTable.clear();
    for (const auto& slice : slices)
    {
        // TODO(JS):
        // This is a bit of a hack. We need to store the string length, along with the string
        // contents. We don't want to write the size as (say) uint32, because most strings are
        // short. So we just save off the length as a utf8 encoding. As it stands this *does* have
        // an arguable problem because encoding isn't of the full 32 bits.
        const int len = int(slice.getLength());

        // We need to write into the the string array
        char prefixBytes[6];
        const int numPrefixBytes = encodeUnicodePointToUTF8(len, prefixBytes);
        const Index baseIndex = stringTable.getCount();

        auto newCount = baseIndex + numPrefixBytes + len;
        stringTable.growToCount(newCount);

        char* dst = stringTable.begin() + baseIndex;

        memcpy(dst, prefixBytes, numPrefixBytes);
        memcpy(dst + numPrefixBytes, slice.begin(), len);
    }
}

/* static */ void SerialStringTableUtil::appendDecodedStringTable(
    const char* table,
    size_t tableSize,
    List<UnownedStringSlice>& slicesOut)
{
    const char* start = table;
    const char* cur = start;
    const char* end = table + tableSize;

    while (cur < end)
    {
        ByteReader reader(cur);
        const int len = getUnicodePointFromUTF8(reader);
        slicesOut.add(UnownedStringSlice(reader.m_pos, len));
        cur = reader.m_pos + len;
    }
}

/* static */ void SerialStringTableUtil::decodeStringTable(
    const char* table,
    size_t tableSize,
    List<UnownedStringSlice>& slicesOut)
{
    slicesOut.setCount(2);
    slicesOut[0] = UnownedStringSlice(nullptr, size_t(0));
    slicesOut[1] = UnownedStringSlice("", size_t(0));

    appendDecodedStringTable(table, tableSize, slicesOut);
}

/* static */ void SerialStringTableUtil::decodeStringTable(
    const char* table,
    size_t tableSize,
    StringSlicePool& outPool)
{
    outPool.clear();

    const char* start = table;
    const char* cur = start;
    const char* end = table + tableSize;

    while (cur < end)
    {
        ByteReader reader(cur);
        const int len = getUnicodePointFromUTF8(reader);
        outPool.add(UnownedStringSlice(reader.m_pos, len));
        cur = reader.m_pos + len;
    }
}

/* static */ void SerialStringTableUtil::calcStringSlicePoolMap(
    const List<UnownedStringSlice>& slices,
    StringSlicePool& pool,
    List<StringSlicePool::Handle>& indexMapOut)
{
    SLANG_ASSERT(slices.getCount() >= StringSlicePool::kDefaultHandlesCount);
    SLANG_ASSERT(
        slices[int(StringSlicePool::kNullHandle)] == "" &&
        slices[int(StringSlicePool::kNullHandle)].begin() == nullptr);
    SLANG_ASSERT(slices[int(StringSlicePool::kEmptyHandle)] == "");

    indexMapOut.setCount(slices.getCount());
    // Set up all of the defaults
    for (int i = 0; i < StringSlicePool::kDefaultHandlesCount; ++i)
    {
        indexMapOut[i] = StringSlicePool::Handle(i);
    }

    const Index numSlices = slices.getCount();
    for (Index i = StringSlicePool::kDefaultHandlesCount; i < numSlices; ++i)
    {
        indexMapOut[i] = pool.add(slices[i]);
    }
}

// !!!!!!!!!!!!!!!!!!!!!!!!!!!! SerialRiffUtil !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

/* static */ Result SerialRiffUtil::writeArrayChunk(
    FourCC chunkId,
    const void* data,
    size_t numEntries,
    size_t typeSize,
    RIFF::BuildCursor& cursor)
{
    if (numEntries == 0)
    {
        return SLANG_OK;
    }

    SLANG_SCOPED_RIFF_BUILDER_DATA_CHUNK(cursor, chunkId);

    SerialBinary::ArrayHeader header;
    header.numEntries = uint32_t(numEntries);

    cursor.addData(&header, sizeof(header));
    cursor.addData(data, typeSize * numEntries);
    return SLANG_OK;
}

/* static */ Result SerialRiffUtil::readArrayChunk(
    RIFF::DataChunk const* dataChunk,
    ListResizer& listOut)
{
    typedef SerialBinary Bin;

    MemoryReader reader(dataChunk->getPayload(), dataChunk->getPayloadSize());
    const Size typeSize = listOut.getTypeSize();

    Bin::ArrayHeader header;
    SLANG_RETURN_ON_FAIL(reader.read(header));
    const Size payloadSize = header.numEntries * typeSize;
    SLANG_ASSERT(payloadSize == reader.getRemainingSize());
    void* dst = listOut.setSize(header.numEntries);
    ::memcpy(dst, reader.getRemainingData(), payloadSize);

    return SLANG_OK;
}

} // namespace Slang