]> git.tdb.fi Git - libs/datafile.git/blobdiff - tool/compiler.cpp
Add a shortcut for wrapping data for a collection file
[libs/datafile.git] / tool / compiler.cpp
index ed5710b97d3bd8d3ffac53d88f4c41855b2866fd..49dd19031cc0408fc958e84afd3fcfa4063bc403 100644 (file)
@@ -1,10 +1,3 @@
-/* $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>
@@ -40,10 +33,10 @@ void Compiler::write(const DataFile::Statement &st)
        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);
 }
 
@@ -52,26 +45,35 @@ bool Compiler::process_statement(const FS::Path &fn, DataFile::Statement &st)
        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))
                {
@@ -81,11 +83,11 @@ bool Compiler::process_statement(const FS::Path &fn, DataFile::Statement &st)
                        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;
@@ -97,19 +99,19 @@ bool Compiler::process_statement(const FS::Path &fn, DataFile::Statement &st)
 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);
                        }
@@ -125,7 +127,7 @@ void Compiler::process_file(const FS::Path &fn)
        DataFile::Parser parser(in, fn.str());
        while(parser)
        {
-               DataFile::Statement st=parser.parse();
+               DataFile::Statement st = parser.parse();
                if(st.valid)
                        writer.write(st);
        }
@@ -157,19 +159,21 @@ ForEach::ForEach(Compiler &c, const FS::Path &b, const list<string> &p):
 {
        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);
        }
@@ -185,6 +189,19 @@ void ForEach::pattern(const string &p)
        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());