-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecentFiles.cpp
More file actions
80 lines (70 loc) · 1.94 KB
/
Copy pathRecentFiles.cpp
File metadata and controls
80 lines (70 loc) · 1.94 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
#include "RecentFiles.h"
#include "IniStore.h"
#include <algorithm>
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
namespace RecentFiles
{
std::vector<std::wstring> Load(const std::wstring& iniPath)
{
if (iniPath.empty())
{
return {};
}
const IniStore ini = IniStore::Load(iniPath);
std::vector<std::wstring> out = IniStore::SplitPipeList(ini.GetString(L"Session", L"RecentFiles"));
if (out.size() > kMaxEntries)
{
out.resize(kMaxEntries);
}
return out;
}
void Add(const std::wstring& iniPath, const std::wstring& path)
{
if (iniPath.empty() || path.empty())
{
return;
}
std::vector<std::wstring> files = Load(iniPath);
files.erase(
std::remove_if(
files.begin(),
files.end(),
[&](const std::wstring& p) { return _wcsicmp(p.c_str(), path.c_str()) == 0; }),
files.end());
files.insert(files.begin(), path);
if (files.size() > kMaxEntries)
{
files.resize(kMaxEntries);
}
IniStore::SetString(iniPath, L"Session", L"RecentFiles", IniStore::JoinPipeList(files));
}
void Clear(const std::wstring& iniPath)
{
if (iniPath.empty())
{
return;
}
IniStore::SetString(iniPath, L"Session", L"RecentFiles", L"");
}
std::wstring MenuLabel(const std::wstring& path)
{
constexpr std::size_t kMax = 64;
std::wstring label = path.size() <= kMax
? path
: L"..." + path.substr(path.size() - (kMax - 3));
std::wstring escaped;
escaped.reserve(label.size() + 4);
for (const wchar_t c : label)
{
escaped += c;
if (c == L'&')
{
escaped += L'&';
}
}
return escaped;
}
}