-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.cc
97 lines (86 loc) · 1.74 KB
/
history.cc
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
/* -*- mode: C++; c-basic-offset: 4; tab-width: 8; -*-
* vi: set shiftwidth=4 tabstop=8:
* :indentSize=4:tabSize=8:
*/
#include "history.h"
#include <fstream>
#include <algorithm>
#include <iterator>
#if defined(_WIN32)
#include <Shlobj.h>
#include "to_wide.h"
#endif
History::History(const std::string& filename)
{
#if defined(_WIN32)
wchar_t *dir;
HRESULT res = SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_DEFAULT_PATH | KF_FLAG_CREATE, nullptr, &dir);
filename_ = std::wstring(dir) + std::wstring(L"\\") + to_wide(filename);
#else
filename_ = std::string(getenv("HOME")) + "/" + filename;
#endif
loadhistory(filename_);
}
History::~History()
{
if (! filename_.empty()) {
std::ofstream os(filename_);
std::copy(v_.begin(), v_.end(), std::ostream_iterator<std::string>(os, "\n"));
}
}
void
History::load(std::istream& is)
{
while (is) {
std::string l;
getline(is, l);
add(l);
}
if (v_.size() > 1000u) {
v_.erase(v_.begin(), v_.begin() + (v_.size() - 1000u));
}
}
void
History::add(const std::string& s)
{
if (s.empty()) return;
v_.push_back(s);
}
std::shared_ptr<History::iterator>
History::begin(const std::string& s)
{
return std::make_shared<History::iterator>(shared_from_this(), s);
}
History::iterator::iterator(std::shared_ptr<History> h, const std::string& s) :
h_(h),
s_(s),
idx_(h->v_.size())
{}
bool
History::iterator::atEnd() const
{
return idx_ == h_->v_.size();
}
const std::string&
History::iterator::next()
{
if (atEnd()) {
return s_;
}
++idx_;
if (atEnd()) {
return s_;
}
return h_->v_[idx_];
}
const std::string&
History::iterator::prev()
{
if (idx_ > 0) {
--idx_;
}
if (atEnd()) {
return s_;
}
return h_->v_[idx_];
}