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