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