-/* $Id$
-
-This file is part of libmspdatafile
-Copyright © 2008 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
#include <msp/datafile/parser.h>
#include <msp/fs/dir.h>
#include <msp/fs/utils.h>
if(st.keyword=="_content")
return true;
+ if(st.keyword=="_extension")
+ {
+ string ext = FS::extpart(FS::basename(fn));
+ if(ext.empty() || ext.size()==1)
+ throw invalid_argument("Compiler::process_statement");
+ st.keyword = ext.substr(1);
+ }
+
for(vector<DataFile::Value>::iterator i = st.args.begin(); i!=st.args.end(); ++i)
- if(i->get_type()==DataFile::STRING)
+ if(i->get_signature()==DataFile::StringType::signature)
{
- if(i->get_raw()=="$filename")
+ string value = i->get<string>();
+ if(value=="$filename")
*i = DataFile::Value(FS::basename(fn.str()));
- else if(i->get_raw()=="$content")
+ else if(value=="$content")
{
IO::File in(fn.str());
string data;
{
add("exclude", &ForEach::exclude);
add("pattern", &ForEach::pattern);
+ add("wrap", &ForEach::wrap);
+ add("wrap", &ForEach::wrap_keyword);
add("write", &ForEach::write);
}
void ForEach::finish()
{
- list<string> files = FS::list_files(base);
- for(list<string>::iterator i = files.begin(); i!=files.end(); ++i)
+ for(const string &f: FS::list_files(base))
{
bool match = false;
for(list<string>::const_iterator j = patterns.begin(); (j!=patterns.end() && !match); ++j)
- match = Regex(*j).match(*i);
+ match = static_cast<bool>(Regex(*j).match(f));
for(list<string>::const_iterator j = excludes.begin(); (j!=excludes.end() && match); ++j)
- match = !Regex(*j).match(*i);
+ match = !Regex(*j).match(f);
if(match)
- compiler.process_file(base / *i, write_st);
+ compiler.process_file(base/f, write_st);
}
}
patterns.push_back(p);
}
+void ForEach::wrap()
+{
+ wrap_keyword("_extension");
+}
+
+void ForEach::wrap_keyword(const string &kwd)
+{
+ DataFile::Statement st(kwd);
+ st.append("$filename");
+ st.sub.push_back(DataFile::Statement("_content"));
+ write_st.push_back(st);
+}
+
void ForEach::write(const DataFile::Statement &st)
{
write_st.insert(write_st.end(), st.sub.begin(), st.sub.end());