00001
00002
00003
00004
00005
00006
00007 #include "cmt_database.h"
00008 #include "cmt_cmtpath_pattern.h"
00009 #include "cmt_syntax.h"
00010
00011
00012
00013
00014
00015
00016
00017 void CmtPathPattern::action (const CmtSystem::cmt_string_vector& words, Use* use)
00018 {
00019 if (words.size () < 1) return;
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 add (words, use);
00033
00034 if (Cmt::get_debug ())
00035 {
00036 cout << "CmtPathPattern::action> add " << endl;
00037 }
00038 }
00039
00040 void CmtPathPattern::add (const CmtSystem::cmt_string_vector& words, Use* use)
00041 {
00042 static CmtPathPatternVector& CmtPathPatterns = patterns ();
00043
00044 CmtPathPattern& p = CmtPathPatterns.add ();
00045
00046 p.clear ();
00047
00048 p.use = use;
00049
00050 int first_word = 1;
00051
00052
00053
00054
00055 for (int i = first_word; i < words.size (); i++)
00056 {
00057 bool need_quotes = (i > (first_word + 1));
00058
00059 cmt_string& s = words[i];
00060
00061 if (i > first_word) p.line += " ";
00062
00063 if (s == ";") first_word = i+1;
00064
00065 if ((s == "\n") | (s == ";"))
00066 {
00067 p.line += "\n ";
00068 }
00069 else
00070 {
00071 cmt_string sep = "\"";
00072
00073 if (s.find (sep) != cmt_string::npos)
00074 {
00075 sep = "\'";
00076 }
00077
00078 if (!need_quotes) sep = "";
00079
00080 p.line += sep;
00081 p.line += s;
00082 p.line += sep;
00083 }
00084 }
00085 }
00086
00090 int CmtPathPattern::pattern_number ()
00091 {
00092 static CmtPathPatternVector& CmtPathPatterns = patterns ();
00093
00094 return (CmtPathPatterns.size ());
00095 }
00096
00100 CmtPathPattern& CmtPathPattern::pattern (int index)
00101 {
00102 static CmtPathPatternVector& CmtPathPatterns = patterns ();
00103
00104 return (CmtPathPatterns[index]);
00105 }
00106
00107 void CmtPathPattern::clear_all ()
00108 {
00109 static CmtPathPatternVector& CmtPathPatterns = patterns ();
00110
00111 for (int i = 0; i < CmtPathPatterns.size (); i++)
00112 {
00113 CmtPathPattern& p = CmtPathPatterns[i];
00114 p.clear ();
00115 }
00116
00117 CmtPathPatterns.clear ();
00118 }
00119
00120 CmtPathPattern::CmtPathPatternVector& CmtPathPattern::patterns ()
00121 {
00122 static Database& db = Database::instance ();
00123 static CmtPathPatternVector& CmtPathPatterns = db.cmtpath_patterns ();
00124
00125 return (CmtPathPatterns);
00126 }
00127
00131 void CmtPathPattern::apply_all ()
00132 {
00133 static CmtPathPatternVector& CmtPathPatterns = patterns ();
00134
00135 int i;
00136
00137 for (i = 0; i < CmtPathPatterns.size (); i++)
00138 {
00139 CmtPathPattern& p = CmtPathPatterns[i];
00140
00141 p.apply ();
00142 }
00143 }
00144
00149 void CmtPathPattern::show_all ()
00150 {
00151 static CmtPathPatternVector& CmtPathPatterns = patterns ();
00152
00153 int i;
00154
00155 for (i = 0; i < CmtPathPatterns.size (); i++)
00156 {
00157 CmtPathPattern& p = CmtPathPatterns[i];
00158
00159 cout << "# " << p.use->get_package_name () << " " << p.use->version
00160 << " adds a cmtpath_pattern as " << endl;
00161 cout << " " << p.line << endl;
00162 }
00163 }
00164
00165
00166 CmtPathPattern::CmtPathPattern ()
00167 {
00168 }
00169
00170
00171 CmtPathPattern::~CmtPathPattern ()
00172
00173 {
00174 }
00175
00176
00177 void CmtPathPattern::clear ()
00178 {
00179 use = 0;
00180 line = "";
00181 }
00182
00183 class CmtPathPatternProjectAction : public IProjectAction
00184 {
00185 public:
00186 CmtPathPatternProjectAction (const CmtPathPattern& pattern, Use& use) :
00187 m_pattern (pattern),
00188 m_current (use)
00189 {
00190 }
00191
00192 bool run (const Project& project)
00193 {
00194 const cmt_string& pname = project.get_name ();
00195 const cmt_string& p = project.get_cmtpath ();
00196 const cmt_string& s = project.get_cmtpath_source ();
00197
00198 if (s == "default path") return (true);
00199
00200 m_pattern.expand (m_buffer, p, pname);
00201
00202 if (Cmt::get_debug ())
00203 {
00204 cout << "CmtPathPattern::apply> text=[" << m_buffer << "]" << endl;
00205 }
00206
00207 SyntaxParser::parse_requirements_text (m_buffer, "", &m_current);
00208 m_buffer = "";
00209
00210 return (true);
00211 }
00212
00213 private:
00214
00215 const CmtPathPattern& m_pattern;
00216 Use& m_current;
00217 cmt_string m_buffer;
00218 };
00219
00220
00224 void CmtPathPattern::apply () const
00225 {
00226 if (Cmt::get_debug ())
00227 {
00228 cout << "CmtPathPattern::apply> cmtpath_pattern defined in " << use->get_package_name () << endl;
00229 }
00230
00231 Use& current_use = Use::current ();
00232
00233 bool is_constant = ((line.find ("<path>") == cmt_string::npos) &&
00234 (line.find ("<project>") == cmt_string::npos));
00235
00236 if (is_constant)
00237 {
00238 cmt_string buffer;
00239
00240 expand (buffer, "", "");
00241
00242 if (Cmt::get_debug ())
00243 {
00244 cout << "CmtPathPattern::apply> text=[" << buffer << "]" << endl;
00245 }
00246
00247 SyntaxParser::parse_requirements_text (buffer, "", ¤t_use);
00248 buffer = "";
00249 }
00250 else
00251 {
00252 CmtPathPatternProjectAction pa (*this, current_use);
00253
00254 Project::reverse_broadcast (pa);
00255 }
00256 }
00257
00258 void CmtPathPattern::expand (cmt_string& replacement,
00259 const cmt_string& path,
00260 const cmt_string& project) const
00261 {
00262 replacement = line;
00263
00264 if (replacement != "")
00265 {
00266
00267 replacement.replace_all ("<path>", path.c_str ());
00268 replacement.replace_all ("<project>", project.c_str ());
00269 }
00270 }
00271