Coverage Report

Created: 2026-03-30 09:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/ogr/ogrsf_frmts/vfk/vfkreader.cpp
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Project:  VFK Reader
4
 * Purpose:  Implements VFKReader class.
5
 * Author:   Martin Landa, landa.martin gmail.com
6
 *
7
 ******************************************************************************
8
 * Copyright (c) 2009-2018, Martin Landa <landa.martin gmail.com>
9
 * Copyright (c) 2012-2018, Even Rouault <even dot rouault at spatialys.com>
10
 *
11
 * SPDX-License-Identifier: MIT
12
 ****************************************************************************/
13
14
#include <sys/stat.h>
15
16
#include "vfkreader.h"
17
#include "vfkreaderp.h"
18
19
#include "cpl_conv.h"
20
#include "cpl_error.h"
21
#include "cpl_string.h"
22
23
#include "ogr_geometry.h"
24
25
static char *GetDataBlockName(const char *);
26
27
/*!
28
  \brief IVFKReader destructor
29
*/
30
IVFKReader::~IVFKReader()
31
2.82k
{
32
2.82k
}
33
34
/*!
35
  \brief Create new instance of VFKReader
36
37
  \return pointer to VFKReader instance
38
*/
39
IVFKReader *CreateVFKReader(const GDALOpenInfo *poOpenInfo)
40
2.82k
{
41
2.82k
    return new VFKReaderSQLite(poOpenInfo);
42
2.82k
}
43
44
/*!
45
  \brief VFKReader constructor
46
*/
47
VFKReader::VFKReader(const GDALOpenInfo *poOpenInfo)
48
2.82k
    : m_pszEncoding("ISO-8859-2"),  // Encoding, supported are ISO-8859-2,
49
                                    // WINDOWS-1250 and UTF-8.
50
2.82k
      m_poFD(nullptr), m_pszFilename(CPLStrdup(poOpenInfo->pszFilename)),
51
2.82k
      m_poFStat((VSIStatBufL *)CPLCalloc(1, sizeof(VSIStatBufL))),
52
      // VFK is provided in two forms - stative and amendment data.
53
2.82k
      m_bAmendment(false),
54
      m_bFileField(
55
2.82k
          CPLFetchBool(poOpenInfo->papszOpenOptions, "FILE_FIELD", false)),
56
2.82k
      m_nDataBlockCount(0), m_papoDataBlock(nullptr)
57
2.82k
{
58
    // Open VFK file for reading.
59
2.82k
    CPLAssert(nullptr != m_pszFilename);
60
61
2.82k
    if (VSIStatL(m_pszFilename, m_poFStat) != 0 ||
62
2.82k
        !VSI_ISREG(m_poFStat->st_mode))
63
0
    {
64
0
        CPLError(CE_Failure, CPLE_OpenFailed, "%s is not a regular file.",
65
0
                 m_pszFilename);
66
0
    }
67
68
2.82k
    m_poFD = VSIFOpenL(m_pszFilename, "rb");
69
2.82k
    if (m_poFD == nullptr)
70
0
    {
71
0
        CPLError(CE_Failure, CPLE_OpenFailed, "Failed to open file %s.",
72
0
                 m_pszFilename);
73
0
    }
74
2.82k
}
75
76
/*!
77
  \brief VFKReader destructor
78
*/
79
VFKReader::~VFKReader()
80
2.82k
{
81
2.82k
    CPLFree(m_pszFilename);
82
83
2.82k
    if (m_poFD)
84
2.82k
        VSIFCloseL(m_poFD);
85
2.82k
    CPLFree(m_poFStat);
86
87
    /* clear data blocks */
88
12.8k
    for (int i = 0; i < m_nDataBlockCount; i++)
89
9.99k
        delete m_papoDataBlock[i];
90
2.82k
    CPLFree(m_papoDataBlock);
91
2.82k
}
92
93
char *GetDataBlockName(const char *pszLine)
94
357k
{
95
357k
    int n = 0;  // Used after for.
96
357k
    const char *pszLineChar = pszLine + 2;
97
98
4.57M
    for (; *pszLineChar != '\0' && *pszLineChar != ';'; pszLineChar++, n++)
99
4.21M
        ;
100
101
357k
    if (*pszLineChar == '\0')
102
27.3k
        return nullptr;
103
104
330k
    char *pszBlockName = (char *)CPLMalloc(n + 1);
105
330k
    strncpy(pszBlockName, pszLine + 2, n);
106
330k
    pszBlockName[n] = '\0';
107
108
330k
    return pszBlockName;
109
357k
}
110
111
/*!
112
  \brief Read a line from file
113
114
  \return a NULL terminated string which should be freed with CPLFree().
115
*/
116
char *VFKReader::ReadLine()
117
4.11M
{
118
4.11M
    int nBufLength;
119
4.11M
    const char *pszRawLine =
120
4.11M
        CPLReadLine3L(m_poFD, 100 * 1024, &nBufLength, nullptr);
121
4.11M
    if (pszRawLine == nullptr)
122
6.49k
        return nullptr;
123
124
4.10M
    char *pszLine = (char *)CPLMalloc(nBufLength + 1);
125
4.10M
    memcpy(pszLine, pszRawLine, nBufLength + 1);
126
127
4.10M
    const int nLineLength = static_cast<int>(strlen(pszRawLine));
128
4.10M
    if (nLineLength != nBufLength)
129
868k
    {
130
        /* replace nul characters in line by spaces */
131
60.1M
        for (int i = nLineLength; i < nBufLength; i++)
132
59.3M
        {
133
59.3M
            if (pszLine[i] == '\0')
134
10.1M
                pszLine[i] = ' ';
135
59.3M
        }
136
868k
    }
137
138
4.10M
    return pszLine;
139
4.11M
}
140
141
/*!
142
  \brief Load text encoding from header (&HENCODING)
143
144
  Called from VFKReader::ReadDataBlocks()
145
*/
146
void VFKReader::ReadEncoding()
147
2.81k
{
148
2.81k
    VSIFSeekL(m_poFD, 0, SEEK_SET);
149
2.81k
    char *pszLine = nullptr;
150
912k
    while ((pszLine = ReadLine()) != nullptr)
151
911k
    {
152
911k
        if (strlen(pszLine) < 2 || pszLine[0] != '&')
153
533k
        {
154
533k
            CPLFree(pszLine);
155
533k
            continue;
156
533k
        }
157
378k
        if (pszLine[1] == 'B' || (pszLine[1] == 'K' && strlen(pszLine) == 2))
158
1.72k
        {
159
            /* 'B' record closes the header section */
160
            /* 'K' record is end of file */
161
1.72k
            CPLFree(pszLine);
162
1.72k
            break;
163
1.72k
        }
164
376k
        if (pszLine[1] != 'H')
165
163k
        {
166
            /* (not) 'H' header */
167
163k
            CPLFree(pszLine);
168
163k
            continue;
169
163k
        }
170
171
213k
        char *pszKey = pszLine + 2; /* &H */
172
213k
        char *pszValue = pszKey;
173
3.08M
        while (*pszValue != '\0' && *pszValue != ';')
174
2.87M
            pszValue++;
175
213k
        if (*pszValue != ';')
176
14.9k
        {
177
            /* no value, ignoring */
178
14.9k
            CPLFree(pszLine);
179
14.9k
            continue;
180
14.9k
        }
181
182
198k
        *pszValue = '\0';
183
198k
        pszValue++; /* skip ; */
184
198k
        if (*pszValue == '"')
185
20.5k
        { /* trim "" */
186
20.5k
            pszValue++;
187
20.5k
            size_t nValueLen = strlen(pszValue);
188
20.5k
            if (nValueLen > 0)
189
15.0k
                pszValue[nValueLen - 1] = '\0';
190
20.5k
        }
191
192
        /* read encoding to m_pszEncoding */
193
198k
        if (EQUAL(pszKey, "CODEPAGE"))
194
1.98k
        {
195
1.98k
            if (EQUAL(pszValue, CPL_ENC_UTF8))
196
0
                m_pszEncoding = CPL_ENC_UTF8;
197
1.98k
            else if (!EQUAL(pszValue, "WE8ISO8859P2"))
198
1.25k
                m_pszEncoding = "WINDOWS-1250";
199
1.98k
        }
200
201
198k
        CPLFree(pszLine);
202
198k
    }
203
2.81k
}
204
205
/*!
206
  \brief Load data block definitions (&B)
207
208
  Call VFKReader::OpenFile() before this function.
209
210
  \param bSuppressGeometry True for skipping geometry resolver (force wkbNone
211
  type)
212
213
  \return number of data blocks or -1 on error
214
*/
215
int VFKReader::ReadDataBlocks(bool bSuppressGeometry)
216
2.81k
{
217
2.81k
    CPLAssert(nullptr != m_pszFilename);
218
219
    /* load text encoding in extra pass through header */
220
2.81k
    ReadEncoding();
221
222
2.81k
    VSIFSeekL(m_poFD, 0, SEEK_SET);
223
2.81k
    bool bInHeader = true;
224
2.81k
    char *pszLine = nullptr;
225
1.57M
    while ((pszLine = ReadLine()) != nullptr)
226
1.57M
    {
227
1.57M
        if (strlen(pszLine) < 2 || pszLine[0] != '&')
228
953k
        {
229
953k
            CPLFree(pszLine);
230
953k
            continue;
231
953k
        }
232
233
618k
        if (pszLine[1] == 'B')
234
57.4k
        {
235
57.4k
            if (bInHeader)
236
1.72k
                bInHeader = false; /* 'B' record closes the header section */
237
238
57.4k
            char *pszBlockName = GetDataBlockName(pszLine);
239
57.4k
            if (pszBlockName == nullptr)
240
215
            {
241
215
                CPLError(CE_Failure, CPLE_NotSupported,
242
215
                         "Corrupted data - line\n%s\n", pszLine);
243
215
                CPLFree(pszLine);
244
215
                return -1;
245
215
            }
246
247
            /* skip duplicated data blocks (when reading multiple files into
248
             * single DB)  */
249
57.1k
            if (!GetDataBlock(pszBlockName))
250
8.29k
            {
251
8.29k
                IVFKDataBlock *poNewDataBlock =
252
8.29k
                    (IVFKDataBlock *)CreateDataBlock(pszBlockName);
253
8.29k
                poNewDataBlock->SetGeometryType(bSuppressGeometry);
254
8.29k
                poNewDataBlock->SetProperties(
255
8.29k
                    pszLine); /* TODO: check consistency on property level */
256
257
8.29k
                AddDataBlock(poNewDataBlock, pszLine);
258
8.29k
            }
259
57.1k
            CPLFree(pszBlockName);
260
57.1k
        }
261
561k
        else if (pszLine[1] == 'H')
262
250k
        {
263
            /* check for amendment file */
264
250k
            if (EQUAL(pszLine, "&HZMENY;1"))
265
587
            {
266
587
                m_bAmendment = true;
267
587
            }
268
269
            /* header - metadata */
270
250k
            AddInfo(pszLine);
271
250k
        }
272
311k
        else if (pszLine[1] == 'K' && strlen(pszLine) == 2)
273
17
        {
274
            /* end of file */
275
17
            CPLFree(pszLine);
276
17
            break;
277
17
        }
278
311k
        else if (bInHeader && pszLine[1] == 'D')
279
141k
        {
280
            /* process 'D' records in the header section */
281
141k
            AddInfo(pszLine);
282
141k
        }
283
284
618k
        CPLFree(pszLine);
285
618k
    }
286
287
2.60k
    return m_nDataBlockCount;
288
2.81k
}
289
290
/*!
291
  \brief Load data records (&D)
292
293
  Call VFKReader::OpenFile() before this function.
294
295
  \param poDataBlock limit to selected data block or NULL for all
296
297
  \return number of data records or -1 on error
298
*/
299
int64_t VFKReader::ReadDataRecords(IVFKDataBlock *poDataBlock)
300
2.81k
{
301
2.81k
    const char *pszName = nullptr;
302
2.81k
    IVFKDataBlock *poDataBlockCurrent = nullptr;
303
304
2.81k
    if (poDataBlock)
305
0
    { /* read only given data block */
306
0
        poDataBlockCurrent = poDataBlock;
307
0
        if (poDataBlockCurrent->GetFeatureCount(FALSE) < 0)
308
0
            poDataBlockCurrent->SetFeatureCount(0);
309
0
        pszName = poDataBlockCurrent->GetName();
310
0
    }
311
2.81k
    else
312
2.81k
    { /* read all data blocks */
313
12.7k
        for (int iDataBlock = 0; iDataBlock < GetDataBlockCount(); iDataBlock++)
314
9.91k
        {
315
9.91k
            poDataBlockCurrent = GetDataBlock(iDataBlock);
316
9.91k
            if (poDataBlockCurrent->GetFeatureCount(FALSE) < 0)
317
9.01k
                poDataBlockCurrent->SetFeatureCount(0);
318
9.91k
        }
319
2.81k
        poDataBlockCurrent = nullptr;
320
2.81k
    }
321
322
2.81k
    VSIFSeekL(m_poFD, 0, SEEK_SET);
323
324
2.81k
    int iLine = 0;
325
2.81k
    int nSkipped = 0;
326
2.81k
    int nDupl = 0;
327
2.81k
    int64_t nRecords = 0;
328
2.81k
    bool bInHeader = true;
329
2.81k
    CPLString osBlockNameLast;
330
2.81k
    char *pszLine = nullptr;
331
332
    /* currency sign in current encoding */
333
2.81k
    const char *pszCurSign = "\244";
334
2.81k
    if (EQUAL(m_pszEncoding, CPL_ENC_UTF8))
335
0
        pszCurSign = "\302\244";
336
2.81k
    size_t nCurSignLen = strlen(pszCurSign);
337
338
1.57M
    while ((pszLine = ReadLine()) != nullptr)
339
1.57M
    {
340
1.57M
        iLine++;
341
1.57M
        size_t nLength = strlen(pszLine);
342
1.57M
        if (nLength < 2)
343
411k
        {
344
411k
            CPLFree(pszLine);
345
411k
            continue;
346
411k
        }
347
348
1.16M
        if (bInHeader && pszLine[1] == 'B')
349
1.99k
            bInHeader = false; /* 'B' record closes the header section */
350
351
1.16M
        if (pszLine[1] == 'D')
352
373k
        {
353
373k
            if (bInHeader)
354
73.0k
            {
355
                /* skip 'D' records from the header section, already
356
                 * processed as metadata */
357
73.0k
                CPLFree(pszLine);
358
73.0k
                continue;
359
73.0k
            }
360
361
300k
            char *pszBlockName = GetDataBlockName(pszLine);
362
363
300k
            if (pszBlockName && (!pszName || EQUAL(pszBlockName, pszName)))
364
272k
            {
365
                /* merge lines if needed
366
367
                   See http://en.wikipedia.org/wiki/ISO/IEC_8859
368
                   - \244 - general currency sign
369
                */
370
272k
                if (EQUAL(pszLine + nLength - nCurSignLen, pszCurSign))
371
18.0k
                {
372
                    /* trim the currency sign and trailing spaces from line */
373
18.0k
                    nLength -= nCurSignLen;
374
28.1k
                    while (nLength > 0 && pszLine[nLength - 1] == ' ')
375
10.1k
                        nLength--;
376
18.0k
                    pszLine[nLength] = '\0';
377
378
18.0k
                    CPLString osMultiLine(pszLine);
379
18.0k
                    CPLFree(pszLine);
380
381
49.9k
                    while ((pszLine = ReadLine()) != nullptr &&
382
49.9k
                           (nLength = strlen(pszLine)) >= nCurSignLen &&
383
44.6k
                           EQUAL(pszLine + nLength - nCurSignLen, pszCurSign))
384
31.9k
                    {
385
                        /* trim leading spaces from continued line */
386
31.9k
                        char *pszLineTrim = pszLine;
387
51.5k
                        while (*pszLineTrim == ' ')
388
19.6k
                            pszLineTrim++;
389
                        /* trim the currency sign and trailing spaces from line
390
                         */
391
31.9k
                        nLength = strlen(pszLineTrim) - nCurSignLen;
392
47.7k
                        while (nLength > 0 && pszLineTrim[nLength - 1] == ' ')
393
15.8k
                            nLength--;
394
31.9k
                        pszLineTrim[nLength] = '\0';
395
                        /* append a space and the trimmed line */
396
31.9k
                        osMultiLine += " ";
397
31.9k
                        osMultiLine += pszLineTrim;
398
399
31.9k
                        CPLFree(pszLine);
400
31.9k
                        if (osMultiLine.size() > 100U * 1024U * 1024U)
401
0
                        {
402
0
                            CPLFree(pszBlockName);
403
0
                            return -1;
404
0
                        }
405
31.9k
                    }
406
18.0k
                    if (pszLine)
407
18.0k
                    {
408
                        /* trim leading spaces from continued line */
409
18.0k
                        char *pszLineTrim = pszLine;
410
27.8k
                        while (*pszLineTrim == ' ')
411
9.77k
                            pszLineTrim++;
412
                        /* append a space and the trimmed line */
413
18.0k
                        osMultiLine += " ";
414
18.0k
                        osMultiLine += pszLineTrim;
415
18.0k
                    }
416
18.0k
                    CPLFree(pszLine);
417
418
18.0k
                    nLength = osMultiLine.size();
419
18.0k
                    if (nLength > 100U * 1024U * 1024U)
420
0
                    {
421
0
                        CPLFree(pszBlockName);
422
0
                        return -1;
423
0
                    }
424
18.0k
                    pszLine = (char *)CPLMalloc(nLength + 1);
425
18.0k
                    strncpy(pszLine, osMultiLine.c_str(), nLength);
426
18.0k
                    pszLine[nLength] = '\0';
427
18.0k
                }
428
429
272k
                if (!poDataBlock)
430
272k
                { /* read all data blocks */
431
272k
                    if (osBlockNameLast.empty() ||
432
139k
                        !EQUAL(pszBlockName, osBlockNameLast.c_str()))
433
201k
                    {
434
201k
                        poDataBlockCurrent = GetDataBlock(pszBlockName);
435
201k
                        osBlockNameLast = CPLString(pszBlockName);
436
201k
                    }
437
272k
                }
438
272k
                if (!poDataBlockCurrent)
439
85.8k
                {
440
85.8k
                    CPLFree(pszBlockName);
441
85.8k
                    CPLFree(pszLine);
442
85.8k
                    continue;  // assert ?
443
85.8k
                }
444
445
187k
                VFKFeature *poNewFeature =
446
187k
                    new VFKFeature(poDataBlockCurrent,
447
187k
                                   poDataBlockCurrent->GetFeatureCount() + 1);
448
187k
                if (poNewFeature->SetProperties(pszLine))
449
111k
                {
450
111k
                    if (AddFeature(poDataBlockCurrent, poNewFeature) !=
451
111k
                        OGRERR_NONE)
452
81.2k
                    {
453
81.2k
                        CPLDebug("OGR-VFK",
454
81.2k
                                 "%s: duplicated VFK data record skipped "
455
81.2k
                                 "(line %d).\n%s\n",
456
81.2k
                                 pszBlockName, iLine, pszLine);
457
81.2k
                        poDataBlockCurrent->SetIncRecordCount(RecordDuplicated);
458
81.2k
                    }
459
30.7k
                    else
460
30.7k
                    {
461
30.7k
                        nRecords++;
462
30.7k
                        poDataBlockCurrent->SetIncRecordCount(RecordValid);
463
30.7k
                    }
464
111k
                    delete poNewFeature;
465
111k
                }
466
75.1k
                else
467
75.1k
                {
468
75.1k
                    CPLDebug("OGR-VFK",
469
75.1k
                             "Invalid VFK data record skipped (line %d).\n%s\n",
470
75.1k
                             iLine, pszLine);
471
75.1k
                    poDataBlockCurrent->SetIncRecordCount(RecordSkipped);
472
75.1k
                    delete poNewFeature;
473
75.1k
                }
474
187k
            }
475
214k
            CPLFree(pszBlockName);
476
214k
        }
477
788k
        else if (pszLine[1] == 'K' && strlen(pszLine) == 2)
478
21
        {
479
            /* end of file */
480
21
            CPLFree(pszLine);
481
21
            break;
482
21
        }
483
484
1.00M
        CPLFree(pszLine);
485
1.00M
    }
486
487
12.7k
    for (int iDataBlock = 0; iDataBlock < GetDataBlockCount(); iDataBlock++)
488
9.91k
    {
489
9.91k
        poDataBlockCurrent = GetDataBlock(iDataBlock);
490
491
9.91k
        if (poDataBlock && poDataBlock != poDataBlockCurrent)
492
0
            continue;
493
494
9.91k
        nSkipped = poDataBlockCurrent->GetRecordCount(RecordSkipped);
495
9.91k
        nDupl = poDataBlockCurrent->GetRecordCount(RecordDuplicated);
496
9.91k
        if (nSkipped > 0)
497
1.47k
            CPLError(CE_Warning, CPLE_AppDefined,
498
1.47k
                     "%s: %d invalid VFK data records skipped",
499
1.47k
                     poDataBlockCurrent->GetName(), nSkipped);
500
9.91k
        if (nDupl > 0)
501
879
            CPLError(CE_Warning, CPLE_AppDefined,
502
879
                     "%s: %d duplicated VFK data records skipped",
503
879
                     poDataBlockCurrent->GetName(), nDupl);
504
505
9.91k
        CPLDebug("OGR-VFK", "VFKReader::ReadDataRecords(): name=%s n=%d",
506
9.91k
                 poDataBlockCurrent->GetName(),
507
9.91k
                 poDataBlockCurrent->GetRecordCount(RecordValid));
508
9.91k
    }
509
510
2.81k
    return nRecords;
511
2.81k
}
512
513
IVFKDataBlock *VFKReader::CreateDataBlock(const char *pszBlockName)
514
0
{
515
0
    return (IVFKDataBlock *)new VFKDataBlock(pszBlockName, (IVFKReader *)this);
516
0
}
517
518
/*!
519
  \brief Add new data block
520
521
  \param poNewDataBlock pointer to VFKDataBlock instance
522
  \param pszDefn unused (see VFKReaderSQLite::AddDataBlock)
523
*/
524
void VFKReader::AddDataBlock(IVFKDataBlock *poNewDataBlock,
525
                             CPL_UNUSED const char *pszDefn)
526
9.99k
{
527
9.99k
    m_nDataBlockCount++;
528
529
9.99k
    m_papoDataBlock = (IVFKDataBlock **)CPLRealloc(
530
9.99k
        m_papoDataBlock, sizeof(IVFKDataBlock *) * m_nDataBlockCount);
531
9.99k
    m_papoDataBlock[m_nDataBlockCount - 1] = poNewDataBlock;
532
9.99k
}
533
534
/*!
535
  \brief Add feature
536
537
  \param poDataBlock pointer to VFKDataBlock instance
538
  \param poFeature pointer to VFKFeature instance
539
*/
540
OGRErr VFKReader::AddFeature(IVFKDataBlock *poDataBlock, VFKFeature *poFeature)
541
0
{
542
0
    poDataBlock->AddFeature(poFeature);
543
0
    return OGRERR_NONE;
544
0
}
545
546
/*!
547
  \brief Get data block
548
549
  \param i index (starting with 0)
550
551
  \return pointer to VFKDataBlock instance or NULL on failure
552
*/
553
IVFKDataBlock *VFKReader::GetDataBlock(int i) const
554
1.05M
{
555
1.05M
    if (i < 0 || i >= m_nDataBlockCount)
556
0
        return nullptr;
557
558
1.05M
    return m_papoDataBlock[i];
559
1.05M
}
560
561
/*!
562
  \brief Get data block
563
564
  \param pszName data block name
565
566
  \return pointer to VFKDataBlock instance or NULL on failure
567
*/
568
IVFKDataBlock *VFKReader::GetDataBlock(const char *pszName) const
569
269k
{
570
900k
    for (int i = 0; i < m_nDataBlockCount; i++)
571
813k
    {
572
813k
        if (EQUAL(GetDataBlock(i)->GetName(), pszName))
573
182k
            return GetDataBlock(i);
574
813k
    }
575
576
87.1k
    return nullptr;
577
269k
}
578
579
/*!
580
  \brief Load geometry (loop datablocks)
581
582
  \return number of invalid features
583
*/
584
int VFKReader::LoadGeometry()
585
0
{
586
0
    long int nfeatures = 0;
587
0
    for (int i = 0; i < m_nDataBlockCount; i++)
588
0
    {
589
0
        nfeatures += m_papoDataBlock[i]->LoadGeometry();
590
0
    }
591
592
0
    CPLDebug("OGR_VFK", "VFKReader::LoadGeometry(): invalid=%ld", nfeatures);
593
594
0
    return static_cast<int>(nfeatures);
595
0
}
596
597
/*!
598
  \brief Add info
599
600
  \param pszLine pointer to line
601
*/
602
void VFKReader::AddInfo(const char *pszLine)
603
391k
{
604
391k
    const int nOffset = pszLine[1] == 'H' ? 2 : 1;  // &DKATUZE
605
606
391k
    const char *poKey = pszLine + nOffset; /* &H */
607
391k
    const char *poChar = poKey;
608
391k
    int iKeyLength = 0;
609
6.94M
    while (*poChar != '\0' && *poChar != ';')
610
6.54M
    {
611
6.54M
        iKeyLength++;
612
6.54M
        poChar++;
613
6.54M
    }
614
391k
    if (*poChar == '\0')
615
39.6k
        return;
616
617
352k
    char *pszKey = (char *)CPLMalloc(iKeyLength + 1);
618
352k
    strncpy(pszKey, poKey, iKeyLength);
619
352k
    pszKey[iKeyLength] = '\0';
620
621
352k
    poChar++; /* skip ; */
622
623
352k
    int iValueLength = 0;
624
352k
    int nSkip = 3; /* &H + ; */
625
6.05M
    while (*poChar != '\0')
626
5.70M
    {
627
5.70M
        if (*poChar == '"' && iValueLength == 0)
628
80.9k
        {
629
80.9k
            nSkip++;
630
80.9k
        }
631
5.62M
        else
632
5.62M
        {
633
5.62M
            iValueLength++;
634
5.62M
        }
635
5.70M
        poChar++;
636
5.70M
    }
637
352k
    if (nSkip > 3 && iValueLength > 0)
638
26.5k
        iValueLength--;
639
640
352k
    char *pszValue = (char *)CPLMalloc(iValueLength + 1);
641
5.94M
    for (int i = 0; i < iValueLength; i++)
642
5.59M
    {
643
5.59M
        pszValue[i] = pszLine[iKeyLength + nSkip + i];
644
5.59M
        if (pszValue[i] == '"')
645
131k
        {
646
131k
            pszValue[i] = '\''; /* " -> ' */
647
131k
        }
648
5.59M
    }
649
650
352k
    pszValue[iValueLength] = '\0';
651
652
    /* recode values */
653
352k
    char *pszValueEnc = CPLRecode(pszValue, m_pszEncoding, CPL_ENC_UTF8);
654
655
352k
    if (poInfo.find(pszKey) == poInfo.end())
656
16.2k
    {
657
16.2k
        poInfo[pszKey] = pszValueEnc;
658
16.2k
    }
659
336k
    else
660
336k
    {
661
        /* max. number of duplicated keys can be 101 */
662
336k
        const size_t nLen = strlen(pszKey) + 5;
663
336k
        char *pszKeyUniq = (char *)CPLMalloc(nLen);
664
665
336k
        int nCount = 1; /* assuming at least one match */
666
336k
        for (std::map<CPLString, CPLString>::iterator i = poInfo.begin();
667
434M
             i != poInfo.end(); ++i)
668
434M
        {
669
434M
            size_t iFound = i->first.find("_");
670
434M
            if (iFound != std::string::npos &&
671
427M
                EQUALN(pszKey, i->first.c_str(), iFound))
672
180M
                nCount += 1;
673
434M
        }
674
675
336k
        snprintf(pszKeyUniq, nLen, "%s_%d", pszKey, nCount);
676
336k
        poInfo[pszKeyUniq] = pszValueEnc;
677
336k
        CPLFree(pszKeyUniq);
678
336k
    }
679
680
352k
    CPLFree(pszKey);
681
352k
    CPLFree(pszValue);
682
352k
    CPLFree(pszValueEnc);
683
352k
}
684
685
/*!
686
  \brief Get info
687
688
  \param key key string
689
690
  \return pointer to value string or NULL if key not found
691
*/
692
const char *VFKReader::GetInfo(const char *key)
693
0
{
694
0
    if (poInfo.find(key) == poInfo.end())
695
0
        return nullptr;
696
697
0
    return poInfo[key].c_str();
698
0
}