00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00025 #include "Makefile.h"
00026 #include "MakefileItem.h"
00027
00028 namespace makefile
00029 {
00030
00031
00032 Makefile* Makefile::instance = NULL;
00033
00034
00036 const std::locale Makefile::locale(std::locale("C"));
00037
00038 const std::locale GetMakefileLocale()
00039 {
00040 return Makefile::locale;
00041 };
00042
00043 Makefile& Makefile::getSingleton()
00044 {
00045 if(!instance)
00046 instance = new Makefile();
00047 return *instance;
00048 }
00049
00050 void Makefile::clean()
00051 {
00052 if(instance)
00053 delete instance;
00054 instance = NULL;
00055 }
00056
00057 void Makefile::remove(MakefileItem* item)
00058 {
00059 if(instance && !instance->written)
00060 {
00061 std::cerr <<
00062 "WARNING: A MakefileItem was removed before the Makefile::writeMakefile() was called.\n"
00063 "This is likely to be a programming error (out of scope?)" << std::endl;
00064 }
00065 clean();
00066 }
00067
00068
00093 string Makefile::quote(const string& in, Makefile::QuoteMode mode)
00094 {
00095 regex toescape;
00096 string output;
00097 switch(mode)
00098 {
00099 case Makefile::SHELL:
00100 #ifdef WIN32
00101 toescape.assign(cstr("(\\$[^\\(])|(\\\\)|(\\#)"));
00102
00103 output.assign(cstr("(?1\\$$&)(?2/)(?3\\\\$&)"));
00104 return string(cstr("\"") + boost::regex_replace(in, toescape, output, boost::match_default | boost::format_all) + cstr("\""));
00105 #else
00106
00107 toescape.assign(cstr("(\\$\\([^\\)]+\\))|(\\$)|([\\\\ \\~\"\\|\\'\\`\\{\\}\\[\\]\\(\\)\\*\\#\\:\\=\\&])"));
00108 output.assign(cstr("(?1$&)(?2\\\\\\$$&)(?3\\\\$&)"));
00109 return boost::regex_replace(in, toescape, output, boost::match_default | boost::format_all);
00110 #endif
00111 break;
00112 case Makefile::MAKE:
00113 #ifdef WIN32
00114 toescape.assign(cstr("(\\$[^\\(])|(\\\\)|([ \\#\\=])"));
00115 output.assign(cstr("(?1\\$$&)(?2/)(?3\\\\$&)"));
00116 return boost::regex_replace(in, toescape, output, boost::match_default | boost::format_all);
00117 #else
00118
00119 toescape.assign(cstr("(\\$[^\\(])|([ \\#\\:\\=])"));
00120 output.assign(cstr("(?1\\$$&)(?2\\\\$&)"));
00121 return boost::regex_replace(in, toescape, output, boost::match_default | boost::format_all);
00122 #endif
00123 break;
00124 default:
00125 return string(in);
00126 }
00127 }
00128
00129 #ifdef WIN32
00130
00133 string Makefile::quoteEnvironment(const string& in)
00134 {
00135 regex toescape;
00136 string output;
00137 toescape.assign(cstr("(\\$)|(\\#)"));
00138 output.assign(cstr("(?1\\$$&)(?2\\\\$&)"));
00139 return boost::regex_replace(in, toescape, output, boost::match_default | boost::format_all);
00140 };
00141 #endif
00142
00143 int Makefile::writeMakefile(ostream& out)
00144 {
00145 for(std::vector<MakefileItem*>::iterator i = items.begin(); i != items.end(); i++)
00146 {
00147 out << **i;
00148 }
00149 written = true;
00150 return items.size();
00151 }
00152
00153 }