Coverage Report

Created: 2026-02-14 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/port/cplstringlist.cpp
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Project:  GDAL
4
 * Purpose:  CPLStringList implementation.
5
 * Author:   Frank Warmerdam, warmerdam@pobox.com
6
 *
7
 ******************************************************************************
8
 * Copyright (c) 2011, Frank Warmerdam <warmerdam@pobox.com>
9
 * Copyright (c) 2011, Even Rouault <even dot rouault at spatialys.com>
10
 *
11
 * SPDX-License-Identifier: MIT
12
 ****************************************************************************/
13
14
#include "cpl_port.h"
15
#include "cpl_string.h"
16
17
#include <cstddef>
18
#include <cstdio>
19
#include <cstdlib>
20
#include <cstring>
21
22
#include <algorithm>
23
#include <limits>
24
#include <string>
25
26
#include "cpl_conv.h"
27
#include "cpl_error.h"
28
29
static int CPLCompareKeyValueString(const char *pszKVa, const char *pszKVb);
30
31
/************************************************************************/
32
/*                           CPLStringList()                            */
33
/************************************************************************/
34
35
15.5k
CPLStringList::CPLStringList() = default;
36
37
/************************************************************************/
38
/*                           CPLStringList()                            */
39
/************************************************************************/
40
41
/**
42
 * CPLStringList constructor.
43
 *
44
 * @param papszListIn the NULL terminated list of strings to consume.
45
 * @param bTakeOwnership TRUE if the CPLStringList should take ownership
46
 * of the list of strings which implies responsibility to free them.
47
 */
48
49
CPLStringList::CPLStringList(char **papszListIn, int bTakeOwnership)
50
4.08k
    : CPLStringList()
51
52
4.08k
{
53
4.08k
    Assign(papszListIn, bTakeOwnership);
54
4.08k
}
55
56
/************************************************************************/
57
/*                           CPLStringList()                            */
58
/************************************************************************/
59
60
/**
61
 * CPLStringList constructor.
62
 *
63
 * The input list is copied.
64
 *
65
 * @param papszListIn the NULL terminated list of strings to ingest.
66
 */
67
68
0
CPLStringList::CPLStringList(CSLConstList papszListIn) : CPLStringList()
69
70
0
{
71
0
    Assign(CSLDuplicate(papszListIn));
72
0
}
73
74
/************************************************************************/
75
/*                           CPLStringList()                            */
76
/************************************************************************/
77
78
/**
79
 * CPLStringList constructor.
80
 *
81
 * The input list is copied.
82
 *
83
 * @param aosList input list.
84
 *
85
 * @since GDAL 3.9
86
 */
87
CPLStringList::CPLStringList(const std::vector<std::string> &aosList)
88
0
{
89
0
    if (!aosList.empty())
90
0
    {
91
0
        bOwnList = true;
92
0
        papszList = static_cast<char **>(
93
0
            VSI_CALLOC_VERBOSE(aosList.size() + 1, sizeof(char *)));
94
0
        nCount = static_cast<int>(aosList.size());
95
0
        for (int i = 0; i < nCount; ++i)
96
0
        {
97
0
            papszList[i] = VSI_STRDUP_VERBOSE(aosList[i].c_str());
98
0
        }
99
0
    }
100
0
}
101
102
/************************************************************************/
103
/*                           CPLStringList()                            */
104
/************************************************************************/
105
106
/**
107
 * CPLStringList constructor.
108
 *
109
 * The input list is copied.
110
 *
111
 * @param oInitList input list.
112
 *
113
 * @since GDAL 3.9
114
 */
115
CPLStringList::CPLStringList(std::initializer_list<const char *> oInitList)
116
0
{
117
0
    for (const char *pszStr : oInitList)
118
0
    {
119
0
        AddString(pszStr);
120
0
    }
121
0
}
122
123
/************************************************************************/
124
/*                           CPLStringList()                            */
125
/************************************************************************/
126
127
//! Copy constructor
128
0
CPLStringList::CPLStringList(const CPLStringList &oOther) : CPLStringList()
129
130
0
{
131
0
    operator=(oOther);
132
0
}
133
134
/************************************************************************/
135
/*                           CPLStringList()                            */
136
/************************************************************************/
137
138
//! Move constructor
139
0
CPLStringList::CPLStringList(CPLStringList &&oOther) : CPLStringList()
140
141
0
{
142
0
    operator=(std::move(oOther));
143
0
}
144
145
/************************************************************************/
146
/*                          BoundToConstList()                          */
147
/************************************************************************/
148
149
/**
150
 * Return a CPLStringList that wraps the passed list.
151
 *
152
 * The input list is *NOT* copied and must be kept alive while the
153
 * return CPLStringList is used.
154
 *
155
 * @param papszListIn a NULL terminated list of strings to wrap into the CPLStringList
156
 * @since GDAL 3.9
157
 */
158
159
/* static */
160
const CPLStringList CPLStringList::BoundToConstList(CSLConstList papszListIn)
161
0
{
162
0
    return CPLStringList(const_cast<char **>(papszListIn),
163
0
                         /* bTakeOwnership= */ false);
164
0
}
165
166
/************************************************************************/
167
/*                             operator=()                              */
168
/************************************************************************/
169
170
CPLStringList &CPLStringList::operator=(const CPLStringList &oOther)
171
0
{
172
0
    if (this != &oOther)
173
0
    {
174
0
        char **l_papszList = CSLDuplicate(oOther.papszList);
175
0
        if (l_papszList)
176
0
        {
177
0
            Assign(l_papszList, TRUE);
178
0
            nAllocation = oOther.nCount > 0 ? oOther.nCount + 1 : 0;
179
0
            nCount = oOther.nCount;
180
0
            bIsSorted = oOther.bIsSorted;
181
0
        }
182
0
    }
183
184
0
    return *this;
185
0
}
186
187
/************************************************************************/
188
/*                             operator=()                              */
189
/************************************************************************/
190
191
CPLStringList &CPLStringList::operator=(CPLStringList &&oOther)
192
0
{
193
0
    if (this != &oOther)
194
0
    {
195
0
        Clear();
196
0
        papszList = oOther.papszList;
197
0
        oOther.papszList = nullptr;
198
0
        nCount = oOther.nCount;
199
0
        oOther.nCount = 0;
200
0
        nAllocation = oOther.nAllocation;
201
0
        oOther.nAllocation = 0;
202
0
        bOwnList = oOther.bOwnList;
203
0
        oOther.bOwnList = false;
204
0
        bIsSorted = oOther.bIsSorted;
205
0
        oOther.bIsSorted = true;
206
0
    }
207
208
0
    return *this;
209
0
}
210
211
/************************************************************************/
212
/*                             operator=()                              */
213
/************************************************************************/
214
215
CPLStringList &CPLStringList::operator=(CSLConstList papszListIn)
216
0
{
217
0
    if (papszListIn != papszList)
218
0
    {
219
0
        Assign(CSLDuplicate(papszListIn));
220
0
        bIsSorted = false;
221
0
    }
222
223
0
    return *this;
224
0
}
225
226
/************************************************************************/
227
/*                           ~CPLStringList()                           */
228
/************************************************************************/
229
230
CPLStringList::~CPLStringList()
231
232
15.5k
{
233
15.5k
    Clear();
234
15.5k
}
235
236
/************************************************************************/
237
/*                               Clear()                                */
238
/************************************************************************/
239
240
/**
241
 * Clear the string list.
242
 */
243
CPLStringList &CPLStringList::Clear()
244
245
22.9k
{
246
22.9k
    if (bOwnList)
247
5.51k
    {
248
5.51k
        CSLDestroy(papszList);
249
5.51k
        papszList = nullptr;
250
251
5.51k
        bOwnList = FALSE;
252
5.51k
        nAllocation = 0;
253
5.51k
        nCount = 0;
254
5.51k
    }
255
256
22.9k
    return *this;
257
22.9k
}
258
259
/************************************************************************/
260
/*                               Assign()                               */
261
/************************************************************************/
262
263
/**
264
 * Assign a list of strings.
265
 *
266
 *
267
 * @param papszListIn the NULL terminated list of strings to consume.
268
 * @param bTakeOwnership TRUE if the CPLStringList should take ownership
269
 * of the list of strings which implies responsibility to free them.
270
 *
271
 * @return a reference to the CPLStringList on which it was invoked.
272
 */
273
274
CPLStringList &CPLStringList::Assign(char **papszListIn, int bTakeOwnership)
275
276
7.41k
{
277
7.41k
    Clear();
278
279
7.41k
    papszList = papszListIn;
280
7.41k
    bOwnList = CPL_TO_BOOL(bTakeOwnership);
281
282
7.41k
    if (papszList == nullptr || *papszList == nullptr)
283
2.05k
        nCount = 0;
284
5.36k
    else
285
5.36k
        nCount = -1;  // unknown
286
287
7.41k
    nAllocation = 0;
288
7.41k
    bIsSorted = FALSE;
289
290
7.41k
    return *this;
291
7.41k
}
292
293
/************************************************************************/
294
/*                               Count()                                */
295
/************************************************************************/
296
297
/**
298
 * @return count of strings in the list, zero if empty.
299
 */
300
301
int CPLStringList::Count() const
302
303
30.0k
{
304
30.0k
    if (nCount == -1)
305
4.08k
    {
306
4.08k
        if (papszList == nullptr)
307
0
        {
308
0
            nCount = 0;
309
0
            nAllocation = 0;
310
0
        }
311
4.08k
        else
312
4.08k
        {
313
4.08k
            nCount = CSLCount(papszList);
314
4.08k
            nAllocation = std::max(nCount + 1, nAllocation);
315
4.08k
        }
316
4.08k
    }
317
318
30.0k
    return nCount;
319
30.0k
}
320
321
/************************************************************************/
322
/*                           MakeOurOwnCopy()                           */
323
/*                                                                      */
324
/*      If we don't own the list, a copy is made which we own.          */
325
/*      Necessary if we are going to modify the list.                   */
326
/************************************************************************/
327
328
bool CPLStringList::MakeOurOwnCopy()
329
330
9.56k
{
331
9.56k
    if (bOwnList)
332
0
        return true;
333
334
9.56k
    if (papszList == nullptr)
335
9.56k
        return true;
336
337
0
    Count();
338
0
    char **papszListNew = CSLDuplicate(papszList);
339
0
    if (papszListNew == nullptr)
340
0
    {
341
0
        return false;
342
0
    }
343
0
    papszList = papszListNew;
344
0
    bOwnList = true;
345
0
    nAllocation = nCount + 1;
346
0
    return true;
347
0
}
348
349
/************************************************************************/
350
/*                          EnsureAllocation()                          */
351
/*                                                                      */
352
/*      Ensure we have enough room allocated for at least the           */
353
/*      requested number of strings (so nAllocation will be at least    */
354
/*      one more than the target)                                       */
355
/************************************************************************/
356
357
bool CPLStringList::EnsureAllocation(int nMaxList)
358
359
40.6k
{
360
40.6k
    if (!bOwnList)
361
9.56k
    {
362
9.56k
        if (!MakeOurOwnCopy())
363
0
            return false;
364
9.56k
    }
365
366
40.6k
    if (papszList == nullptr || nAllocation <= nMaxList)
367
9.68k
    {
368
        // we need to be able to store nMaxList+1 as an int,
369
        // and allocate (nMaxList+1) * sizeof(char*) bytes
370
9.68k
        if (nMaxList < 0 || nMaxList > std::numeric_limits<int>::max() - 1 ||
371
9.68k
            static_cast<size_t>(nMaxList) >
372
9.68k
                std::numeric_limits<size_t>::max() / sizeof(char *) - 1)
373
0
        {
374
0
            return false;
375
0
        }
376
9.68k
        int nNewAllocation = nMaxList + 1;
377
9.68k
        if (nNewAllocation <= (std::numeric_limits<int>::max() - 20) / 2 /
378
9.68k
                                  static_cast<int>(sizeof(char *)))
379
9.68k
            nNewAllocation = std::max(nNewAllocation * 2 + 20, nMaxList + 1);
380
9.68k
        if (papszList == nullptr)
381
9.56k
        {
382
9.56k
            papszList = static_cast<char **>(
383
9.56k
                VSI_CALLOC_VERBOSE(nNewAllocation, sizeof(char *)));
384
9.56k
            bOwnList = true;
385
9.56k
            nCount = 0;
386
9.56k
            if (papszList == nullptr)
387
0
                return false;
388
9.56k
        }
389
113
        else
390
113
        {
391
113
            char **papszListNew = static_cast<char **>(VSI_REALLOC_VERBOSE(
392
113
                papszList, nNewAllocation * sizeof(char *)));
393
113
            if (papszListNew == nullptr)
394
0
                return false;
395
113
            papszList = papszListNew;
396
113
        }
397
9.68k
        nAllocation = nNewAllocation;
398
9.68k
    }
399
40.6k
    return true;
400
40.6k
}
401
402
/************************************************************************/
403
/*                         AddStringDirectly()                          */
404
/************************************************************************/
405
406
/**
407
 * Add a string to the list.
408
 *
409
 * This method is similar to AddString(), but ownership of the
410
 * pszNewString is transferred to the CPLStringList class.
411
 *
412
 * @param pszNewString the string to add to the list.
413
 */
414
415
CPLStringList &CPLStringList::AddStringDirectly(char *pszNewString)
416
417
40.6k
{
418
40.6k
    if (nCount == -1)
419
0
        Count();
420
421
40.6k
    if (!EnsureAllocation(nCount + 1))
422
0
    {
423
0
        VSIFree(pszNewString);
424
0
        return *this;
425
0
    }
426
427
40.6k
    papszList[nCount++] = pszNewString;
428
40.6k
    papszList[nCount] = nullptr;
429
430
40.6k
    bIsSorted = false;
431
432
40.6k
    return *this;
433
40.6k
}
434
435
/************************************************************************/
436
/*                             AddString()                              */
437
/************************************************************************/
438
439
/**
440
 * Add a string to the list.
441
 *
442
 * A copy of the passed in string is made and inserted in the list.
443
 *
444
 * @param pszNewString the string to add to the list.
445
 */
446
447
CPLStringList &CPLStringList::AddString(const char *pszNewString)
448
449
40.6k
{
450
40.6k
    char *pszDupString = VSI_STRDUP_VERBOSE(pszNewString);
451
40.6k
    if (pszDupString == nullptr)
452
0
        return *this;
453
40.6k
    return AddStringDirectly(pszDupString);
454
40.6k
}
455
456
/************************************************************************/
457
/*                             AddString()                              */
458
/************************************************************************/
459
/**
460
 * Add a string to the list.
461
 *
462
 * A copy of the passed in string is made and inserted in the list.
463
 *
464
 * @param newString the string to add to the list.
465
 * @return a reference to the CPLStringList on which it was invoked.
466
 */
467
468
CPLStringList &CPLStringList::AddString(const std::string &newString)
469
0
{
470
0
    return AddString(newString.c_str());
471
0
}
472
473
/************************************************************************/
474
/*                            AddNameValue()                            */
475
/************************************************************************/
476
477
/**
478
 * Add a name=value entry to the list.
479
 *
480
 * A key=value string is prepared and appended to the list.  There is no
481
 * check for other values for the same key in the list.
482
 *
483
 * @param pszKey the key name to add.
484
 * @param pszValue the key value to add.
485
 */
486
487
CPLStringList &CPLStringList::AddNameValue(const char *pszKey,
488
                                           const char *pszValue)
489
490
0
{
491
0
    if (pszKey == nullptr || pszValue == nullptr)
492
0
        return *this;
493
494
0
    if (!MakeOurOwnCopy())
495
0
        return *this;
496
497
    /* -------------------------------------------------------------------- */
498
    /*      Format the line.                                                */
499
    /* -------------------------------------------------------------------- */
500
0
    if (strlen(pszKey) >
501
0
            std::numeric_limits<size_t>::max() - strlen(pszValue) ||
502
0
        strlen(pszKey) + strlen(pszValue) >
503
0
            std::numeric_limits<size_t>::max() - 2)
504
0
    {
505
0
        CPLError(CE_Failure, CPLE_OutOfMemory,
506
0
                 "Too big strings in AddNameValue()");
507
0
        return *this;
508
0
    }
509
0
    const size_t nLen = strlen(pszKey) + strlen(pszValue) + 2;
510
0
    char *pszLine = static_cast<char *>(VSI_MALLOC_VERBOSE(nLen));
511
0
    if (pszLine == nullptr)
512
0
        return *this;
513
0
    snprintf(pszLine, nLen, "%s=%s", pszKey, pszValue);
514
515
    /* -------------------------------------------------------------------- */
516
    /*      If we don't need to keep the sort order things are pretty       */
517
    /*      straight forward.                                               */
518
    /* -------------------------------------------------------------------- */
519
0
    if (!IsSorted())
520
0
        return AddStringDirectly(pszLine);
521
522
    /* -------------------------------------------------------------------- */
523
    /*      Find the proper insertion point.                                */
524
    /* -------------------------------------------------------------------- */
525
0
    CPLAssert(IsSorted());
526
0
    const int iKey = FindSortedInsertionPoint(pszLine);
527
0
    InsertStringDirectly(iKey, pszLine);
528
0
    bIsSorted = true;  // We have actually preserved sort order.
529
530
0
    return *this;
531
0
}
532
533
/************************************************************************/
534
/*                            SetNameValue()                            */
535
/************************************************************************/
536
537
/**
538
 * Set name=value entry in the list.
539
 *
540
 * Similar to AddNameValue(), except if there is already a value for
541
 * the key in the list it is replaced instead of adding a new entry to
542
 * the list.  If pszValue is NULL any existing key entry is removed.
543
 *
544
 * @param pszKey the key name to add.
545
 * @param pszValue the key value to add.
546
 */
547
548
CPLStringList &CPLStringList::SetNameValue(const char *pszKey,
549
                                           const char *pszValue)
550
551
0
{
552
0
    int iKey = FindName(pszKey);
553
554
0
    if (iKey == -1)
555
0
        return AddNameValue(pszKey, pszValue);
556
557
0
    Count();
558
0
    if (!MakeOurOwnCopy())
559
0
        return *this;
560
561
0
    CPLFree(papszList[iKey]);
562
0
    if (pszValue == nullptr)  // delete entry
563
0
    {
564
565
        // shift everything down by one.
566
0
        do
567
0
        {
568
0
            papszList[iKey] = papszList[iKey + 1];
569
0
        } while (papszList[iKey++] != nullptr);
570
571
0
        nCount--;
572
0
    }
573
0
    else
574
0
    {
575
0
        if (strlen(pszKey) >
576
0
                std::numeric_limits<size_t>::max() - strlen(pszValue) ||
577
0
            strlen(pszKey) + strlen(pszValue) >
578
0
                std::numeric_limits<size_t>::max() - 2)
579
0
        {
580
0
            CPLError(CE_Failure, CPLE_OutOfMemory,
581
0
                     "Too big strings in AddNameValue()");
582
0
            return *this;
583
0
        }
584
0
        const size_t nLen = strlen(pszKey) + strlen(pszValue) + 2;
585
0
        char *pszLine = static_cast<char *>(VSI_MALLOC_VERBOSE(nLen));
586
0
        if (pszLine == nullptr)
587
0
            return *this;
588
0
        snprintf(pszLine, nLen, "%s=%s", pszKey, pszValue);
589
590
0
        papszList[iKey] = pszLine;
591
0
    }
592
593
0
    return *this;
594
0
}
595
596
/************************************************************************/
597
/*                             SetString()                              */
598
/************************************************************************/
599
600
/**
601
 * Replace a string within the list.
602
 *
603
 * @param pos 0-index position of the string to replace
604
 * @param pszString value to be used (will be copied)
605
 * @return a reference to the CPLStringList on which it was invoked.
606
 * @since 3.13
607
 */
608
CPLStringList &CPLStringList::SetString(int pos, const char *pszString)
609
0
{
610
0
    return SetStringDirectly(pos, VSI_STRDUP_VERBOSE(pszString));
611
0
}
612
613
/**
614
 * Replace a string within the list.
615
 *
616
 * @param pos 0-index position of the string to replace
617
 * @param osString value to be used (will be copied)
618
 * @return a reference to the CPLStringList on which it was invoked.
619
 * @since 3.13
620
 */
621
CPLStringList &CPLStringList::SetString(int pos, const std::string &osString)
622
0
{
623
0
    return SetString(pos, osString.c_str());
624
0
}
625
626
/**
627
 * Replace a string within the list.
628
 *
629
 * @param pos 0-index position of the string to replace
630
 * @param pszString value to be used (ownership is taken)
631
 * @return a reference to the CPLStringList on which it was invoked.
632
 * @since 3.13
633
 */
634
CPLStringList &CPLStringList::SetStringDirectly(int pos, char *pszString)
635
0
{
636
0
    if (!MakeOurOwnCopy())
637
0
        return *this;
638
639
0
    CPLFree(papszList[pos]);
640
0
    papszList[pos] = pszString;
641
642
0
    if (bIsSorted)
643
0
    {
644
0
        if (pos > 0 &&
645
0
            CPLCompareKeyValueString(papszList[pos], papszList[pos - 1]) == -1)
646
0
        {
647
0
            bIsSorted = false;
648
0
        }
649
0
        if (pos < Count() - 1 &&
650
0
            CPLCompareKeyValueString(papszList[pos], papszList[pos + 1]) == 1)
651
0
        {
652
0
            bIsSorted = false;
653
0
        }
654
0
    }
655
656
0
    return *this;
657
0
}
658
659
/************************************************************************/
660
/*                              operator[]                              */
661
/************************************************************************/
662
663
/**
664
 * Fetch entry "i".
665
 *
666
 * Fetches the requested item in the list.  Note that the returned string
667
 * remains owned by the CPLStringList.  If "i" is out of range NULL is
668
 * returned.
669
 *
670
 * @param i the index of the list item to return.
671
 * @return selected entry in the list.
672
 */
673
char *CPLStringList::operator[](int i)
674
675
0
{
676
0
    if (nCount == -1)
677
0
        Count();
678
679
0
    if (i < 0 || i >= nCount)
680
0
        return nullptr;
681
682
0
    return papszList[i];
683
0
}
684
685
const char *CPLStringList::operator[](int i) const
686
687
26.4k
{
688
26.4k
    if (nCount == -1)
689
0
        Count();
690
691
26.4k
    if (i < 0 || i >= nCount)
692
0
        return nullptr;
693
694
26.4k
    return papszList[i];
695
26.4k
}
696
697
/************************************************************************/
698
/*                             StealList()                              */
699
/************************************************************************/
700
701
/**
702
 * Seize ownership of underlying string array.
703
 *
704
 * This method is similar to List(), except that the returned list is
705
 * now owned by the caller and the CPLStringList is emptied.
706
 *
707
 * @return the C style string list.
708
 */
709
char **CPLStringList::StealList()
710
711
11.4k
{
712
11.4k
    char **papszRetList = papszList;
713
714
11.4k
    bOwnList = false;
715
11.4k
    papszList = nullptr;
716
11.4k
    nCount = 0;
717
11.4k
    nAllocation = 0;
718
719
11.4k
    return papszRetList;
720
11.4k
}
721
722
/* Case insensitive comparison function */
723
static int CPLCompareKeyValueString(const char *pszKVa, const char *pszKVb)
724
0
{
725
0
    const char *pszItera = pszKVa;
726
0
    const char *pszIterb = pszKVb;
727
0
    while (true)
728
0
    {
729
0
        char cha = *pszItera;
730
0
        char chb = *pszIterb;
731
0
        if (cha == '=' || cha == '\0')
732
0
        {
733
0
            if (chb == '=' || chb == '\0')
734
0
                return 0;
735
0
            else
736
0
                return -1;
737
0
        }
738
0
        if (chb == '=' || chb == '\0')
739
0
        {
740
0
            return 1;
741
0
        }
742
0
        if (cha >= 'a' && cha <= 'z')
743
0
            cha -= ('a' - 'A');
744
0
        if (chb >= 'a' && chb <= 'z')
745
0
            chb -= ('a' - 'A');
746
0
        if (cha < chb)
747
0
            return -1;
748
0
        else if (cha > chb)
749
0
            return 1;
750
0
        pszItera++;
751
0
        pszIterb++;
752
0
    }
753
0
}
754
755
/************************************************************************/
756
/*                                Sort()                                */
757
/************************************************************************/
758
759
/**
760
 * Sort the entries in the list and mark list sorted.
761
 *
762
 * Note that once put into "sorted" mode, the CPLStringList will attempt to
763
 * keep things in sorted order through calls to AddString(),
764
 * AddStringDirectly(), AddNameValue(), SetNameValue(). Complete list
765
 * assignments (via Assign() and operator= will clear the sorting state.
766
 * When in sorted order FindName(), FetchNameValue() and FetchNameValueDef()
767
 * will do a binary search to find the key, substantially improve lookup
768
 * performance in large lists.
769
 */
770
771
CPLStringList &CPLStringList::Sort()
772
773
0
{
774
0
    Count();
775
0
    if (!MakeOurOwnCopy())
776
0
        return *this;
777
778
0
    if (nCount > 1)
779
0
    {
780
0
        std::sort(papszList, papszList + nCount,
781
0
                  [](const char *a, const char *b)
782
0
                  { return CPLCompareKeyValueString(a, b) < 0; });
783
0
    }
784
0
    bIsSorted = true;
785
786
0
    return *this;
787
0
}
788
789
/************************************************************************/
790
/*                              FindName()                              */
791
/************************************************************************/
792
793
/**
794
 * Get index of given name/value keyword.
795
 *
796
 * Note that this search is for a line in the form name=value or name:value.
797
 * Use FindString() or PartialFindString() for searches not based on name=value
798
 * pairs.
799
 *
800
 * @param pszKey the name to search for.
801
 *
802
 * @return the string list index of this name, or -1 on failure.
803
 */
804
805
int CPLStringList::FindName(const char *pszKey) const
806
807
0
{
808
0
    if (!IsSorted())
809
0
        return CSLFindName(papszList, pszKey);
810
811
    // If we are sorted, we can do an optimized binary search.
812
0
    int iStart = 0;
813
0
    int iEnd = nCount - 1;
814
0
    size_t nKeyLen = strlen(pszKey);
815
816
0
    while (iStart <= iEnd)
817
0
    {
818
0
        const int iMiddle = (iEnd + iStart) / 2;
819
0
        const char *pszMiddle = papszList[iMiddle];
820
821
0
        if (EQUALN(pszMiddle, pszKey, nKeyLen) &&
822
0
            (pszMiddle[nKeyLen] == '=' || pszMiddle[nKeyLen] == ':'))
823
0
            return iMiddle;
824
825
0
        if (CPLCompareKeyValueString(pszKey, pszMiddle) < 0)
826
0
            iEnd = iMiddle - 1;
827
0
        else
828
0
            iStart = iMiddle + 1;
829
0
    }
830
831
0
    return -1;
832
0
}
833
834
/************************************************************************/
835
/*                             FetchBool()                              */
836
/************************************************************************/
837
/**
838
 *
839
 * Check for boolean key value.
840
 *
841
 * In a CPLStringList of "Name=Value" pairs, look to see if there is a key
842
 * with the given name, and if it can be interpreted as being TRUE.  If
843
 * the key appears without any "=Value" portion it will be considered true.
844
 * If the value is NO, FALSE or 0 it will be considered FALSE otherwise
845
 * if the key appears in the list it will be considered TRUE.  If the key
846
 * doesn't appear at all, the indicated default value will be returned.
847
 *
848
 * @param pszKey the key value to look for (case insensitive).
849
 * @param bDefault the value to return if the key isn't found at all.
850
 *
851
 * @return true or false
852
 */
853
854
bool CPLStringList::FetchBool(const char *pszKey, bool bDefault) const
855
856
0
{
857
0
    const char *pszValue = FetchNameValue(pszKey);
858
859
0
    if (pszValue == nullptr)
860
0
        return bDefault;
861
862
0
    return CPLTestBool(pszValue);
863
0
}
864
865
/************************************************************************/
866
/*                            FetchBoolean()                            */
867
/************************************************************************/
868
/**
869
 *
870
 * DEPRECATED: Check for boolean key value.
871
 *
872
 * In a CPLStringList of "Name=Value" pairs, look to see if there is a key
873
 * with the given name, and if it can be interpreted as being TRUE.  If
874
 * the key appears without any "=Value" portion it will be considered true.
875
 * If the value is NO, FALSE or 0 it will be considered FALSE otherwise
876
 * if the key appears in the list it will be considered TRUE.  If the key
877
 * doesn't appear at all, the indicated default value will be returned.
878
 *
879
 * @param pszKey the key value to look for (case insensitive).
880
 * @param bDefault the value to return if the key isn't found at all.
881
 *
882
 * @return TRUE or FALSE
883
 */
884
885
int CPLStringList::FetchBoolean(const char *pszKey, int bDefault) const
886
887
0
{
888
0
    return FetchBool(pszKey, CPL_TO_BOOL(bDefault)) ? TRUE : FALSE;
889
0
}
890
891
/************************************************************************/
892
/*                           FetchNameValue()                           */
893
/************************************************************************/
894
895
/**
896
 * Fetch value associated with this key name.
897
 *
898
 * If this list sorted, a fast binary search is done, otherwise a linear
899
 * scan is done.  Name lookup is case insensitive.
900
 *
901
 * @param pszName the key name to search for.
902
 *
903
 * @return the corresponding value or NULL if not found.  The returned string
904
 * should not be modified and points into internal object state that may
905
 * change on future calls.
906
 */
907
908
const char *CPLStringList::FetchNameValue(const char *pszName) const
909
910
0
{
911
0
    const int iKey = FindName(pszName);
912
913
0
    if (iKey == -1)
914
0
        return nullptr;
915
916
0
    CPLAssert(papszList[iKey][strlen(pszName)] == '=' ||
917
0
              papszList[iKey][strlen(pszName)] == ':');
918
919
0
    return papszList[iKey] + strlen(pszName) + 1;
920
0
}
921
922
/************************************************************************/
923
/*                         FetchNameValueDef()                          */
924
/************************************************************************/
925
926
/**
927
 * Fetch value associated with this key name.
928
 *
929
 * If this list sorted, a fast binary search is done, otherwise a linear
930
 * scan is done.  Name lookup is case insensitive.
931
 *
932
 * @param pszName the key name to search for.
933
 * @param pszDefault the default value returned if the named entry isn't found.
934
 *
935
 * @return the corresponding value or the passed default if not found.
936
 */
937
938
const char *CPLStringList::FetchNameValueDef(const char *pszName,
939
                                             const char *pszDefault) const
940
941
0
{
942
0
    const char *pszValue = FetchNameValue(pszName);
943
0
    if (pszValue == nullptr)
944
0
        return pszDefault;
945
946
0
    return pszValue;
947
0
}
948
949
/************************************************************************/
950
/*                            InsertString()                            */
951
/************************************************************************/
952
953
/**
954
 * \fn CPLStringList *CPLStringList::InsertString( int nInsertAtLineNo,
955
 *                                                 const char *pszNewLine );
956
 *
957
 * \brief Insert into the list at identified location.
958
 *
959
 * This method will insert a string into the list at the identified
960
 * location.  The insertion point must be within or at the end of the list.
961
 * The following entries are pushed down to make space.
962
 *
963
 * @param nInsertAtLineNo the line to insert at, zero to insert at front.
964
 * @param pszNewLine to the line to insert.  This string will be copied.
965
 */
966
967
/************************************************************************/
968
/*                        InsertStringDirectly()                        */
969
/************************************************************************/
970
971
/**
972
 * Insert into the list at identified location.
973
 *
974
 * This method will insert a string into the list at the identified
975
 * location.  The insertion point must be within or at the end of the list.
976
 * The following entries are pushed down to make space.
977
 *
978
 * @param nInsertAtLineNo the line to insert at, zero to insert at front.
979
 * @param pszNewLine to the line to insert, the ownership of this string
980
 * will be taken over the by the object.  It must have been allocated on the
981
 * heap.
982
 */
983
984
CPLStringList &CPLStringList::InsertStringDirectly(int nInsertAtLineNo,
985
                                                   char *pszNewLine)
986
987
0
{
988
0
    if (nCount == -1)
989
0
        Count();
990
991
0
    if (!EnsureAllocation(nCount + 1))
992
0
    {
993
0
        VSIFree(pszNewLine);
994
0
        return *this;
995
0
    }
996
997
0
    if (nInsertAtLineNo < 0 || nInsertAtLineNo > nCount)
998
0
    {
999
0
        CPLError(CE_Failure, CPLE_AppDefined,
1000
0
                 "CPLStringList::InsertString() requested beyond list end.");
1001
0
        return *this;
1002
0
    }
1003
1004
0
    bIsSorted = false;
1005
1006
0
    for (int i = nCount; i > nInsertAtLineNo; i--)
1007
0
        papszList[i] = papszList[i - 1];
1008
1009
0
    papszList[nInsertAtLineNo] = pszNewLine;
1010
0
    papszList[++nCount] = nullptr;
1011
1012
0
    return *this;
1013
0
}
1014
1015
/************************************************************************/
1016
/*                           RemoveStrings()                            */
1017
/************************************************************************/
1018
1019
/**
1020
 * Remove strings inside a CPLStringList.
1021
 *
1022
 * @param nFirstLineToDelete the 0-based index of the first string to
1023
 * remove. If this value is -1 or is larger than the actual
1024
 * number of strings in list then the nNumToRemove last strings are
1025
 * removed.
1026
 * @param nNumToRemove the number of strings to remove
1027
 *
1028
 * @return a reference to the CPLStringList on which it was invoked.
1029
 * @since 3.13
1030
 */
1031
CPLStringList &CPLStringList::RemoveStrings(int nFirstLineToDelete,
1032
                                            int nNumToRemove)
1033
0
{
1034
0
    if (!MakeOurOwnCopy())
1035
0
        return *this;
1036
1037
0
    papszList =
1038
0
        CSLRemoveStrings(papszList, nFirstLineToDelete, nNumToRemove, nullptr);
1039
0
    nCount = -1;
1040
0
    return *this;
1041
0
}
1042
1043
/************************************************************************/
1044
/*                      FindSortedInsertionPoint()                      */
1045
/*                                                                      */
1046
/*      Find the location at which the indicated line should be         */
1047
/*      inserted in order to keep things in sorted order.               */
1048
/************************************************************************/
1049
1050
int CPLStringList::FindSortedInsertionPoint(const char *pszLine)
1051
1052
0
{
1053
0
    CPLAssert(IsSorted());
1054
1055
0
    int iStart = 0;
1056
0
    int iEnd = nCount - 1;
1057
1058
0
    while (iStart <= iEnd)
1059
0
    {
1060
0
        const int iMiddle = (iEnd + iStart) / 2;
1061
0
        const char *pszMiddle = papszList[iMiddle];
1062
1063
0
        if (CPLCompareKeyValueString(pszLine, pszMiddle) < 0)
1064
0
            iEnd = iMiddle - 1;
1065
0
        else
1066
0
            iStart = iMiddle + 1;
1067
0
    }
1068
1069
0
    iEnd++;
1070
0
    CPLAssert(iEnd >= 0 && iEnd <= nCount);
1071
0
    CPLAssert(iEnd == 0 ||
1072
0
              CPLCompareKeyValueString(pszLine, papszList[iEnd - 1]) >= 0);
1073
0
    CPLAssert(iEnd == nCount ||
1074
0
              CPLCompareKeyValueString(pszLine, papszList[iEnd]) <= 0);
1075
1076
0
    return iEnd;
1077
0
}
1078
1079
namespace cpl
1080
{
1081
1082
/************************************************************************/
1083
/*          CSLIterator::operator==(const CSLIterator &other)           */
1084
/************************************************************************/
1085
1086
/*! @cond Doxygen_Suppress */
1087
bool CSLIterator::operator==(const CSLIterator &other) const
1088
0
{
1089
0
    if (!m_bAtEnd && other.m_bAtEnd)
1090
0
    {
1091
0
        return m_papszList == nullptr || *m_papszList == nullptr;
1092
0
    }
1093
0
    if (!m_bAtEnd && !other.m_bAtEnd)
1094
0
    {
1095
0
        return m_papszList == other.m_papszList;
1096
0
    }
1097
0
    if (m_bAtEnd && other.m_bAtEnd)
1098
0
    {
1099
0
        return true;
1100
0
    }
1101
0
    return false;
1102
0
}
1103
1104
/*! @endcond */
1105
1106
/************************************************************************/
1107
/*                  CSLNameValueIterator::operator*()                   */
1108
/************************************************************************/
1109
1110
/*! @cond Doxygen_Suppress */
1111
CSLNameValueIterator::value_type CSLNameValueIterator::operator*()
1112
0
{
1113
0
    if (m_papszList)
1114
0
    {
1115
0
        while (*m_papszList)
1116
0
        {
1117
0
            char *pszKey = nullptr;
1118
0
            const char *pszValue = CPLParseNameValue(*m_papszList, &pszKey);
1119
0
            if (pszKey)
1120
0
            {
1121
0
                m_osKey = pszKey;
1122
0
                CPLFree(pszKey);
1123
0
                return {m_osKey.c_str(), pszValue};
1124
0
            }
1125
0
            else if (m_bReturnNullKeyIfNotNameValue)
1126
0
            {
1127
0
                return {nullptr, *m_papszList};
1128
0
            }
1129
            // Skip entries that are not name=value pairs.
1130
0
            ++m_papszList;
1131
0
        }
1132
0
    }
1133
    // Should not happen
1134
0
    CPLAssert(false);
1135
0
    return {"", ""};
1136
0
}
1137
1138
/*! @endcond */
1139
1140
/************************************************************************/
1141
/*                  CSLNameValueIteratorWrapper::end()                  */
1142
/************************************************************************/
1143
1144
/*! @cond Doxygen_Suppress */
1145
CSLNameValueIterator CSLNameValueIteratorWrapper::end() const
1146
0
{
1147
0
    int nCount = CSLCount(m_papszList);
1148
0
    if (!m_bReturnNullKeyIfNotNameValue)
1149
0
    {
1150
0
        while (nCount > 0 && strchr(m_papszList[nCount - 1], '=') == nullptr)
1151
0
            --nCount;
1152
0
    }
1153
0
    return CSLNameValueIterator{m_papszList + nCount,
1154
0
                                m_bReturnNullKeyIfNotNameValue};
1155
0
}
1156
1157
/*! @endcond */
1158
1159
}  // namespace cpl