#include #include #include #include #include namespace xp = boost::xpressive; namespace regexes { using namespace xp; sregex re_ref = bos >> '[' >> (s1= +_d) >> ']'; sregex re_txt = (s1= -*_) >> '[' >> (s2= +_d) >> ']'; } using regexes::re_ref; using regexes::re_txt; void process(char const * filename) { std::ifstream file(filename); std::string line; while (getline(file, line) and line != "@footnote:") { } std::tr1::unordered_map dict; int counter = 1; xp::smatch what; while (getline(file, line)) { if (xp::regex_search(line, what, re_ref)) dict[boost::lexical_cast(what[1])] = counter++; } file.clear(); file.seekg(0); while (getline(file, line)) { std::string::const_iterator from = line.begin(); std::string::const_iterator to = line.end(); while (xp::regex_search(from, to, what, re_txt)) { from = what[0].second; int ref = boost::lexical_cast(what[2]); std::cout.write(&*what[1].first, what.length(1)); std::cout << "[" << (dict[ref] ? dict[ref] : ref) << "]"; } std::cout << std::string(from, to) << "\n"; } } int main(int argc, char ** argv) { std::ios::sync_with_stdio(false); for (int i = 1; i < argc; ++i) process(argv[i]); }