]> git.ipfire.org Git - thirdparty/squid.git/blob - src/acl/StringData.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / acl / StringData.cc
1 /*
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9 /* DEBUG: section 28 Access Control */
10
11 #include "squid.h"
12 #include "acl/Checklist.h"
13 #include "acl/StringData.h"
14 #include "cache_cf.h"
15 #include "Debug.h"
16
17 ACLStringData::ACLStringData() : values (NULL)
18 {}
19
20 ACLStringData::ACLStringData(ACLStringData const &old) : values (NULL)
21 {
22 assert (!old.values);
23 }
24
25 template<class T>
26 inline void
27 xRefFree(T &thing)
28 {
29 xfree (thing);
30 }
31
32 ACLStringData::~ACLStringData()
33 {
34 if (values)
35 values->destroy(xRefFree);
36 }
37
38 static int
39 splaystrcmp (char * const &l, char * const &r)
40 {
41 return strcmp (l,r);
42 }
43
44 void
45 ACLStringData::insert(const char *value)
46 {
47 values = values->insert(xstrdup(value), splaystrcmp);
48 }
49
50 bool
51 ACLStringData::match(char const *toFind)
52 {
53 if (!values || !toFind)
54 return 0;
55
56 debugs(28, 3, "aclMatchStringList: checking '" << toFind << "'");
57
58 values = values->splay((char *)toFind, splaystrcmp);
59
60 debugs(28, 3, "aclMatchStringList: '" << toFind << "' " << (splayLastResult ? "NOT found" : "found"));
61
62 return !splayLastResult;
63 }
64
65 static void
66 aclDumpStringWalkee(char * const & node_data, void *outlist)
67 {
68 /* outlist is really a SBufList* */
69 static_cast<SBufList*>(outlist)->push_back(SBuf(node_data));
70 }
71
72 SBufList
73 ACLStringData::dump() const
74 {
75 SBufList sl;
76 /* damn this is VERY inefficient for long ACL lists... filling
77 * a SBufList this way costs Sum(1,N) iterations. For instance
78 * a 1000-elements list will be filled in 499500 iterations.
79 */
80 values->walk(aclDumpStringWalkee, &sl);
81 return sl;
82 }
83
84 void
85 ACLStringData::parse()
86 {
87 char *t;
88
89 while ((t = strtokFile()))
90 values = values->insert(xstrdup(t), splaystrcmp);
91 }
92
93 bool
94 ACLStringData::empty() const
95 {
96 return values->empty();
97 }
98
99 ACLData<char const *> *
100 ACLStringData::clone() const
101 {
102 /* Splay trees don't clone yet. */
103 assert (!values);
104 return new ACLStringData(*this);
105 }
106