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 | cmt_string pattern = use->real_path; |
---|
356 | pattern += CmtSystem::file_separator (); |
---|
357 | pattern += use->package; |
---|
358 | pattern += CmtSystem::file_separator (); |
---|
359 | pattern += use->version; |
---|
360 | |
---|
361 | cmt_string replacement = "${"; |
---|
362 | replacement += use->prefix; |
---|
363 | replacement += "ROOT}"; |
---|
364 | |
---|
365 | the_path.replace (pattern, replacement); |
---|
366 | |
---|
367 | cout << the_path; |
---|
368 | |
---|
369 | if (suffix != "") |
---|
370 | { |
---|
371 | cout << "->" << suffix; |
---|
372 | } |
---|
373 | |
---|
374 | cout << endl; |
---|
375 | |
---|
376 | return (result); |
---|
377 | } |
---|
378 | |
---|
379 | /*----------------------------------------------------------*/ |
---|
380 | bool Fragment::locate () |
---|
381 | { |
---|
382 | if (path == "") |
---|
383 | { |
---|
384 | if (use == 0) |
---|
385 | { |
---|
386 | // Assume CMT |
---|
387 | use = Use::find ("CMT"); |
---|
388 | } |
---|
389 | |
---|
390 | path = use->real_path; |
---|
391 | path += CmtSystem::file_separator (); |
---|
392 | path += use->package; |
---|
393 | path += CmtSystem::file_separator (); |
---|
394 | path += use->version; |
---|
395 | path += CmtSystem::file_separator (); |
---|
396 | path += "fragments"; |
---|
397 | path += CmtSystem::file_separator (); |
---|
398 | |
---|
399 | if (Cmt::build_nmake) |
---|
400 | { |
---|
401 | path += "nmake"; |
---|
402 | path += CmtSystem::file_separator (); |
---|
403 | } |
---|
404 | |
---|
405 | path += name; |
---|
406 | } |
---|
407 | |
---|
408 | if (CmtSystem::test_file (path)) return (true); |
---|
409 | |
---|
410 | path = use->real_path; |
---|
411 | path += CmtSystem::file_separator (); |
---|
412 | path += use->package; |
---|
413 | path += CmtSystem::file_separator (); |
---|
414 | path += use->version; |
---|
415 | path += CmtSystem::file_separator (); |
---|
416 | path += "fragments"; |
---|
417 | path += CmtSystem::file_separator (); |
---|
418 | path += name; |
---|
419 | |
---|
420 | if (CmtSystem::test_file (path)) return (true); |
---|
421 | |
---|
422 | path = use->real_path; |
---|
423 | path += CmtSystem::file_separator (); |
---|
424 | path += use->package; |
---|
425 | path += CmtSystem::file_separator (); |
---|
426 | path += use->version; |
---|
427 | path += CmtSystem::file_separator (); |
---|
428 | |
---|
429 | if (use->style == mgr_style) path += "mgr"; |
---|
430 | else path += "cmt"; |
---|
431 | |
---|
432 | path += CmtSystem::file_separator (); |
---|
433 | path += "fragments"; |
---|
434 | path += CmtSystem::file_separator (); |
---|
435 | |
---|
436 | if (Cmt::build_nmake) |
---|
437 | { |
---|
438 | path += "nmake"; |
---|
439 | path += CmtSystem::file_separator (); |
---|
440 | } |
---|
441 | |
---|
442 | path += name; |
---|
443 | |
---|
444 | if (CmtSystem::test_file (path)) return (true); |
---|
445 | |
---|
446 | path = use->real_path; |
---|
447 | path += CmtSystem::file_separator (); |
---|
448 | path += use->package; |
---|
449 | path += CmtSystem::file_separator (); |
---|
450 | path += use->version; |
---|
451 | path += CmtSystem::file_separator (); |
---|
452 | |
---|
453 | if (use->style == mgr_style) path += "mgr"; |
---|
454 | else path += "cmt"; |
---|
455 | |
---|
456 | path += CmtSystem::file_separator (); |
---|
457 | path += "fragments"; |
---|
458 | path += CmtSystem::file_separator (); |
---|
459 | |
---|
460 | path += name; |
---|
461 | |
---|
462 | if (CmtSystem::test_file (path)) return (true); |
---|
463 | |
---|
464 | return (false); |
---|
465 | } |
---|
466 | |
---|
467 | //-------------------------------------------------- |
---|
468 | bool Fragment::copy (FILE* out, |
---|
469 | const cmt_string& name, |
---|
470 | int variables, ...) |
---|
471 | { |
---|
472 | va_list ids; |
---|
473 | |
---|
474 | Fragment* fragment = Fragment::find (name); |
---|
475 | if (fragment == 0) return (false); |
---|
476 | |
---|
477 | va_start (ids, variables); |
---|
478 | bool result = fragment->copy (out, variables, ids); |
---|
479 | va_end (ids); |
---|
480 | |
---|
481 | return (result); |
---|
482 | } |
---|
483 | |
---|
484 | //-------------------------------------------------- |
---|
485 | bool Fragment::copy (cmt_string& out, |
---|
486 | const cmt_string& name, |
---|
487 | int variables, ...) |
---|
488 | { |
---|
489 | va_list ids; |
---|
490 | |
---|
491 | Fragment* fragment = Fragment::find (name); |
---|
492 | if (fragment == 0) return (false); |
---|
493 | |
---|
494 | va_start (ids, variables); |
---|
495 | bool result = fragment->copy (out, variables, ids); |
---|
496 | va_end (ids); |
---|
497 | |
---|
498 | return (result); |
---|
499 | } |
---|
500 | |
---|
501 | //-------------------------------------------------- |
---|
502 | bool Fragment::copy (FILE* out, int variables, ...) |
---|
503 | { |
---|
504 | va_list ids; |
---|
505 | |
---|
506 | va_start (ids, variables); |
---|
507 | bool result = copy (out, variables, ids); |
---|
508 | va_end (ids); |
---|
509 | |
---|
510 | return (result); |
---|
511 | } |
---|
512 | |
---|
513 | //-------------------------------------------------- |
---|
514 | bool Fragment::copy (cmt_string& out, int variables, ...) |
---|
515 | { |
---|
516 | va_list ids; |
---|
517 | |
---|
518 | va_start (ids, variables); |
---|
519 | bool result = copy (out, variables, ids); |
---|
520 | va_end (ids); |
---|
521 | |
---|
522 | return (result); |
---|
523 | } |
---|
524 | |
---|
525 | //-------------------------------------------------- |
---|
526 | bool Fragment::copy (FILE* out, int variables, va_list ids) |
---|
527 | { |
---|
528 | static cmt_string cline; |
---|
529 | |
---|
530 | bool result = copy (cline, variables, ids); |
---|
531 | if (result) |
---|
532 | { |
---|
533 | cline.write (out); |
---|
534 | return (true); |
---|
535 | } |
---|
536 | else |
---|
537 | { |
---|
538 | return (false); |
---|
539 | } |
---|
540 | } |
---|
541 | |
---|
542 | //-------------------------------------------------- |
---|
543 | bool Fragment::copy (cmt_string& out, int variables, va_list ids) |
---|
544 | { |
---|
545 | int i; |
---|
546 | |
---|
547 | if (!locate ()) return (false); |
---|
548 | |
---|
549 | out.read (path); |
---|
550 | |
---|
551 | Variable* var = 0; |
---|
552 | for (i = 0; i < variables; i++) |
---|
553 | { |
---|
554 | var = va_arg (ids, Variable*); |
---|
555 | out.replace_all (var->macro_braces (), var->value); |
---|
556 | out.replace_all (var->macro_pars (), var->value); |
---|
557 | } |
---|
558 | |
---|
559 | return (true); |
---|
560 | } |
---|
561 | |
---|
562 | //-------------------------------------------------- |
---|
563 | bool Fragment::wincopy (FILE* out, int variables, va_list ids) |
---|
564 | { |
---|
565 | static cmt_string cline; |
---|
566 | |
---|
567 | bool result = wincopy (cline, variables, ids); |
---|
568 | |
---|
569 | if (result) |
---|
570 | { |
---|
571 | cline.write (out); |
---|
572 | return (true); |
---|
573 | } |
---|
574 | else |
---|
575 | { |
---|
576 | return (false); |
---|
577 | } |
---|
578 | } |
---|
579 | |
---|
580 | //-------------------------------------------------- |
---|
581 | bool Fragment::wincopy (cmt_string& out, int variables, va_list ids) |
---|
582 | { |
---|
583 | int i; |
---|
584 | |
---|
585 | if (!locate ()) return (false); |
---|
586 | |
---|
587 | out.read (path); |
---|
588 | |
---|
589 | Variable* var = 0; |
---|
590 | for (i = 0; i < variables; i++) |
---|
591 | { |
---|
592 | var = va_arg (ids, Variable*); |
---|
593 | out.replace_all (var->macro_braces (), var->value); |
---|
594 | out.replace_all (var->macro_pars (), var->value); |
---|
595 | } |
---|
596 | |
---|
597 | cmt_string pattern; |
---|
598 | cmt_string macro_name; |
---|
599 | char end_pattern; |
---|
600 | |
---|
601 | int start = 0; |
---|
602 | |
---|
603 | for (;;) |
---|
604 | { |
---|
605 | // |
---|
606 | // Try and substitute all ${xxx} or $(xxx) patterns |
---|
607 | // using symbol values. |
---|
608 | // |
---|
609 | int par; |
---|
610 | int brace; |
---|
611 | int begin; |
---|
612 | |
---|
613 | par = out.find (start, "$("); |
---|
614 | brace = out.find (start, "${"); |
---|
615 | |
---|
616 | if (par == cmt_string::npos) |
---|
617 | { |
---|
618 | // No parentheses. Look for brace |
---|
619 | if (brace == cmt_string::npos) |
---|
620 | { |
---|
621 | // No pattern, finish the scan. |
---|
622 | break; |
---|
623 | } |
---|
624 | |
---|
625 | // Brace found |
---|
626 | end_pattern = '}'; |
---|
627 | begin = brace; |
---|
628 | } |
---|
629 | else |
---|
630 | { |
---|
631 | // Parenthese found. Look for closest from {par, brace} |
---|
632 | if ((brace == cmt_string::npos) || |
---|
633 | (brace > par)) |
---|
634 | { |
---|
635 | end_pattern = ')'; |
---|
636 | begin = par; |
---|
637 | } |
---|
638 | else |
---|
639 | { |
---|
640 | end_pattern = '}'; |
---|
641 | begin = brace; |
---|
642 | } |
---|
643 | } |
---|
644 | |
---|
645 | // Skip the pattern intro. |
---|
646 | start = begin + 2; |
---|
647 | |
---|
648 | int end; |
---|
649 | end = out.find (start, end_pattern); |
---|
650 | if (end == cmt_string::npos) |
---|
651 | { |
---|
652 | // The pattern is a fake one (no ending!) |
---|
653 | break; |
---|
654 | } |
---|
655 | |
---|
656 | // This should never happen... |
---|
657 | if (end < begin) break; |
---|
658 | |
---|
659 | // Extract the complete pattern |
---|
660 | out.substr (begin, end - begin + 1, pattern); |
---|
661 | |
---|
662 | // Then only the macro name |
---|
663 | out.substr (begin + 2, end - begin - 2, macro_name); |
---|
664 | |
---|
665 | if (macro_name == "CFG") |
---|
666 | { |
---|
667 | // This is a Windows reserved keyword... |
---|
668 | start = end + 1; |
---|
669 | } |
---|
670 | else |
---|
671 | { |
---|
672 | Symbol* macro = Symbol::find (macro_name); |
---|
673 | if (macro != 0) |
---|
674 | { |
---|
675 | // Macro found |
---|
676 | cmt_string value = macro->resolve_macro_value (); |
---|
677 | //cout << "resolve_macro_value2> value=" << value << endl; |
---|
678 | out.replace_all (pattern, value); |
---|
679 | |
---|
680 | // The substitution will restart from the same place |
---|
681 | // allowing for recursive replacements |
---|
682 | start = begin; |
---|
683 | } |
---|
684 | else |
---|
685 | { |
---|
686 | // Macro not found. Look for env. variable |
---|
687 | cmt_string value = CmtSystem::getenv (macro_name); |
---|
688 | //cout << "resolve_macro_value3> " << macro_name << "=" << value << endl; |
---|
689 | out.replace_all (pattern, value); |
---|
690 | |
---|
691 | // The substitution will restart from the same place |
---|
692 | // allowing for recursive replacements |
---|
693 | start = begin; |
---|
694 | } |
---|
695 | } |
---|
696 | } |
---|
697 | |
---|
698 | // Uniformly install CR-LF doublets. |
---|
699 | |
---|
700 | out.replace_all ("\r\n", "\n"); |
---|
701 | out.replace_all ("\n", "\r\n"); |
---|
702 | |
---|
703 | return (true); |
---|
704 | } |
---|
705 | |
---|
706 | |
---|
707 | |
---|
708 | |
---|
709 | |
---|
710 | |
---|
711 | |
---|
712 | |
---|
713 | //-------------------------------------------------- |
---|
714 | bool Fragment::copy (FILE* out, |
---|
715 | const cmt_string& name, |
---|
716 | const Variable::VariableVector& vector, |
---|
717 | int variables, ...) |
---|
718 | { |
---|
719 | va_list ids; |
---|
720 | |
---|
721 | Fragment* fragment = Fragment::find (name); |
---|
722 | if (fragment == 0) return (false); |
---|
723 | |
---|
724 | va_start (ids, variables); |
---|
725 | bool result = fragment->copy (out, vector, variables, ids); |
---|
726 | va_end (ids); |
---|
727 | |
---|
728 | return (result); |
---|
729 | } |
---|
730 | |
---|
731 | //-------------------------------------------------- |
---|
732 | bool Fragment::copy (cmt_string& out, |
---|
733 | const cmt_string& name, |
---|
734 | const Variable::VariableVector& vector, |
---|
735 | int variables, ...) |
---|
736 | { |
---|
737 | va_list ids; |
---|
738 | |
---|
739 | Fragment* fragment = Fragment::find (name); |
---|
740 | if (fragment == 0) return (false); |
---|
741 | |
---|
742 | va_start (ids, variables); |
---|
743 | bool result = fragment->copy (out, vector, variables, ids); |
---|
744 | va_end (ids); |
---|
745 | |
---|
746 | return (result); |
---|
747 | } |
---|
748 | |
---|
749 | //-------------------------------------------------- |
---|
750 | bool Fragment::copy (FILE* out, const Variable::VariableVector& vector, int variables, ...) |
---|
751 | { |
---|
752 | va_list ids; |
---|
753 | |
---|
754 | va_start (ids, variables); |
---|
755 | bool result = copy (out, vector, variables, ids); |
---|
756 | va_end (ids); |
---|
757 | |
---|
758 | return (result); |
---|
759 | } |
---|
760 | |
---|
761 | //-------------------------------------------------- |
---|
762 | bool Fragment::copy (cmt_string& out, const Variable::VariableVector& vector, int variables, ...) |
---|
763 | { |
---|
764 | va_list ids; |
---|
765 | |
---|
766 | va_start (ids, variables); |
---|
767 | bool result = copy (out, vector, variables, ids); |
---|
768 | va_end (ids); |
---|
769 | |
---|
770 | return (result); |
---|
771 | } |
---|
772 | |
---|
773 | //-------------------------------------------------- |
---|
774 | bool Fragment::copy (FILE* out, const Variable::VariableVector& vector, int variables, va_list ids) |
---|
775 | { |
---|
776 | static cmt_string cline; |
---|
777 | |
---|
778 | bool result = copy (cline, vector, variables, ids); |
---|
779 | if (result) |
---|
780 | { |
---|
781 | cline.write (out); |
---|
782 | return (true); |
---|
783 | } |
---|
784 | else |
---|
785 | { |
---|
786 | return (false); |
---|
787 | } |
---|
788 | } |
---|
789 | |
---|
790 | //-------------------------------------------------- |
---|
791 | bool Fragment::copy (cmt_string& out, const Variable::VariableVector& vector, int variables, va_list ids) |
---|
792 | { |
---|
793 | int i; |
---|
794 | |
---|
795 | if (!locate ()) return (false); |
---|
796 | |
---|
797 | out.read (path); |
---|
798 | |
---|
799 | Variable* var = 0; |
---|
800 | |
---|
801 | for (i = 0; i < vector.size (); i++) |
---|
802 | { |
---|
803 | var = &(vector[i]); |
---|
804 | out.replace_all (var->macro_braces (), var->value); |
---|
805 | out.replace_all (var->macro_pars (), var->value); |
---|
806 | } |
---|
807 | |
---|
808 | for (i = 0; i < variables; i++) |
---|
809 | { |
---|
810 | var = va_arg (ids, Variable*); |
---|
811 | out.replace_all (var->macro_braces (), var->value); |
---|
812 | out.replace_all (var->macro_pars (), var->value); |
---|
813 | } |
---|
814 | |
---|
815 | return (true); |
---|
816 | } |
---|
817 | |
---|
818 | //-------------------------------------------------- |
---|
819 | bool Fragment::wincopy (FILE* out, const Variable::VariableVector& vector, int variables, va_list ids) |
---|
820 | { |
---|
821 | static cmt_string cline; |
---|
822 | |
---|
823 | bool result = wincopy (cline, vector, variables, ids); |
---|
824 | if (result) |
---|
825 | { |
---|
826 | cline.write (out); |
---|
827 | return (true); |
---|
828 | } |
---|
829 | else |
---|
830 | { |
---|
831 | return (false); |
---|
832 | } |
---|
833 | } |
---|
834 | |
---|
835 | //-------------------------------------------------- |
---|
836 | bool Fragment::wincopy (cmt_string& out, const Variable::VariableVector& vector, |
---|
837 | int variables, va_list ids) |
---|
838 | { |
---|
839 | int i; |
---|
840 | |
---|
841 | if (!locate ()) return (false); |
---|
842 | |
---|
843 | out.read (path); |
---|
844 | |
---|
845 | Variable* var = 0; |
---|
846 | |
---|
847 | for (i = 0; i < vector.size (); i++) |
---|
848 | { |
---|
849 | var = &(vector[i]); |
---|
850 | out.replace_all (var->macro_braces (), var->value); |
---|
851 | out.replace_all (var->macro_pars (), var->value); |
---|
852 | } |
---|
853 | |
---|
854 | for (i = 0; i < variables; i++) |
---|
855 | { |
---|
856 | var = va_arg (ids, Variable*); |
---|
857 | out.replace_all (var->macro_braces (), var->value); |
---|
858 | out.replace_all (var->macro_pars (), var->value); |
---|
859 | } |
---|
860 | |
---|
861 | cmt_string pattern; |
---|
862 | cmt_string macro_name; |
---|
863 | char end_pattern; |
---|
864 | |
---|
865 | int start = 0; |
---|
866 | |
---|
867 | for (;;) |
---|
868 | { |
---|
869 | // |
---|
870 | // Try and substitute all ${xxx} or $(xxx) patterns |
---|
871 | // using symbol values. |
---|
872 | // |
---|
873 | int par; |
---|
874 | int brace; |
---|
875 | int begin; |
---|
876 | |
---|
877 | par = out.find (start, "$("); |
---|
878 | brace = out.find (start, "${"); |
---|
879 | |
---|
880 | if (par == cmt_string::npos) |
---|
881 | { |
---|
882 | // No parentheses. Look for brace |
---|
883 | if (brace == cmt_string::npos) |
---|
884 | { |
---|
885 | // No pattern, finish the scan. |
---|
886 | break; |
---|
887 | } |
---|
888 | |
---|
889 | // Brace found |
---|
890 | end_pattern = '}'; |
---|
891 | begin = brace; |
---|
892 | } |
---|
893 | else |
---|
894 | { |
---|
895 | // Parenthese found. Look for closest from {par, brace} |
---|
896 | if ((brace == cmt_string::npos) || |
---|
897 | (brace > par)) |
---|
898 | { |
---|
899 | end_pattern = ')'; |
---|
900 | begin = par; |
---|
901 | } |
---|
902 | else |
---|
903 | { |
---|
904 | end_pattern = '}'; |
---|
905 | begin = brace; |
---|
906 | } |
---|
907 | } |
---|
908 | |
---|
909 | // Skip the pattern intro. |
---|
910 | start = begin + 2; |
---|
911 | |
---|
912 | int end; |
---|
913 | end = out.find (start, end_pattern); |
---|
914 | if (end == cmt_string::npos) |
---|
915 | { |
---|
916 | // The pattern is a fake one (no ending!) |
---|
917 | break; |
---|
918 | } |
---|
919 | |
---|
920 | // This should never happen... |
---|
921 | if (end < begin) break; |
---|
922 | |
---|
923 | // Extract the complete pattern |
---|
924 | out.substr (begin, end - begin + 1, pattern); |
---|
925 | |
---|
926 | // Then only the macro name |
---|
927 | out.substr (begin + 2, end - begin - 2, macro_name); |
---|
928 | |
---|
929 | if (macro_name == "CFG") |
---|
930 | { |
---|
931 | // This is a Windows reserved keyword... |
---|
932 | start = end + 1; |
---|
933 | } |
---|
934 | else |
---|
935 | { |
---|
936 | Symbol* macro = Symbol::find (macro_name); |
---|
937 | if (macro != 0) |
---|
938 | { |
---|
939 | // Macro found |
---|
940 | cmt_string value = macro->resolve_macro_value (); |
---|
941 | //cout << "resolve_macro_value2> value=" << value << endl; |
---|
942 | out.replace_all (pattern, value); |
---|
943 | |
---|
944 | // The substitution will restart from the same place |
---|
945 | // allowing for recursive replacements |
---|
946 | start = begin; |
---|
947 | } |
---|
948 | else |
---|
949 | { |
---|
950 | // Macro not found. Look for env. variable |
---|
951 | cmt_string value = CmtSystem::getenv (macro_name); |
---|
952 | //cout << "resolve_macro_value3> " << macro_name << "=" << value << endl; |
---|
953 | out.replace_all (pattern, value); |
---|
954 | |
---|
955 | // The substitution will restart from the same place |
---|
956 | // allowing for recursive replacements |
---|
957 | start = begin; |
---|
958 | } |
---|
959 | } |
---|
960 | } |
---|
961 | |
---|
962 | // Uniformly install CR-LF doublets. |
---|
963 | |
---|
964 | out.replace_all ("\r\n", "\n"); |
---|
965 | out.replace_all ("\n", "\r\n"); |
---|
966 | |
---|
967 | return (true); |
---|
968 | } |
---|
969 | |
---|
970 | //-------------------------------------------------- |
---|
971 | FragmentHandle::FragmentHandle () |
---|
972 | { |
---|
973 | _fragment = 0; |
---|
974 | _initialized = false; |
---|
975 | } |
---|
976 | |
---|
977 | //-------------------------------------------------- |
---|
978 | FragmentHandle::FragmentHandle (const cmt_string name) : _name(name) |
---|
979 | { |
---|
980 | _fragment = 0; |
---|
981 | _initialized = false; |
---|
982 | } |
---|
983 | |
---|
984 | //-------------------------------------------------- |
---|
985 | FragmentHandle& FragmentHandle::operator = (const FragmentHandle& other) |
---|
986 | { |
---|
987 | _name = other._name; |
---|
988 | _fragment = 0; |
---|
989 | _initialized = false; |
---|
990 | |
---|
991 | return (*this); |
---|
992 | } |
---|
993 | |
---|
994 | //-------------------------------------------------- |
---|
995 | void FragmentHandle::reset () |
---|
996 | { |
---|
997 | _fragment = 0; |
---|
998 | _initialized = false; |
---|
999 | } |
---|
1000 | |
---|
1001 | //-------------------------------------------------- |
---|
1002 | void FragmentHandle::set (const cmt_string name) |
---|
1003 | { |
---|
1004 | _name = name; |
---|
1005 | _fragment = 0; |
---|
1006 | _initialized = false; |
---|
1007 | } |
---|
1008 | |
---|
1009 | //-------------------------------------------------- |
---|
1010 | cmt_string& FragmentHandle::name () |
---|
1011 | { |
---|
1012 | static cmt_string null_string; |
---|
1013 | |
---|
1014 | if (!setup ()) return (null_string); |
---|
1015 | |
---|
1016 | return (_fragment->name); |
---|
1017 | } |
---|
1018 | |
---|
1019 | //-------------------------------------------------- |
---|
1020 | cmt_string& FragmentHandle::suffix () |
---|
1021 | { |
---|
1022 | static cmt_string null_string; |
---|
1023 | |
---|
1024 | if (!setup ()) return (null_string); |
---|
1025 | |
---|
1026 | return (_fragment->suffix); |
---|
1027 | } |
---|
1028 | |
---|
1029 | //-------------------------------------------------- |
---|
1030 | cmt_string& FragmentHandle::header () |
---|
1031 | { |
---|
1032 | static cmt_string null_string; |
---|
1033 | |
---|
1034 | if (!setup ()) return (null_string); |
---|
1035 | |
---|
1036 | return (_fragment->header); |
---|
1037 | } |
---|
1038 | |
---|
1039 | //-------------------------------------------------- |
---|
1040 | cmt_string& FragmentHandle::trailer () |
---|
1041 | { |
---|
1042 | static cmt_string null_string; |
---|
1043 | |
---|
1044 | if (!setup ()) return (null_string); |
---|
1045 | |
---|
1046 | return (_fragment->trailer); |
---|
1047 | } |
---|
1048 | |
---|
1049 | //-------------------------------------------------- |
---|
1050 | bool FragmentHandle::need_dependencies () |
---|
1051 | { |
---|
1052 | if (!setup ()) return (false); |
---|
1053 | |
---|
1054 | return (_fragment->need_dependencies); |
---|
1055 | } |
---|
1056 | |
---|
1057 | //-------------------------------------------------- |
---|
1058 | bool FragmentHandle::copy (FILE* out, int variables, ...) |
---|
1059 | { |
---|
1060 | if (!setup ()) return (false); |
---|
1061 | |
---|
1062 | va_list ids; |
---|
1063 | |
---|
1064 | va_start (ids, variables); |
---|
1065 | bool result = _fragment->copy (out, variables, ids); |
---|
1066 | va_end (ids); |
---|
1067 | |
---|
1068 | if (!result) |
---|
1069 | { |
---|
1070 | cout << "#CMT> Fragment " << _name << " not found" << endl; |
---|
1071 | _fragment = 0; |
---|
1072 | } |
---|
1073 | |
---|
1074 | return (result); |
---|
1075 | } |
---|
1076 | |
---|
1077 | //-------------------------------------------------- |
---|
1078 | bool FragmentHandle::copy (cmt_string& out, int variables, ...) |
---|
1079 | { |
---|
1080 | if (!setup ()) return (false); |
---|
1081 | |
---|
1082 | va_list ids; |
---|
1083 | |
---|
1084 | va_start (ids, variables); |
---|
1085 | bool result = _fragment->copy (out, variables, ids); |
---|
1086 | va_end (ids); |
---|
1087 | |
---|
1088 | if (!result) |
---|
1089 | { |
---|
1090 | cout << "#CMT> Fragment " << _name << " not found" << endl; |
---|
1091 | _fragment = 0; |
---|
1092 | } |
---|
1093 | |
---|
1094 | return (result); |
---|
1095 | } |
---|
1096 | |
---|
1097 | //-------------------------------------------------- |
---|
1098 | bool FragmentHandle::wincopy (FILE* out, int variables, ...) |
---|
1099 | { |
---|
1100 | if (!setup ()) return (false); |
---|
1101 | |
---|
1102 | va_list ids; |
---|
1103 | |
---|
1104 | va_start (ids, variables); |
---|
1105 | bool result = _fragment->wincopy (out, variables, ids); |
---|
1106 | va_end (ids); |
---|
1107 | |
---|
1108 | if (!result) |
---|
1109 | { |
---|
1110 | cout << "#CMT> Fragment " << _name << " not found" << endl; |
---|
1111 | _fragment = 0; |
---|
1112 | } |
---|
1113 | |
---|
1114 | return (result); |
---|
1115 | } |
---|
1116 | |
---|
1117 | //-------------------------------------------------- |
---|
1118 | bool FragmentHandle::wincopy (cmt_string& out, int variables, ...) |
---|
1119 | { |
---|
1120 | if (!setup ()) return (false); |
---|
1121 | |
---|
1122 | va_list ids; |
---|
1123 | |
---|
1124 | va_start (ids, variables); |
---|
1125 | bool result = _fragment->wincopy (out, variables, ids); |
---|
1126 | va_end (ids); |
---|
1127 | |
---|
1128 | if (!result) |
---|
1129 | { |
---|
1130 | cout << "#CMT> Fragment " << _name << " not found" << endl; |
---|
1131 | _fragment = 0; |
---|
1132 | } |
---|
1133 | |
---|
1134 | return (result); |
---|
1135 | } |
---|
1136 | |
---|
1137 | |
---|
1138 | |
---|
1139 | |
---|
1140 | //-------------------------------------------------- |
---|
1141 | bool FragmentHandle::copy (FILE* out, const Variable::VariableVector& vector, int variables, ...) |
---|
1142 | { |
---|
1143 | if (!setup ()) return (false); |
---|
1144 | |
---|
1145 | va_list ids; |
---|
1146 | |
---|
1147 | va_start (ids, variables); |
---|
1148 | bool result = _fragment->copy (out, vector, variables, ids); |
---|
1149 | va_end (ids); |
---|
1150 | |
---|
1151 | if (!result) |
---|
1152 | { |
---|
1153 | cout << "#CMT> Fragment " << _name << " not found" << endl; |
---|
1154 | _fragment = 0; |
---|
1155 | } |
---|
1156 | |
---|
1157 | return (result); |
---|
1158 | } |
---|
1159 | |
---|
1160 | //-------------------------------------------------- |
---|
1161 | bool FragmentHandle::copy (cmt_string& out, const Variable::VariableVector& vector, int variables, ...) |
---|
1162 | { |
---|
1163 | if (!setup ()) return (false); |
---|
1164 | |
---|
1165 | va_list ids; |
---|
1166 | |
---|
1167 | va_start (ids, variables); |
---|
1168 | bool result = _fragment->copy (out, vector, variables, ids); |
---|
1169 | va_end (ids); |
---|
1170 | |
---|
1171 | if (!result) |
---|
1172 | { |
---|
1173 | cout << "#CMT> Fragment " << _name << " not found" << endl; |
---|
1174 | _fragment = 0; |
---|
1175 | } |
---|
1176 | |
---|
1177 | return (result); |
---|
1178 | } |
---|
1179 | |
---|
1180 | //-------------------------------------------------- |
---|
1181 | bool FragmentHandle::wincopy (FILE* out, const Variable::VariableVector& vector, 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::wincopy (cmt_string& out, |
---|
1202 | const Variable::VariableVector& vector, |
---|
1203 | int variables, ...) |
---|
1204 | { |
---|
1205 | if (!setup ()) return (false); |
---|
1206 | |
---|
1207 | va_list ids; |
---|
1208 | |
---|
1209 | va_start (ids, variables); |
---|
1210 | bool result = _fragment->wincopy (out, vector, variables, ids); |
---|
1211 | va_end (ids); |
---|
1212 | |
---|
1213 | if (!result) |
---|
1214 | { |
---|
1215 | cout << "#CMT> Fragment " << _name << " not found" << endl; |
---|
1216 | _fragment = 0; |
---|
1217 | } |
---|
1218 | |
---|
1219 | return (result); |
---|
1220 | } |
---|
1221 | |
---|
1222 | //-------------------------------------------------- |
---|
1223 | bool FragmentHandle::setup () |
---|
1224 | { |
---|
1225 | if (!_initialized) |
---|
1226 | { |
---|
1227 | _initialized = true; |
---|
1228 | |
---|
1229 | _fragment = Fragment::find (_name); |
---|
1230 | if (_fragment == 0) |
---|
1231 | { |
---|
1232 | cout << "#CMT> Fragment " << _name << " not found" << endl; |
---|
1233 | } |
---|
1234 | } |
---|
1235 | |
---|
1236 | if (_fragment == 0) |
---|
1237 | { |
---|
1238 | return (false); |
---|
1239 | } |
---|
1240 | else |
---|
1241 | { |
---|
1242 | return (true); |
---|
1243 | } |
---|
1244 | } |
---|