source: CMT/HEAD/source/cmt_cmtpath_pattern.cxx @ 663

Last change on this file since 663 was 663, checked in by rybkin, 10 years ago

See C.L. 522

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