]> git.ipfire.org Git - thirdparty/squid.git/blame - src/wordlist.h
Implement iterator for wordlist (#1729)
[thirdparty/squid.git] / src / wordlist.h
CommitLineData
d295d770 1/*
b8ae064d 2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
d295d770 3 *
bbc27441
AJ
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
d295d770 7 */
bbc27441 8
ff9d9458
FC
9#ifndef SQUID_SRC_WORDLIST_H
10#define SQUID_SRC_WORDLIST_H
d295d770 11
582c2af2 12#include "globals.h"
58d8264f 13#include "sbuf/List.h"
d295d770 14
5a231dcd
FC
15#include <iterator>
16
17class wordlist;
18
19/// minimal iterator for read-only traversal of wordlist objects
20class WordlistIterator
21{
22public:
23 using value_type = const char *;
24
25 explicit WordlistIterator(const wordlist * const wl): w(wl) {}
26
27 auto operator ==(const WordlistIterator &rhs) const { return this->w == rhs.w; }
28 auto operator !=(const WordlistIterator &rhs) const { return this->w != rhs.w; }
29
30 inline value_type operator *() const;
31 inline WordlistIterator &operator++();
32
33private:
34 const wordlist *w;
35};
36
6a17a36d
FC
37/** A list of C-strings
38 *
39 * \deprecated use SBufList instead
40 */
d295d770 41class wordlist
42{
d295d770 43 MEMPROXY_CLASS(wordlist);
0d225c05 44 friend char *wordlistChopHead(wordlist **);
741c2986
AJ
45
46public:
5a231dcd
FC
47 using const_iterator = WordlistIterator;
48
0d225c05
FC
49 wordlist() : key(nullptr), next(nullptr) {}
50 // create a new wordlist node, with a copy of k as key
51 explicit wordlist(const char *k) : key(xstrdup(k)), next(nullptr) {}
52
53 wordlist(const wordlist &) = delete;
54 wordlist &operator=(const wordlist &) = delete;
55
5a231dcd
FC
56 auto begin() const { return const_iterator(this); }
57 auto end() const { return const_iterator(nullptr); }
58
d295d770 59 char *key;
60 wordlist *next;
0d225c05
FC
61
62private:
2f373c58 63 // does not free data members.
0d225c05 64 ~wordlist() = default;
d295d770 65};
66
9ce629cf 67class MemBuf;
514fc315 68
6a17a36d
FC
69/** Add a null-terminated c-string to a wordlist
70 *
71 * \deprecated use SBufList.push_back(SBuf(word)) instead
72 */
8a648e8d 73const char *wordlistAdd(wordlist **, const char *);
514fc315 74
6a17a36d
FC
75/** Concatenate a wordlist
76 *
5218815a 77 * \deprecated use SBufListContainerJoin(SBuf()) from sbuf/Algorithms.h instead
6a17a36d
FC
78 */
79void wordlistCat(const wordlist *, MemBuf *);
514fc315 80
6a17a36d 81/// destroy a wordlist
8a648e8d 82void wordlistDestroy(wordlist **);
d295d770 83
2f373c58 84/** Remove and destroy the first element while preserving and returning its key
0d225c05
FC
85 *
86 * \note the returned key must be freed by the caller using safe_free
87 * \note wl is altered so that it points to the second element
88 * \return nullptr if pointed-to wordlist is nullptr.
89 */
90char *wordlistChopHead(wordlist **);
91
5a231dcd
FC
92inline WordlistIterator &
93WordlistIterator::operator++()
94{
95 w = w->next;
96 return *this;
97}
98
99inline WordlistIterator::value_type
100WordlistIterator::operator*() const
101{
102 return w->key;
103}
514fc315 104
ff9d9458 105#endif /* SQUID_SRC_WORDLIST_H */
f53969cc 106