| 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 |   VV.vectorstring = values.at(0);
 | 
|---|
| 72 |   v = boost::any(VectorValue(VV));
 | 
|---|
| 73 | }
 | 
|---|
| 74 | 
 | 
|---|
| 75 | void validate(boost::any& v, const std::vector<std::string>& values, RealSpaceMatrixValue *, int)
 | 
|---|
| 76 | {
 | 
|---|
| 77 |   RealSpaceMatrixValue RSMV;
 | 
|---|
| 78 |   std::vector<std::string> components;
 | 
|---|
| 79 | 
 | 
|---|
| 80 |   // split comma-separated values
 | 
|---|
| 81 |   if (values.size() != 1) {
 | 
|---|
| 82 |     std::cerr <<  "Not one vector but " << values.size() << " given " << std::endl;
 | 
|---|
| 83 | #if BOOST_VERSION < 104200
 | 
|---|
| 84 |     throw boost::program_options::validation_error("Unequal to one vector given");
 | 
|---|
| 85 | #else
 | 
|---|
| 86 |     throw boost::program_options::validation_error(
 | 
|---|
| 87 |         boost::program_options::validation_error::invalid_option_value,
 | 
|---|
| 88 |         std::string("value"),
 | 
|---|
| 89 |         std::string("BoxValue")
 | 
|---|
| 90 |     );
 | 
|---|
| 91 | #endif
 | 
|---|
| 92 |   }
 | 
|---|
| 93 |   std::string argument(values.at(0));
 | 
|---|
| 94 |   std::string::iterator Aiter = argument.begin();
 | 
|---|
| 95 |   std::string::iterator Biter = argument.begin();
 | 
|---|
| 96 |   for (; Aiter != argument.end(); ++Aiter) {
 | 
|---|
| 97 |     if (*Aiter == ',') {
 | 
|---|
| 98 |       components.push_back(std::string(Biter,Aiter));
 | 
|---|
| 99 |       do {
 | 
|---|
| 100 |         Aiter++;
 | 
|---|
| 101 |       } while (*Aiter == ' ' || *Aiter == '\t');
 | 
|---|
| 102 |       Biter = Aiter;
 | 
|---|
| 103 |     }
 | 
|---|
| 104 |   }
 | 
|---|
| 105 |   components.push_back(std::string(Biter,argument.end()));
 | 
|---|
| 106 | 
 | 
|---|
| 107 |   if (components.size() != 6) {
 | 
|---|
| 108 |     std::cerr <<  "Specified vector does not have three components but " << components.size() << std::endl;
 | 
|---|
| 109 | #if BOOST_VERSION < 104200
 | 
|---|
| 110 |     throw boost::program_options::validation_error("Specified symmetric box matrix does not have six components");
 | 
|---|
| 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 |   for (size_t i=0;i<(NDIM*(NDIM+1))/2; ++i)
 | 
|---|
| 120 |     RSMV.matrix[i] = boost::lexical_cast<double>(components.at(i));
 | 
|---|
| 121 |   v = boost::any(RealSpaceMatrixValue(RSMV));
 | 
|---|
| 122 | }
 | 
|---|
| 123 | 
 | 
|---|
| 124 | /** boost::program_options validator specialization for boost::filesystem::path.
 | 
|---|
| 125 |  * \param &v  reference for return value
 | 
|---|
| 126 |  * \param &values string vector of scanned options
 | 
|---|
| 127 |  * \param *
 | 
|---|
| 128 |  * \param
 | 
|---|
| 129 |  *
 | 
|---|
| 130 |  */
 | 
|---|
| 131 | void validate(boost::any& v, const std::vector<std::string>& values, boost::filesystem::path *, int)
 | 
|---|
| 132 | {
 | 
|---|
| 133 | //  std::cerr << "boost::filesystem::path validator used." << std::endl;
 | 
|---|
| 134 | 
 | 
|---|
| 135 |   // Make sure no previous assignment to 'a' was made.
 | 
|---|
| 136 |   boost::program_options::validators::check_first_occurrence(v);
 | 
|---|
| 137 |   // Extract the first string from 'values'. If there is more than
 | 
|---|
| 138 |   // one string, it's an error, and exception will be thrown.
 | 
|---|
| 139 |   const std::string& s = boost::program_options::validators::get_single_string(values);
 | 
|---|
| 140 | 
 | 
|---|
| 141 |   v = boost::any(boost::filesystem::path(s));
 | 
|---|
| 142 | }
 | 
|---|
| 143 | 
 | 
|---|
| 144 | /** boost::program_options validator specialization for boost::filesystem::path.
 | 
|---|
| 145 |  * \param &v  reference for return value
 | 
|---|
| 146 |  * \param &values string vector of scanned options
 | 
|---|
| 147 |  * \param *
 | 
|---|
| 148 |  * \param
 | 
|---|
| 149 |  *
 | 
|---|
| 150 |  */
 | 
|---|
| 151 | void validate(boost::any& v, const std::vector<std::string>& values, KeyValuePair *, int)
 | 
|---|
| 152 | {
 | 
|---|
| 153 | //  std::cerr << "KeyValuePair validator used." << std::endl;
 | 
|---|
| 154 | 
 | 
|---|
| 155 |   // Make sure no previous assignment to 'a' was made.
 | 
|---|
| 156 |   boost::program_options::validators::check_first_occurrence(v);
 | 
|---|
| 157 |   // Extract the first string from 'values'. If there is more than
 | 
|---|
| 158 |   // one string, it's an error, and exception will be thrown.
 | 
|---|
| 159 |   const std::string& s = boost::program_options::validators::get_single_string(values);
 | 
|---|
| 160 | 
 | 
|---|
| 161 |   if (s.find("=") == std::string::npos) {
 | 
|---|
| 162 | #if BOOST_VERSION < 104200
 | 
|---|
| 163 |     throw boost::program_options::validation_error("Invalid KeyValue given");
 | 
|---|
| 164 | #else
 | 
|---|
| 165 | throw boost::program_options::validation_error(
 | 
|---|
| 166 |     boost::program_options::validation_error::invalid_option_value,
 | 
|---|
| 167 |     std::string("value"),
 | 
|---|
| 168 |     std::string("vector<KeyValuePair>")
 | 
|---|
| 169 |     );
 | 
|---|
| 170 | #endif
 | 
|---|
| 171 |   }
 | 
|---|
| 172 |   v = boost::any(KeyValuePair(s));
 | 
|---|
| 173 | }
 | 
|---|
| 174 | 
 | 
|---|
| 175 | /** boost::program_options validator specialization for boost::filesystem::path.
 | 
|---|
| 176 |  * \param &v  reference for return value
 | 
|---|
| 177 |  * \param &values string vector of scanned options
 | 
|---|
| 178 |  * \param *
 | 
|---|
| 179 |  * \param
 | 
|---|
| 180 |  *
 | 
|---|
| 181 |  */
 | 
|---|
| 182 | void validate(boost::any& v, const std::vector<std::string>& values, std::vector<KeyValuePair> *, int)
 | 
|---|
| 183 | {
 | 
|---|
| 184 | //  std::cerr << "vector<KeyValuePair> validator used." << std::endl;
 | 
|---|
| 185 | 
 | 
|---|
| 186 |   // split comma-separated values
 | 
|---|
| 187 |   if (values.size() > 1) {
 | 
|---|
| 188 | #if BOOST_VERSION < 104200
 | 
|---|
| 189 |     throw boost::program_options::validation_error("Unequal to one file given");
 | 
|---|
| 190 | #else
 | 
|---|
| 191 |     if (values.size() == 0) {
 | 
|---|
| 192 |       throw boost::program_options::validation_error(
 | 
|---|
| 193 |           boost::program_options::validation_error::at_least_one_value_required,
 | 
|---|
| 194 |           std::string("value"),
 | 
|---|
| 195 |           std::string("vector<KeyValuePair>")
 | 
|---|
| 196 |       );
 | 
|---|
| 197 |     }
 | 
|---|
| 198 | #endif
 | 
|---|
| 199 |     for (std::vector<std::string>::const_iterator iter = values.begin();
 | 
|---|
| 200 |         iter != values.end();++iter)
 | 
|---|
| 201 |       if ((*iter).find("=") == std::string::npos) {
 | 
|---|
| 202 | #if BOOST_VERSION < 104200
 | 
|---|
| 203 |     throw boost::program_options::validation_error("Invalid KeyValue given");
 | 
|---|
| 204 | #else
 | 
|---|
| 205 |     throw boost::program_options::validation_error(
 | 
|---|
| 206 |         boost::program_options::validation_error::invalid_option_value,
 | 
|---|
| 207 |         std::string("value"),
 | 
|---|
| 208 |         std::string("vector<KeyValuePair>")
 | 
|---|
| 209 |     );
 | 
|---|
| 210 | #endif
 | 
|---|
| 211 |       }
 | 
|---|
| 212 |   }
 | 
|---|
| 213 |   std::vector<KeyValuePair> temp;
 | 
|---|
| 214 |   for (std::vector<std::string>::const_iterator iter = values.begin();
 | 
|---|
| 215 |       iter != values.end();++iter)
 | 
|---|
| 216 |     temp.push_back(KeyValuePair(*iter));
 | 
|---|
| 217 |   v = boost::any(temp);
 | 
|---|
| 218 | }
 | 
|---|