-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathIgnoreList.cpp
More file actions
246 lines (224 loc) · 4.38 KB
/
Copy pathIgnoreList.cpp
File metadata and controls
246 lines (224 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// IgnoreList.cpp: implementation of the CIgnoreList class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "IgnoreList.h"
#include <iostream>
#include <fstream>
#include <set>
#include <ctime>
#include <iomanip>
#include <algorithm>
#include <no5tl\mystring.h>
using namespace std;
class CIgnoreList : public IIgnoreList
{
private:
struct Data
{
//
CString m_name;
time_t m_time;
//
Data()
{
m_time = 0;
}
Data(LPCSTR name)
{
m_name = name;
m_time = time(NULL);
}
bool operator < ( const Data &data) const
{
return m_name.CompareNoCase(data.m_name) < 0;
}
friend ostream & operator << ( ostream & out,const Data &data)
{
if(!data.m_name.IsEmpty()){
out << data.m_name.GetLength() << endl;
out << (LPCSTR) data.m_name << endl;
out << (int)data.m_time;
}
return out;
}
friend istream & operator >> ( istream &in,Data &data)
{
int len;
// read string length
in >> ws >> len;
if(len > 0){
data.m_name.Empty();
data.m_time = 0;
{
NO5TL::CStringBuffer buf(data.m_name,len);
// skip white spaces
in >> ws;
// read name
in.read(buf,len);
// read time as int
in >> ws >> len;
// assign time
data.m_time = (time_t)len;
}
}
return in;
}
};
typedef set<Data> ListType;
typedef ListType::iterator iter;
typedef ListType::const_iterator citer;
ListType m_list;
bool m_dirty;
public:
CIgnoreList()
{
m_dirty = false;
}
virtual ~CIgnoreList()
{
}
virtual bool read(LPCTSTR file)
{
ifstream in(file);
int count = 0;
bool res = false;
// if the file doesnt exist it will fail, coz we are using ifstream. but thats ok
if(in.is_open()){
int len = 0;
Data data;
int i;
time_t cur = time(NULL);
long days;
// read number of items
in >> count;
m_list.clear();
m_dirty = false;
for(i=0;i<count;i++){
if( in >> data ){
days = long(data.m_time - cur)/( 24 * 60 * 60 );
if(days > 0)
m_list.insert(data);
}
}
res = true;
}
return res;
}
virtual bool write(LPCTSTR file)
{
ofstream out(file);
bool res = false;
if(out){
time_t cur = time(NULL);
long days;
// write number of items
out << m_list.size() << endl;
res = true;
//sort();
for(citer it = m_list.begin();res && it != m_list.end(); it++){
// dont add if time expired
days = long(it->m_time - cur)/( 24 * 60 * 60 );
if(days > 0){
if(!(out << (*it) << endl)){
res = false;
}
}
}
}
if(res)
m_dirty = false;
return res;
}
// add at a sorted position
virtual void add(LPCTSTR name)
{
Data d;
iter it;
d.m_name = name;
d.m_time = time(NULL);
m_list.insert(d);
m_dirty = true;
}
virtual void add(LPCTSTR name,long t)
{
Data d;
iter it;
d.m_name = name;
d.m_time = static_cast<time_t>(t);
m_list.insert(d);
m_dirty = true;
}
virtual void remove(LPCTSTR name)
{
Data d;
d.m_name = name;
d.m_time = 0;
m_list.erase(d);
m_dirty = true;
}
bool find(LPCTSTR name,Data *p)
{
Data d(name);
citer it = m_list.find(d);
if(p && it != m_list.end())
*p = *it;
return it != m_list.end();
}
virtual bool find(LPCTSTR name)
{
return find(name,NULL);
}
virtual bool find(LPCTSTR name,long &t)
{
Data d;
bool res = find(name,&d);
if(res)
t = static_cast<long>(d.m_time);
return res;
}
virtual void clear(void)
{
m_list.clear();
m_dirty = true;
}
virtual int size(void) const
{
return (int)m_list.size();
}
virtual bool is_dirty(void) const
{
return m_dirty;
}
virtual bool getat(int i,CString &name,long &t)
{
citer it = m_list.begin();
bool res = false;
int j = 0;
while(it != m_list.end() && !res){
if(j == i){
name = it->m_name;
t = static_cast<long>(it->m_time);
res = true;
}
else{
j++;
it++;
}
}
return res;
}
};
void IIgnoreList::CreateMe(IIgnoreList **pp)
{
ATLASSERT(pp && ( *pp == NULL ) );
*pp = (IIgnoreList *) new CIgnoreList();
}
void IIgnoreList::DestroyMe(IIgnoreList **pp)
{
ATLASSERT(pp && (*pp != NULL));
if(*pp){
delete (CIgnoreList *)(*pp);
*pp = NULL;
}
}