File: validator.cpp

package info (click to toggle)
libm2k 0.9.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 95,580 kB
  • sloc: xml: 1,611,497; cpp: 16,278; python: 4,181; cs: 516; sh: 471; ansic: 403; makefile: 35
file content (165 lines) | stat: -rw-r--r-- 5,128 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright (c) 2019 Analog Devices Inc.
 *
 * This file is part of libm2k
 * (see http://www.github.com/analogdevicesinc/libm2k).
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <sstream>
#include <stdexcept>
#include <cctype>
#include "validator.h"

void Validator::validate(const std::string &argument, const char *argumentName, int &value)
{
	std::string strValue = validateName(argument, argumentName);
	if (is_number(strValue)) {
		value = std::stoi(strValue);
	} else if(is_hexa_number(strValue)) {
		value = std::stoul(strValue, nullptr, 16);
	} else {
		throw std::invalid_argument(std::string(argumentName) + " must be an integer\n");
	}
}

void Validator::validate(const std::string &argument, const char *argumentName, double &value)
{
	std::string strValue = validateName(argument, argumentName);
	validate(strValue, value);
}

void Validator::validate(const std::string &argument, const char *argumentName, bool &value)
{
	std::string strValue = validateName(argument, argumentName);
	if (strValue != "0" && strValue != "1") {
		throw std::invalid_argument("Possible values for raw : {0 | 1}\n");
	}
	value = static_cast<bool>(std::stoi(strValue));
}

void Validator::validate(const std::string &argument, const char *argumentName, std::string &value)
{
	value = validateName(argument, argumentName);
}

void Validator::validate(const std::string &argument, const char *argumentName, std::vector<unsigned int> &value)
{
	std::string strValue = validateName(argument, argumentName);
	std::istringstream iss(strValue);
	while (getline(iss, strValue, ',')) {
		if (!(is_number(strValue) && std::stoi(strValue) >= 0 && std::stoi(strValue) <= 15)) {
			throw std::runtime_error(
				"Possible values for digital channel : {0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 \n"
				"8 | 9 | 10 | 11 | 12 | 13 | 14 | 15}\n");
		}
		value.push_back(std::stoi(strValue));
	}
}

void Validator::validate(const std::string &argument, const char *argumentName, std::vector<uint8_t > &value)
{
	std::string strValue = validateName(argument, argumentName);
	std::istringstream iss(strValue);
	while (getline(iss, strValue, ',')) {
		if (is_number(strValue)) {
			value.push_back(std::stoi(strValue));
		} else if (is_hexa_number(strValue)) {
			value.push_back(std::stoul(strValue, nullptr, 16));
		} else {
			for (auto c : strValue) {
				value.push_back((uint8_t )c);
			}
		}

	}
}

std::map<std::string, std::string> Validator::validate(std::vector<std::string> arguments)
{
	std::map<std::string, std::string> values;
	for (auto it = arguments.begin(); it != arguments.end(); it++) {
		std::pair<std::string, std::string> value;
		if ((*it).find('=') == std::string::npos) {
			throw std::runtime_error("Expecting the following format: <argument>=<value>\n");
		}
		std::istringstream iss(*it);
		getline(iss, value.first, '=');
		value.second = *it;
		values.insert(value);
	}
	return values;
}

void Validator::validate(std::string strNumber, double &number)
{
	try {
		number = std::stod(strNumber);
	} catch (std::exception const& e) {
		throw std::runtime_error("'" + strNumber + "' is not a number.\n");
	}
}

void Validator::validate(std::string strNumber, uint16_t &number)
{
	try {
		number = std::stoi(strNumber);
	} catch (std::exception const& e) {
		throw std::runtime_error("'" + strNumber + "' is not a number.\n");
	}
}

bool Validator::validateUri(const std::string &uri)
{
	std::vector<std::string> uries{"usb:", "ip:", "local:", "xml:", "serial:"};
	for (auto &validUri : uries) {
		if (uri.find(validUri) != std::string::npos) {
			return true;
		}
	}
	return false;
}

std::string Validator::validateName(const std::string &argument, const char *argumentName)
{
	if (argument.find('=') == std::string::npos) {
		throw std::runtime_error("Expecting the following format: " + std::string(argumentName) + "=<value>\n");
	}
	std::istringstream iss(argument);
	std::string token;
	getline(iss, token, '=');
	if (token != argumentName) {
		throw std::runtime_error("Expecting the following format: " + std::string(argumentName) + "=<value>\n");
	}
	getline(iss, token, '=');
	return token;
}

bool Validator::is_number(const std::string &s)
{
	std::string::const_iterator it = s.begin();
	while (it != s.end() && std::isdigit(*it)) {
		++it;
	}
	return !s.empty() && it == s.end();
}

bool Validator::is_hexa_number(const std::string &s)
{
	return s.compare(0, 2, "0x") == 0
	       && s.size() > 2
	       && s.find_first_not_of("0123456789abcdefABCDEF", 2) == std::string::npos;
}