-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathcounter.hpp
85 lines (71 loc) · 1.51 KB
/
counter.hpp
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
//
// counter.h
// qqbot
//
// Created by hyq on 13-1-15.
//
//
#ifndef __QQBOT__COUNTER__
#define __QQBOT__COUNTER__
#include <iostream>
#include <fstream>
#include <map>
#include <boost/noncopyable.hpp>
#include <boost/foreach.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/tuple/tuple.hpp>
class counter : public boost::noncopyable
{
public:
counter(std::string filename = "counter.db")
: filename_(filename)
{
load();
}
void save()
{
std::fstream out(filename_.c_str(), std::fstream::out);
typedef typename std::map<
std::string,
boost::tuples::tuple<
std::size_t,
boost::posix_time::ptime
>
>::iterator iterator;
for (iterator iter = map_.begin(); iter != map_.end(); iter++)
{
out << iter->first << "\t" << boost::get<0>(iter->second)
<< "\t" << boost::get<1>(iter->second) << "\n";
}
}
void increace(std::string qq)
{
boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
boost::get<0>(map_[qq]) ++ ;
boost::get<1>(map_[qq]) = now;
}
private:
std::string filename_;
std::map<
std::string,
boost::tuples::tuple<
std::size_t, boost::posix_time::ptime
>
> map_;
void load()
{
std::fstream in(filename_.c_str(), std::ios_base::in);
std::string key;
std::size_t count;
boost::posix_time::ptime time;
if (in.good())
{
while (in >> key >> count >> time)
{
boost::get<0>(map_[key]) = count;
boost::get<1>(map_[key]) = time;
}
}
}
};
#endif /* defined(__QQBOT__COUNTER__) */