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