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 <stdio.h> |
---|
8 | #include <stdlib.h> |
---|
9 | #include <string.h> |
---|
10 | #include <ctype.h> |
---|
11 | |
---|
12 | #include "cmt_fragment.h" |
---|
13 | #include "cmt_use.h" |
---|
14 | #include "cmt_symbol.h" |
---|
15 | #include "cmt_system.h" |
---|
16 | #include "cmt_database.h" |
---|
17 | #include "cmt_log.h" |
---|
18 | #include "cmt_error.h" |
---|
19 | |
---|
20 | /*----------------------------------------------------------*/ |
---|
21 | /* */ |
---|
22 | /* Operations on Variables */ |
---|
23 | /* */ |
---|
24 | /*----------------------------------------------------------*/ |
---|
25 | |
---|
26 | //---------------------------------------------------------- |
---|
27 | Variable* Variable::find (VariableVector& vector, |
---|
28 | const cmt_string& name) |
---|
29 | { |
---|
30 | for (int i = 0; i < vector.size (); i++) |
---|
31 | { |
---|
32 | Variable& v = vector[i]; |
---|
33 | |
---|
34 | if (v.name == name) return (&v); |
---|
35 | } |
---|
36 | |
---|
37 | return (0); |
---|
38 | } |
---|
39 | |
---|
40 | //---------------------------------------------------------- |
---|
41 | Variable::Variable () |
---|
42 | { |
---|
43 | } |
---|
44 | |
---|
45 | //---------------------------------------------------------- |
---|
46 | Variable::Variable (const cmt_string& n) : name (n) |
---|
47 | { |
---|
48 | m_macro_braces = "${"; |
---|
49 | m_macro_braces += name; |
---|
50 | m_macro_braces += "}"; |
---|
51 | |
---|
52 | m_macro_pars = "$("; |
---|
53 | m_macro_pars += name; |
---|
54 | m_macro_pars += ")"; |
---|
55 | } |
---|
56 | |
---|
57 | //---------------------------------------------------------- |
---|
58 | const cmt_string& Variable::macro_braces () const |
---|
59 | { |
---|
60 | return (m_macro_braces); |
---|
61 | } |
---|
62 | |
---|
63 | //---------------------------------------------------------- |
---|
64 | const cmt_string& Variable::macro_pars () const |
---|
65 | { |
---|
66 | return (m_macro_pars); |
---|
67 | } |
---|
68 | |
---|
69 | //---------------------------------------------------------- |
---|
70 | void Variable::set (const cmt_string& new_name, |
---|
71 | const cmt_string& new_value) |
---|
72 | { |
---|
73 | name = new_name; |
---|
74 | value = new_value; |
---|
75 | |
---|
76 | m_macro_braces = "${"; |
---|
77 | m_macro_braces += name; |
---|
78 | m_macro_braces += "}"; |
---|
79 | |
---|
80 | m_macro_pars = "$("; |
---|
81 | m_macro_pars += name; |
---|
82 | m_macro_pars += ")"; |
---|
83 | } |
---|
84 | |
---|
85 | //---------------------------------------------------------- |
---|
86 | Variable& Variable::operator = (const Variable& other) |
---|
87 | { |
---|
88 | value = other.value; |
---|
89 | return (*this); |
---|
90 | } |
---|
91 | |
---|
92 | //---------------------------------------------------------- |
---|
93 | Variable& Variable::operator = (const cmt_string& v) |
---|
94 | { |
---|
95 | value = v; |
---|
96 | return (*this); |
---|
97 | } |
---|
98 | |
---|
99 | //---------------------------------------------------------- |
---|
100 | void Variable::operator += (const cmt_string& v) |
---|
101 | { |
---|
102 | value += v; |
---|
103 | } |
---|
104 | |
---|
105 | //---------------------------------------------------------- |
---|
106 | cmt_string Variable::operator + (const cmt_string& v) const |
---|
107 | { |
---|
108 | return (value + v); |
---|
109 | } |
---|
110 | |
---|
111 | //---------------------------------------------------------- |
---|
112 | Variable::operator const cmt_string& () const |
---|
113 | { |
---|
114 | return (value); |
---|
115 | } |
---|
116 | |
---|
117 | //---------------------------------------------------------- |
---|
118 | bool Variable::operator == (const cmt_string& v) const |
---|
119 | { |
---|
120 | return ((value == v)); |
---|
121 | } |
---|
122 | |
---|
123 | //---------------------------------------------------------- |
---|
124 | bool Variable::operator != (const cmt_string& v) const |
---|
125 | { |
---|
126 | return ((value != v)); |
---|
127 | } |
---|
128 | |
---|
129 | /*----------------------------------------------------------*/ |
---|
130 | /* */ |
---|
131 | /* Operations on Fragments */ |
---|
132 | /* */ |
---|
133 | /*----------------------------------------------------------*/ |
---|
134 | |
---|
135 | /*----------------------------------------------------------*/ |
---|
136 | void Fragment::show (const cmt_string& name) |
---|
137 | { |
---|
138 | Fragment* fragment = Fragment::find (name); |
---|
139 | if (fragment == 0) |
---|
140 | { |
---|
141 | cmt_string t (name); |
---|
142 | // t += " is not defined"; |
---|
143 | CmtError::set (CmtError::fragment_not_found, t); |
---|
144 | // CmtMessage::info ("Fragment " + name + " not found"); |
---|
145 | // cerr << "Fragment " << name << " not found" << endl; |
---|
146 | } |
---|
147 | else |
---|
148 | { |
---|
149 | fragment->print (); |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | /*----------------------------------------------------------*/ |
---|
154 | void Fragment::show_all () |
---|
155 | { |
---|
156 | static FragmentVector& Fragments = fragments (); |
---|
157 | |
---|
158 | int number; |
---|
159 | |
---|
160 | for (number = 0; number < Fragments.size (); number++) |
---|
161 | { |
---|
162 | Fragment& fragment = Fragments[number]; |
---|
163 | |
---|
164 | fragment.print (); |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | class fragment_action_iterator |
---|
169 | { |
---|
170 | public: |
---|
171 | |
---|
172 | fragment_action_iterator (Use* use) : |
---|
173 | m_need_dependencies (false), |
---|
174 | m_state (need_name), |
---|
175 | m_use (use) |
---|
176 | { |
---|
177 | } |
---|
178 | |
---|
179 | void add_word (const cmt_string& w) |
---|
180 | { |
---|
181 | switch (m_state) |
---|
182 | { |
---|
183 | case need_name: |
---|
184 | m_name = w; |
---|
185 | m_state = need_options; |
---|
186 | break; |
---|
187 | case need_options: |
---|
188 | if (w.find ("-suffix=") != cmt_string::npos) |
---|
189 | { |
---|
190 | m_suffix = w; |
---|
191 | m_suffix.replace ("-suffix=", ""); |
---|
192 | } |
---|
193 | else if (w.find ("-dependencies") != cmt_string::npos) |
---|
194 | { |
---|
195 | m_need_dependencies = true; |
---|
196 | } |
---|
197 | else if (w.find ("-header=") != cmt_string::npos) |
---|
198 | { |
---|
199 | m_header = w; |
---|
200 | m_header.replace ("-header=", ""); |
---|
201 | |
---|
202 | if (Fragment::find (m_header) == 0) |
---|
203 | { |
---|
204 | Fragment::add (m_header, "", "", "", false, m_use); |
---|
205 | } |
---|
206 | } |
---|
207 | else if (w.find ("-trailer=") != cmt_string::npos) |
---|
208 | { |
---|
209 | m_trailer = w; |
---|
210 | m_trailer.replace ("-trailer=", ""); |
---|
211 | |
---|
212 | if (Fragment::find (m_trailer) == 0) |
---|
213 | { |
---|
214 | Fragment::add (m_trailer, "", "", "", false, m_use); |
---|
215 | } |
---|
216 | } |
---|
217 | break; |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | void commit () |
---|
222 | { |
---|
223 | Fragment::add (m_name, m_suffix, m_header, m_trailer, m_need_dependencies, m_use); |
---|
224 | } |
---|
225 | |
---|
226 | private: |
---|
227 | |
---|
228 | enum |
---|
229 | { |
---|
230 | need_name, |
---|
231 | need_options |
---|
232 | } m_state; |
---|
233 | |
---|
234 | cmt_string m_name; |
---|
235 | cmt_string m_suffix; |
---|
236 | cmt_string m_header; |
---|
237 | cmt_string m_trailer; |
---|
238 | bool m_need_dependencies; |
---|
239 | Use* m_use; |
---|
240 | }; |
---|
241 | |
---|
242 | |
---|
243 | /*----------------------------------------------------------*/ |
---|
244 | void Fragment::action (const CmtSystem::cmt_string_vector& words, |
---|
245 | Use* use) |
---|
246 | { |
---|
247 | if ((Cmt::get_current_access () == UserMode) && |
---|
248 | (use->get_current_scope () == ScopePrivate)) return; |
---|
249 | |
---|
250 | if (words.size () <= 1) return; |
---|
251 | |
---|
252 | fragment_action_iterator it (use); |
---|
253 | |
---|
254 | for (int i = 1; i < words.size (); i++) |
---|
255 | { |
---|
256 | const cmt_string& w = words[i]; |
---|
257 | cmt_string ew = w; |
---|
258 | |
---|
259 | Symbol::expand (ew); |
---|
260 | if (ew != w) |
---|
261 | { |
---|
262 | CmtSystem::cmt_string_vector ws; |
---|
263 | |
---|
264 | CmtSystem::split (ew, " ", ws); |
---|
265 | |
---|
266 | for (int j = 0; j < ws.size (); ++j) |
---|
267 | { |
---|
268 | const cmt_string& ww = ws[j]; |
---|
269 | it.add_word (ww); |
---|
270 | } |
---|
271 | } |
---|
272 | else |
---|
273 | { |
---|
274 | it.add_word (ew); |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | it.commit (); |
---|
279 | } |
---|
280 | |
---|
281 | /*----------------------------------------------------------*/ |
---|
282 | Fragment* Fragment::find (const cmt_string& name) |
---|
283 | { |
---|
284 | static FragmentVector& Fragments = fragments (); |
---|
285 | |
---|
286 | int fragment_index; |
---|
287 | |
---|
288 | if (Fragments.size () == 0) return (0); |
---|
289 | |
---|
290 | for (fragment_index = 0; |
---|
291 | fragment_index < Fragments.size (); |
---|
292 | fragment_index++) |
---|
293 | { |
---|
294 | Fragment& fragment = Fragments[fragment_index]; |
---|
295 | |
---|
296 | if (fragment.name == name) |
---|
297 | { |
---|
298 | return (&fragment); |
---|
299 | } |
---|
300 | } |
---|
301 | |
---|
302 | return (0); |
---|
303 | } |
---|
304 | |
---|
305 | /*----------------------------------------------------------*/ |
---|
306 | void Fragment::add (const cmt_string& name, |
---|
307 | const cmt_string& suffix, |
---|
308 | const cmt_string& header, |
---|
309 | const cmt_string& trailer, |
---|
310 | bool need_dependencies, |
---|
311 | Use* use) |
---|
312 | { |
---|
313 | static FragmentVector& Fragments = fragments (); |
---|
314 | |
---|
315 | { |
---|
316 | Fragment* fragment; |
---|
317 | |
---|
318 | if (name == "") return; |
---|
319 | |
---|
320 | fragment = find (name); |
---|
321 | if (fragment != 0) |
---|
322 | { |
---|
323 | if (suffix != "") |
---|
324 | { |
---|
325 | fragment->suffix = suffix; |
---|
326 | } |
---|
327 | |
---|
328 | if (header != "") |
---|
329 | { |
---|
330 | fragment->header = header; |
---|
331 | } |
---|
332 | |
---|
333 | if (trailer != "") |
---|
334 | { |
---|
335 | fragment->trailer = trailer; |
---|
336 | } |
---|
337 | |
---|
338 | fragment->need_dependencies = need_dependencies; |
---|
339 | |
---|
340 | fragment->use = use; |
---|
341 | return; |
---|
342 | } |
---|
343 | } |
---|
344 | |
---|
345 | Fragment& fragment = Fragments.add (); |
---|
346 | |
---|
347 | fragment.name = name; |
---|
348 | fragment.suffix = suffix; |
---|
349 | fragment.header = header; |
---|
350 | fragment.trailer = trailer; |
---|
351 | fragment.need_dependencies = need_dependencies; |
---|
352 | fragment.use = use; |
---|
353 | } |
---|
354 | |
---|
355 | /*----------------------------------------------------------*/ |
---|
356 | void Fragment::clear_all () |
---|
357 | { |
---|
358 | static FragmentVector& Fragments = fragments (); |
---|
359 | |
---|
360 | for (int i = 0; i < Fragments.size (); i++) |
---|
361 | { |
---|
362 | Fragment& f = Fragments[i]; |
---|
363 | f.clear (); |
---|
364 | } |
---|
365 | |
---|
366 | Fragments.clear (); |
---|
367 | } |
---|
368 | |
---|
369 | /*----------------------------------------------------------*/ |
---|
370 | Fragment::FragmentVector& Fragment::fragments () |
---|
371 | { |
---|
372 | static Database& db = Database::instance (); |
---|
373 | static FragmentVector& Fragments = db.fragments (); |
---|
374 | |
---|
375 | return (Fragments); |
---|
376 | } |
---|
377 | |
---|
378 | /*----------------------------------------------------------*/ |
---|
379 | Fragment::Fragment () |
---|
380 | { |
---|
381 | use = 0; |
---|
382 | } |
---|
383 | |
---|
384 | /*----------------------------------------------------------*/ |
---|
385 | Fragment::Fragment (const cmt_string& fragment_name) |
---|
386 | { |
---|
387 | name = fragment_name; |
---|
388 | use = 0; |
---|
389 | path = ""; |
---|
390 | } |
---|
391 | |
---|
392 | /*----------------------------------------------------------*/ |
---|
393 | Fragment::~Fragment () |
---|
394 | { |
---|
395 | use = 0; |
---|
396 | } |
---|
397 | |
---|
398 | /*----------------------------------------------------------*/ |
---|
399 | void Fragment::clear () |
---|
400 | { |
---|
401 | name = ""; |
---|
402 | suffix = ""; |
---|
403 | header = ""; |
---|
404 | trailer = ""; |
---|
405 | need_dependencies = false; |
---|
406 | path = ""; |
---|
407 | use = 0; |
---|
408 | } |
---|
409 | |
---|
410 | /*----------------------------------------------------------*/ |
---|
411 | int Fragment::print () |
---|
412 | { |
---|
413 | int result = 1; |
---|
414 | |
---|
415 | if (name == "") return (0); |
---|
416 | |
---|
417 | locate (); |
---|
418 | |
---|
419 | if (use == 0) |
---|
420 | { |
---|
421 | Use& u = Use::current(); |
---|
422 | use = &u; |
---|
423 | } |
---|
424 | |
---|
425 | cmt_string the_path = path; |
---|
426 | |
---|
427 | use->reduce_path (the_path); |
---|
428 | |
---|
429 | cout << the_path; |
---|
430 | |
---|
431 | if (suffix != "") |
---|
432 | { |
---|
433 | cout << "->" << suffix; |
---|
434 | } |
---|
435 | |
---|
436 | cout << endl; |
---|
437 | |
---|
438 | return (result); |
---|
439 | } |
---|
440 | |
---|
441 | /*----------------------------------------------------------*/ |
---|
442 | bool Fragment::locate () |
---|
443 | { |
---|
444 | cmt_string root_path; |
---|
445 | |
---|
446 | if (use == 0) |
---|
447 | { |
---|
448 | // Assume CMT |
---|
449 | use = Use::find ("CMT"); |
---|
450 | } |
---|
451 | |
---|
452 | use->get_full_path (root_path); |
---|
453 | |
---|
454 | if (path != "") return (true); |
---|
455 | |
---|
456 | // First try <root>/fragments/<name> or <root>/fragments/nmake/<name> |
---|
457 | |
---|
458 | path = root_path; |
---|
459 | path += CmtSystem::file_separator (); |
---|
460 | path += "fragments"; |
---|
461 | path += CmtSystem::file_separator (); |
---|
462 | |
---|
463 | if (Cmt::build_nmake ()) |
---|
464 | { |
---|
465 | path += "nmake"; |
---|
466 | path += CmtSystem::file_separator (); |
---|
467 | } |
---|
468 | |
---|
469 | path += name; |
---|
470 | |
---|
471 | if (CmtSystem::test_file (path)) return (true); |
---|
472 | |
---|
473 | // Then try <root>/fragments/<name> for both Win and Unix |
---|
474 | |
---|
475 | path = root_path; |
---|
476 | path += CmtSystem::file_separator (); |
---|
477 | path += "fragments"; |
---|
478 | path += CmtSystem::file_separator (); |
---|
479 | path += name; |
---|
480 | |
---|
481 | if (CmtSystem::test_file (path)) return (true); |
---|
482 | |
---|
483 | // Then try <root>/cmt/fragments/<name> or <root>/cmt/fragments/nmake/<name> |
---|
484 | |
---|
485 | root_path += CmtSystem::file_separator (); |
---|
486 | |
---|
487 | if (use->style == mgr_style) root_path += "mgr"; |
---|
488 | else root_path += "cmt"; |
---|
489 | |
---|
490 | root_path += CmtSystem::file_separator (); |
---|
491 | root_path += "fragments"; |
---|
492 | root_path += CmtSystem::file_separator (); |
---|
493 | |
---|
494 | path = root_path; |
---|
495 | |
---|
496 | if (Cmt::build_nmake ()) |
---|
497 | { |
---|
498 | path += "nmake"; |
---|
499 | path += CmtSystem::file_separator (); |
---|
500 | } |
---|
501 | |
---|
502 | path += name; |
---|
503 | |
---|
504 | if (CmtSystem::test_file (path)) return (true); |
---|
505 | |
---|
506 | // Then try <root>/cmt/fragments/<name> for both Win and Unix |
---|
507 | |
---|
508 | path = root_path; |
---|
509 | |
---|
510 | path += name; |
---|
511 | |
---|
512 | if (CmtSystem::test_file (path)) return (true); |
---|
513 | |
---|
514 | return (false); |
---|
515 | } |
---|
516 | |
---|
517 | //-------------------------------------------------- |
---|
518 | bool Fragment::copy (FILE* out, |
---|
519 | const cmt_string& name, |
---|
520 | int variables, ...) |
---|
521 | { |
---|
522 | va_list ids; |
---|
523 | |
---|
524 | Fragment* fragment = Fragment::find (name); |
---|
525 | if (fragment == 0) return (false); |
---|
526 | |
---|
527 | va_start (ids, variables); |
---|
528 | bool result = fragment->copy (out, variables, ids); |
---|
529 | va_end (ids); |
---|
530 | |
---|
531 | return (result); |
---|
532 | } |
---|
533 | |
---|
534 | //-------------------------------------------------- |
---|
535 | bool Fragment::copy (cmt_string& out, |
---|
536 | const cmt_string& name, |
---|
537 | int variables, ...) |
---|
538 | { |
---|
539 | va_list ids; |
---|
540 | |
---|
541 | Fragment* fragment = Fragment::find (name); |
---|
542 | if (fragment == 0) return (false); |
---|
543 | |
---|
544 | va_start (ids, variables); |
---|
545 | bool result = fragment->copy (out, variables, ids); |
---|
546 | va_end (ids); |
---|
547 | |
---|
548 | return (result); |
---|
549 | } |
---|
550 | |
---|
551 | //-------------------------------------------------- |
---|
552 | bool Fragment::copy (FILE* out, int variables, ...) |
---|
553 | { |
---|
554 | va_list ids; |
---|
555 | |
---|
556 | va_start (ids, variables); |
---|
557 | bool result = copy (out, variables, ids); |
---|
558 | va_end (ids); |
---|
559 | |
---|
560 | return (result); |
---|
561 | } |
---|
562 | |
---|
563 | //-------------------------------------------------- |
---|
564 | bool Fragment::copy (cmt_string& out, int variables, ...) |
---|
565 | { |
---|
566 | va_list ids; |
---|
567 | |
---|
568 | va_start (ids, variables); |
---|
569 | bool result = copy (out, variables, ids); |
---|
570 | va_end (ids); |
---|
571 | |
---|
572 | return (result); |
---|
573 | } |
---|
574 | |
---|
575 | //-------------------------------------------------- |
---|
576 | bool Fragment::copy (FILE* out, int variables, va_list ids) |
---|
577 | { |
---|
578 | static cmt_string cline; |
---|
579 | |
---|
580 | bool result = copy (cline, variables, ids); |
---|
581 | if (result) |
---|
582 | { |
---|
583 | cline.write (out); |
---|
584 | return (true); |
---|
585 | } |
---|
586 | else |
---|
587 | { |
---|
588 | return (false); |
---|
589 | } |
---|
590 | } |
---|
591 | |
---|
592 | //-------------------------------------------------- |
---|
593 | bool Fragment::copy (cmt_string& out, int variables, va_list ids) |
---|
594 | { |
---|
595 | int i; |
---|
596 | |
---|
597 | if (!locate ()) return (false); |
---|
598 | |
---|
599 | out.read (path); |
---|
600 | |
---|
601 | Variable* var = 0; |
---|
602 | for (i = 0; i < variables; i++) |
---|
603 | { |
---|
604 | var = va_arg (ids, Variable*); |
---|
605 | out.replace_all (var->macro_braces (), var->value); |
---|
606 | out.replace_all (var->macro_pars (), var->value); |
---|
607 | } |
---|
608 | |
---|
609 | return (true); |
---|
610 | } |
---|
611 | |
---|
612 | //-------------------------------------------------- |
---|
613 | bool Fragment::wincopy (FILE* out, int variables, va_list ids) |
---|
614 | { |
---|
615 | static cmt_string cline; |
---|
616 | |
---|
617 | bool result = wincopy (cline, variables, ids); |
---|
618 | |
---|
619 | if (result) |
---|
620 | { |
---|
621 | cline.write (out); |
---|
622 | return (true); |
---|
623 | } |
---|
624 | else |
---|
625 | { |
---|
626 | return (false); |
---|
627 | } |
---|
628 | } |
---|
629 | |
---|
630 | //-------------------------------------------------- |
---|
631 | bool Fragment::wincopy (cmt_string& out, int variables, va_list ids) |
---|
632 | { |
---|
633 | int i; |
---|
634 | |
---|
635 | if (!locate ()) return (false); |
---|
636 | |
---|
637 | out.read (path); |
---|
638 | |
---|
639 | Variable* var = 0; |
---|
640 | for (i = 0; i < variables; i++) |
---|
641 | { |
---|
642 | var = va_arg (ids, Variable*); |
---|
643 | out.replace_all (var->macro_braces (), var->value); |
---|
644 | out.replace_all (var->macro_pars (), var->value); |
---|
645 | } |
---|
646 | |
---|
647 | cmt_string pattern; |
---|
648 | cmt_string macro_name; |
---|
649 | char end_pattern; |
---|
650 | |
---|
651 | int start = 0; |
---|
652 | |
---|
653 | for (;;) |
---|
654 | { |
---|
655 | // |
---|
656 | // Try and substitute all ${xxx} or $(xxx) patterns |
---|
657 | // using symbol values. |
---|
658 | // |
---|
659 | int par; |
---|
660 | int brace; |
---|
661 | int begin; |
---|
662 | |
---|
663 | par = out.find (start, "$("); |
---|
664 | brace = out.find (start, "${"); |
---|
665 | |
---|
666 | if (par == cmt_string::npos) |
---|
667 | { |
---|
668 | // No parentheses. Look for brace |
---|
669 | if (brace == cmt_string::npos) |
---|
670 | { |
---|
671 | // No pattern, finish the scan. |
---|
672 | break; |
---|
673 | } |
---|
674 | |
---|
675 | // Brace found |
---|
676 | end_pattern = '}'; |
---|
677 | begin = brace; |
---|
678 | } |
---|
679 | else |
---|
680 | { |
---|
681 | // Parenthese found. Look for closest from {par, brace} |
---|
682 | if ((brace == cmt_string::npos) || |
---|
683 | (brace > par)) |
---|
684 | { |
---|
685 | end_pattern = ')'; |
---|
686 | begin = par; |
---|
687 | } |
---|
688 | else |
---|
689 | { |
---|
690 | end_pattern = '}'; |
---|
691 | begin = brace; |
---|
692 | } |
---|
693 | } |
---|
694 | |
---|
695 | // Skip the pattern intro. |
---|
696 | start = begin + 2; |
---|
697 | |
---|
698 | int end; |
---|
699 | end = out.find (start, end_pattern); |
---|
700 | if (end == cmt_string::npos) |
---|
701 | { |
---|
702 | // The pattern is a fake one (no ending!) |
---|
703 | break; |
---|
704 | } |
---|
705 | |
---|
706 | // This should never happen... |
---|
707 | if (end < begin) break; |
---|
708 | |
---|
709 | // Extract the complete pattern |
---|
710 | out.substr (begin, end - begin + 1, pattern); |
---|
711 | |
---|
712 | // Then only the macro name |
---|
713 | out.substr (begin + 2, end - begin - 2, macro_name); |
---|
714 | |
---|
715 | if (macro_name == "CFG") |
---|
716 | { |
---|
717 | // This is a Windows reserved keyword... |
---|
718 | start = end + 1; |
---|
719 | } |
---|
720 | else |
---|
721 | { |
---|
722 | Symbol* macro = Symbol::find (macro_name); |
---|
723 | if (macro != 0) |
---|
724 | { |
---|
725 | // Macro found |
---|
726 | cmt_string value = macro->resolve_macro_value (); |
---|
727 | |
---|
728 | out.replace_all (pattern, value); |
---|
729 | |
---|
730 | // The substitution will restart from the same place |
---|
731 | // allowing for recursive replacements |
---|
732 | start = begin; |
---|
733 | } |
---|
734 | else |
---|
735 | { |
---|
736 | // Macro not found. Look for env. variable |
---|
737 | cmt_string value = CmtSystem::getenv (macro_name); |
---|
738 | |
---|
739 | out.replace_all (pattern, value); |
---|
740 | |
---|
741 | // The substitution will restart from the same place |
---|
742 | // allowing for recursive replacements |
---|
743 | start = begin; |
---|
744 | } |
---|
745 | } |
---|
746 | } |
---|
747 | |
---|
748 | // Uniformly install CR-LF doublets. |
---|
749 | |
---|
750 | out.replace_all ("\r\n", "\n"); |
---|
751 | out.replace_all ("\n", "\r\n"); |
---|
752 | |
---|
753 | return (true); |
---|
754 | } |
---|
755 | |
---|
756 | |
---|
757 | |
---|
758 | |
---|
759 | |
---|
760 | |
---|
761 | |
---|
762 | |
---|
763 | //-------------------------------------------------- |
---|
764 | bool Fragment::copy (FILE* out, |
---|
765 | const cmt_string& name, |
---|
766 | const Variable::VariableVector& vector, |
---|
767 | int variables, ...) |
---|
768 | { |
---|
769 | va_list ids; |
---|
770 | |
---|
771 | Fragment* fragment = Fragment::find (name); |
---|
772 | if (fragment == 0) return (false); |
---|
773 | |
---|
774 | va_start (ids, variables); |
---|
775 | bool result = fragment->copy (out, vector, variables, ids); |
---|
776 | va_end (ids); |
---|
777 | |
---|
778 | return (result); |
---|
779 | } |
---|
780 | |
---|
781 | //-------------------------------------------------- |
---|
782 | bool Fragment::copy (cmt_string& out, |
---|
783 | const cmt_string& name, |
---|
784 | const Variable::VariableVector& vector, |
---|
785 | int variables, ...) |
---|
786 | { |
---|
787 | va_list ids; |
---|
788 | |
---|
789 | Fragment* fragment = Fragment::find (name); |
---|
790 | if (fragment == 0) return (false); |
---|
791 | |
---|
792 | va_start (ids, variables); |
---|
793 | bool result = fragment->copy (out, vector, variables, ids); |
---|
794 | va_end (ids); |
---|
795 | |
---|
796 | return (result); |
---|
797 | } |
---|
798 | |
---|
799 | //-------------------------------------------------- |
---|
800 | bool Fragment::copy (FILE* out, const Variable::VariableVector& vector, int variables, ...) |
---|
801 | { |
---|
802 | va_list ids; |
---|
803 | |
---|
804 | va_start (ids, variables); |
---|
805 | bool result = copy (out, vector, variables, ids); |
---|
806 | va_end (ids); |
---|
807 | |
---|
808 | return (result); |
---|
809 | } |
---|
810 | |
---|
811 | //-------------------------------------------------- |
---|
812 | bool Fragment::copy (cmt_string& out, const Variable::VariableVector& vector, int variables, ...) |
---|
813 | { |
---|
814 | va_list ids; |
---|
815 | |
---|
816 | va_start (ids, variables); |
---|
817 | bool result = copy (out, vector, variables, ids); |
---|
818 | va_end (ids); |
---|
819 | |
---|
820 | return (result); |
---|
821 | } |
---|
822 | |
---|
823 | //-------------------------------------------------- |
---|
824 | bool Fragment::copy (FILE* out, const Variable::VariableVector& vector, int variables, va_list ids) |
---|
825 | { |
---|
826 | static cmt_string cline; |
---|
827 | |
---|
828 | bool result = copy (cline, vector, variables, ids); |
---|
829 | if (result) |
---|
830 | { |
---|
831 | cline.write (out); |
---|
832 | return (true); |
---|
833 | } |
---|
834 | else |
---|
835 | { |
---|
836 | return (false); |
---|
837 | } |
---|
838 | } |
---|
839 | |
---|
840 | //-------------------------------------------------- |
---|
841 | bool Fragment::copy (cmt_string& out, const Variable::VariableVector& vector, int variables, va_list ids) |
---|
842 | { |
---|
843 | int i; |
---|
844 | |
---|
845 | if (!locate ()) return (false); |
---|
846 | |
---|
847 | out.read (path); |
---|
848 | |
---|
849 | Variable* var = 0; |
---|
850 | |
---|
851 | for (i = 0; i < vector.size (); i++) |
---|
852 | { |
---|
853 | var = &(vector[i]); |
---|
854 | out.replace_all (var->macro_braces (), var->value); |
---|
855 | out.replace_all (var->macro_pars (), var->value); |
---|
856 | } |
---|
857 | |
---|
858 | for (i = 0; i < variables; i++) |
---|
859 | { |
---|
860 | var = va_arg (ids, Variable*); |
---|
861 | out.replace_all (var->macro_braces (), var->value); |
---|
862 | out.replace_all (var->macro_pars (), var->value); |
---|
863 | } |
---|
864 | |
---|
865 | return (true); |
---|
866 | } |
---|
867 | |
---|
868 | //-------------------------------------------------- |
---|
869 | bool Fragment::wincopy (FILE* out, const Variable::VariableVector& vector, int variables, va_list ids) |
---|
870 | { |
---|
871 | static cmt_string cline; |
---|
872 | |
---|
873 | bool result = wincopy (cline, vector, variables, ids); |
---|
874 | if (result) |
---|
875 | { |
---|
876 | cline.write (out); |
---|
877 | return (true); |
---|
878 | } |
---|
879 | else |
---|
880 | { |
---|
881 | return (false); |
---|
882 | } |
---|
883 | } |
---|
884 | |
---|
885 | //-------------------------------------------------- |
---|
886 | bool Fragment::wincopy (cmt_string& out, const Variable::VariableVector& vector, |
---|
887 | int variables, va_list ids) |
---|
888 | { |
---|
889 | int i; |
---|
890 | |
---|
891 | if (!locate ()) return (false); |
---|
892 | |
---|
893 | out.read (path); |
---|
894 | |
---|
895 | Variable* var = 0; |
---|
896 | |
---|
897 | for (i = 0; i < vector.size (); i++) |
---|
898 | { |
---|
899 | var = &(vector[i]); |
---|
900 | out.replace_all (var->macro_braces (), var->value); |
---|
901 | out.replace_all (var->macro_pars (), var->value); |
---|
902 | } |
---|
903 | |
---|
904 | for (i = 0; i < variables; i++) |
---|
905 | { |
---|
906 | var = va_arg (ids, Variable*); |
---|
907 | out.replace_all (var->macro_braces (), var->value); |
---|
908 | out.replace_all (var->macro_pars (), var->value); |
---|
909 | } |
---|
910 | |
---|
911 | cmt_string pattern; |
---|
912 | cmt_string macro_name; |
---|
913 | char end_pattern; |
---|
914 | |
---|
915 | int start = 0; |
---|
916 | |
---|
917 | for (;;) |
---|
918 | { |
---|
919 | // |
---|
920 | // Try and substitute all ${xxx} or $(xxx) patterns |
---|
921 | // using symbol values. |
---|
922 | // |
---|
923 | int par; |
---|
924 | int brace; |
---|
925 | int begin; |
---|
926 | |
---|
927 | par = out.find (start, "$("); |
---|
928 | brace = out.find (start, "${"); |
---|
929 | |
---|
930 | if (par == cmt_string::npos) |
---|
931 | { |
---|
932 | // No parentheses. Look for brace |
---|
933 | if (brace == cmt_string::npos) |
---|
934 | { |
---|
935 | // No pattern, finish the scan. |
---|
936 | break; |
---|
937 | } |
---|
938 | |
---|
939 | // Brace found |
---|
940 | end_pattern = '}'; |
---|
941 | begin = brace; |
---|
942 | } |
---|
943 | else |
---|
944 | { |
---|
945 | // Parenthese found. Look for closest from {par, brace} |
---|
946 | if ((brace == cmt_string::npos) || |
---|
947 | (brace > par)) |
---|
948 | { |
---|
949 | end_pattern = ')'; |
---|
950 | begin = par; |
---|
951 | } |
---|
952 | else |
---|
953 | { |
---|
954 | end_pattern = '}'; |
---|
955 | begin = brace; |
---|
956 | } |
---|
957 | } |
---|
958 | |
---|
959 | // Skip the pattern intro. |
---|
960 | start = begin + 2; |
---|
961 | |
---|
962 | int end; |
---|
963 | end = out.find (start, end_pattern); |
---|
964 | if (end == cmt_string::npos) |
---|
965 | { |
---|
966 | // The pattern is a fake one (no ending!) |
---|
967 | break; |
---|
968 | } |
---|
969 | |
---|
970 | // This should never happen... |
---|
971 | if (end < begin) break; |
---|
972 | |
---|
973 | // Extract the complete pattern |
---|
974 | out.substr (begin, end - begin + 1, pattern); |
---|
975 | |
---|
976 | // Then only the macro name |
---|
977 | out.substr (begin + 2, end - begin - 2, macro_name); |
---|
978 | |
---|
979 | if (macro_name == "CFG") |
---|
980 | { |
---|
981 | // This is a Windows reserved keyword... |
---|
982 | start = end + 1; |
---|
983 | } |
---|
984 | else |
---|
985 | { |
---|
986 | Symbol* macro = Symbol::find (macro_name); |
---|
987 | if (macro != 0) |
---|
988 | { |
---|
989 | // Macro found |
---|
990 | cmt_string value = macro->resolve_macro_value (); |
---|
991 | out.replace_all (pattern, value); |
---|
992 | |
---|
993 | // The substitution will restart from the same place |
---|
994 | // allowing for recursive replacements |
---|
995 | start = begin; |
---|
996 | } |
---|
997 | else |
---|
998 | { |
---|
999 | // Macro not found. Look for env. variable |
---|
1000 | cmt_string value = CmtSystem::getenv (macro_name); |
---|
1001 | out.replace_all (pattern, value); |
---|
1002 | |
---|
1003 | // The substitution will restart from the same place |
---|
1004 | // allowing for recursive replacements |
---|
1005 | start = begin; |
---|
1006 | } |
---|
1007 | } |
---|
1008 | } |
---|
1009 | |
---|
1010 | // Uniformly install CR-LF doublets. |
---|
1011 | |
---|
1012 | out.replace_all ("\r\n", "\n"); |
---|
1013 | out.replace_all ("\n", "\r\n"); |
---|
1014 | |
---|
1015 | return (true); |
---|
1016 | } |
---|
1017 | |
---|
1018 | //-------------------------------------------------- |
---|
1019 | FragmentHandle::FragmentHandle () |
---|
1020 | { |
---|
1021 | _fragment = 0; |
---|
1022 | _initialized = false; |
---|
1023 | } |
---|
1024 | |
---|
1025 | //-------------------------------------------------- |
---|
1026 | FragmentHandle::FragmentHandle (const cmt_string name) : _name(name) |
---|
1027 | { |
---|
1028 | _fragment = 0; |
---|
1029 | _initialized = false; |
---|
1030 | } |
---|
1031 | |
---|
1032 | //-------------------------------------------------- |
---|
1033 | FragmentHandle& FragmentHandle::operator = (const FragmentHandle& other) |
---|
1034 | { |
---|
1035 | _name = other._name; |
---|
1036 | _fragment = 0; |
---|
1037 | _initialized = false; |
---|
1038 | |
---|
1039 | return (*this); |
---|
1040 | } |
---|
1041 | |
---|
1042 | //-------------------------------------------------- |
---|
1043 | void FragmentHandle::reset () |
---|
1044 | { |
---|
1045 | _fragment = 0; |
---|
1046 | _initialized = false; |
---|
1047 | } |
---|
1048 | |
---|
1049 | //-------------------------------------------------- |
---|
1050 | void FragmentHandle::set (const cmt_string name) |
---|
1051 | { |
---|
1052 | _name = name; |
---|
1053 | _fragment = 0; |
---|
1054 | _initialized = false; |
---|
1055 | } |
---|
1056 | |
---|
1057 | //-------------------------------------------------- |
---|
1058 | cmt_string& FragmentHandle::name () |
---|
1059 | { |
---|
1060 | static cmt_string null_string; |
---|
1061 | |
---|
1062 | if (!setup ()) return (null_string); |
---|
1063 | |
---|
1064 | return (_fragment->name); |
---|
1065 | } |
---|
1066 | |
---|
1067 | //-------------------------------------------------- |
---|
1068 | cmt_string& FragmentHandle::suffix () |
---|
1069 | { |
---|
1070 | static cmt_string null_string; |
---|
1071 | |
---|
1072 | if (!setup ()) return (null_string); |
---|
1073 | |
---|
1074 | return (_fragment->suffix); |
---|
1075 | } |
---|
1076 | |
---|
1077 | //-------------------------------------------------- |
---|
1078 | cmt_string& FragmentHandle::header () |
---|
1079 | { |
---|
1080 | static cmt_string null_string; |
---|
1081 | |
---|
1082 | if (!setup ()) return (null_string); |
---|
1083 | |
---|
1084 | return (_fragment->header); |
---|
1085 | } |
---|
1086 | |
---|
1087 | //-------------------------------------------------- |
---|
1088 | cmt_string& FragmentHandle::trailer () |
---|
1089 | { |
---|
1090 | static cmt_string null_string; |
---|
1091 | |
---|
1092 | if (!setup ()) return (null_string); |
---|
1093 | |
---|
1094 | return (_fragment->trailer); |
---|
1095 | } |
---|
1096 | |
---|
1097 | //-------------------------------------------------- |
---|
1098 | bool FragmentHandle::need_dependencies () |
---|
1099 | { |
---|
1100 | if (!setup ()) return (false); |
---|
1101 | |
---|
1102 | return (_fragment->need_dependencies); |
---|
1103 | } |
---|
1104 | |
---|
1105 | //-------------------------------------------------- |
---|
1106 | bool FragmentHandle::copy (FILE* out, int variables, ...) |
---|
1107 | { |
---|
1108 | if (!setup ()) return (false); |
---|
1109 | |
---|
1110 | va_list ids; |
---|
1111 | |
---|
1112 | va_start (ids, variables); |
---|
1113 | bool result = _fragment->copy (out, variables, ids); |
---|
1114 | va_end (ids); |
---|
1115 | |
---|
1116 | if (!result) |
---|
1117 | { |
---|
1118 | CmtMessage::info ("Fragment " + _name + " not found"); |
---|
1119 | // cerr << "#CMT> Fragment " << _name << " not found" << endl; |
---|
1120 | _fragment = 0; |
---|
1121 | } |
---|
1122 | |
---|
1123 | return (result); |
---|
1124 | } |
---|
1125 | |
---|
1126 | //-------------------------------------------------- |
---|
1127 | bool FragmentHandle::copy (cmt_string& out, int variables, ...) |
---|
1128 | { |
---|
1129 | if (!setup ()) return (false); |
---|
1130 | |
---|
1131 | va_list ids; |
---|
1132 | |
---|
1133 | va_start (ids, variables); |
---|
1134 | bool result = _fragment->copy (out, variables, ids); |
---|
1135 | va_end (ids); |
---|
1136 | |
---|
1137 | if (!result) |
---|
1138 | { |
---|
1139 | CmtMessage::info ("Fragment " + _name + " not found"); |
---|
1140 | // cerr << "#CMT> Fragment " << _name << " not found" << endl; |
---|
1141 | _fragment = 0; |
---|
1142 | } |
---|
1143 | |
---|
1144 | return (result); |
---|
1145 | } |
---|
1146 | |
---|
1147 | //-------------------------------------------------- |
---|
1148 | bool FragmentHandle::wincopy (FILE* out, int variables, ...) |
---|
1149 | { |
---|
1150 | if (!setup ()) return (false); |
---|
1151 | |
---|
1152 | va_list ids; |
---|
1153 | |
---|
1154 | va_start (ids, variables); |
---|
1155 | bool result = _fragment->wincopy (out, variables, ids); |
---|
1156 | va_end (ids); |
---|
1157 | |
---|
1158 | if (!result) |
---|
1159 | { |
---|
1160 | CmtMessage::info ("Fragment " + _name + " not found"); |
---|
1161 | // cerr << "#CMT> Fragment " << _name << " not found" << endl; |
---|
1162 | _fragment = 0; |
---|
1163 | } |
---|
1164 | |
---|
1165 | return (result); |
---|
1166 | } |
---|
1167 | |
---|
1168 | //-------------------------------------------------- |
---|
1169 | bool FragmentHandle::wincopy (cmt_string& out, int variables, ...) |
---|
1170 | { |
---|
1171 | if (!setup ()) return (false); |
---|
1172 | |
---|
1173 | va_list ids; |
---|
1174 | |
---|
1175 | va_start (ids, variables); |
---|
1176 | bool result = _fragment->wincopy (out, variables, ids); |
---|
1177 | va_end (ids); |
---|
1178 | |
---|
1179 | if (!result) |
---|
1180 | { |
---|
1181 | CmtMessage::info ("Fragment " + _name + " not found"); |
---|
1182 | // cerr << "#CMT> Fragment " << _name << " not found" << endl; |
---|
1183 | _fragment = 0; |
---|
1184 | } |
---|
1185 | |
---|
1186 | return (result); |
---|
1187 | } |
---|
1188 | |
---|
1189 | |
---|
1190 | |
---|
1191 | |
---|
1192 | //-------------------------------------------------- |
---|
1193 | bool FragmentHandle::copy (FILE* out, const Variable::VariableVector& vector, int variables, ...) |
---|
1194 | { |
---|
1195 | if (!setup ()) return (false); |
---|
1196 | |
---|
1197 | va_list ids; |
---|
1198 | |
---|
1199 | va_start (ids, variables); |
---|
1200 | bool result = _fragment->copy (out, vector, variables, ids); |
---|
1201 | va_end (ids); |
---|
1202 | |
---|
1203 | if (!result) |
---|
1204 | { |
---|
1205 | CmtMessage::info ("Fragment " + _name + " not found"); |
---|
1206 | // cerr << "#CMT> Fragment " << _name << " not found" << endl; |
---|
1207 | _fragment = 0; |
---|
1208 | } |
---|
1209 | |
---|
1210 | return (result); |
---|
1211 | } |
---|
1212 | |
---|
1213 | //-------------------------------------------------- |
---|
1214 | bool FragmentHandle::copy (cmt_string& out, const Variable::VariableVector& vector, int variables, ...) |
---|
1215 | { |
---|
1216 | if (!setup ()) return (false); |
---|
1217 | |
---|
1218 | va_list ids; |
---|
1219 | |
---|
1220 | va_start (ids, variables); |
---|
1221 | bool result = _fragment->copy (out, vector, variables, ids); |
---|
1222 | va_end (ids); |
---|
1223 | |
---|
1224 | if (!result) |
---|
1225 | { |
---|
1226 | CmtMessage::info ("Fragment " + _name + " not found"); |
---|
1227 | // cerr << "#CMT> Fragment " << _name << " not found" << endl; |
---|
1228 | _fragment = 0; |
---|
1229 | } |
---|
1230 | |
---|
1231 | return (result); |
---|
1232 | } |
---|
1233 | |
---|
1234 | //-------------------------------------------------- |
---|
1235 | bool FragmentHandle::wincopy (FILE* out, const Variable::VariableVector& vector, int variables, ...) |
---|
1236 | { |
---|
1237 | if (!setup ()) return (false); |
---|
1238 | |
---|
1239 | va_list ids; |
---|
1240 | |
---|
1241 | va_start (ids, variables); |
---|
1242 | bool result = _fragment->wincopy (out, vector, variables, ids); |
---|
1243 | va_end (ids); |
---|
1244 | |
---|
1245 | if (!result) |
---|
1246 | { |
---|
1247 | CmtMessage::info ("Fragment " + _name + " not found"); |
---|
1248 | // cerr << "#CMT> Fragment " << _name << " not found" << endl; |
---|
1249 | _fragment = 0; |
---|
1250 | } |
---|
1251 | |
---|
1252 | return (result); |
---|
1253 | } |
---|
1254 | |
---|
1255 | //-------------------------------------------------- |
---|
1256 | bool FragmentHandle::wincopy (cmt_string& out, |
---|
1257 | const Variable::VariableVector& vector, |
---|
1258 | int variables, ...) |
---|
1259 | { |
---|
1260 | if (!setup ()) return (false); |
---|
1261 | |
---|
1262 | va_list ids; |
---|
1263 | |
---|
1264 | va_start (ids, variables); |
---|
1265 | bool result = _fragment->wincopy (out, vector, variables, ids); |
---|
1266 | va_end (ids); |
---|
1267 | |
---|
1268 | if (!result) |
---|
1269 | { |
---|
1270 | CmtMessage::info ("Fragment " + _name + " not found"); |
---|
1271 | // cerr << "#CMT> Fragment " << _name << " not found" << endl; |
---|
1272 | _fragment = 0; |
---|
1273 | } |
---|
1274 | |
---|
1275 | return (result); |
---|
1276 | } |
---|
1277 | |
---|
1278 | //-------------------------------------------------- |
---|
1279 | bool FragmentHandle::setup () |
---|
1280 | { |
---|
1281 | if (!_initialized) |
---|
1282 | { |
---|
1283 | _initialized = true; |
---|
1284 | |
---|
1285 | _fragment = Fragment::find (_name); |
---|
1286 | if (_fragment == 0) |
---|
1287 | { |
---|
1288 | CmtMessage::info ("Fragment " + _name + " not found"); |
---|
1289 | // cerr << "#CMT> Fragment " << _name << " not found" << endl; |
---|
1290 | } |
---|
1291 | } |
---|
1292 | |
---|
1293 | if (_fragment == 0) |
---|
1294 | { |
---|
1295 | return (false); |
---|
1296 | } |
---|
1297 | else |
---|
1298 | { |
---|
1299 | return (true); |
---|
1300 | } |
---|
1301 | } |
---|