3 This file is part of libmspdatafile
4 Copyright © 2008 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/datafile/parser.h>
9 #include <msp/fs/dir.h>
10 #include <msp/fs/utils.h>
11 #include <msp/strings/regex.h>
17 Compiler::Compiler(DataFile::Writer &w):
21 add("file", &Compiler::file);
22 add("for_each", &Compiler::for_each);
23 add("write", &Compiler::write);
26 void Compiler::file(const string &fn)
28 File fl(*this, FS::dirname(get_source())/fn);
32 void Compiler::for_each(const vector<string> &patterns)
34 ForEach fe(*this, FS::dirname(get_source()), list<string>(patterns.begin(), patterns.end()));
38 void Compiler::write(const DataFile::Statement &st)
42 writer.write((DataFile::Statement("__src"), string()));
46 for(list<DataFile::Statement>::const_iterator i=st.sub.begin(); i!=st.sub.end(); ++i)
50 bool Compiler::process_statement(const FS::Path &fn, DataFile::Statement &st)
52 if(st.keyword=="_content")
55 for(vector<DataFile::Value>::iterator i=st.args.begin(); i!=st.args.end(); ++i)
56 if(i->get_type()==DataFile::STRING)
58 if(i->get_raw()=="$filename")
59 *i=DataFile::Value(FS::basename(fn.str()));
60 else if(i->get_raw()=="$content")
62 IO::File in(fn.str());
67 unsigned len=in.read(buf, sizeof(buf));
68 data.append(buf, len);
70 *i=DataFile::Value(data);
74 for(list<DataFile::Statement>::iterator i=st.sub.begin(); i!=st.sub.end();)
76 if(process_statement(fn, *i))
78 IO::File in(fn.str());
81 DataFile::Parser parser(in, fn.str());
84 DataFile::Statement ss=parser.parse();
97 void Compiler::process_file(const FS::Path &fn, const list<DataFile::Statement> &st)
99 writer.write((DataFile::Statement("__src"), FS::basename(fn.str())));
106 for(list<DataFile::Statement>::const_iterator i=st.begin(); i!=st.end(); ++i)
108 if(i->keyword=="_content")
112 DataFile::Statement s=*i;
113 process_statement(fn, s);
120 void Compiler::process_file(const FS::Path &fn)
122 IO::File in(fn.str());
123 IO::Buffered buf(in);
125 DataFile::Parser parser(in, fn.str());
128 DataFile::Statement st=parser.parse();
135 File::File(Compiler &c, const FS::Path &fn):
139 add("write", &File::write);
144 compiler.process_file(filename, write_st);
147 void File::write(const DataFile::Statement &st)
149 write_st.insert(write_st.end(), st.sub.begin(), st.sub.end());
153 ForEach::ForEach(Compiler &c, const FS::Path &b, const list<string> &p):
158 add("exclude", &ForEach::exclude);
159 add("pattern", &ForEach::pattern);
160 add("write", &ForEach::write);
163 void ForEach::finish()
165 list<string> files=FS::list_files(base);
166 for(list<string>::iterator i=files.begin(); i!=files.end(); ++i)
169 for(list<string>::const_iterator j=patterns.begin(); (j!=patterns.end() && !match); ++j)
170 match=Regex(*j).match(*i);
171 for(list<string>::const_iterator j=excludes.begin(); (j!=excludes.end() && match); ++j)
172 match=!Regex(*j).match(*i);
174 compiler.process_file(base / *i, write_st);
178 void ForEach::exclude(const string &p)
180 excludes.push_back(p);
183 void ForEach::pattern(const string &p)
185 patterns.push_back(p);
188 void ForEach::write(const DataFile::Statement &st)
190 write_st.insert(write_st.end(), st.sub.begin(), st.sub.end());