[ee50c1] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[ee50c1] | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | /*
|
---|
| 9 | * FormatParser_Parameters.cpp
|
---|
| 10 | *
|
---|
| 11 | * Created on: Sep 30, 2011
|
---|
| 12 | * Author: heber
|
---|
| 13 | */
|
---|
| 14 |
|
---|
| 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
| 20 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 21 |
|
---|
[7f570c] | 22 | #include <iostream>
|
---|
| 23 | #include <boost/algorithm/string.hpp>
|
---|
| 24 | #include <boost/tokenizer.hpp>
|
---|
| 25 | #include <sstream>
|
---|
[ee50c1] | 26 | #include <string>
|
---|
| 27 |
|
---|
[7f570c] | 28 | #include "CodePatterns/Assert.hpp"
|
---|
| 29 | #include "CodePatterns/Log.hpp"
|
---|
| 30 |
|
---|
[ee50c1] | 31 | #include "Parameters/Parameter.hpp"
|
---|
| 32 | #include "FormatParser_Parameters.hpp"
|
---|
| 33 |
|
---|
| 34 | /** Constructor of class FormatParser_Parameters.
|
---|
| 35 | *
|
---|
| 36 | * \note we make sure that storage is always present
|
---|
| 37 | *
|
---|
| 38 | */
|
---|
| 39 | FormatParser_Parameters::FormatParser_Parameters() :
|
---|
| 40 | storage(new ParameterStorage)
|
---|
| 41 | {}
|
---|
| 42 |
|
---|
| 43 | /** Copy constructor of class FormatParser_Parameters.
|
---|
| 44 | *
|
---|
| 45 | * @param _parameters instance to copy from
|
---|
| 46 | */
|
---|
| 47 | FormatParser_Parameters::FormatParser_Parameters(const FormatParser_Parameters &_parameters) :
|
---|
| 48 | storage(new ParameterStorage(*_parameters.storage))
|
---|
| 49 | {}
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 | /** Destructor of class FormatParser_Parameters.
|
---|
| 53 | *
|
---|
| 54 | */
|
---|
| 55 | FormatParser_Parameters::~FormatParser_Parameters()
|
---|
| 56 | {
|
---|
| 57 | delete storage;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | /** Implements the Parameter::clone() function.
|
---|
| 61 | *
|
---|
| 62 | * @return another instance with an identical copy of each parameter in \a storage
|
---|
| 63 | */
|
---|
| 64 | FormatParser_Parameters* FormatParser_Parameters::clone() const
|
---|
| 65 | {
|
---|
| 66 | return (new FormatParser_Parameters(*this));
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | /** This makes this instance a clone of \a _instance.
|
---|
| 70 | *
|
---|
| 71 | * \note This is basically the other way round to clone().
|
---|
| 72 | *
|
---|
| 73 | * @param _instance instance to clone from
|
---|
| 74 | */
|
---|
| 75 | void FormatParser_Parameters::makeClone(const FormatParser_Parameters &_instance)
|
---|
| 76 | {
|
---|
| 77 | // we simply remove storage
|
---|
| 78 | delete storage;
|
---|
| 79 | // and clone the one from _instance
|
---|
| 80 | storage = new ParameterStorage(*_instance.storage);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /** Adds a parameter to \a storage.
|
---|
| 84 | *
|
---|
| 85 | * This just eases use, saves some typing and is more clear as to what is done.
|
---|
| 86 | *
|
---|
| 87 | * @param instance parameter to add
|
---|
| 88 | */
|
---|
| 89 | void FormatParser_Parameters::appendParameter(Parameter *instance)
|
---|
| 90 | {
|
---|
| 91 | storage->registerInstance(instance);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | /** Checks for presence of a parameter in \a storage.
|
---|
| 95 | *
|
---|
| 96 | * This just eases use, saves some typing and is more clear as to what is done.
|
---|
| 97 | *
|
---|
| 98 | * @return true - parameter by this \a _name is present in storage, false - else
|
---|
| 99 | */
|
---|
| 100 | bool FormatParser_Parameters::haveParameter(const std::string &_name) const
|
---|
| 101 | {
|
---|
| 102 | return storage->isPresentByName(_name);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /** Gets a parameter from \a storage.
|
---|
| 106 | *
|
---|
| 107 | * This just eases use, saves some typing and is more clear as to what is done.
|
---|
| 108 | *
|
---|
| 109 | * @return pointer to instance with this \a _name
|
---|
| 110 | */
|
---|
| 111 | Parameter *FormatParser_Parameters::getParameter(const std::string &_name) const
|
---|
| 112 | {
|
---|
| 113 | return storage->getByName(_name);
|
---|
| 114 | }
|
---|
[7f570c] | 115 |
|
---|
| 116 | /** Output operator for the contents of FormatParser_Parameters::params.
|
---|
| 117 | *
|
---|
| 118 | * @param ost output stream
|
---|
| 119 | * @param params reference to FormatParser_Parameters containing params.
|
---|
| 120 | * @return reference to output stream for concatenation
|
---|
| 121 | */
|
---|
| 122 | std::ostream & operator << (std::ostream& ost, const FormatParser_Parameters ¶ms)
|
---|
| 123 | {
|
---|
| 124 | // this is ugly, but with boost::any to safeguard const-ness is plain impossible
|
---|
| 125 | std::ostringstream output;
|
---|
| 126 | ASSERT(params.storage != NULL,
|
---|
| 127 | "operator<<(FormatParser_Parameters) - storage is NULL.");
|
---|
| 128 | for (ParameterStorage::const_iterator iter = params.storage->getBeginIter();
|
---|
| 129 | iter != params.storage->getEndIter();
|
---|
| 130 | ++iter)
|
---|
[41d023] | 131 | if (!iter->second->get().empty())
|
---|
| 132 | output << iter->first << "=" << iter->second->get() << ";";
|
---|
[7f570c] | 133 | ost << output.str();
|
---|
| 134 | return ost;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /** Input operator for a list of parameters to place into \a params.
|
---|
| 138 | *
|
---|
| 139 | * @param ist input stream
|
---|
| 140 | * @param params parameters to parse into
|
---|
| 141 | * @return input stream for concatenation
|
---|
| 142 | */
|
---|
| 143 | std::istream & operator >> (std::istream& ist, FormatParser_Parameters ¶ms)
|
---|
| 144 | {
|
---|
| 145 | typedef boost::tokenizer<boost::char_separator<char> >
|
---|
| 146 | tokenizer;
|
---|
| 147 | boost::char_separator<char> semicolonsep(";");
|
---|
| 148 | boost::char_separator<char> equalitysep("=");
|
---|
| 149 | boost::char_separator<char> ticksep("\"");
|
---|
| 150 | std::string line;
|
---|
| 151 | std::getline( ist, line );
|
---|
[47d041] | 152 | //LOG(0, "INFO: full line of parameters is '" << line << "'");
|
---|
[7f570c] | 153 | tokenizer tokens(line, semicolonsep);
|
---|
| 154 | ASSERT(tokens.begin() != tokens.end(),
|
---|
| 155 | "operator<< on FormatParser_Parameters - empty string, need at least ';' in line "+line+"!");
|
---|
| 156 | for (tokenizer::iterator tok_iter = tokens.begin();
|
---|
| 157 | tok_iter != tokens.end(); ++tok_iter) {
|
---|
| 158 | //LOG(2, "INFO: (key,value) pair is: " << *tok_iter << ".");
|
---|
| 159 | tokenizer paramtokens(*tok_iter, equalitysep);
|
---|
| 160 | if (paramtokens.begin() != paramtokens.end()) {
|
---|
| 161 | tokenizer::iterator tok_paramiter = paramtokens.begin();
|
---|
| 162 | ASSERT(tok_paramiter != paramtokens.end(),
|
---|
| 163 | "operator<< on FormatParser_Parameters - missing value before ' =' in token "+*tok_iter+"!");
|
---|
[41d023] | 164 | tokenizer::iterator tok_valueiter = tok_paramiter;
|
---|
| 165 | tokenizer::iterator tok_checkiter = ++tok_valueiter;
|
---|
[7f570c] | 166 | ASSERT(tok_valueiter != paramtokens.end(),
|
---|
| 167 | "operator<< on FormatParser_Parameters - missing value after ' =' in token "+*tok_iter+"!");
|
---|
[41d023] | 168 | ++tok_checkiter;
|
---|
[7f570c] | 169 | ASSERT(tok_checkiter == paramtokens.end(),
|
---|
| 170 | "operator<< on FormatParser_Parameters - still more tokens after ' =' in token "+*tok_iter+":"
|
---|
| 171 | +*tok_checkiter+"!");
|
---|
[41d023] | 172 | LOG(3, "INFO: key is '" << *tok_paramiter << "', value is '" << *tok_valueiter << "'.");
|
---|
| 173 | // TODO: throw exception instead of ASSERT
|
---|
[7f570c] | 174 | std::string key(*tok_paramiter);
|
---|
| 175 | std::string value(*tok_valueiter);
|
---|
| 176 | boost::trim(key);
|
---|
| 177 | boost::trim(value);
|
---|
| 178 | tokenizer ticklesstokens(value, ticksep);
|
---|
| 179 | ASSERT(ticklesstokens.begin() != ticklesstokens.end(),
|
---|
| 180 | "operator<< on FormatParser_Parameters - no tokens present after removing ticks in token "+*tok_valueiter+"!");
|
---|
| 181 | std::stringstream valuestream(*(ticklesstokens.begin()));
|
---|
| 182 | //LOG(2, "INFO: Token pair is " << key << "," << valuestream.str());
|
---|
| 183 |
|
---|
| 184 | // TODO: throw exception instead of DoeLog()
|
---|
| 185 | ASSERT(params.haveParameter(key),
|
---|
| 186 | "operator >> on FormatParser_Parameters - unknown parameter name '"
|
---|
| 187 | +key+"' with value "+valuestream.str()+"!");
|
---|
| 188 | if (params.haveParameter(key)) {
|
---|
| 189 | Parameter *instance = params.getParameter(key);
|
---|
| 190 | instance->set(valuestream.str());
|
---|
| 191 | }
|
---|
| 192 | } else {
|
---|
| 193 | ist.setstate(std::ios::eofbit);
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
| 196 | return ist;
|
---|
| 197 | }
|
---|