aboutsummaryrefslogtreecommitdiff
path: root/dep/CascLib/src/common/Csv.cpp
blob: 24b654daa86f5f4244156cd651fce46a0789a8c9 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*****************************************************************************/
/* Csv.cpp                                Copyright (c) Ladislav Zezula 2019 */
/*---------------------------------------------------------------------------*/
/* Implementation for the CSV handler class                                  */
/*---------------------------------------------------------------------------*/
/*   Date    Ver   Who  Comment                                              */
/* --------  ----  ---  -------                                              */
/* 24.05.19  1.00  Lad  Created                                              */
/*****************************************************************************/

#define __CASCLIB_SELF__
#include "../CascLib.h"
#include "../CascCommon.h"

//-----------------------------------------------------------------------------
// Local variables

static const CASC_CSV_LINE NullLine;
#define NullColumn NullLine.Columns[0];

//-----------------------------------------------------------------------------
// Local functions

static char * NextLine(char * szLine)
{
    // Find the end of the line
    while(szLine[0] != 0 && szLine[0] != 0x0A && szLine[0] != 0x0D)
        szLine++;

    // Terminate the line
    while(szLine[0] == 0x0A || szLine[0] == 0x0D)
        *szLine++ = 0;

    // If there's an end-of-string, it's over
    return (szLine[0] != 0) ? szLine : NULL;
}

static char * NextColumn(char * szColumn)
{
    // Find the end of the column
    while(szColumn[0] != 0 && szColumn[0] != '|')
        szColumn++;

    // Terminate the column
    if (szColumn[0] == '|')
    {
        *szColumn++ = 0;
        return szColumn;
    }

    // If there's an end-of-string, it's over
    return NULL;
}

static size_t CalcHashValue(const char * szString)
{
    size_t dwHash = 0x7EEE7EEE;

    // Hash the string itself
    while(szString[0] != 0)
    {
        dwHash = (dwHash >> 24) ^ (dwHash << 5) ^ dwHash ^ szString[0];
        szString++;
    }

    // Return the hash limited by the table size
    return dwHash;
}

static BYTE HashToIndex(size_t dwHashValue)
{
    return (BYTE)(dwHashValue & (CSV_HASH_TABLE_SIZE - 1));
}

//-----------------------------------------------------------------------------
// Class for CSV line

CASC_CSV_LINE::CASC_CSV_LINE()
{
    m_pParent = NULL;
    m_nColumns = 0;
}

CASC_CSV_LINE::~CASC_CSV_LINE()
{}

bool CASC_CSV_LINE::SetLine(CASC_CSV * pParent, char * szCsvLine)
{
    char * szCsvColumn;
    size_t nHdrColumns = 0;
    size_t nColumns = 0;

    // Reset the number of column to zero
    m_pParent = pParent;
    m_nColumns = 0;

    // Parse each column
    while(szCsvLine != NULL && nColumns < CSV_MAX_COLUMNS)
    {
        // Get current line and the next one
        szCsvColumn = szCsvLine;
        szCsvLine = NextColumn(szCsvLine);

        // Save the current line
        Columns[nColumns].szValue = szCsvColumn;
        Columns[nColumns].nLength = strlen(szCsvColumn);
        nColumns++;
    }

    // Columns overflow
    if(nColumns >= CSV_MAX_COLUMNS)
    {
        assert(false);
        return false;
    }

    // If the parent has header lines, then the number of columns must match
    // In the case of mismatched column count, ignore the line
    nHdrColumns = pParent->GetHeaderColumns();
    if(nHdrColumns != 0 && nColumns != nHdrColumns)
        return false;

    // All OK
    m_nColumns = nColumns;
    return true;
}

const CASC_CSV_COLUMN & CASC_CSV_LINE::operator[](const char * szColumnName) const
{
    size_t nIndex;

    // The column must have a hash table
    if(m_pParent != NULL)
    {
        nIndex = m_pParent->GetColumnIndex(szColumnName);
        if(nIndex != CSV_INVALID_INDEX && nIndex < m_nColumns)
        {
            return Columns[nIndex];
        }
    }

    return NullColumn;
}

const CASC_CSV_COLUMN & CASC_CSV_LINE::operator[](size_t nIndex) const
{
    return (nIndex < m_nColumns) ? Columns[nIndex] : NullColumn;
}

//-----------------------------------------------------------------------------
// Class for CSV object

CASC_CSV::CASC_CSV(size_t nLinesMax, bool bHasHeader)
{
    // Initialize the class variables
    memset(HashTable, 0xFF, sizeof(HashTable));
    m_szCsvFile = NULL;
    m_szCsvPtr = NULL;
    m_nCsvFile = 0;
    m_nLines = 0;
    m_bHasHeader = bHasHeader;

    // Nonzero number of lines means a finite CSV handler. The CSV will be loaded at once.
    // Zero means that the user needs to call LoadNextLine() and then the line data
    if(nLinesMax != 0)
    {
        m_pLines = new CASC_CSV_LINE[nLinesMax];
        m_nLinesMax = nLinesMax;
        m_bHasAllLines = true;
    }
    else
    {
        m_pLines = new CASC_CSV_LINE[1];
        m_nLinesMax = 1;
        m_bHasAllLines = false;
    }
}

CASC_CSV::~CASC_CSV()
{
    if(m_pLines != NULL)
        delete[] m_pLines;
    if(m_szCsvFile != NULL)
        delete [] m_szCsvFile;
    m_szCsvFile = NULL;
}

int CASC_CSV::Load(const TCHAR * szFileName)
{
    DWORD cbFileData = 0;
    int nError = ERROR_SUCCESS;

    m_szCsvFile = (char *)LoadFileToMemory(szFileName, &cbFileData);
    if (m_szCsvFile != NULL)
    {
        // There is one extra byte reserved by LoadFileToMemory, so we can put zero there
        m_szCsvFile[cbFileData] = 0;
        m_szCsvPtr = m_szCsvFile;
        m_nCsvFile = cbFileData;

        // Parse the data
        nError = ParseCsvData() ? ERROR_SUCCESS : ERROR_BAD_FORMAT;
    }
    else
    {
        nError = GetLastError();
    }

    return nError;
}

int CASC_CSV::Load(LPBYTE pbData, size_t cbData)
{
    int nError = ERROR_NOT_ENOUGH_MEMORY;

    m_szCsvFile = new char[cbData + 1];
    if (m_szCsvFile != NULL)
    {
        // Copy the entire data and terminate them with zero
        memcpy(m_szCsvFile, pbData, cbData);
        m_szCsvFile[cbData] = 0;
        m_szCsvPtr = m_szCsvFile;
        m_nCsvFile = cbData;

        // Parse the data
        nError = ParseCsvData() ? ERROR_SUCCESS : ERROR_BAD_FORMAT;
    }

    return nError;
}

bool CASC_CSV::LoadNextLine()
{
    bool bResult = false;

    // Only endless CSV handler can do this
    if(m_bHasAllLines == false)
    {
        // A few checks
        assert(m_pLines != NULL);
        assert(m_nLinesMax == 1);

        // Reset the current line and load it
        bResult = LoadNextLine(m_pLines[0]);
        m_nLines = (bResult) ? 1 : 0;
    }

    return bResult;
}

bool CASC_CSV::InitializeHashTable()
{
    // Create the hash table of HeaderText -> ColumnIndex
    for(size_t i = 0; i < Header.GetColumnCount(); i++)
    {
        // Calculate the start slot and the current slot
        size_t nStartIndex = HashToIndex(CalcHashValue(Header[i].szValue));
        size_t nHashIndex = nStartIndex;

        // Go as long as there is not a free space
        while(HashTable[nHashIndex] != 0xFF)
        {
            nHashIndex = HashToIndex(nHashIndex + 1);
        }

        // Set the slot
        HashTable[nHashIndex] = (BYTE)i;
    }

    return true;
}

bool CASC_CSV::LoadNextLine(CASC_CSV_LINE & Line)
{
    // Overwatch ROOT's header begins with "#"
    if(m_szCsvPtr == m_szCsvFile && m_szCsvPtr[0] == '#')
        m_szCsvPtr++;

    // Parse the entire line
    while(m_szCsvPtr != NULL && m_szCsvPtr[0] != 0)
    {
        char * szCsvLine = m_szCsvPtr;

        // Get the next line
        m_szCsvPtr = NextLine(m_szCsvPtr);

        // Initialize the line
        if (Line.SetLine(this, szCsvLine))
            return true;
    }

    // End-of-file found
    return false;
}

bool CASC_CSV::ParseCsvData()
{
    // Sanity checks
    assert(m_nLines == 0);

    // If we have header, then parse it and set the pointer to the next line
    if(m_bHasHeader)
    {
        // Load the current line to the header
        if (!LoadNextLine(Header))
            return false;

        // Initialize the hash table
        if(!InitializeHashTable())
            return false;
    }

    // Are we supposed to load all lines?
    if(m_bHasAllLines)
    {
        // Parse each line
        for(size_t i = 0; i < m_nLinesMax; i++)
        {
            if(!LoadNextLine(m_pLines[i]))
                break;
            m_nLines++;
        }
    }

    return true;
}

const CASC_CSV_COLUMN & CASC_CSV::operator[](const char * szColumnName) const
{
    if (m_pLines == NULL || m_nLines == 0)
        return NullColumn;
    return m_pLines[0][GetColumnIndex(szColumnName)];
}

const CASC_CSV_LINE & CASC_CSV::operator[](size_t nIndex) const
{
    if (m_pLines == NULL || nIndex >= m_nLines)
        return NullLine;
    return m_pLines[nIndex];
}

size_t CASC_CSV::GetHeaderColumns() const
{
    return (m_bHasHeader) ? Header.GetColumnCount() : 0;
}

size_t CASC_CSV::GetColumnIndex(const char * szColumnName) const
{
    if(m_bHasHeader)
    {
        // Calculate the start slot and the current slot
        size_t nStartIndex = HashToIndex(CalcHashValue(szColumnName));
        size_t nHashIndex = nStartIndex;
        size_t nIndex;

        // Go as long as there is not a free space
        while((nIndex = HashTable[nHashIndex]) != 0xFF)
        {
            // Compare the string
            if(!strcmp(Header[nIndex].szValue, szColumnName))
                return nIndex;

            // Move to the next column
            nHashIndex = HashToIndex(nHashIndex + 1);
        }
    }

    return CSV_INVALID_INDEX;
}