1 #include "recentfiles.hpp"
14 path::path(const std::string
& p
)
19 std::string
path::serialize() const
24 path
path::deserialize(const std::string
& s
)
30 std::string
path::get_path() const
35 bool path::check() const
40 return zip::file_exists(pth
);
46 std::string
path::display() const
51 bool path::operator==(const path
& p
) const
61 std::string
multirom::serialize() const
64 JSON::node
output(JSON::object
);
66 output
["pack"] = JSON::string(packfile
);
67 } else if(singlefile
!= "") {
68 output
["file"] = JSON::string(singlefile
);
69 if(core
!= "") output
["core"] = JSON::string(core
);
70 if(system
!= "") output
["system"] = JSON::string(system
);
71 if(region
!= "") output
["region"] = JSON::string(region
);
73 output
["files"] = JSON::array();
74 JSON::node
& f
= output
["files"];
76 f
.append(JSON::string(i
));
80 if(core
!= "") output
["core"] = JSON::string(core
);
81 if(system
!= "") output
["system"] = JSON::string(system
);
82 if(region
!= "") output
["region"] = JSON::string(region
);
84 if(packfile
!= "" || singlefile
!= "" || core
!= "" || system
!= "" || region
!= "") any
= true;
86 return output
.serialize();
89 multirom
multirom::deserialize(const std::string
& s
)
92 if(s
.length() > 0 && s
[0] == '{') {
96 if(d
.field_exists("pack")) r
.packfile
= d
["pack"].as_string8();
97 if(d
.field_exists("file")) r
.singlefile
= d
["file"].as_string8();
98 if(d
.field_exists("core")) r
.core
= d
["core"].as_string8();
99 if(d
.field_exists("system")) r
.system
= d
["system"].as_string8();
100 if(d
.field_exists("region")) r
.region
= d
["region"].as_string8();
101 if(d
.field_exists("files")) {
102 size_t cnt
= d
["files"].index_count();
104 for(unsigned i
= 0; i
< cnt
; i
++)
105 r
.files
[i
] = d
["files"].index(i
).as_string8();
122 bool multirom::check() const
124 if(packfile
== "" && singlefile
== "" && core
== "" && system
== "" && region
== "" && files
.empty())
126 if(packfile
!= "" && !zip::file_exists(packfile
))
128 if(singlefile
!= "" && !zip::file_exists(singlefile
))
131 if(i
!= "" && !zip::file_exists(i
))
136 std::string
multirom::display() const
140 if(singlefile
!= "") {
141 return singlefile
+ " <" + core
+ "/" + system
+ ">";
143 if(files
.size() > 1 && files
[1] != "")
144 return (stringfmt() << files
[1] << " (+" << files
.size() << ")").str();
145 else if(files
.size() > 0)
146 return (stringfmt() << files
[0] << " (+" << files
.size() << ")").str();
148 return "(blank) <" + core
+ "/" + system
+ ">";
152 bool multirom::operator==(const multirom
& p
) const
154 if(packfile
!= p
.packfile
)
156 if(singlefile
!= p
.singlefile
)
160 if(system
!= p
.system
)
162 if(region
!= p
.region
)
164 if(files
.size() != p
.files
.size())
166 for(unsigned i
= 0; i
< files
.size(); i
++)
167 if(files
[i
] != p
.files
[i
])
176 std::string
namedobj::serialize() const
178 if(_id
== "" && _filename
== "" && _display
== "") return "";
179 JSON::node
output(JSON::object
);
180 output
["id"] = JSON::string(_id
);
181 output
["filename"] = JSON::string(_filename
);
182 output
["display"] = JSON::string(_display
);
183 return output
.serialize();
186 namedobj
namedobj::deserialize(const std::string
& s
)
190 if(d
.field_exists("id")) obj
._id
= d
["id"].as_string8();
191 if(d
.field_exists("filename")) obj
._filename
= d
["filename"].as_string8();
192 if(d
.field_exists("display")) obj
._display
= d
["display"].as_string8();
196 bool namedobj::check() const
198 return zip::file_exists(_filename
);
201 std::string
namedobj::display() const
206 bool namedobj::operator==(const namedobj
& p
) const
208 return (_id
== p
._id
);
211 template<class T
> set
<T
>::set(const std::string
& _cfgfile
, size_t _maxcount
)
214 maxcount
= _maxcount
;
217 template<class T
> void set
<T
>::add(const T
& file
)
222 std::ifstream
in(cfgfile
);
228 g
= T::deserialize(f
);
236 //Search for matches. If found, move to front, otherwise push to first.
237 auto itr
= ents
.begin();
238 for(; itr
!= ents
.end(); itr
++)
241 if(itr
!= ents
.end())
243 ents
.push_front(file
);
244 //Write up to maxcount entries.
247 std::ofstream
out(cfgfile
);
248 for(itr
= ents
.begin(); itr
!= ents
.end() && i
< maxcount
; itr
++, i
++)
249 out
<< itr
->serialize() << std::endl
;
255 template<class T
> void set
<T
>::add_hook(hook
& h
)
260 template<class T
> void set
<T
>::remove_hook(hook
& h
)
262 for(auto itr
= hooks
.begin(); itr
!= hooks
.end(); itr
++)
269 template<class T
> std::list
<T
> set
<T
>::get()
275 std::ifstream
in(cfgfile
);
281 g
= T::deserialize(f
);
285 if(c
< maxcount
&& g
.check()) {
298 void _dummy_63263632747434353545()
301 eat_argument(&set
<path
>::add
);
302 eat_argument(&set
<path
>::add_hook
);
303 eat_argument(&set
<path
>::remove_hook
);
304 eat_argument(&set
<path
>::get
);
305 set
<multirom
> y("", 0);
306 eat_argument(&set
<multirom
>::add
);
307 eat_argument(&set
<multirom
>::add_hook
);
308 eat_argument(&set
<multirom
>::remove_hook
);
309 eat_argument(&set
<multirom
>::get
);
310 set
<namedobj
> z("", 0);
311 eat_argument(&set
<namedobj
>::add
);
312 eat_argument(&set
<namedobj
>::add_hook
);
313 eat_argument(&set
<namedobj
>::remove_hook
);
314 eat_argument(&set
<namedobj
>::get
);