[1] | 1 | //----------------------------------------------------------- |
---|
| 2 | // Copyright Christian Arnault LAL-Orsay CNRS |
---|
| 3 | // arnault@lal.in2p3.fr |
---|
| 4 | // See the complete license in cmt_license.txt "http://www.cecill.info". |
---|
| 5 | //----------------------------------------------------------- |
---|
| 6 | |
---|
| 7 | #include "cmt_database.h" |
---|
| 8 | #include "cmt_cmtpath_pattern.h" |
---|
| 9 | #include "cmt_syntax.h" |
---|
| 10 | |
---|
| 11 | //---------------------------------------------------------- |
---|
| 12 | // |
---|
| 13 | // Operations on CmtPathPatterns |
---|
| 14 | // |
---|
| 15 | //---------------------------------------------------------- |
---|
| 16 | |
---|
| 17 | void CmtPathPattern::action (const CmtSystem::cmt_string_vector& words, Use* use) |
---|
| 18 | { |
---|
| 19 | if (words.size () < 1) return; |
---|
| 20 | |
---|
| 21 | // |
---|
| 22 | // expected syntax is: |
---|
| 23 | // |
---|
| 24 | // cmtpath_pattern any-cmt-statement |
---|
| 25 | // |
---|
| 26 | // where any-cmt-statement may contain the "template" |
---|
| 27 | // |
---|
| 28 | // <path> |
---|
| 29 | // <project> |
---|
| 30 | // |
---|
| 31 | |
---|
| 32 | add (words, use); |
---|
| 33 | |
---|
| 34 | if (Cmt::get_debug ()) |
---|
| 35 | { |
---|
| 36 | cout << "CmtPathPattern::action> add " << endl; |
---|
| 37 | } |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | void CmtPathPattern::add (const CmtSystem::cmt_string_vector& words, Use* use) |
---|
| 41 | { |
---|
| 42 | static CmtPathPatternVector& CmtPathPatterns = patterns (); |
---|
| 43 | |
---|
| 44 | CmtPathPattern& p = CmtPathPatterns.add (); |
---|
| 45 | |
---|
| 46 | p.clear (); |
---|
| 47 | |
---|
| 48 | p.use = use; |
---|
| 49 | |
---|
| 50 | int first_word = 1; |
---|
| 51 | |
---|
| 52 | // |
---|
| 53 | // Install the cmt-statement as a vector of words. |
---|
| 54 | // |
---|
| 55 | for (int i = first_word; i < words.size (); i++) |
---|
| 56 | { |
---|
| 57 | bool need_quotes = (i > (first_word + 1)); |
---|
| 58 | |
---|
| 59 | cmt_string& s = words[i]; |
---|
| 60 | |
---|
| 61 | if (i > first_word) p.line += " "; |
---|
| 62 | |
---|
| 63 | if (s == ";") first_word = i+1; |
---|
| 64 | |
---|
| 65 | if ((s == "\n") | (s == ";")) |
---|
| 66 | { |
---|
| 67 | p.line += "\n "; |
---|
| 68 | } |
---|
| 69 | else |
---|
| 70 | { |
---|
| 71 | cmt_string sep = "\""; |
---|
| 72 | |
---|
| 73 | if (s.find (sep) != cmt_string::npos) |
---|
| 74 | { |
---|
| 75 | sep = "\'"; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | if (!need_quotes) sep = ""; |
---|
| 79 | |
---|
| 80 | p.line += sep; |
---|
| 81 | p.line += s; |
---|
| 82 | p.line += sep; |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | * Get the number of registered patterns |
---|
| 89 | */ |
---|
| 90 | int CmtPathPattern::pattern_number () |
---|
| 91 | { |
---|
| 92 | static CmtPathPatternVector& CmtPathPatterns = patterns (); |
---|
| 93 | |
---|
| 94 | return (CmtPathPatterns.size ()); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | * Get the index'th pattern in the database |
---|
| 99 | */ |
---|
| 100 | CmtPathPattern& CmtPathPattern::pattern (int index) |
---|
| 101 | { |
---|
| 102 | static CmtPathPatternVector& CmtPathPatterns = patterns (); |
---|
| 103 | |
---|
| 104 | return (CmtPathPatterns[index]); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | void CmtPathPattern::clear_all () |
---|
| 108 | { |
---|
| 109 | static CmtPathPatternVector& CmtPathPatterns = patterns (); |
---|
| 110 | |
---|
| 111 | for (int i = 0; i < CmtPathPatterns.size (); i++) |
---|
| 112 | { |
---|
| 113 | CmtPathPattern& p = CmtPathPatterns[i]; |
---|
| 114 | p.clear (); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | CmtPathPatterns.clear (); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | CmtPathPattern::CmtPathPatternVector& CmtPathPattern::patterns () |
---|
| 121 | { |
---|
| 122 | static Database& db = Database::instance (); |
---|
| 123 | static CmtPathPatternVector& CmtPathPatterns = db.cmtpath_patterns (); |
---|
| 124 | |
---|
| 125 | return (CmtPathPatterns); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | /** |
---|
| 129 | * Applies all patterns to all CMTPATH item |
---|
| 130 | */ |
---|
| 131 | void CmtPathPattern::apply_all () |
---|
| 132 | { |
---|
| 133 | static CmtPathPatternVector& CmtPathPatterns = patterns (); |
---|
| 134 | |
---|
| 135 | int i; |
---|
| 136 | |
---|
| 137 | for (i = 0; i < CmtPathPatterns.size (); i++) |
---|
| 138 | { |
---|
| 139 | CmtPathPattern& p = CmtPathPatterns[i]; |
---|
| 140 | |
---|
| 141 | p.apply (); |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | /** |
---|
| 146 | * this is the cmt show cmtpath_patterns command |
---|
| 147 | * It just shows the cmtpath_pattern declarations. |
---|
| 148 | */ |
---|
| 149 | void CmtPathPattern::show_all () |
---|
| 150 | { |
---|
| 151 | static CmtPathPatternVector& CmtPathPatterns = patterns (); |
---|
| 152 | |
---|
| 153 | int i; |
---|
| 154 | |
---|
| 155 | for (i = 0; i < CmtPathPatterns.size (); i++) |
---|
| 156 | { |
---|
| 157 | CmtPathPattern& p = CmtPathPatterns[i]; |
---|
| 158 | |
---|
| 159 | cout << "# " << p.use->get_package_name () << " " << p.use->version |
---|
| 160 | << " adds a cmtpath_pattern as " << endl; |
---|
| 161 | cout << " " << p.line << endl; |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | //---------------------------------------------------------- |
---|
| 166 | CmtPathPattern::CmtPathPattern () |
---|
| 167 | { |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | //---------------------------------------------------------- |
---|
| 171 | CmtPathPattern::~CmtPathPattern () |
---|
| 172 | |
---|
| 173 | { |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | //---------------------------------------------------------- |
---|
| 177 | void CmtPathPattern::clear () |
---|
| 178 | { |
---|
| 179 | use = 0; |
---|
| 180 | line = ""; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | class CmtPathPatternProjectAction : public IProjectAction |
---|
| 184 | { |
---|
| 185 | public: |
---|
| 186 | CmtPathPatternProjectAction (const CmtPathPattern& pattern, Use& use) : |
---|
| 187 | m_pattern (pattern), |
---|
| 188 | m_current (use) |
---|
| 189 | { |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | bool run (const Project& project) |
---|
| 193 | { |
---|
| 194 | const cmt_string& pname = project.get_name (); |
---|
| 195 | const cmt_string& p = project.get_cmtpath (); |
---|
| 196 | const cmt_string& s = project.get_cmtpath_source (); |
---|
| 197 | |
---|
| 198 | if (s == "default path") return (true); |
---|
| 199 | |
---|
| 200 | m_pattern.expand (m_buffer, p, pname); |
---|
| 201 | |
---|
| 202 | if (Cmt::get_debug ()) |
---|
| 203 | { |
---|
| 204 | cout << "CmtPathPattern::apply> text=[" << m_buffer << "]" << endl; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | SyntaxParser::parse_requirements_text (m_buffer, "", &m_current); |
---|
| 208 | m_buffer = ""; |
---|
| 209 | |
---|
| 210 | return (true); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | private: |
---|
| 214 | |
---|
| 215 | const CmtPathPattern& m_pattern; |
---|
| 216 | Use& m_current; |
---|
| 217 | cmt_string m_buffer; |
---|
| 218 | }; |
---|
| 219 | |
---|
| 220 | |
---|
| 221 | /** |
---|
| 222 | * Applies a pattern to all CMTPATH entries. |
---|
| 223 | */ |
---|
| 224 | void CmtPathPattern::apply () const |
---|
| 225 | { |
---|
| 226 | if (Cmt::get_debug ()) |
---|
| 227 | { |
---|
| 228 | cout << "CmtPathPattern::apply> cmtpath_pattern defined in " << use->get_package_name () << endl; |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | Use& current_use = Use::current (); |
---|
| 232 | |
---|
| 233 | bool is_constant = ((line.find ("<path>") == cmt_string::npos) && |
---|
| 234 | (line.find ("<project>") == cmt_string::npos)); |
---|
| 235 | |
---|
| 236 | if (is_constant) |
---|
| 237 | { |
---|
| 238 | cmt_string buffer; |
---|
| 239 | |
---|
| 240 | expand (buffer, "", ""); |
---|
| 241 | |
---|
| 242 | if (Cmt::get_debug ()) |
---|
| 243 | { |
---|
| 244 | cout << "CmtPathPattern::apply> text=[" << buffer << "]" << endl; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | SyntaxParser::parse_requirements_text (buffer, "", ¤t_use); |
---|
| 248 | buffer = ""; |
---|
| 249 | } |
---|
| 250 | else |
---|
| 251 | { |
---|
| 252 | CmtPathPatternProjectAction pa (*this, current_use); |
---|
| 253 | |
---|
| 254 | Project::reverse_broadcast (pa); |
---|
| 255 | } |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | void CmtPathPattern::expand (cmt_string& replacement, |
---|
| 259 | const cmt_string& path, |
---|
| 260 | const cmt_string& project) const |
---|
| 261 | { |
---|
| 262 | replacement = line; |
---|
| 263 | |
---|
| 264 | if (replacement != "") |
---|
| 265 | { |
---|
| 266 | // Substitute <path> template from the cmt statement |
---|
| 267 | replacement.replace_all ("<path>", path.c_str ()); |
---|
| 268 | replacement.replace_all ("<project>", project.c_str ()); |
---|
| 269 | } |
---|
| 270 | } |
---|
| 271 | |
---|