test_simple.cpp

Go to the documentation of this file.
00001 /*
00002 This file is part of hugin.
00003 
00004 hugin is free software: you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation, either version 2 of the License, or
00007 (at your option) any later version.
00008 
00009 hugin is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with hugin.  If not, see <http://www.gnu.org/licenses/>.
00016 */
00017 
00025 #include <iostream>
00026 #include <stdexcept>
00027 #include "Comment.h"
00028 #include "Variable.h"
00029 #include "VariableDef.h"
00030 #include "VariableRef.h"
00031 #include "MakefileItem.h"
00032 #include "Makefile.h"
00033 #include "AutoVariable.h"
00034 #include "Newline.h"
00035 #include "Rule.h"
00036 #include "Conditional.h"
00037 #include "StringAdapter.h"
00038 
00039 #include <boost/scoped_ptr.hpp>
00040 
00041 using namespace makefile;
00042 
00043 #ifdef USE_WCHAR
00044 ostream& out =  std::wcout;
00045 #else
00046 ostream& out =  std::cout;
00047 #endif
00048 
00049 int tryall()
00050 {
00051         Comment comment(cstr("First line"));
00052         comment.appendLine(cstr("second line"));
00053         comment.appendLine(cstr("third line\nfourth\r line"));
00054         out << comment;
00055 
00056         Variable myname(cstr("MYNAME"), cstr("Flo"));
00057         out << myname.getName() << std::endl;
00058         out << myname.getValue() << std::endl;
00059         out << myname.getDef();
00060         out << myname.getRef() << std::endl;
00061 
00062         Variable myfullname(cstr("MYFULLNAME"), myname.getRef().toString() + cstr(" Achleitner"));
00063         out << myfullname.getDef() << myfullname.getRef() << std::endl;
00064         myfullname.setQuoteMode(Makefile::MAKE);
00065         out << myfullname.getDef() << myfullname.getRef() << std::endl;
00066 
00067         try
00068         {
00069         Variable namesucks(cstr("This name sucks"), cstr("anyvalue"));
00070         out << namesucks.getDef();
00071         }
00072         catch(std::exception& e)
00073         {
00074                 std::cerr << e.what() << std::endl;
00075         }
00076         try
00077         {
00078         Variable valuesucks(cstr("This_value_sucks"), cstr("any\nnewline"));
00079         out << valuesucks.getDef();
00080         }
00081         catch(std::exception& e)
00082         {
00083                 std::cerr << e.what() << std::endl;
00084         }
00085         try
00086         {
00087         Variable valuesucks(cstr("This_value_sucks_not"), cstr("any escaped\\\nnewline"));
00088         out << valuesucks.getDef();
00089         }
00090         catch(std::exception& e)
00091         {
00092                 std::cerr << e.what() << std::endl;
00093         }
00094         Variable namesucksless(cstr("This_name_sucks_less"), cstr("~~(bad:){\\value}"));
00095         namesucksless.setQuoteMode(Makefile::SHELL);
00096         out << namesucksless.getDef();
00097         namesucksless.setQuoteMode(Makefile::MAKE);
00098         out << namesucksless.getDef();
00099 
00100         AutoVariable autovar(cstr("@"));
00101 //      out << autovar.getDef(); causes an exception as it should.
00102         out << autovar.getRef() << std::endl;
00103 
00104         return 0;
00105 }
00106 
00107 int tryreplace()
00108 {
00109         regex toescape;
00110 //      regex toescape(cstr("(p)|([Da])"));
00111         string output;
00112 //      string output(cstr("(?1--$&--)(?2__$&__)"));
00113         toescape.assign(cstr("(\\$\\([^\\)]+\\))|(\\$[^\\(])|([\\\\ \\~\"\\|\\'\\`\\{\\}\\[\\]\\(\\)\\*\\#\\:\\=])"));
00114         output.assign(cstr("(?1$&)(?2\\\\\\$$&)(?3\\\\$&)"));
00115         string text(cstr("Ein_Dollar$ $ und_paar_andere (Sachen) werden $(richtig) escaped. backslash\\__ ein sternchen * doppelpunkt :=*~"));
00116         out << boost::regex_replace(text, toescape, output, boost::match_default | boost::format_all) << std::endl;
00117         return 0;
00118 
00119         out << "SHELL mode" << std::endl << Makefile::quote(text, Makefile::SHELL) << std::endl;
00120         out << "MAKE mode" << std::endl << Makefile::quote(text, Makefile::MAKE) << std::endl;
00121 
00122         return 0;
00123 }
00124 
00125 int trymakefile()
00126 {
00127         Comment comment(cstr("First line"));
00128         comment.appendLine(cstr("second line"));
00129         comment.appendLine(cstr("third line\nfourth line\rfifth line"));
00130         comment.add();
00131 
00132         Variable myname(cstr("MYNAME"), cstr("Flo"));
00133         myname.getDef().add();
00134 
00135         Variable myfullname(cstr("MYFULLNAME"), myname.getRef().toString() + cstr(" Achleitner"));
00136         myfullname.getDef().add();
00137         myfullname.getRef().add();
00138 
00139 
00140         Newline nl1(2); nl1.add();
00141         Comment c1(cstr("Escaping modes:"));
00142         Variable shellvar(cstr("SHELLVAR"), cstr("'has some special (char)s # [or] {not}"), Makefile::SHELL);
00143         Variable makevar(cstr("MAKEVAR"), cstr("'has some special (char)s # [or] {not}"), Makefile::MAKE);
00144         shellvar.getDef().add();
00145         makevar.getDef().add();
00146         Newline nl2; nl2.add();
00147 
00148         Makefile::getSingleton().writeMakefile(out);
00149         Makefile::clean();
00150         return 0;
00151 }
00152 
00153 int tryrule()
00154 {
00155         Comment c(cstr("Try how a rule looks like")); c.add();
00156 
00157         Variable t1(cstr("TARGET1"), cstr("t1.o")); t1.getDef().add();
00158         Variable t2(cstr("TARGET2"), cstr("t2.o")); t2.getDef().add();
00159         Variable p1(cstr("PRERE1"), cstr("t1.c")); p1.getDef().add();
00160         Variable p2(cstr("PRERE2"), cstr("t2.c")); p2.getDef().add();
00161         AutoVariable all(cstr("@"));
00162 
00163         Rule r;
00164         r.addTarget(t1.getRef().toString());
00165         r.addTarget(t2.getRef().toString());
00166         r.addPrereq(p1.getRef().toString());
00167         r.addPrereq(p2.getRef().toString());
00168         r.addCommand(cstr("echo ") + all.getRef().toString());
00169         r.add();
00170 
00171         r.toString();
00172 
00173         Makefile::getSingleton().writeMakefile(out);
00174         Makefile::clean();
00175 
00176         return 0;
00177 }
00178 int trycond()
00179 {
00180         Variable t1(cstr("TARGET1"), cstr("t1.o")); t1.getDef().add();
00181         Variable t2(cstr("TARGET2"), cstr("t2.o")); t2.getDef().add();
00182         Variable p1(cstr("PRERE1"), cstr("t1.c")); p1.getDef().add();
00183         Variable p2(cstr("PRERE2"), cstr("t2.c")); p2.getDef().add();
00184         AutoVariable all(cstr("@"));
00185         Rule r;
00186         r.addTarget(t1.getRef().toString());
00187         r.addTarget(t2.getRef().toString());
00188         r.addPrereq(p1.getRef().toString());
00189         r.addPrereq(p2.getRef().toString());
00190         r.addCommand(cstr("echo ") + all.getRef().toString());
00191 
00192         Variable iftrue(cstr("TRUE"), cstr("if_is_true"));
00193         Variable iffales(cstr("FALSE"), cstr("if_is_false"));
00194 
00195         ConditionalEQ cond1(iftrue.getRef().toString(), cstr("if_is_true"));
00196         cond1.addToIf(r);
00197         cond1.addToIf(iftrue.getDef());
00198         cond1.addToElse(iffales.getDef());
00199         cond1.addToElse(r);
00200         cond1.add();
00201 
00202         ConditionalNEQ cond2(iftrue.getRef().toString(), cstr("if_is_true"));
00203         cond2.addToIf(r);
00204         cond2.addToIf(iftrue.getDef());
00205         cond2.addToElse(iffales.getDef());
00206         cond2.addToElse(r);
00207         cond2.add();
00208 
00209         ConditionalDEF cond3(iftrue.getName());
00210         cond3.addToIf(r);
00211         cond3.addToIf(iftrue.getDef());
00212         cond3.addToElse(iffales.getDef());
00213         cond3.addToElse(r);
00214         cond3.add();
00215 
00216         ConditionalNDEF cond4(iftrue.getName());
00217         cond4.addToIf(r);
00218         cond4.addToIf(iftrue.getDef());
00219         cond4.add();
00220 
00221         Makefile::getSingleton().writeMakefile(out);
00222         Makefile::clean();
00223         return 0;
00224 }
00225 int main(int argc, char *argv[])
00226 {
00227         return
00228 //      trymakefile() ||
00229 //      tryreplace() ||
00230         tryall() ||
00231 //      tryrule() ||
00232 //      trycond() ||
00233         0;
00234 }

Generated on Tue Sep 30 01:25:37 2014 for Hugintrunk by  doxygen 1.3.9.1