1 #include "recentfiles.hpp"
8 recentfile_path::recentfile_path()
12 recentfile_path::recentfile_path(const std::string
& p
)
17 std::string
recentfile_path::serialize() const
22 recentfile_path
recentfile_path::deserialize(const std::string
& s
)
28 std::string
recentfile_path::get_path() const
33 bool recentfile_path::check() const
38 return zip::file_exists(path
);
45 std::string
recentfile_path::display() const
50 bool recentfile_path::operator==(const recentfile_path
& p
) const
52 return p
.path
== path
;
55 recentfile_multirom::recentfile_multirom()
60 std::string
recentfile_multirom::serialize() const
63 JSON::node
output(JSON::object
);
65 output
["pack"] = JSON::string(packfile
);
66 } else if(singlefile
!= "") {
67 output
["file"] = JSON::string(singlefile
);
68 if(core
!= "") output
["core"] = JSON::string(core
);
69 if(system
!= "") output
["system"] = JSON::string(system
);
70 if(region
!= "") output
["region"] = JSON::string(region
);
72 output
["files"] = JSON::array();
73 JSON::node
& f
= output
["files"];
75 f
.append(JSON::string(i
));
79 if(core
!= "") output
["core"] = JSON::string(core
);
80 if(system
!= "") output
["system"] = JSON::string(system
);
81 if(region
!= "") output
["region"] = JSON::string(region
);
83 if(packfile
!= "" || singlefile
!= "" || core
!= "" || system
!= "" || region
!= "") any
= true;
85 return output
.serialize();
88 recentfile_multirom
recentfile_multirom::deserialize(const std::string
& s
)
90 recentfile_multirom r
;
91 if(s
.length() > 0 && s
[0] == '{') {
95 if(d
.field_exists("pack")) r
.packfile
= d
["pack"].as_string8();
96 if(d
.field_exists("file")) r
.singlefile
= d
["file"].as_string8();
97 if(d
.field_exists("core")) r
.core
= d
["core"].as_string8();
98 if(d
.field_exists("system")) r
.system
= d
["system"].as_string8();
99 if(d
.field_exists("region")) r
.region
= d
["region"].as_string8();
100 if(d
.field_exists("files")) {
101 size_t cnt
= d
["files"].index_count();
103 for(unsigned i
= 0; i
< cnt
; i
++)
104 r
.files
[i
] = d
["files"].index(i
).as_string8();
121 bool recentfile_multirom::check() const
123 if(packfile
== "" && singlefile
== "" && core
== "" && system
== "" && region
== "" && files
.empty())
125 if(packfile
!= "" && !zip::file_exists(packfile
))
127 if(singlefile
!= "" && !zip::file_exists(singlefile
))
130 if(i
!= "" && !zip::file_exists(i
))
135 std::string
recentfile_multirom::display() const
139 if(singlefile
!= "") {
140 return singlefile
+ " <" + core
+ "/" + system
+ ">";
142 if(files
.size() > 1 && files
[1] != "")
143 return (stringfmt() << files
[1] << " (+" << files
.size() << ")").str();
144 else if(files
.size() > 0)
145 return (stringfmt() << files
[0] << " (+" << files
.size() << ")").str();
147 return "(blank) <" + core
+ "/" + system
+ ">";
151 bool recentfile_multirom::operator==(const recentfile_multirom
& p
) const
153 if(packfile
!= p
.packfile
)
155 if(singlefile
!= p
.singlefile
)
159 if(system
!= p
.system
)
161 if(region
!= p
.region
)
163 if(files
.size() != p
.files
.size())
165 for(unsigned i
= 0; i
< files
.size(); i
++)
166 if(files
[i
] != p
.files
[i
])
171 template<class T
> recent_files
<T
>::recent_files(const std::string
& _cfgfile
, size_t _maxcount
)
174 maxcount
= _maxcount
;
177 template<class T
> void recent_files
<T
>::add(const T
& file
)
182 std::ifstream
in(cfgfile
);
186 T g
= T::deserialize(f
);
191 //Search for matches. If found, move to front, otherwise push to first.
192 auto itr
= ents
.begin();
193 for(; itr
!= ents
.end(); itr
++)
196 if(itr
!= ents
.end())
198 ents
.push_front(file
);
199 //Write up to maxcount entries.
202 std::ofstream
out(cfgfile
);
203 for(itr
= ents
.begin(); itr
!= ents
.end() && i
< maxcount
; itr
++, i
++)
204 out
<< itr
->serialize() << std::endl
;
210 template<class T
> void recent_files
<T
>::add_hook(recent_files_hook
& h
)
215 template<class T
> void recent_files
<T
>::remove_hook(recent_files_hook
& h
)
217 for(auto itr
= hooks
.begin(); itr
!= hooks
.end(); itr
++)
224 template<class T
> std::list
<T
> recent_files
<T
>::get()
230 std::ifstream
in(cfgfile
);
234 T g
= T::deserialize(f
);
235 if(c
< maxcount
&& g
.check()) {
244 recent_files_hook::~recent_files_hook()
248 void _dummy_63263632747434353545()
250 recent_files
<recentfile_path
> x("", 0);
251 eat_argument(&recent_files
<recentfile_path
>::add
);
252 eat_argument(&recent_files
<recentfile_path
>::add_hook
);
253 eat_argument(&recent_files
<recentfile_path
>::remove_hook
);
254 eat_argument(&recent_files
<recentfile_path
>::get
);
255 recent_files
<recentfile_multirom
> y("", 0);
256 eat_argument(&recent_files
<recentfile_multirom
>::add
);
257 eat_argument(&recent_files
<recentfile_multirom
>::add_hook
);
258 eat_argument(&recent_files
<recentfile_multirom
>::remove_hook
);
259 eat_argument(&recent_files
<recentfile_multirom
>::get
);