2bf3a4fdce028273741cf42110938f66255108ce
[android/external-libkmsxx.git] / kms++util / src / helpers.cpp
1 #include <kms++util/kms++util.h>
3 using namespace std;
5 namespace kms {
7 Connector* resolve_connector(Card& card, const string& str)
8 {
9         if (str.length() == 0)
10                 return nullptr;
12         auto connectors = card.get_connectors();
14         if (str[0] == '@') {
15                 char* endptr;
16                 unsigned id = strtoul(str.c_str() + 1, &endptr, 10);
17                 if (*endptr == 0) {
18                         Connector* c = card.get_connector(id);
19                         if (!c)
20                                 return nullptr;
21                         else
22                                 return c;
23                 }
24         } else {
25                 char* endptr;
26                 unsigned idx = strtoul(str.c_str(), &endptr, 10);
27                 if (*endptr == 0) {
28                         if (idx >= connectors.size())
29                                 return nullptr;
30                         else
31                                 return connectors[idx];
32                 }
33         }
35         for (Connector* conn : connectors) {
36                 if (to_lower(conn->fullname()).find(to_lower(str)) != string::npos)
37                         return conn;
38         }
40         return nullptr;
41 }
43 }