]> git.tdb.fi Git - libs/datafile.git/blob - source/directorysource.cpp
Add a flag to control replacement of DirectorySource entries
[libs/datafile.git] / source / directorysource.cpp
1 #include <msp/fs/dir.h>
2 #include <msp/fs/stat.h>
3 #include "collection.h"
4 #include "directorysource.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace DataFile {
10
11 void DirectorySource::add_directory(const FS::Path &d, bool replace)
12 {
13         list<string> files = FS::list_files(d);
14         for(list<string>::const_iterator i=files.begin(); i!=files.end(); ++i)
15         {
16                 if(!objects.count(*i) || replace)
17                         objects[*i] = d / *i;
18         }
19 }
20
21 bool DirectorySource::is_loadable(const CollectionItemTypeBase &, const string &name) const
22 {
23         return objects.count(name);
24 }
25
26 CollectionSource::NameList DirectorySource::get_names(const CollectionItemTypeBase &type) const
27 {
28         NameList names;
29         for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i)
30                 if(type.match_name(i->first))
31                         names.push_back(i->first);
32         return names;
33 }
34
35 void DirectorySource::load(Collection &coll, const CollectionItemTypeBase &type, const string &name) const
36 {
37         FS::Path file;
38         if(lookup_file(name, file))
39         {
40                 IO::BufferedFile in(file.str());
41                 Parser parser(in, file.str());
42                 type.load_item(coll, parser, name);
43         }
44 }
45
46 IO::Seekable *DirectorySource::open(const string &name) const
47 {
48         FS::Path file;
49         if(lookup_file(name, file))
50                 return new IO::BufferedFile(file.str());
51
52         return 0;
53 }
54
55 bool DirectorySource::lookup_file(const string &name, FS::Path &result) const
56 {
57         ObjectMap::const_iterator i = objects.find(name);
58         if(i==objects.end())
59                 return false;
60
61         result = i->second;
62         return true;
63 }
64
65 } // namespace DataFile
66 } // namespace Msp