// Copyright (c) 2005-2008 Hartmut Kaiser // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #include #include #include #include #include #include #include #include #include // This tool always reads from static_adaptor_data.hpp.in and writes to // static_adaptor_data.hpp, if nothing else has been specified #define STATIC_DATA_IN "static_adaptor_data.hpp.in" #define STATIC_DATA_OUT "static_adaptor_data.hpp" #define REPLACE_TAG "@SAGA_LITE_REPLACE@" #define NAME_TAG "@ADAPTOR_NAME@" #define PACKAGE_TAG "@PACKAGE_NAME@" /////////////////////////////////////////////////////////////////////////////// inline std::string trim(std::string const& s) { std::string::size_type first = s.find_first_not_of(" \t"); if (first == std::string::npos) return std::string(""); std::string::size_type last = s.find_last_not_of(" \t"); return s.substr(first, last-first+1); } /////////////////////////////////////////////////////////////////////////////// bool split_line(std::string const& line, std::string& adaptor, std::set& packages) { using namespace boost::algorithm; std::vector entries; split(entries, line, is_any_of(",")); if (entries.size() < 1) { return false; } adaptor = entries[0]; if (entries.size () > 1 && entries[1].length () > 0 ) { std::copy(++entries.begin(), entries.end(), std::inserter(packages, packages.end())); } return true; } /////////////////////////////////////////////////////////////////////////////// bool read_adaptor_list(char const* listname, std::set& adaptors, std::set& packages) { std::ifstream adaptor_list(listname); if (!adaptor_list.is_open()) { std::cout << "not open\n"; return false; } std::string line; while (std::getline(adaptor_list, line)) { boost::algorithm::trim(line); if (line.empty() || line[0] == '#') continue; // empty or comment line // split the line into adaptor name and packages std::string adaptor; if (!split_line(line, adaptor, packages)) { std::cout << "split failed: " << line << "\n"; return false; } adaptors.insert(adaptor); } return true; } /////////////////////////////////////////////////////////////////////////////// bool replace_tags(std::ofstream& out, std::string templ, std::set const& adaptors, std::set const& packages) { { std::set::const_iterator end = adaptors.end(); for (std::set::const_iterator it = adaptors.begin(); it != end; ++it) { std::string::size_type p = templ.find(NAME_TAG); if (p != std::string::npos) { std::string s (templ); s.replace(p, sizeof(NAME_TAG)-1, *it); out << s; } } } { std::set::const_iterator end = packages.end(); for (std::set::const_iterator it = packages.begin(); it != end; ++it) { std::string::size_type p = templ.find(PACKAGE_TAG); if (p != std::string::npos) { std::string s (templ); s.replace(p, sizeof(PACKAGE_TAG)-1, *it); out << s; } } } return true; } /////////////////////////////////////////////////////////////////////////////// int main(int argc, char* argv[]) { // verify command line if (argc < 2 || argc > 4) { std::cerr << "usage: create_static_adaptor_data " "[ []]" << std::endl; std::cerr << " defaults to: " << STATIC_DATA_IN << std::endl; std::cerr << " defaults to: " << STATIC_DATA_OUT << std::endl; return -1; } // read adaptor list std::set adaptors; std::set packages; if (!read_adaptor_list(argv[1], adaptors, packages)) { std::cerr << "Couldn't read adaptor list: " << argv[1] << std::endl; return -2; } // open input file std::string inname(STATIC_DATA_IN); if (argc > 2) inname = argv[2]; std::ifstream in(inname.c_str()); if (!in.is_open()) { std::cerr << "Couldn't open input file: " << inname << std::endl; return -3; } // open output file std::string outname(STATIC_DATA_OUT); if (argc > 3) outname = argv[3]; std::ofstream out(outname.c_str()); if (!out.is_open()) { std::cerr << "Couldn't open output file: " << outname << std::endl; return -4; } // loop over the input lines and replace the tags int linenum = 0; std::string line; while (std::getline(in, line)) { ++linenum; std::string::size_type p = line.find(REPLACE_TAG); if (p == std::string::npos) { out << line << std::endl; continue; } // output the rest of the line out << line.substr(0, p); std::string templ; std::string::size_type first = line.find("[[", p); if (first == std::string::npos) { std::cerr << "Tag: " << REPLACE_TAG << " should be followed by '[['." << std::endl; return -5; } std::string::size_type last = line.find("]]", first); if (last != std::string::npos) { templ = line.substr(first+2, last-first-2); } else { templ = line.substr(first+2); templ += "\n"; // find the line containing the closing parenthesis while (std::getline(in, line)) { ++linenum; last = line.find("]]"); if (last == std::string::npos) { templ += line; templ += "\n"; } else { templ += line.substr(0, last); break; } } } if (templ[0] == '\n') templ.erase(0, 1); if (!replace_tags(out, templ, adaptors, packages)) { std::cerr << "Failed to replace name tag at line: " << linenum << " (" << templ << ")" << std::endl; } } return 0; }