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