| 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 |  | 
|---|
| 45 | /** boost::program_options validator specialization for VectorValue. | 
|---|
| 46 | * \param &v  reference for return value | 
|---|
| 47 | * \param &values string vector of scanned options | 
|---|
| 48 | * \param * | 
|---|
| 49 | * \param | 
|---|
| 50 | * | 
|---|
| 51 | */ | 
|---|
| 52 | void validate(boost::any& v, const std::vector<std::string>& values, VectorValue *, int) | 
|---|
| 53 | { | 
|---|
| 54 | VectorValue VV; | 
|---|
| 55 | std::vector<std::string> components; | 
|---|
| 56 |  | 
|---|
| 57 | // split comma-separated values | 
|---|
| 58 | if (values.size() != 1) { | 
|---|
| 59 | std::cerr <<  "Not one vector but " << values.size() << " given " << std::endl; | 
|---|
| 60 | #if BOOST_VERSION < 104200 | 
|---|
| 61 | throw boost::program_options::validation_error("Unequal to one vector given"); | 
|---|
| 62 | #else | 
|---|
| 63 | throw boost::program_options::validation_error( | 
|---|
| 64 | boost::program_options::validation_error::invalid_option_value, | 
|---|
| 65 | std::string("value"), | 
|---|
| 66 | std::string("VectorValue") | 
|---|
| 67 | ); | 
|---|
| 68 | #endif | 
|---|
| 69 | } | 
|---|
| 70 | std::string argument(values.at(0)); | 
|---|
| 71 | std::string::iterator Aiter = argument.begin(); | 
|---|
| 72 | std::string::iterator Biter = argument.begin(); | 
|---|
| 73 | for (; Aiter != argument.end(); ++Aiter) { | 
|---|
| 74 | if (*Aiter == ',') { | 
|---|
| 75 | components.push_back(std::string(Biter,Aiter)); | 
|---|
| 76 | do { | 
|---|
| 77 | Aiter++; | 
|---|
| 78 | } while (*Aiter == ' ' || *Aiter == '\t'); | 
|---|
| 79 | Biter = Aiter; | 
|---|
| 80 | } | 
|---|
| 81 | } | 
|---|
| 82 | components.push_back(std::string(Biter,argument.end())); | 
|---|
| 83 |  | 
|---|
| 84 | if (components.size() != 3) { | 
|---|
| 85 | std::cerr <<  "Specified vector does not have three components but " << components.size() << std::endl; | 
|---|
| 86 | #if BOOST_VERSION < 104200 | 
|---|
| 87 | throw boost::program_options::validation_error("Specified vector does not have three components"); | 
|---|
| 88 | #else | 
|---|
| 89 | throw boost::program_options::validation_error( | 
|---|
| 90 | boost::program_options::validation_error::invalid_option_value, | 
|---|
| 91 | std::string("value"), | 
|---|
| 92 | std::string("VectorValue") | 
|---|
| 93 | ); | 
|---|
| 94 | #endif | 
|---|
| 95 | } | 
|---|
| 96 | for (size_t i=0;i<NDIM;++i) | 
|---|
| 97 | VV.vector[i] = boost::lexical_cast<double>(components.at(i)); | 
|---|
| 98 | v = boost::any(VectorValue(VV)); | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | void validate(boost::any& v, const std::vector<std::string>& values, RealSpaceMatrixValue *, int) | 
|---|
| 102 | { | 
|---|
| 103 | RealSpaceMatrixValue RSMV; | 
|---|
| 104 | std::vector<std::string> components; | 
|---|
| 105 |  | 
|---|
| 106 | // split comma-separated values | 
|---|
| 107 | if (values.size() != 1) { | 
|---|
| 108 | std::cerr <<  "Not one vector but " << values.size() << " given " << std::endl; | 
|---|
| 109 | #if BOOST_VERSION < 104200 | 
|---|
| 110 | throw boost::program_options::validation_error("Unequal to one vector given"); | 
|---|
| 111 | #else | 
|---|
| 112 | throw boost::program_options::validation_error( | 
|---|
| 113 | boost::program_options::validation_error::invalid_option_value, | 
|---|
| 114 | std::string("value"), | 
|---|
| 115 | std::string("BoxValue") | 
|---|
| 116 | ); | 
|---|
| 117 | #endif | 
|---|
| 118 | } | 
|---|
| 119 | std::string argument(values.at(0)); | 
|---|
| 120 | std::string::iterator Aiter = argument.begin(); | 
|---|
| 121 | std::string::iterator Biter = argument.begin(); | 
|---|
| 122 | for (; Aiter != argument.end(); ++Aiter) { | 
|---|
| 123 | if (*Aiter == ',') { | 
|---|
| 124 | components.push_back(std::string(Biter,Aiter)); | 
|---|
| 125 | do { | 
|---|
| 126 | Aiter++; | 
|---|
| 127 | } while (*Aiter == ' ' || *Aiter == '\t'); | 
|---|
| 128 | Biter = Aiter; | 
|---|
| 129 | } | 
|---|
| 130 | } | 
|---|
| 131 | components.push_back(std::string(Biter,argument.end())); | 
|---|
| 132 |  | 
|---|
| 133 | if (components.size() != 6) { | 
|---|
| 134 | std::cerr <<  "Specified vector does not have three components but " << components.size() << std::endl; | 
|---|
| 135 | #if BOOST_VERSION < 104200 | 
|---|
| 136 | throw boost::program_options::validation_error("Specified symmetric box matrix does not have six components"); | 
|---|
| 137 | #else | 
|---|
| 138 | throw boost::program_options::validation_error( | 
|---|
| 139 | boost::program_options::validation_error::invalid_option_value, | 
|---|
| 140 | std::string("value"), | 
|---|
| 141 | std::string("BoxValue") | 
|---|
| 142 | ); | 
|---|
| 143 | #endif | 
|---|
| 144 | } | 
|---|
| 145 | for (size_t i=0;i<(NDIM*(NDIM+1))/2; ++i) | 
|---|
| 146 | RSMV.matrix[i] = boost::lexical_cast<double>(components.at(i)); | 
|---|
| 147 | v = boost::any(RealSpaceMatrixValue(RSMV)); | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | /** boost::program_options validator specialization for boost::filesystem::path. | 
|---|
| 151 | * \param &v  reference for return value | 
|---|
| 152 | * \param &values string vector of scanned options | 
|---|
| 153 | * \param * | 
|---|
| 154 | * \param | 
|---|
| 155 | * | 
|---|
| 156 | */ | 
|---|
| 157 | void validate(boost::any& v, const std::vector<std::string>& values, boost::filesystem::path *, int) | 
|---|
| 158 | { | 
|---|
| 159 | boost::filesystem::path filename; | 
|---|
| 160 |  | 
|---|
| 161 | std::cerr << "boost::filesystem::path validator used." << std::endl; | 
|---|
| 162 |  | 
|---|
| 163 | // split comma-separated values | 
|---|
| 164 | if (values.size() != 1) { | 
|---|
| 165 | std::cerr <<  "Not one file but " << values.size() << " given " << std::endl; | 
|---|
| 166 | #if BOOST_VERSION < 104200 | 
|---|
| 167 | throw boost::program_options::validation_error("Unequal to one file given"); | 
|---|
| 168 | #else | 
|---|
| 169 | if (values.size() == 0) { | 
|---|
| 170 | throw boost::program_options::validation_error( | 
|---|
| 171 | boost::program_options::validation_error::at_least_one_value_required, | 
|---|
| 172 | std::string("value"), | 
|---|
| 173 | std::string("boost::filesystem::path") | 
|---|
| 174 | ); | 
|---|
| 175 | } else { | 
|---|
| 176 | throw boost::program_options::validation_error( | 
|---|
| 177 | boost::program_options::validation_error::multiple_values_not_allowed, | 
|---|
| 178 | std::string("value"), | 
|---|
| 179 | std::string("boost::filesystem::path") | 
|---|
| 180 | ); | 
|---|
| 181 | } | 
|---|
| 182 | #endif | 
|---|
| 183 | } | 
|---|
| 184 | filename = values.at(0); | 
|---|
| 185 | v = boost::any(boost::filesystem::path(filename)); | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|