summaryrefslogtreecommitdiff
path: root/libkms++util/helpers.cpp
blob: 4aa3194c60ab7d4abfdc4b800b40cb6092ddf03c (plain)
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
#include "kms++util.h"
#include "strhelpers.h"

using namespace std;

namespace kms {

Connector* resolve_connector(Card& card, const string& str)
{
	if (str.length() == 0)
		return nullptr;

	auto connectors = card.get_connectors();

	if (str[0] == '@') {
		char* endptr;
		unsigned id = strtoul(str.c_str() + 1, &endptr, 10);
		if (*endptr == 0) {
			Connector* c = card.get_connector(id);
			if (!c)
				return nullptr;
			else
				return c;
		}
	} else {
		char* endptr;
		unsigned idx = strtoul(str.c_str(), &endptr, 10);
		if (*endptr == 0) {
			if (idx >= connectors.size())
				return nullptr;
			else
				return connectors[idx];
		}
	}

	for (Connector* conn : connectors) {
		if (to_lower(conn->fullname()).find(to_lower(str)) != string::npos)
			return conn;
	}

	return nullptr;
}

}