]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/collection.cpp
Give DirectoryCollection the ability to add files as future objects
[libs/datafile.git] / source / collection.cpp
index d52ee22399ba7500c880798f2fcf3036d9cd095c..a16ae5a9dc2e7a9f70f638093348b59c8bd561eb 100644 (file)
@@ -1,5 +1,7 @@
 #include "collection.h"
 
+using namespace std;
+
 namespace Msp {
 namespace DataFile {
 
@@ -9,6 +11,22 @@ Collection::~Collection()
                delete *i;
 }
 
+void Collection::add_future(const std::string &name)
+{
+       if(items.count(name))
+               throw key_error(typeid(ItemMap));
+
+       for(TypeList::const_iterator i=types.begin(); i!=types.end(); ++i)
+               if((*i)->match_name(name))
+               {
+                       items.insert(ItemMap::value_type(name, (*i)->create_future()));
+                       return;
+               }
+
+       /* XXX throw something?  If we do, DirectoryCollection needs some way to
+       check if a name matches any item type. */
+}
+
 
 Collection::Loader::Loader(Collection &c):
        coll(c)
@@ -27,5 +45,25 @@ CollectionItemTypeBase::~CollectionItemTypeBase()
        delete tag;
 }
 
+void CollectionItemTypeBase::set_keyword(const string &k)
+{
+       kwd = k;
+       if(suffixes.empty())
+               add_suffix("."+kwd);
+}
+
+void CollectionItemTypeBase::add_suffix(const string &s)
+{
+       suffixes.push_back(s);
+}
+
+bool CollectionItemTypeBase::match_name(const string &name) const
+{
+       for(vector<string>::const_iterator i=suffixes.begin(); i!=suffixes.end(); ++i)
+               if(name.size()>i->size() && !name.compare(name.size()-i->size(), string::npos, *i))
+                       return true;
+       return false;
+}
+
 } // namespace DataFile
 } // namespace Msp