Variable.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 "Variable.h"
00026 #include "StringAdapter.h"
00027 #include <stdexcept>
00028 #include <sstream>
00029 #include <algorithm>
00030 
00031 namespace makefile
00032 {
00033 
00039 void Variable::checkName()
00040 {
00041         static const regex validname(cstr("\\w+"));
00042         if( !boost::regex_match(name, validname))
00043                 throw std::invalid_argument("Bad Variable name: " + StringAdapter(name));
00044 }
00045 
00050 void Variable::checkValue()
00051 {
00052         static const regex invalid(cstr("[^\\\\][\n\r]"));
00053         if(boost::regex_search(getValue(), invalid))
00054                 throw std::invalid_argument("Bad Variable value: " + StringAdapter(getValue()));
00055 }
00056 
00057 void Variable::Create()
00058 {
00059     def=new VariableDef(*this);
00060     ref=new VariableRef(*this);
00061 };
00062 
00063 Variable::Variable(string name_)
00064 : name(name_), quotemode(Makefile::SHELL)
00065 {
00066     Create();
00067 }
00068 
00069 Variable::Variable(string name_, string value_, Makefile::QuoteMode quotemode_)
00070 : name(name_), quotemode(quotemode_), exported(false)
00071 {
00072     Create();
00073         values.push_back(value_);
00074         checkName();
00075         checkValue();
00076 }
00077 Variable::Variable(string name_, double value_, Makefile::QuoteMode quotemode_)
00078 : name(name_), quotemode(quotemode_), exported(false)
00079 {
00080     Create();
00081         checkName();
00082         std::ostringstream val;
00083         val.imbue(Makefile::locale);
00084         val << value_;
00085         values.push_back(val.str());
00086 }
00087 
00088 Variable::Variable(string name_, std::vector<string>::iterator start, std::vector<string>::iterator end,
00089                 Makefile::QuoteMode quotemode_, string separator_)
00090 : name(name_), separator(separator_), quotemode(quotemode_), exported(false)
00091 {
00092     Create();
00093         checkName();
00094         copy(start, end, std::back_inserter(values));
00095         checkValue();
00096 }
00097 
00098 Variable::~Variable()
00099 {
00100     if(def)
00101         delete def;
00102     if(ref)
00103         delete ref;
00104 };
00105 
00106 const string Variable::getValue()
00107 {
00108         string v;
00109         for(std::vector<string>::iterator it = values.begin(); it != values.end(); it++)
00110         {
00111                 if(it != values.begin()) v += separator;
00112                 v += *it;
00113         }
00114         return v;
00115 }
00116 const string Variable::getquotedValue()
00117 {
00118         string v;
00119         for(std::vector<string>::iterator it = values.begin(); it != values.end(); it++)
00120         {
00121                 if(it != values.begin()) v += separator;
00122                 v += Makefile::quote(*it, quotemode);
00123         }
00124         return v;
00125 }
00126 
00127 }

Generated on 26 Oct 2014 for Hugintrunk by  doxygen 1.4.7