#include "cmt_database.h" #include "cmt_cmtpath_pattern.h" #include "cmt_syntax.h" //---------------------------------------------------------- // // Operations on CmtPathPatterns // //---------------------------------------------------------- void CmtPathPattern::action (const CmtSystem::cmt_string_vector& words, Use* use) { if (words.size () < 1) return; // // expected syntax is: // // cmtpath_pattern any-cmt-statement // // where any-cmt-statement may contain the "template" // // // add (words, use); if (Cmt::get_debug ()) { cout << "CmtPathPattern::action> add " << endl; } } void CmtPathPattern::add (const CmtSystem::cmt_string_vector& words, Use* use) { static CmtPathPatternVector& CmtPathPatterns = patterns (); CmtPathPattern& p = CmtPathPatterns.add (); p.clear (); p.use = use; // // Install the cmt-statement as a vector of words. // for (int i = 1; i < words.size (); i++) { cmt_string& s = p.words.add (); s = words[i]; } } /** * Get the number of registered patterns */ int CmtPathPattern::pattern_number () { static CmtPathPatternVector& CmtPathPatterns = patterns (); return (CmtPathPatterns.size ()); } /** * Get the index'th pattern in the database */ CmtPathPattern& CmtPathPattern::pattern (int index) { static CmtPathPatternVector& CmtPathPatterns = patterns (); return (CmtPathPatterns[index]); } void CmtPathPattern::clear_all () { static CmtPathPatternVector& CmtPathPatterns = patterns (); for (int i = 0; i < CmtPathPatterns.size (); i++) { CmtPathPattern& p = CmtPathPatterns[i]; p.clear (); } CmtPathPatterns.clear (); } CmtPathPattern::CmtPathPatternVector& CmtPathPattern::patterns () { static Database& db = Database::instance (); static CmtPathPatternVector& CmtPathPatterns = db.cmtpath_patterns (); return (CmtPathPatterns); } /** * Applies all patterns to all CMTPATH item */ void CmtPathPattern::apply_all () { static CmtPathPatternVector& CmtPathPatterns = patterns (); int i; for (i = 0; i < CmtPathPatterns.size (); i++) { CmtPathPattern& p = CmtPathPatterns[i]; p.apply (); } } /** * this is the cmt show cmtpath_patterns command * It just shows the cmtpath_pattern declarations. */ void CmtPathPattern::show_all () { static CmtPathPatternVector& CmtPathPatterns = patterns (); int i; for (i = 0; i < CmtPathPatterns.size (); i++) { CmtPathPattern& p = CmtPathPatterns[i]; cout << "# " << p.use->get_package_name () << " " << p.use->version << " defines "; cout << "cmtpath_pattern " << " as ["; for (int wi = 0; wi < p.words.size (); wi++) { const cmt_string& s = p.words[wi]; if (wi > 0) cout << " "; cout << s; } cout << "]" << endl; } } //---------------------------------------------------------- CmtPathPattern::CmtPathPattern () { } //---------------------------------------------------------- CmtPathPattern::~CmtPathPattern () { } //---------------------------------------------------------- void CmtPathPattern::clear () { use = 0; words.clear (); } /** * Applies a pattern to all CMTPATH entries. */ void CmtPathPattern::apply () const { if (Cmt::get_debug ()) { cout << "CmtPathPattern::apply> cmtpath_pattern defined in " << use->get_package_name () << endl; } Use& current_use = Use::current (); const CmtSystem::cmt_string_vector& paths = Cmt::get_cmt_path (); const CmtSystem::cmt_string_vector& path_sources = Cmt::get_cmt_path_sources (); cmt_string buffer; bool is_constant = true; int i; for (i = 0; i < words.size (); i++) { const cmt_string& w = words[i]; int pos = w.find (""); if (pos != cmt_string::npos) { is_constant = false; break; } } if (is_constant) { expand (buffer, ""); SyntaxParser::parse_requirements_text (buffer, "", ¤t_use); buffer = ""; } else { for (i = (paths.size () - 1); i >= 0; i--) { const cmt_string& p = paths[i]; const cmt_string& s = path_sources[i]; if (s == "default path") continue; expand (buffer, p); SyntaxParser::parse_requirements_text (buffer, "", ¤t_use); buffer = ""; } } } void CmtPathPattern::expand (cmt_string& line, const cmt_string& path) const { line = ""; // First re-create the cmt statement as one line. for (int i = 0; i < words.size (); i++) { const cmt_string& w = words[i]; if (i > 0) line += " "; if ((w == "\n") | (w == ";")) { line += "\n"; } else { line += "\""; line += w; line += "\""; } } if (line != "") { // Substitute template from the cmt statement line.replace_all ("", path.c_str ()); } }