Remove String->const char* conversion operator in favor of c_str method
[clinton/bobotpp.git] / source / ShitList.C
1 // ShitList.C -*- C++ -*-
2 // Copyright (c) 1998 Etienne BERNARD
3 // Copyright (C) 2002,2005 Clinton Ebadi
4
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // any later version.
9
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "ShitList.H"
25
26 #include <fstream>
27
28 #include <cstdlib>
29
30 #include "ShitEntry.H"
31 #include "StringTokenizer.H"
32 #include "Utils.H"
33
34 ShitList::ShitList(String filename)
35 : listFileName(filename)
36 {
37 #ifdef HAVE_STL_CLEAR
38 l.clear();
39 #endif
40 read();
41 }
42
43 ShitList::~ShitList()
44 {
45 clear();
46 }
47
48 void
49 ShitList::read()
50 {
51 std::ifstream file(listFileName.c_str ());
52 String temp;
53 int line = 1;
54
55 clear();
56
57 if (!file) {
58 std::cerr << "I cannot find the file " << listFileName << std::endl;
59 return;
60 }
61
62 while (file >> temp, temp.length() != 0) {
63 StringTokenizer st(temp);
64 String mask = st.next_token(':');
65 String channelMask = st.next_token(':');
66 String level = st.next_token(':');
67 String expiration = st.next_token(':');
68 String reason = Utils::trim_str (st.rest());
69 l.push_back (new ShitEntry(mask, channelMask, std::atoi(level.c_str ()),
70 std::atol(expiration.c_str ()), reason));
71 line++;
72 }
73 file.close();
74 }
75
76 void
77 ShitList::save()
78 {
79 std::list<ShitEntry *>::iterator it = l.begin();
80 std::ofstream file(listFileName.c_str ());
81
82 if (!file)
83 return;
84
85 for ( ; it != l.end(); ++it)
86 if ((*it)->isStillValid()) {
87 file << (*it)->shitMask.getMask() << ":"
88 << (*it)->shitChannelMask.getMask() << ":"
89 << (*it)->shitLevel << ":"
90 << (*it)->expirationDate << ":"
91 << (*it)->shitReason << std::endl;
92 }
93 }
94
95 void
96 ShitList::clear()
97 {
98 ShitEntry *se;
99
100 while (!l.empty()) {
101 se = (*l.begin());
102 l.erase(l.begin());
103 delete se;
104 }
105 }
106
107 void
108 ShitList::addShit(String m, String mc, int lev, time_t e, String r)
109 {
110 l.push_back (new ShitEntry(m, mc, lev, e, r));
111 }
112
113
114 void
115 ShitList::delShit(String mask, String channelMask)
116 {
117 for (std::list<ShitEntry *>::iterator it = l.begin();
118 it != l.end();
119 ++it)
120 if ((*it)->shitMask.getMask() == mask &&
121 (*it)->shitChannelMask.getMask() == channelMask) {
122 delete (*it);
123 l.erase(it);
124 return;
125 }
126 }
127
128 ShitEntry *
129 ShitList::getShit(String nuh, String channel)
130 {
131 for (std::list<ShitEntry *>::iterator it = l.begin();
132 it != l.end();
133 ++it)
134 if ((*it)->matches(nuh, channel)) {
135 return (*it);
136 }
137
138 return 0;
139 }