| 1 | /*
|
|---|
| 2 | * Project: MoleCuilder
|
|---|
| 3 | * Description: creates and alters molecular systems
|
|---|
| 4 | * Copyright (C) 2010 University of Bonn. All rights reserved.
|
|---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | /*
|
|---|
| 9 | * CommandLineParser_validate.cpp
|
|---|
| 10 | *
|
|---|
| 11 | * Created on: Nov 8, 2010
|
|---|
| 12 | * Author: heber
|
|---|
| 13 | */
|
|---|
| 14 |
|
|---|
| 15 | // include config.h
|
|---|
| 16 | #ifdef HAVE_CONFIG_H
|
|---|
| 17 | #include <config.h>
|
|---|
| 18 | #endif
|
|---|
| 19 |
|
|---|
| 20 | #include "Helpers/MemDebug.hpp"
|
|---|
| 21 |
|
|---|
| 22 | #include <boost/version.hpp>
|
|---|
| 23 |
|
|---|
| 24 | #include <iostream>
|
|---|
| 25 | #include <string>
|
|---|
| 26 |
|
|---|
| 27 | #include "Actions/Values.hpp"
|
|---|
| 28 | #include "CommandLineParser_validate.hpp"
|
|---|
| 29 |
|
|---|
| 30 | /** boost::program_options validator specialization for VectorValue.
|
|---|
| 31 | * \param &v reference for return value
|
|---|
| 32 | * \param &values string vector of scanned options
|
|---|
| 33 | * \param *
|
|---|
| 34 | * \param
|
|---|
| 35 | *
|
|---|
| 36 | */
|
|---|
| 37 | void validate(boost::any& v, const std::vector<std::string>& values, VectorValue *, int)
|
|---|
| 38 | {
|
|---|
| 39 | VectorValue VV;
|
|---|
| 40 | std::vector<std::string> components;
|
|---|
| 41 |
|
|---|
| 42 | // split comma-separated values
|
|---|
| 43 | if (values.size() != 1) {
|
|---|
| 44 | std::cerr << "Not one vector but " << values.size() << " given " << std::endl;
|
|---|
| 45 | #if BOOST_VERSION < 104200
|
|---|
| 46 | throw boost::program_options::validation_error("Unequal to one vector given");
|
|---|
| 47 | #else
|
|---|
| 48 | throw boost::program_options::validation_error(
|
|---|
| 49 | boost::program_options::validation_error::invalid_option_value,
|
|---|
| 50 | std::string("value"),
|
|---|
| 51 | std::string("VectorValue")
|
|---|
| 52 | );
|
|---|
| 53 | #endif
|
|---|
| 54 | }
|
|---|
| 55 | std::string argument(values.at(0));
|
|---|
| 56 | std::string::iterator Aiter = argument.begin();
|
|---|
| 57 | std::string::iterator Biter = argument.begin();
|
|---|
| 58 | for (; Aiter != argument.end(); ++Aiter) {
|
|---|
| 59 | if (*Aiter == ',') {
|
|---|
| 60 | components.push_back(std::string(Biter,Aiter));
|
|---|
| 61 | do {
|
|---|
| 62 | Aiter++;
|
|---|
| 63 | } while (*Aiter == ' ' || *Aiter == '\t');
|
|---|
| 64 | Biter = Aiter;
|
|---|
| 65 | }
|
|---|
| 66 | }
|
|---|
| 67 | components.push_back(std::string(Biter,argument.end()));
|
|---|
| 68 |
|
|---|
| 69 | if (components.size() != 3) {
|
|---|
| 70 | std::cerr << "Specified vector does not have three components but " << components.size() << std::endl;
|
|---|
| 71 | #if BOOST_VERSION < 104200
|
|---|
| 72 | throw boost::program_options::validation_error("Specified vector does not have three components");
|
|---|
| 73 | #else
|
|---|
| 74 | throw boost::program_options::validation_error(
|
|---|
| 75 | boost::program_options::validation_error::invalid_option_value,
|
|---|
| 76 | std::string("value"),
|
|---|
| 77 | std::string("VectorValue")
|
|---|
| 78 | );
|
|---|
| 79 | #endif
|
|---|
| 80 | }
|
|---|
| 81 | VV.x = boost::lexical_cast<double>(components.at(0));
|
|---|
| 82 | VV.y = boost::lexical_cast<double>(components.at(1));
|
|---|
| 83 | VV.z = boost::lexical_cast<double>(components.at(2));
|
|---|
| 84 | v = boost::any(VectorValue(VV));
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | void validate(boost::any& v, const std::vector<std::string>& values, BoxValue *, int)
|
|---|
| 88 | {
|
|---|
| 89 | BoxValue BV;
|
|---|
| 90 | std::vector<std::string> components;
|
|---|
| 91 |
|
|---|
| 92 | // split comma-separated values
|
|---|
| 93 | if (values.size() != 1) {
|
|---|
| 94 | std::cerr << "Not one vector but " << values.size() << " given " << std::endl;
|
|---|
| 95 | #if BOOST_VERSION < 104200
|
|---|
| 96 | throw boost::program_options::validation_error("Unequal to one vector given");
|
|---|
| 97 | #else
|
|---|
| 98 | throw boost::program_options::validation_error(
|
|---|
| 99 | boost::program_options::validation_error::invalid_option_value,
|
|---|
| 100 | std::string("value"),
|
|---|
| 101 | std::string("BoxValue")
|
|---|
| 102 | );
|
|---|
| 103 | #endif
|
|---|
| 104 | }
|
|---|
| 105 | std::string argument(values.at(0));
|
|---|
| 106 | std::string::iterator Aiter = argument.begin();
|
|---|
| 107 | std::string::iterator Biter = argument.begin();
|
|---|
| 108 | for (; Aiter != argument.end(); ++Aiter) {
|
|---|
| 109 | if (*Aiter == ',') {
|
|---|
| 110 | components.push_back(std::string(Biter,Aiter));
|
|---|
| 111 | do {
|
|---|
| 112 | Aiter++;
|
|---|
| 113 | } while (*Aiter == ' ' || *Aiter == '\t');
|
|---|
| 114 | Biter = Aiter;
|
|---|
| 115 | }
|
|---|
| 116 | }
|
|---|
| 117 | components.push_back(std::string(Biter,argument.end()));
|
|---|
| 118 |
|
|---|
| 119 | if (components.size() != 6) {
|
|---|
| 120 | std::cerr << "Specified vector does not have three components but " << components.size() << std::endl;
|
|---|
| 121 | #if BOOST_VERSION < 104200
|
|---|
| 122 | throw boost::program_options::validation_error("Specified symmetric box matrix does not have six components");
|
|---|
| 123 | #else
|
|---|
| 124 | throw boost::program_options::validation_error(
|
|---|
| 125 | boost::program_options::validation_error::invalid_option_value,
|
|---|
| 126 | std::string("value"),
|
|---|
| 127 | std::string("BoxValue")
|
|---|
| 128 | );
|
|---|
| 129 | #endif
|
|---|
| 130 | }
|
|---|
| 131 | BV.xx = boost::lexical_cast<double>(components.at(0));
|
|---|
| 132 | BV.yx = boost::lexical_cast<double>(components.at(1));
|
|---|
| 133 | BV.yy = boost::lexical_cast<double>(components.at(2));
|
|---|
| 134 | BV.zx = boost::lexical_cast<double>(components.at(3));
|
|---|
| 135 | BV.zy = boost::lexical_cast<double>(components.at(4));
|
|---|
| 136 | BV.zz = boost::lexical_cast<double>(components.at(5));
|
|---|
| 137 | v = boost::any(BoxValue(BV));
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | /** boost::program_options validator specialization for boost::filesystem::path.
|
|---|
| 141 | * \param &v reference for return value
|
|---|
| 142 | * \param &values string vector of scanned options
|
|---|
| 143 | * \param *
|
|---|
| 144 | * \param
|
|---|
| 145 | *
|
|---|
| 146 | */
|
|---|
| 147 | void validate(boost::any& v, const std::vector<std::string>& values, boost::filesystem::path *, int)
|
|---|
| 148 | {
|
|---|
| 149 | boost::filesystem::path filename;
|
|---|
| 150 |
|
|---|
| 151 | std::cerr << "boost::filesystem::path validator used." << std::endl;
|
|---|
| 152 |
|
|---|
| 153 | // split comma-separated values
|
|---|
| 154 | if (values.size() != 1) {
|
|---|
| 155 | std::cerr << "Not one file but " << values.size() << " given " << std::endl;
|
|---|
| 156 | #if BOOST_VERSION < 104200
|
|---|
| 157 | throw boost::program_options::validation_error("Unequal to one file given");
|
|---|
| 158 | #else
|
|---|
| 159 | if (values.size() == 0) {
|
|---|
| 160 | throw boost::program_options::validation_error(
|
|---|
| 161 | boost::program_options::validation_error::at_least_one_value_required,
|
|---|
| 162 | std::string("value"),
|
|---|
| 163 | std::string("boost::filesystem::path")
|
|---|
| 164 | );
|
|---|
| 165 | } else {
|
|---|
| 166 | throw boost::program_options::validation_error(
|
|---|
| 167 | boost::program_options::validation_error::multiple_values_not_allowed,
|
|---|
| 168 | std::string("value"),
|
|---|
| 169 | std::string("boost::filesystem::path")
|
|---|
| 170 | );
|
|---|
| 171 | }
|
|---|
| 172 | #endif
|
|---|
| 173 | }
|
|---|
| 174 | filename = values.at(0);
|
|---|
| 175 | v = boost::any(boost::filesystem::path(filename));
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|