blob: d60417c098ae85b2f4f9958faa22f48f7900bbe2 (
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
#include "IdManager.h"
#include <sstream>
bool IdManager::add(const std::string &id) {
if(!valid(id)) return false;
return usedIds.insert(id).second;
}
bool IdManager::remove(const std::string &id) {
return (usedIds.erase(id) > 0);
}
bool IdManager::isValid(const std::string &id) const {
if(id.empty()) return false;
if(id[0] >= '0' && id[0] <= '9') return false;
if(id.size() >= 3) {
if(std::tolower(id[0]) == 'x' && std::tolower(id[1]) == 'm' && std::tolower(id[2]) == 'l')
return false;
}
for(std::string::const_iterator c = id.begin(); c != id.end(); c++) {
if(!(*c >= '0' && *c <= '9') && !(*c >= 'A' && *c <= 'Z') && !(*c >= 'a' && *c <= 'z')
&& *c != '_' && *c != '-' && *c != '.')
return false;
}
return true;
}
bool IdManager::unique(const std::string &id) const {
return (usedIds.find(id) == usedIds.end());
}
bool IdManager::valid(const std::string &id) const {
return isValid(id) && unique(id);
}
std::string IdManager::generate(const std::string &prefix) {
if(!isValid(prefix)) return std::string();
unsigned long n = 0;
std::map<std::string, unsigned long>::iterator it = prefixMap.find(prefix);
if(it != prefixMap.end()) {
n = it->second + 1;
prefixMap.erase(it);
}
std::ostringstream id;
do {
id.str(std::string());
id << prefix << n++;
} while(!unique(id.str()));
prefixMap.insert(std::make_pair<std::string, unsigned long>(prefix, n-1));
usedIds.insert(id.str());
return id.str();
}
|