]> git.tdb.fi Git - libs/datafile.git/blob - source/directorysource.cpp
Use nullptr instead of 0 for pointers
[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         for(const string &f: FS::list_files(d))
14                 if(!objects.count(f) || replace)
15                         objects[f] = d/f;
16 }
17
18 bool DirectorySource::is_loadable(const CollectionItemTypeBase &, const string &name) const
19 {
20         return objects.count(name);
21 }
22
23 CollectionSource::NameList DirectorySource::get_names(const CollectionItemTypeBase &type) const
24 {
25         NameList names;
26         for(const auto &kvp: objects)
27                 if(type.match_name(kvp.first))
28                         names.push_back(kvp.first);
29         return names;
30 }
31
32 void DirectorySource::load(Collection &coll, const CollectionItemTypeBase &type, const string &name) const
33 {
34         FS::Path file;
35         if(lookup_file(name, file))
36         {
37                 IO::BufferedFile in(file.str());
38                 Parser parser(in, file.str());
39                 type.load_item(coll, parser, name);
40         }
41 }
42
43 IO::Seekable *DirectorySource::open(const string &name) const
44 {
45         FS::Path file;
46         if(lookup_file(name, file))
47                 return new IO::BufferedFile(file.str());
48
49         return nullptr;
50 }
51
52 bool DirectorySource::lookup_file(const string &name, FS::Path &result) const
53 {
54         ObjectMap::const_iterator i = objects.find(name);
55         if(i==objects.end())
56                 return false;
57
58         result = i->second;
59         return true;
60 }
61
62 } // namespace DataFile
63 } // namespace Msp