Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

cmt_fragment.cxx

Go to the documentation of this file.
00001 
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <string.h>
00005 #include <ctype.h>
00006 
00007 #include "cmt_fragment.h"
00008 #include "cmt_use.h"
00009 #include "cmt_symbol.h"
00010 #include "cmt_system.h"
00011 #include "cmt_database.h"
00012 
00013 /*----------------------------------------------------------*/
00014 /*                                                          */
00015 /*  Operations on Variables                                 */
00016 /*                                                          */
00017 /*----------------------------------------------------------*/
00018 
00019 //----------------------------------------------------------
00020 Variable* Variable::find (VariableVector& vector, 
00021                           const cmt_string& name)
00022 {
00023   for (int i = 0; i < vector.size (); i++)
00024     {
00025       Variable& v = vector[i];
00026 
00027       if (v.name == name) return (&v);
00028     }
00029 
00030   return (0);
00031 }
00032 
00033 //----------------------------------------------------------
00034 Variable::Variable ()
00035 {
00036 }
00037 
00038 //----------------------------------------------------------
00039 Variable::Variable (const cmt_string& n) : name (n)
00040 {
00041   m_macro_braces = "${";
00042   m_macro_braces += name;
00043   m_macro_braces += "}";
00044 
00045   m_macro_pars = "$(";
00046   m_macro_pars += name;
00047   m_macro_pars += ")";
00048 }
00049 
00050 //----------------------------------------------------------
00051 const cmt_string& Variable::macro_braces () const
00052 {
00053   return (m_macro_braces);
00054 }
00055 
00056 //----------------------------------------------------------
00057 const cmt_string& Variable::macro_pars () const
00058 {
00059   return (m_macro_pars);
00060 }
00061 
00062 //----------------------------------------------------------
00063 void Variable::set (const cmt_string& new_name,
00064                     const cmt_string& new_value)
00065 {
00066   name = new_name;
00067   value = new_value;
00068 
00069   m_macro_braces = "${";
00070   m_macro_braces += name;
00071   m_macro_braces += "}";
00072 
00073   m_macro_pars = "$(";
00074   m_macro_pars += name;
00075   m_macro_pars += ")";
00076 }
00077 
00078 //----------------------------------------------------------
00079 Variable& Variable::operator = (const Variable& other)
00080 {
00081   value = other.value;
00082   return (*this);
00083 }
00084 
00085 //----------------------------------------------------------
00086 Variable& Variable::operator = (const cmt_string& v)
00087 {
00088   value = v;
00089   return (*this);
00090 }
00091 
00092 //----------------------------------------------------------
00093 void Variable::operator += (const cmt_string& v)
00094 {
00095   value += v;
00096 }
00097 
00098 //----------------------------------------------------------
00099 cmt_string Variable::operator + (const cmt_string& v) const
00100 {
00101   return (value + v);
00102 }
00103 
00104 //----------------------------------------------------------
00105 Variable::operator const cmt_string& () const
00106 {
00107   return (value);
00108 }
00109 
00110 //----------------------------------------------------------
00111 bool Variable::operator == (const cmt_string& v) const
00112 {
00113   return ((value == v));
00114 }
00115 
00116 //----------------------------------------------------------
00117 bool Variable::operator != (const cmt_string& v) const
00118 {
00119   return ((value != v));
00120 }
00121 
00122 /*----------------------------------------------------------*/
00123 /*                                                          */
00124 /*  Operations on Fragments                                 */
00125 /*                                                          */
00126 /*----------------------------------------------------------*/
00127 
00128 /*----------------------------------------------------------*/
00129 void Fragment::show (const cmt_string& name)
00130 {
00131   Fragment* fragment = Fragment::find (name);
00132   if (fragment == 0)
00133     {
00134       cout << "Fragment " << name << " not found" << endl;
00135     }
00136   else
00137     {
00138       fragment->print ();
00139     }
00140 }
00141 
00142 /*----------------------------------------------------------*/
00143 void Fragment::show_all ()
00144 {
00145   static FragmentVector& Fragments = fragments ();
00146 
00147   int number;
00148 
00149   for (number = 0; number < Fragments.size (); number++)
00150     {
00151       Fragment& fragment = Fragments[number];
00152 
00153       fragment.print ();
00154     }
00155 }
00156 
00157 /*----------------------------------------------------------*/
00158 void Fragment::action (const CmtSystem::cmt_string_vector& words,
00159                        Use* use)
00160 {
00161   if (words.size () > 1)
00162     {
00163       cmt_string suffix;
00164       cmt_string header;
00165       cmt_string trailer;
00166       bool need_dependencies = false;
00167 
00168       cmt_string name = words[1];
00169 
00170       for (int i = 2; i < words.size (); i++)
00171         {
00172           const cmt_string& word = words[i];
00173 
00174           if (word.find ("-suffix=") != cmt_string::npos)
00175             {
00176               suffix = word;
00177               suffix.replace ("-suffix=", "");
00178             }
00179           else if (word.find ("-dependencies") != cmt_string::npos)
00180             {
00181               need_dependencies = true;
00182             }
00183           else if (word.find ("-header=") != cmt_string::npos)
00184             {
00185               header = word;
00186               header.replace ("-header=", "");
00187 
00188               if (find (header) == 0)
00189                 {
00190                   add (header, "", "", "", false, use);
00191                 }
00192             }
00193           else if (word.find ("-trailer=") != cmt_string::npos)
00194             {
00195               trailer = word;
00196               trailer.replace ("-trailer=", "");
00197 
00198               if (find (trailer) == 0)
00199                 {
00200                   add (trailer, "", "", "", false, use);
00201                 }
00202             }
00203         }
00204 
00205       add (name, suffix, header, trailer, need_dependencies, use);
00206     }
00207 }
00208 
00209 /*----------------------------------------------------------*/
00210 Fragment* Fragment::find (const cmt_string& name)
00211 {
00212   static FragmentVector& Fragments = fragments ();
00213 
00214   int fragment_index;
00215 
00216   if (Fragments.size () == 0) return (0);
00217 
00218   for (fragment_index = 0;
00219        fragment_index < Fragments.size ();
00220        fragment_index++)
00221     {
00222       Fragment& fragment = Fragments[fragment_index];
00223 
00224       if (fragment.name == name)
00225         {
00226           return (&fragment);
00227         }
00228     }
00229 
00230   return (0);
00231 }
00232 
00233 /*----------------------------------------------------------*/
00234 void Fragment::add (const cmt_string& name,
00235                     const cmt_string& suffix,
00236                     const cmt_string& header,
00237                     const cmt_string& trailer,
00238                     bool need_dependencies,
00239                     Use* use)
00240 {
00241   static FragmentVector& Fragments = fragments ();
00242 
00243   {
00244     Fragment* fragment;
00245 
00246     if (name == "") return;
00247 
00248     fragment = find (name);
00249     if (fragment != 0)
00250       {
00251         if (suffix != "")
00252           {
00253             fragment->suffix = suffix;
00254           }
00255 
00256         if (header != "")
00257           {
00258             fragment->header = header;
00259           }
00260 
00261         if (trailer != "")
00262           {
00263             fragment->trailer = trailer;
00264           }
00265 
00266         fragment->need_dependencies = need_dependencies;
00267 
00268         fragment->use = use;
00269         return;
00270       }
00271   }
00272 
00273   Fragment& fragment = Fragments.add ();
00274 
00275   fragment.name              = name;
00276   fragment.suffix            = suffix;
00277   fragment.header            = header;
00278   fragment.trailer           = trailer;
00279   fragment.need_dependencies = need_dependencies;
00280   fragment.use               = use;
00281 }
00282 
00283 /*----------------------------------------------------------*/
00284 void Fragment::clear_all ()
00285 {
00286   static FragmentVector& Fragments = fragments ();
00287 
00288   for (int i = 0; i < Fragments.size (); i++)
00289     {
00290       Fragment& f = Fragments[i];
00291       f.clear ();
00292     }
00293 
00294   Fragments.clear ();
00295 }
00296 
00297 /*----------------------------------------------------------*/
00298 Fragment::FragmentVector& Fragment::fragments ()
00299 {
00300   static Database& db = Database::instance ();
00301   static FragmentVector& Fragments = db.fragments ();
00302 
00303   return (Fragments);
00304 }
00305 
00306 /*----------------------------------------------------------*/
00307 Fragment::Fragment ()
00308 {
00309   use = 0;
00310 }
00311 
00312 /*----------------------------------------------------------*/
00313 Fragment::Fragment (const cmt_string& fragment_name)
00314 {
00315   name = fragment_name;
00316   use = 0;
00317   path = "";
00318 }
00319 
00320 /*----------------------------------------------------------*/
00321 Fragment::~Fragment ()
00322 {
00323   use = 0;
00324 }
00325 
00326 /*----------------------------------------------------------*/
00327 void Fragment::clear ()
00328 {
00329   name    = "";
00330   suffix  = "";
00331   header  = "";
00332   trailer = "";
00333   need_dependencies = false;
00334   path = "";
00335   use  = 0;
00336 }
00337 
00338 /*----------------------------------------------------------*/
00339 int Fragment::print ()
00340 {
00341   int result = 1;
00342 
00343   if (name == "") return (0);
00344   
00345   locate ();
00346 
00347   if (use == 0)
00348     {
00349       Use& u = Use::current();
00350       use = &u;
00351     }
00352   
00353   cmt_string the_path = path;
00354   
00355   cmt_string pattern = use->real_path;
00356   pattern += CmtSystem::file_separator ();
00357   pattern += use->package;
00358   pattern += CmtSystem::file_separator ();
00359   pattern += use->version;
00360   
00361   cmt_string replacement = "${";
00362   replacement += use->prefix;
00363   replacement += "ROOT}";
00364   
00365   the_path.replace (pattern, replacement);
00366 
00367   cout << the_path;
00368 
00369   if (suffix != "")
00370     {
00371       cout << "->" << suffix;
00372     }
00373 
00374   cout << endl;
00375 
00376   return (result);
00377 }
00378 
00379 /*----------------------------------------------------------*/
00380 bool Fragment::locate ()
00381 {
00382   if (path == "")
00383     {
00384       if (use == 0)
00385         {
00386           // Assume CMT
00387           use = Use::find ("CMT");
00388         }
00389 
00390       path = use->real_path;
00391       path += CmtSystem::file_separator ();
00392       path += use->package;
00393       path += CmtSystem::file_separator ();
00394       path += use->version;
00395       path += CmtSystem::file_separator ();
00396       path += "fragments";
00397       path += CmtSystem::file_separator ();
00398       
00399       if (Cmt::build_nmake ())
00400         {
00401           path += "nmake";
00402           path += CmtSystem::file_separator ();
00403         }
00404       
00405       path += name;
00406     }
00407 
00408   if (CmtSystem::test_file (path)) return (true);
00409 
00410   path = use->real_path;
00411   path += CmtSystem::file_separator ();
00412   path += use->package;
00413   path += CmtSystem::file_separator ();
00414   path += use->version;
00415   path += CmtSystem::file_separator ();
00416   path += "fragments";
00417   path += CmtSystem::file_separator ();
00418   path += name;
00419 
00420   if (CmtSystem::test_file (path)) return (true);
00421 
00422   path = use->real_path;
00423   path += CmtSystem::file_separator ();
00424   path += use->package;
00425   path += CmtSystem::file_separator ();
00426   path += use->version;
00427   path += CmtSystem::file_separator ();
00428   
00429   if (use->style == mgr_style) path += "mgr";
00430   else path += "cmt";
00431   
00432   path += CmtSystem::file_separator ();
00433   path += "fragments";
00434   path += CmtSystem::file_separator ();
00435   
00436   if (Cmt::build_nmake ())
00437     {
00438       path += "nmake";
00439       path += CmtSystem::file_separator ();
00440     }
00441   
00442   path += name;
00443   
00444   if (CmtSystem::test_file (path)) return (true);
00445 
00446   path = use->real_path;
00447   path += CmtSystem::file_separator ();
00448   path += use->package;
00449   path += CmtSystem::file_separator ();
00450   path += use->version;
00451   path += CmtSystem::file_separator ();
00452   
00453   if (use->style == mgr_style) path += "mgr";
00454   else path += "cmt";
00455   
00456   path += CmtSystem::file_separator ();
00457   path += "fragments";
00458   path += CmtSystem::file_separator ();
00459   
00460   path += name;
00461 
00462   if (CmtSystem::test_file (path)) return (true);
00463 
00464   return (false);
00465 }
00466 
00467 //--------------------------------------------------
00468 bool Fragment::copy (FILE* out,
00469                      const cmt_string& name,
00470                      int variables, ...)
00471 {
00472   va_list ids;
00473 
00474   Fragment* fragment = Fragment::find (name);
00475   if (fragment == 0) return (false);
00476 
00477   va_start (ids, variables);
00478   bool result = fragment->copy (out, variables, ids);
00479   va_end (ids);
00480 
00481   return (result);
00482 }
00483 
00484 //--------------------------------------------------
00485 bool Fragment::copy (cmt_string& out,
00486                      const cmt_string& name,
00487                      int variables, ...)
00488 {
00489   va_list ids;
00490 
00491   Fragment* fragment = Fragment::find (name);
00492   if (fragment == 0) return (false);
00493 
00494   va_start (ids, variables);
00495   bool result = fragment->copy (out, variables, ids);
00496   va_end (ids);
00497 
00498   return (result);
00499 }
00500 
00501 //--------------------------------------------------
00502 bool Fragment::copy (FILE* out, int variables, ...)
00503 {
00504   va_list ids;
00505 
00506   va_start (ids, variables);
00507   bool result = copy (out, variables, ids);
00508   va_end (ids);
00509 
00510   return (result);
00511 }
00512 
00513 //--------------------------------------------------
00514 bool Fragment::copy (cmt_string& out, int variables, ...)
00515 {
00516   va_list ids;
00517 
00518   va_start (ids, variables);
00519   bool result = copy (out, variables, ids);
00520   va_end (ids);
00521 
00522   return (result);
00523 }
00524 
00525 //--------------------------------------------------
00526 bool Fragment::copy (FILE* out, int variables, va_list ids)
00527 {
00528   static cmt_string cline;
00529 
00530   bool result = copy (cline, variables, ids);
00531   if (result)
00532     {
00533       cline.write (out);
00534       return (true);
00535     }
00536   else
00537     {
00538       return (false);
00539     }
00540 }
00541 
00542 //--------------------------------------------------
00543 bool Fragment::copy (cmt_string& out, int variables, va_list ids)
00544 {
00545   int i;
00546 
00547   if (!locate ()) return (false);
00548 
00549   out.read (path);
00550 
00551   Variable* var = 0;
00552   for (i = 0; i < variables; i++)
00553     {
00554       var = va_arg (ids, Variable*);
00555       out.replace_all (var->macro_braces (), var->value);
00556       out.replace_all (var->macro_pars (), var->value);
00557     }
00558 
00559   return (true);
00560 }
00561 
00562 //--------------------------------------------------
00563 bool Fragment::wincopy (FILE* out, int variables, va_list ids)
00564 {
00565   static cmt_string cline;
00566 
00567   bool result = wincopy (cline, variables, ids);
00568 
00569   if (result)
00570     {
00571       cline.write (out);
00572       return (true);
00573     }
00574   else
00575     {
00576       return (false);
00577     }
00578 }
00579 
00580 //--------------------------------------------------
00581 bool Fragment::wincopy (cmt_string& out, int variables, va_list ids)
00582 {
00583   int i;
00584 
00585   if (!locate ()) return (false);
00586 
00587   out.read (path);
00588 
00589   Variable* var = 0;
00590   for (i = 0; i < variables; i++)
00591     {
00592       var = va_arg (ids, Variable*);
00593       out.replace_all (var->macro_braces (), var->value);
00594       out.replace_all (var->macro_pars (), var->value);
00595     }
00596 
00597   cmt_string pattern;
00598   cmt_string macro_name;
00599   char end_pattern;
00600 
00601   int start = 0;
00602 
00603   for (;;)
00604     {
00605       //
00606       // Try and substitute all ${xxx} or $(xxx) patterns
00607       // using symbol values.
00608       //
00609       int par;
00610       int brace;
00611       int begin;
00612 
00613       par = out.find (start, "$(");
00614       brace = out.find (start, "${");
00615 
00616       if (par == cmt_string::npos)
00617         {
00618           // No parentheses. Look for brace
00619           if (brace == cmt_string::npos)
00620             {
00621               // No pattern, finish the scan.
00622               break;
00623             }
00624 
00625           // Brace found
00626           end_pattern = '}';
00627           begin = brace;
00628         }
00629       else
00630         {
00631           // Parenthese found. Look for closest from {par, brace}
00632           if ((brace == cmt_string::npos) ||
00633               (brace > par))
00634             {
00635               end_pattern = ')';
00636               begin = par;
00637             }
00638           else
00639             {
00640               end_pattern = '}';
00641               begin = brace;
00642             }
00643         }
00644 
00645       // Skip the pattern intro.
00646       start = begin + 2;
00647 
00648       int end;
00649       end = out.find (start, end_pattern);
00650       if (end == cmt_string::npos)
00651         {
00652           // The pattern is a fake one (no ending!)
00653           break;
00654         }
00655 
00656       // This should never happen...
00657       if (end < begin) break;
00658 
00659       // Extract the complete pattern
00660       out.substr (begin, end - begin + 1, pattern);
00661 
00662       // Then only the macro name
00663       out.substr (begin + 2, end - begin - 2, macro_name);
00664 
00665       if (macro_name == "CFG")
00666         {
00667           // This is a Windows reserved keyword...
00668           start = end + 1;
00669         }
00670       else
00671         {
00672           Symbol* macro = Symbol::find (macro_name);
00673           if (macro != 0)
00674             {
00675               // Macro found
00676               cmt_string value = macro->resolve_macro_value ();
00677               //cout << "resolve_macro_value2> value=" << value << endl;
00678               out.replace_all (pattern, value);
00679 
00680               // The substitution will restart from the same place
00681               // allowing for recursive replacements
00682               start = begin;
00683             }
00684           else
00685             {
00686               // Macro not found. Look for env. variable
00687               cmt_string value = CmtSystem::getenv (macro_name);
00688               //cout << "resolve_macro_value3> " << macro_name << "=" << value << endl;
00689               out.replace_all (pattern, value);
00690 
00691               // The substitution will restart from the same place
00692               // allowing for recursive replacements
00693               start = begin;
00694             }
00695         }
00696     }
00697 
00698   // Uniformly install CR-LF doublets.
00699 
00700   out.replace_all ("\r\n", "\n");
00701   out.replace_all ("\n", "\r\n");
00702 
00703   return (true);
00704 }
00705 
00706 
00707 
00708 
00709 
00710 
00711 
00712 
00713 //--------------------------------------------------
00714 bool Fragment::copy (FILE* out,
00715                      const cmt_string& name,
00716                      const Variable::VariableVector& vector, 
00717                      int variables, ...)
00718 {
00719   va_list ids;
00720 
00721   Fragment* fragment = Fragment::find (name);
00722   if (fragment == 0) return (false);
00723 
00724   va_start (ids, variables);
00725   bool result = fragment->copy (out, vector, variables, ids);
00726   va_end (ids);
00727 
00728   return (result);
00729 }
00730 
00731 //--------------------------------------------------
00732 bool Fragment::copy (cmt_string& out,
00733                      const cmt_string& name,
00734                      const Variable::VariableVector& vector, 
00735                      int variables, ...)
00736 {
00737   va_list ids;
00738 
00739   Fragment* fragment = Fragment::find (name);
00740   if (fragment == 0) return (false);
00741 
00742   va_start (ids, variables);
00743   bool result = fragment->copy (out, vector, variables, ids);
00744   va_end (ids);
00745 
00746   return (result);
00747 }
00748 
00749 //--------------------------------------------------
00750 bool Fragment::copy (FILE* out, const Variable::VariableVector& vector, int variables, ...)
00751 {
00752   va_list ids;
00753 
00754   va_start (ids, variables);
00755   bool result = copy (out, vector, variables, ids);
00756   va_end (ids);
00757 
00758   return (result);
00759 }
00760 
00761 //--------------------------------------------------
00762 bool Fragment::copy (cmt_string& out, const Variable::VariableVector& vector, int variables, ...)
00763 {
00764   va_list ids;
00765 
00766   va_start (ids, variables);
00767   bool result = copy (out, vector, variables, ids);
00768   va_end (ids);
00769 
00770   return (result);
00771 }
00772 
00773 //--------------------------------------------------
00774 bool Fragment::copy (FILE* out, const Variable::VariableVector& vector, int variables, va_list ids)
00775 {
00776   static cmt_string cline;
00777 
00778   bool result = copy (cline, vector, variables, ids);
00779   if (result)
00780     {
00781       cline.write (out);
00782       return (true);
00783     }
00784   else
00785     {
00786       return (false);
00787     }
00788 }
00789 
00790 //--------------------------------------------------
00791 bool Fragment::copy (cmt_string& out, const Variable::VariableVector& vector, int variables, va_list ids)
00792 {
00793   int i;
00794 
00795   if (!locate ()) return (false);
00796 
00797   out.read (path);
00798 
00799   Variable* var = 0;
00800 
00801   for (i = 0; i < vector.size (); i++)
00802     {
00803       var = &(vector[i]);
00804       out.replace_all (var->macro_braces (), var->value);
00805       out.replace_all (var->macro_pars (), var->value);
00806     }
00807 
00808   for (i = 0; i < variables; i++)
00809     {
00810       var = va_arg (ids, Variable*);
00811       out.replace_all (var->macro_braces (), var->value);
00812       out.replace_all (var->macro_pars (), var->value);
00813     }
00814 
00815   return (true);
00816 }
00817 
00818 //--------------------------------------------------
00819 bool Fragment::wincopy (FILE* out, const Variable::VariableVector& vector, int variables, va_list ids)
00820 {
00821   static cmt_string cline;
00822 
00823   bool result = wincopy (cline, vector, variables, ids);
00824   if (result)
00825     {
00826       cline.write (out);
00827       return (true);
00828     }
00829   else
00830     {
00831       return (false);
00832     }
00833 }
00834 
00835 //--------------------------------------------------
00836 bool Fragment::wincopy (cmt_string& out, const Variable::VariableVector& vector, 
00837                         int variables, va_list ids)
00838 {
00839   int i;
00840 
00841   if (!locate ()) return (false);
00842 
00843   out.read (path);
00844 
00845   Variable* var = 0;
00846 
00847   for (i = 0; i < vector.size (); i++)
00848     {
00849       var = &(vector[i]);
00850       out.replace_all (var->macro_braces (), var->value);
00851       out.replace_all (var->macro_pars (), var->value);
00852     }
00853 
00854   for (i = 0; i < variables; i++)
00855     {
00856       var = va_arg (ids, Variable*);
00857       out.replace_all (var->macro_braces (), var->value);
00858       out.replace_all (var->macro_pars (), var->value);
00859     }
00860 
00861   cmt_string pattern;
00862   cmt_string macro_name;
00863   char end_pattern;
00864 
00865   int start = 0;
00866 
00867   for (;;)
00868     {
00869       //
00870       // Try and substitute all ${xxx} or $(xxx) patterns
00871       // using symbol values.
00872       //
00873       int par;
00874       int brace;
00875       int begin;
00876 
00877       par = out.find (start, "$(");
00878       brace = out.find (start, "${");
00879 
00880       if (par == cmt_string::npos)
00881         {
00882           // No parentheses. Look for brace
00883           if (brace == cmt_string::npos)
00884             {
00885               // No pattern, finish the scan.
00886               break;
00887             }
00888 
00889           // Brace found
00890           end_pattern = '}';
00891           begin = brace;
00892         }
00893       else
00894         {
00895           // Parenthese found. Look for closest from {par, brace}
00896           if ((brace == cmt_string::npos) ||
00897               (brace > par))
00898             {
00899               end_pattern = ')';
00900               begin = par;
00901             }
00902           else
00903             {
00904               end_pattern = '}';
00905               begin = brace;
00906             }
00907         }
00908 
00909       // Skip the pattern intro.
00910       start = begin + 2;
00911 
00912       int end;
00913       end = out.find (start, end_pattern);
00914       if (end == cmt_string::npos)
00915         {
00916           // The pattern is a fake one (no ending!)
00917           break;
00918         }
00919 
00920       // This should never happen...
00921       if (end < begin) break;
00922 
00923       // Extract the complete pattern
00924       out.substr (begin, end - begin + 1, pattern);
00925 
00926       // Then only the macro name
00927       out.substr (begin + 2, end - begin - 2, macro_name);
00928 
00929       if (macro_name == "CFG")
00930         {
00931           // This is a Windows reserved keyword...
00932           start = end + 1;
00933         }
00934       else
00935         {
00936           Symbol* macro = Symbol::find (macro_name);
00937           if (macro != 0)
00938             {
00939               // Macro found
00940               cmt_string value = macro->resolve_macro_value ();
00941               //cout << "resolve_macro_value2> value=" << value << endl;
00942               out.replace_all (pattern, value);
00943 
00944               // The substitution will restart from the same place
00945               // allowing for recursive replacements
00946               start = begin;
00947             }
00948           else
00949             {
00950               // Macro not found. Look for env. variable
00951               cmt_string value = CmtSystem::getenv (macro_name);
00952               //cout << "resolve_macro_value3> " << macro_name << "=" << value << endl;
00953               out.replace_all (pattern, value);
00954 
00955               // The substitution will restart from the same place
00956               // allowing for recursive replacements
00957               start = begin;
00958             }
00959         }
00960     }
00961 
00962   // Uniformly install CR-LF doublets.
00963 
00964   out.replace_all ("\r\n", "\n");
00965   out.replace_all ("\n", "\r\n");
00966 
00967   return (true);
00968 }
00969 
00970 //--------------------------------------------------
00971 FragmentHandle::FragmentHandle ()
00972 {
00973   _fragment = 0;
00974   _initialized = false;
00975 }
00976 
00977 //--------------------------------------------------
00978 FragmentHandle::FragmentHandle (const cmt_string name) : _name(name)
00979 {
00980   _fragment = 0;
00981   _initialized = false;
00982 }
00983 
00984 //--------------------------------------------------
00985 FragmentHandle& FragmentHandle::operator = (const FragmentHandle& other)
00986 {
00987   _name = other._name;
00988   _fragment = 0;
00989   _initialized = false;
00990 
00991   return (*this);
00992 }
00993 
00994 //--------------------------------------------------
00995 void FragmentHandle::reset ()
00996 {
00997   _fragment = 0;
00998   _initialized = false;
00999 }
01000 
01001 //--------------------------------------------------
01002 void FragmentHandle::set (const cmt_string name)
01003 {
01004   _name = name;
01005   _fragment = 0;
01006   _initialized = false;
01007 }
01008 
01009 //--------------------------------------------------
01010 cmt_string& FragmentHandle::name ()
01011 {
01012   static cmt_string null_string;
01013 
01014   if (!setup ()) return (null_string);
01015 
01016   return (_fragment->name);
01017 }
01018 
01019 //--------------------------------------------------
01020 cmt_string& FragmentHandle::suffix ()
01021 {
01022   static cmt_string null_string;
01023 
01024   if (!setup ()) return (null_string);
01025 
01026   return (_fragment->suffix);
01027 }
01028 
01029 //--------------------------------------------------
01030 cmt_string& FragmentHandle::header ()
01031 {
01032   static cmt_string null_string;
01033 
01034   if (!setup ()) return (null_string);
01035 
01036   return (_fragment->header);
01037 }
01038 
01039 //--------------------------------------------------
01040 cmt_string& FragmentHandle::trailer ()
01041 {
01042   static cmt_string null_string;
01043 
01044   if (!setup ()) return (null_string);
01045 
01046   return (_fragment->trailer);
01047 }
01048 
01049 //--------------------------------------------------
01050 bool FragmentHandle::need_dependencies ()
01051 {
01052   if (!setup ()) return (false);
01053 
01054   return (_fragment->need_dependencies);
01055 }
01056 
01057 //--------------------------------------------------
01058 bool FragmentHandle::copy (FILE* out, int variables, ...)
01059 {
01060   if (!setup ()) return (false);
01061 
01062   va_list ids;
01063 
01064   va_start (ids, variables);
01065   bool result = _fragment->copy (out, variables, ids);
01066   va_end (ids);
01067 
01068   if (!result)
01069     {
01070       cout << "#CMT> Fragment " << _name << " not found" << endl;
01071       _fragment = 0;
01072     }
01073 
01074   return (result);
01075 }
01076 
01077 //--------------------------------------------------
01078 bool FragmentHandle::copy (cmt_string& out, int variables, ...)
01079 {
01080   if (!setup ()) return (false);
01081 
01082   va_list ids;
01083 
01084   va_start (ids, variables);
01085   bool result = _fragment->copy (out, variables, ids);
01086   va_end (ids);
01087 
01088   if (!result)
01089     {
01090       cout << "#CMT> Fragment " << _name << " not found" << endl;
01091       _fragment = 0;
01092     }
01093 
01094   return (result);
01095 }
01096 
01097 //--------------------------------------------------
01098 bool FragmentHandle::wincopy (FILE* out, int variables, ...)
01099 {
01100   if (!setup ()) return (false);
01101 
01102   va_list ids;
01103 
01104   va_start (ids, variables);
01105   bool result = _fragment->wincopy (out, variables, ids);
01106   va_end (ids);
01107 
01108   if (!result)
01109     {
01110       cout << "#CMT> Fragment " << _name << " not found" << endl;
01111       _fragment = 0;
01112     }
01113 
01114   return (result);
01115 }
01116 
01117 //--------------------------------------------------
01118 bool FragmentHandle::wincopy (cmt_string& out, int variables, ...)
01119 {
01120   if (!setup ()) return (false);
01121 
01122   va_list ids;
01123 
01124   va_start (ids, variables);
01125   bool result = _fragment->wincopy (out, variables, ids);
01126   va_end (ids);
01127 
01128   if (!result)
01129     {
01130       cout << "#CMT> Fragment " << _name << " not found" << endl;
01131       _fragment = 0;
01132     }
01133 
01134   return (result);
01135 }
01136 
01137 
01138 
01139 
01140 //--------------------------------------------------
01141 bool FragmentHandle::copy (FILE* out, const Variable::VariableVector& vector, int variables, ...)
01142 {
01143   if (!setup ()) return (false);
01144 
01145   va_list ids;
01146 
01147   va_start (ids, variables);
01148   bool result = _fragment->copy (out, vector, variables, ids);
01149   va_end (ids);
01150 
01151   if (!result)
01152     {
01153       cout << "#CMT> Fragment " << _name << " not found" << endl;
01154       _fragment = 0;
01155     }
01156 
01157   return (result);
01158 }
01159 
01160 //--------------------------------------------------
01161 bool FragmentHandle::copy (cmt_string& out, const Variable::VariableVector& vector, int variables, ...)
01162 {
01163   if (!setup ()) return (false);
01164 
01165   va_list ids;
01166 
01167   va_start (ids, variables);
01168   bool result = _fragment->copy (out, vector, variables, ids);
01169   va_end (ids);
01170 
01171   if (!result)
01172     {
01173       cout << "#CMT> Fragment " << _name << " not found" << endl;
01174       _fragment = 0;
01175     }
01176 
01177   return (result);
01178 }
01179 
01180 //--------------------------------------------------
01181 bool FragmentHandle::wincopy (FILE* out, const Variable::VariableVector& vector, int variables, ...)
01182 {
01183   if (!setup ()) return (false);
01184 
01185   va_list ids;
01186 
01187   va_start (ids, variables);
01188   bool result = _fragment->wincopy (out, vector, variables, ids);
01189   va_end (ids);
01190 
01191   if (!result)
01192     {
01193       cout << "#CMT> Fragment " << _name << " not found" << endl;
01194       _fragment = 0;
01195     }
01196 
01197   return (result);
01198 }
01199 
01200 //--------------------------------------------------
01201 bool FragmentHandle::wincopy (cmt_string& out, 
01202                               const Variable::VariableVector& vector, 
01203                               int variables, ...)
01204 {
01205   if (!setup ()) return (false);
01206 
01207   va_list ids;
01208 
01209   va_start (ids, variables);
01210   bool result = _fragment->wincopy (out, vector, variables, ids);
01211   va_end (ids);
01212 
01213   if (!result)
01214     {
01215       cout << "#CMT> Fragment " << _name << " not found" << endl;
01216       _fragment = 0;
01217     }
01218 
01219   return (result);
01220 }
01221 
01222 //--------------------------------------------------
01223 bool FragmentHandle::setup ()
01224 {
01225   if (!_initialized)
01226     {
01227       _initialized = true;
01228 
01229       _fragment = Fragment::find (_name);
01230       if (_fragment == 0)
01231         {
01232           cout << "#CMT> Fragment " << _name << " not found" << endl;
01233         }
01234     }
01235 
01236   if (_fragment == 0)
01237     {
01238       return (false);
01239     }
01240   else
01241     {
01242       return (true);
01243     }
01244 }

Generated at Thu May 16 16:27:05 2002 for CMT by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000