1 #include <msp/datafile/parser.h>
2 #include <msp/fs/dir.h>
3 #include <msp/fs/utils.h>
4 #include <msp/strings/regex.h>
10 Compiler::Compiler(DataFile::Writer &w):
14 add("file", &Compiler::file);
15 add("for_each", &Compiler::for_each);
16 add("write", &Compiler::write);
19 void Compiler::file(const string &fn)
21 File fl(*this, FS::dirname(get_source())/fn);
25 void Compiler::for_each(const vector<string> &patterns)
27 ForEach fe(*this, FS::dirname(get_source()), list<string>(patterns.begin(), patterns.end()));
31 void Compiler::write(const DataFile::Statement &st)
35 writer.write((DataFile::Statement("__src"), string()));
39 for(list<DataFile::Statement>::const_iterator i = st.sub.begin(); i!=st.sub.end(); ++i)
43 bool Compiler::process_statement(const FS::Path &fn, DataFile::Statement &st)
45 if(st.keyword=="_content")
48 for(vector<DataFile::Value>::iterator i = st.args.begin(); i!=st.args.end(); ++i)
49 if(i->get_signature()==DataFile::StringType::signature)
51 string value = i->get<string>();
52 if(value=="$filename")
53 *i = DataFile::Value(FS::basename(fn.str()));
54 else if(value=="$content")
56 IO::File in(fn.str());
61 unsigned len = in.read(buf, sizeof(buf));
62 data.append(buf, len);
64 *i = DataFile::Value(data);
68 for(list<DataFile::Statement>::iterator i = st.sub.begin(); i!=st.sub.end();)
70 if(process_statement(fn, *i))
72 IO::File in(fn.str());
75 DataFile::Parser parser(in, fn.str());
78 DataFile::Statement ss = parser.parse();
91 void Compiler::process_file(const FS::Path &fn, const list<DataFile::Statement> &st)
93 writer.write((DataFile::Statement("__src"), FS::basename(fn.str())));
100 for(list<DataFile::Statement>::const_iterator i = st.begin(); i!=st.end(); ++i)
102 if(i->keyword=="_content")
106 DataFile::Statement s = *i;
107 process_statement(fn, s);
114 void Compiler::process_file(const FS::Path &fn)
116 IO::File in(fn.str());
117 IO::Buffered buf(in);
119 DataFile::Parser parser(in, fn.str());
122 DataFile::Statement st = parser.parse();
129 File::File(Compiler &c, const FS::Path &fn):
133 add("write", &File::write);
138 compiler.process_file(filename, write_st);
141 void File::write(const DataFile::Statement &st)
143 write_st.insert(write_st.end(), st.sub.begin(), st.sub.end());
147 ForEach::ForEach(Compiler &c, const FS::Path &b, const list<string> &p):
152 add("exclude", &ForEach::exclude);
153 add("pattern", &ForEach::pattern);
154 add("write", &ForEach::write);
157 void ForEach::finish()
159 list<string> files = FS::list_files(base);
160 for(list<string>::iterator i = files.begin(); i!=files.end(); ++i)
163 for(list<string>::const_iterator j = patterns.begin(); (j!=patterns.end() && !match); ++j)
164 match = Regex(*j).match(*i);
165 for(list<string>::const_iterator j = excludes.begin(); (j!=excludes.end() && match); ++j)
166 match = !Regex(*j).match(*i);
168 compiler.process_file(base / *i, write_st);
172 void ForEach::exclude(const string &p)
174 excludes.push_back(p);
177 void ForEach::pattern(const string &p)
179 patterns.push_back(p);
182 void ForEach::write(const DataFile::Statement &st)
184 write_st.insert(write_st.end(), st.sub.begin(), st.sub.end());