| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| 5 | * | 
|---|
| 6 | * | 
|---|
| 7 | *   This file is part of MoleCuilder. | 
|---|
| 8 | * | 
|---|
| 9 | *    MoleCuilder is free software: you can redistribute it and/or modify | 
|---|
| 10 | *    it under the terms of the GNU General Public License as published by | 
|---|
| 11 | *    the Free Software Foundation, either version 2 of the License, or | 
|---|
| 12 | *    (at your option) any later version. | 
|---|
| 13 | * | 
|---|
| 14 | *    MoleCuilder is distributed in the hope that it will be useful, | 
|---|
| 15 | *    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 16 | *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 17 | *    GNU General Public License for more details. | 
|---|
| 18 | * | 
|---|
| 19 | *    You should have received a copy of the GNU General Public License | 
|---|
| 20 | *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 21 | */ | 
|---|
| 22 |  | 
|---|
| 23 | /* | 
|---|
| 24 | * CommandLineParser_validate.cpp | 
|---|
| 25 | * | 
|---|
| 26 | *  Created on: Nov 8, 2010 | 
|---|
| 27 | *      Author: heber | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | // include config.h | 
|---|
| 31 | #ifdef HAVE_CONFIG_H | 
|---|
| 32 | #include <config.h> | 
|---|
| 33 | #endif | 
|---|
| 34 |  | 
|---|
| 35 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 36 |  | 
|---|
| 37 | #include <boost/version.hpp> | 
|---|
| 38 |  | 
|---|
| 39 | #include <iostream> | 
|---|
| 40 | #include <string> | 
|---|
| 41 |  | 
|---|
| 42 | #include "Actions/Values.hpp" | 
|---|
| 43 | #include "CommandLineParser_validate.hpp" | 
|---|
| 44 | #include "Parameters/Specifics/KeyValuePair.hpp" | 
|---|
| 45 |  | 
|---|
| 46 | /** boost::program_options validator specialization for VectorValue. | 
|---|
| 47 | * \param &v  reference for return value | 
|---|
| 48 | * \param &values string vector of scanned options | 
|---|
| 49 | * \param * | 
|---|
| 50 | * \param | 
|---|
| 51 | * | 
|---|
| 52 | */ | 
|---|
| 53 | void validate(boost::any& v, const std::vector<std::string>& values, VectorValue *, int) | 
|---|
| 54 | { | 
|---|
| 55 | VectorValue VV; | 
|---|
| 56 | std::vector<std::string> components; | 
|---|
| 57 |  | 
|---|
| 58 | // split comma-separated values | 
|---|
| 59 | if (values.size() != 1) { | 
|---|
| 60 | std::cerr <<  "Not one vector but " << values.size() << " given " << std::endl; | 
|---|
| 61 | #if BOOST_VERSION < 104200 | 
|---|
| 62 | throw boost::program_options::validation_error("Unequal to one vector given"); | 
|---|
| 63 | #else | 
|---|
| 64 | throw boost::program_options::validation_error( | 
|---|
| 65 | boost::program_options::validation_error::invalid_option_value, | 
|---|
| 66 | std::string("value"), | 
|---|
| 67 | std::string("VectorValue") | 
|---|
| 68 | ); | 
|---|
| 69 | #endif | 
|---|
| 70 | } | 
|---|
| 71 | std::string argument(values.at(0)); | 
|---|
| 72 | std::string::iterator Aiter = argument.begin(); | 
|---|
| 73 | std::string::iterator Biter = argument.begin(); | 
|---|
| 74 | for (; Aiter != argument.end(); ++Aiter) { | 
|---|
| 75 | if (*Aiter == ',') { | 
|---|
| 76 | components.push_back(std::string(Biter,Aiter)); | 
|---|
| 77 | do { | 
|---|
| 78 | Aiter++; | 
|---|
| 79 | } while (*Aiter == ' ' || *Aiter == '\t'); | 
|---|
| 80 | Biter = Aiter; | 
|---|
| 81 | } | 
|---|
| 82 | } | 
|---|
| 83 | components.push_back(std::string(Biter,argument.end())); | 
|---|
| 84 |  | 
|---|
| 85 | if (components.size() != 3) { | 
|---|
| 86 | std::cerr <<  "Specified vector does not have three components but " << components.size() << std::endl; | 
|---|
| 87 | #if BOOST_VERSION < 104200 | 
|---|
| 88 | throw boost::program_options::validation_error("Specified vector does not have three components"); | 
|---|
| 89 | #else | 
|---|
| 90 | throw boost::program_options::validation_error( | 
|---|
| 91 | boost::program_options::validation_error::invalid_option_value, | 
|---|
| 92 | std::string("value"), | 
|---|
| 93 | std::string("VectorValue") | 
|---|
| 94 | ); | 
|---|
| 95 | #endif | 
|---|
| 96 | } | 
|---|
| 97 | for (size_t i=0;i<NDIM;++i) | 
|---|
| 98 | VV.vector[i] = boost::lexical_cast<double>(components.at(i)); | 
|---|
| 99 | v = boost::any(VectorValue(VV)); | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | void validate(boost::any& v, const std::vector<std::string>& values, RealSpaceMatrixValue *, int) | 
|---|
| 103 | { | 
|---|
| 104 | RealSpaceMatrixValue RSMV; | 
|---|
| 105 | std::vector<std::string> components; | 
|---|
| 106 |  | 
|---|
| 107 | // split comma-separated values | 
|---|
| 108 | if (values.size() != 1) { | 
|---|
| 109 | std::cerr <<  "Not one vector but " << values.size() << " given " << std::endl; | 
|---|
| 110 | #if BOOST_VERSION < 104200 | 
|---|
| 111 | throw boost::program_options::validation_error("Unequal to one vector given"); | 
|---|
| 112 | #else | 
|---|
| 113 | throw boost::program_options::validation_error( | 
|---|
| 114 | boost::program_options::validation_error::invalid_option_value, | 
|---|
| 115 | std::string("value"), | 
|---|
| 116 | std::string("BoxValue") | 
|---|
| 117 | ); | 
|---|
| 118 | #endif | 
|---|
| 119 | } | 
|---|
| 120 | std::string argument(values.at(0)); | 
|---|
| 121 | std::string::iterator Aiter = argument.begin(); | 
|---|
| 122 | std::string::iterator Biter = argument.begin(); | 
|---|
| 123 | for (; Aiter != argument.end(); ++Aiter) { | 
|---|
| 124 | if (*Aiter == ',') { | 
|---|
| 125 | components.push_back(std::string(Biter,Aiter)); | 
|---|
| 126 | do { | 
|---|
| 127 | Aiter++; | 
|---|
| 128 | } while (*Aiter == ' ' || *Aiter == '\t'); | 
|---|
| 129 | Biter = Aiter; | 
|---|
| 130 | } | 
|---|
| 131 | } | 
|---|
| 132 | components.push_back(std::string(Biter,argument.end())); | 
|---|
| 133 |  | 
|---|
| 134 | if (components.size() != 6) { | 
|---|
| 135 | std::cerr <<  "Specified vector does not have three components but " << components.size() << std::endl; | 
|---|
| 136 | #if BOOST_VERSION < 104200 | 
|---|
| 137 | throw boost::program_options::validation_error("Specified symmetric box matrix does not have six components"); | 
|---|
| 138 | #else | 
|---|
| 139 | throw boost::program_options::validation_error( | 
|---|
| 140 | boost::program_options::validation_error::invalid_option_value, | 
|---|
| 141 | std::string("value"), | 
|---|
| 142 | std::string("BoxValue") | 
|---|
| 143 | ); | 
|---|
| 144 | #endif | 
|---|
| 145 | } | 
|---|
| 146 | for (size_t i=0;i<(NDIM*(NDIM+1))/2; ++i) | 
|---|
| 147 | RSMV.matrix[i] = boost::lexical_cast<double>(components.at(i)); | 
|---|
| 148 | v = boost::any(RealSpaceMatrixValue(RSMV)); | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | /** boost::program_options validator specialization for boost::filesystem::path. | 
|---|
| 152 | * \param &v  reference for return value | 
|---|
| 153 | * \param &values string vector of scanned options | 
|---|
| 154 | * \param * | 
|---|
| 155 | * \param | 
|---|
| 156 | * | 
|---|
| 157 | */ | 
|---|
| 158 | void validate(boost::any& v, const std::vector<std::string>& values, boost::filesystem::path *, int) | 
|---|
| 159 | { | 
|---|
| 160 | //  std::cerr << "boost::filesystem::path validator used." << std::endl; | 
|---|
| 161 |  | 
|---|
| 162 | // Make sure no previous assignment to 'a' was made. | 
|---|
| 163 | boost::program_options::validators::check_first_occurrence(v); | 
|---|
| 164 | // Extract the first string from 'values'. If there is more than | 
|---|
| 165 | // one string, it's an error, and exception will be thrown. | 
|---|
| 166 | const std::string& s = boost::program_options::validators::get_single_string(values); | 
|---|
| 167 |  | 
|---|
| 168 | v = boost::any(boost::filesystem::path(s)); | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | /** boost::program_options validator specialization for boost::filesystem::path. | 
|---|
| 172 | * \param &v  reference for return value | 
|---|
| 173 | * \param &values string vector of scanned options | 
|---|
| 174 | * \param * | 
|---|
| 175 | * \param | 
|---|
| 176 | * | 
|---|
| 177 | */ | 
|---|
| 178 | void validate(boost::any& v, const std::vector<std::string>& values, KeyValuePair *, int) | 
|---|
| 179 | { | 
|---|
| 180 | //  std::cerr << "KeyValuePair validator used." << std::endl; | 
|---|
| 181 |  | 
|---|
| 182 | // Make sure no previous assignment to 'a' was made. | 
|---|
| 183 | boost::program_options::validators::check_first_occurrence(v); | 
|---|
| 184 | // Extract the first string from 'values'. If there is more than | 
|---|
| 185 | // one string, it's an error, and exception will be thrown. | 
|---|
| 186 | const std::string& s = boost::program_options::validators::get_single_string(values); | 
|---|
| 187 |  | 
|---|
| 188 | if (s.find("=") == std::string::npos) { | 
|---|
| 189 | #if BOOST_VERSION < 104200 | 
|---|
| 190 | throw boost::program_options::validation_error("Invalid KeyValue given"); | 
|---|
| 191 | #else | 
|---|
| 192 | throw boost::program_options::validation_error( | 
|---|
| 193 | boost::program_options::validation_error::invalid_option_value, | 
|---|
| 194 | std::string("value"), | 
|---|
| 195 | std::string("vector<KeyValuePair>") | 
|---|
| 196 | ); | 
|---|
| 197 | #endif | 
|---|
| 198 | } | 
|---|
| 199 | v = boost::any(KeyValuePair(s)); | 
|---|
| 200 | } | 
|---|
| 201 |  | 
|---|
| 202 | /** boost::program_options validator specialization for boost::filesystem::path. | 
|---|
| 203 | * \param &v  reference for return value | 
|---|
| 204 | * \param &values string vector of scanned options | 
|---|
| 205 | * \param * | 
|---|
| 206 | * \param | 
|---|
| 207 | * | 
|---|
| 208 | */ | 
|---|
| 209 | void validate(boost::any& v, const std::vector<std::string>& values, std::vector<KeyValuePair> *, int) | 
|---|
| 210 | { | 
|---|
| 211 | //  std::cerr << "vector<KeyValuePair> validator used." << std::endl; | 
|---|
| 212 |  | 
|---|
| 213 | // split comma-separated values | 
|---|
| 214 | if (values.size() > 1) { | 
|---|
| 215 | #if BOOST_VERSION < 104200 | 
|---|
| 216 | throw boost::program_options::validation_error("Unequal to one file given"); | 
|---|
| 217 | #else | 
|---|
| 218 | if (values.size() == 0) { | 
|---|
| 219 | throw boost::program_options::validation_error( | 
|---|
| 220 | boost::program_options::validation_error::at_least_one_value_required, | 
|---|
| 221 | std::string("value"), | 
|---|
| 222 | std::string("vector<KeyValuePair>") | 
|---|
| 223 | ); | 
|---|
| 224 | } | 
|---|
| 225 | #endif | 
|---|
| 226 | for (std::vector<std::string>::const_iterator iter = values.begin(); | 
|---|
| 227 | iter != values.end();++iter) | 
|---|
| 228 | if ((*iter).find("=") == std::string::npos) { | 
|---|
| 229 | #if BOOST_VERSION < 104200 | 
|---|
| 230 | throw boost::program_options::validation_error("Invalid KeyValue given"); | 
|---|
| 231 | #else | 
|---|
| 232 | throw boost::program_options::validation_error( | 
|---|
| 233 | boost::program_options::validation_error::invalid_option_value, | 
|---|
| 234 | std::string("value"), | 
|---|
| 235 | std::string("vector<KeyValuePair>") | 
|---|
| 236 | ); | 
|---|
| 237 | #endif | 
|---|
| 238 | } | 
|---|
| 239 | } | 
|---|
| 240 | std::vector<KeyValuePair> temp; | 
|---|
| 241 | for (std::vector<std::string>::const_iterator iter = values.begin(); | 
|---|
| 242 | iter != values.end();++iter) | 
|---|
| 243 | temp.push_back(KeyValuePair(*iter)); | 
|---|
| 244 | v = boost::any(temp); | 
|---|
| 245 | } | 
|---|