diff options
author | neoraider <devnull@localhost> | 2007-09-28 23:41:00 +0200 |
---|---|---|
committer | neoraider <devnull@localhost> | 2007-09-28 23:41:00 +0200 |
commit | 38b318a57f0a713eb2aa8407893eea142e57427e (patch) | |
tree | 34d7b79874d6bab2c06f413270666da0d624e5cf /IdManager.cpp | |
parent | 50b65f72c02382d826802fa7c76ebc7bf53611fc (diff) | |
download | zoomedit-38b318a57f0a713eb2aa8407893eea142e57427e.tar zoomedit-38b318a57f0a713eb2aa8407893eea142e57427e.zip |
zoomedit: IdManager implementiert.
Diffstat (limited to 'IdManager.cpp')
-rw-r--r-- | IdManager.cpp | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/IdManager.cpp b/IdManager.cpp new file mode 100644 index 0000000..a610036 --- /dev/null +++ b/IdManager.cpp @@ -0,0 +1,57 @@ +#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::valid(const std::string &id) const { + if(id.empty()) return false; + if(id[0] >= '0' && id[0] <= '9') 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 { + if(!valid(id)) return false; + + return (usedIds.find(id) == usedIds.end()); +} + +std::string IdManager::generate(const std::string &prefix) { + if(!valid(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(); +} |