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