]> git.tdb.fi Git - libs/datafile.git/blob - source/directorysource.cpp
Add an API to open files from a collection's sources
[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)
12 {
13         list<string> files = FS::list_files(d);
14         for(list<string>::const_iterator i=files.begin(); i!=files.end(); ++i)
15                 objects[*i] = d / *i;
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(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i)
27                 if(type.match_name(i->first))
28                         names.push_back(i->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 0;
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