-/* $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(reset_src)
{
writer.write((DataFile::Statement("__src"), string()));
- reset_src=false;
+ reset_src = false;
}
- for(list<DataFile::Statement>::const_iterator i=st.sub.begin(); i!=st.sub.end(); ++i)
+ for(list<DataFile::Statement>::const_iterator i = st.sub.begin(); i!=st.sub.end(); ++i)
writer.write(*i);
}
if(st.keyword=="_content")
return true;
- for(vector<DataFile::Value>::iterator i=st.args.begin(); i!=st.args.end(); ++i)
- if(i->get_type()==DataFile::STRING)
+ 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_signature()==DataFile::StringType::signature)
{
- if(i->get_raw()=="$filename")
- *i=DataFile::Value(FS::basename(fn.str()));
- else if(i->get_raw()=="$content")
+ string value = i->get<string>();
+ if(value=="$filename")
+ *i = DataFile::Value(FS::basename(fn.str()));
+ else if(value=="$content")
{
IO::File in(fn.str());
string data;
while(!in.eof())
{
char buf[4096];
- unsigned len=in.read(buf, sizeof(buf));
+ unsigned len = in.read(buf, sizeof(buf));
data.append(buf, len);
}
- *i=DataFile::Value(data);
+ *i = DataFile::Value(data);
}
}
- for(list<DataFile::Statement>::iterator i=st.sub.begin(); i!=st.sub.end();)
+ for(list<DataFile::Statement>::iterator i = st.sub.begin(); i!=st.sub.end();)
{
if(process_statement(fn, *i))
{
DataFile::Parser parser(in, fn.str());
while(parser)
{
- DataFile::Statement ss=parser.parse();
+ DataFile::Statement ss = parser.parse();
if(ss.valid)
st.sub.insert(i, ss);
}
- i=st.sub.erase(i);
+ i = st.sub.erase(i);
}
else
++i;
void Compiler::process_file(const FS::Path &fn, const list<DataFile::Statement> &st)
{
writer.write((DataFile::Statement("__src"), FS::basename(fn.str())));
- reset_src=true;
+ reset_src = true;
if(st.empty())
process_file(fn);
else
{
- for(list<DataFile::Statement>::const_iterator i=st.begin(); i!=st.end(); ++i)
+ for(list<DataFile::Statement>::const_iterator i = st.begin(); i!=st.end(); ++i)
{
if(i->keyword=="_content")
process_file(fn);
else
{
- DataFile::Statement s=*i;
+ DataFile::Statement s = *i;
process_statement(fn, s);
writer.write(s);
}
DataFile::Parser parser(in, fn.str());
while(parser)
{
- DataFile::Statement st=parser.parse();
+ DataFile::Statement st = parser.parse();
if(st.valid)
writer.write(st);
}
{
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)
+ list<string> files = FS::list_files(base);
+ for(list<string>::iterator i = files.begin(); i!=files.end(); ++i)
{
- bool match=false;
- for(list<string>::const_iterator j=patterns.begin(); (j!=patterns.end() && !match); ++j)
- match=Regex(*j).match(*i);
- for(list<string>::const_iterator j=excludes.begin(); (j!=excludes.end() && match); ++j)
- match=!Regex(*j).match(*i);
+ bool match = false;
+ for(list<string>::const_iterator j = patterns.begin(); (j!=patterns.end() && !match); ++j)
+ match = Regex(*j).match(*i);
+ for(list<string>::const_iterator j = excludes.begin(); (j!=excludes.end() && match); ++j)
+ match = !Regex(*j).match(*i);
if(match)
compiler.process_file(base / *i, 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());