X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=tool%2Fcompiler.cpp;h=8c280c4a2ec2a3d0ec3ed07fd72235d15a8bc92c;hb=cbd0ddd6ee033e46646bfb85d19232c816ea1eda;hp=53c1b5388c4792829743b2db13092c005b125573;hpb=52e6bd3e02522f68166c70f83d2ef3d7cf0c15ff;p=libs%2Fdatafile.git diff --git a/tool/compiler.cpp b/tool/compiler.cpp index 53c1b53..8c280c4 100644 --- a/tool/compiler.cpp +++ b/tool/compiler.cpp @@ -15,7 +15,8 @@ using namespace std; using namespace Msp; Compiler::Compiler(DataFile::Writer &w): - writer(w) + writer(w), + reset_src(false) { add("file", &Compiler::file); add("for_each", &Compiler::for_each); @@ -36,7 +37,13 @@ void Compiler::for_each(const vector &patterns) void Compiler::write(const DataFile::Statement &st) { - for(list::const_iterator i=st.sub.begin(); i!=st.sub.end(); ++i) + if(reset_src) + { + writer.write((DataFile::Statement("__src"), string())); + reset_src = false; + } + + for(list::const_iterator i = st.sub.begin(); i!=st.sub.end(); ++i) writer.write(*i); } @@ -45,11 +52,11 @@ bool Compiler::process_statement(const FS::Path &fn, DataFile::Statement &st) if(st.keyword=="_content") return true; - for(vector::iterator i=st.args.begin(); i!=st.args.end(); ++i) + for(vector::iterator i = st.args.begin(); i!=st.args.end(); ++i) if(i->get_type()==DataFile::STRING) { if(i->get_raw()=="$filename") - *i=DataFile::Value(FS::basename(fn.str())); + *i = DataFile::Value(FS::basename(fn.str())); else if(i->get_raw()=="$content") { IO::File in(fn.str()); @@ -57,14 +64,14 @@ bool Compiler::process_statement(const FS::Path &fn, DataFile::Statement &st) 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::iterator i=st.sub.begin(); i!=st.sub.end();) + for(list::iterator i = st.sub.begin(); i!=st.sub.end();) { if(process_statement(fn, *i)) { @@ -74,11 +81,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; @@ -89,15 +96,23 @@ bool Compiler::process_statement(const FS::Path &fn, DataFile::Statement &st) void Compiler::process_file(const FS::Path &fn, const list &st) { - for(list::const_iterator i=st.begin(); i!=st.end(); ++i) + writer.write((DataFile::Statement("__src"), FS::basename(fn.str()))); + reset_src = true; + + if(st.empty()) + process_file(fn); + else { - if(i->keyword=="_content") - process_file(fn); - else + for(list::const_iterator i = st.begin(); i!=st.end(); ++i) { - DataFile::Statement s=*i; - process_statement(fn, s); - writer.write(s); + if(i->keyword=="_content") + process_file(fn); + else + { + DataFile::Statement s = *i; + process_statement(fn, s); + writer.write(s); + } } } } @@ -110,7 +125,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); } @@ -126,10 +141,7 @@ File::File(Compiler &c, const FS::Path &fn): void File::finish() { - if(write_st.empty()) - compiler.process_file(filename); - else - compiler.process_file(filename, write_st); + compiler.process_file(filename, write_st); } void File::write(const DataFile::Statement &st) @@ -150,14 +162,14 @@ ForEach::ForEach(Compiler &c, const FS::Path &b, const list &p): void ForEach::finish() { - list files=FS::list_files(base); - for(list::iterator i=files.begin(); i!=files.end(); ++i) + list files = FS::list_files(base); + for(list::iterator i = files.begin(); i!=files.end(); ++i) { - bool match=false; - for(list::const_iterator j=patterns.begin(); (j!=patterns.end() && !match); ++j) - match=Regex(*j).match(*i); - for(list::const_iterator j=excludes.begin(); (j!=excludes.end() && match); ++j) - match=!Regex(*j).match(*i); + bool match = false; + for(list::const_iterator j = patterns.begin(); (j!=patterns.end() && !match); ++j) + match = Regex(*j).match(*i); + for(list::const_iterator j = excludes.begin(); (j!=excludes.end() && match); ++j) + match = !Regex(*j).match(*i); if(match) compiler.process_file(base / *i, write_st); }