Allow specifying ROM type in file load dialog
[lsnes.git] / src / library / recentfiles.cpp
blob988d2bbf05d0706ac32bfd74561a0876d770578d
1 #include "recentfiles.hpp"
2 #include "zip.hpp"
3 #include "string.hpp"
4 #include <fstream>
6 recent_files::recent_files(const std::string& _cfgfile, size_t _maxcount)
8 cfgfile = _cfgfile;
9 maxcount = _maxcount;
12 void recent_files::add(const std::string& file)
14 std::list<std::string> ents;
15 //Load the list.
17 std::ifstream in(cfgfile);
18 std::string f;
19 while(in) {
20 std::getline(in, f);
21 if(f != "") {
22 bool exists = true;
23 try {
24 //Probe for existence.
25 std::string g = f;
26 regex_results r = regex("(.*) <.*>", f);
27 if(r)
28 g = r[1];
29 delete &open_file_relative(g, "");
30 } catch(std::exception& e) {
31 exists = false;
33 if(exists)
34 ents.push_back(f);
38 //Search for matches. If found, move to front, otherwise push to first.
39 auto itr = ents.begin();
40 for(; itr != ents.end(); itr++)
41 if(*itr == file)
42 break;
43 if(itr != ents.end())
44 ents.erase(itr);
45 ents.push_front(file);
46 //Write up to maxcount entries.
48 size_t i = 0;
49 std::ofstream out(cfgfile);
50 for(itr = ents.begin(); itr != ents.end() && i < maxcount; itr++, i++)
51 out << *itr << std::endl;
53 for(auto i : hooks)
54 (*i)();
57 void recent_files::add_hook(hook& h)
59 hooks.push_back(&h);
62 void recent_files::remove_hook(hook& h)
64 for(auto itr = hooks.begin(); itr != hooks.end(); itr++)
65 if(*itr == &h) {
66 hooks.erase(itr);
67 break;
71 std::list<std::string> recent_files::get()
73 size_t c = 0;
74 std::list<std::string> ents;
75 //Load the list.
77 std::ifstream in(cfgfile);
78 std::string f;
79 while(in) {
80 std::getline(in, f);
81 if(f != "") {
82 if(c < maxcount) {
83 ents.push_back(f);
84 c++;
89 return ents;
92 recent_files::recent_files::hook::~hook()