summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--IdManager.cpp57
-rw-r--r--IdManager.h24
-rw-r--r--Makefile.am3
-rw-r--r--Makefile.in21
4 files changed, 102 insertions, 3 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();
+}
diff --git a/IdManager.h b/IdManager.h
new file mode 100644
index 0000000..bc3efb7
--- /dev/null
+++ b/IdManager.h
@@ -0,0 +1,24 @@
+#ifndef IDMANAGER_H_
+#define IDMANAGER_H_
+
+#include <string>
+#include <set>
+#include <map>
+
+
+class IdManager {
+ private:
+ std::set<std::string> usedIds;
+ std::map<std::string, unsigned long> prefixMap;
+
+ public:
+ bool add(const std::string &id);
+ bool remove(const std::string &id);
+
+ bool valid(const std::string &id) const;
+ bool unique(const std::string &id) const;
+
+ std::string generate(const std::string &prefix);
+};
+
+#endif /*IDMANAGER_H_*/
diff --git a/Makefile.am b/Makefile.am
index 94ded86..86aa883 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,5 +1,6 @@
bin_PROGRAMS = zoomedit
zoomedit_SOURCES = zoomedit.cpp window.cpp ui.cpp draw.cpp edit.cpp Vertex.cpp \
- Line.cpp Polygon.cpp Rectangle.cpp Room.cpp Triangle.cpp
+ Line.cpp Polygon.cpp Rectangle.cpp Room.cpp Triangle.cpp \
+ IdManager.cpp
zoomedit_CPPFLAGS = @GTK_CFLAGS@ @GTKGLEXT_CFLAGS@
zoomedit_LDADD = @GTK_LIBS@ @GTKGLEXT_LIBS@ \ No newline at end of file
diff --git a/Makefile.in b/Makefile.in
index 9762995..9265def 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -53,7 +53,8 @@ am_zoomedit_OBJECTS = zoomedit-zoomedit.$(OBJEXT) \
zoomedit-draw.$(OBJEXT) zoomedit-edit.$(OBJEXT) \
zoomedit-Vertex.$(OBJEXT) zoomedit-Line.$(OBJEXT) \
zoomedit-Polygon.$(OBJEXT) zoomedit-Rectangle.$(OBJEXT) \
- zoomedit-Room.$(OBJEXT) zoomedit-Triangle.$(OBJEXT)
+ zoomedit-Room.$(OBJEXT) zoomedit-Triangle.$(OBJEXT) \
+ zoomedit-IdManager.$(OBJEXT)
zoomedit_OBJECTS = $(am_zoomedit_OBJECTS)
zoomedit_DEPENDENCIES =
DEFAULT_INCLUDES = -I.@am__isrc@
@@ -174,7 +175,8 @@ target_alias = @target_alias@
top_builddir = @top_builddir@
top_srcdir = @top_srcdir@
zoomedit_SOURCES = zoomedit.cpp window.cpp ui.cpp draw.cpp edit.cpp Vertex.cpp \
- Line.cpp Polygon.cpp Rectangle.cpp Room.cpp Triangle.cpp
+ Line.cpp Polygon.cpp Rectangle.cpp Room.cpp Triangle.cpp \
+ IdManager.cpp
zoomedit_CPPFLAGS = @GTK_CFLAGS@ @GTKGLEXT_CFLAGS@
zoomedit_LDADD = @GTK_LIBS@ @GTKGLEXT_LIBS@
@@ -266,6 +268,7 @@ mostlyclean-compile:
distclean-compile:
-rm -f *.tab.c
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/zoomedit-IdManager.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/zoomedit-Line.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/zoomedit-Polygon.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/zoomedit-Rectangle.Po@am__quote@
@@ -446,6 +449,20 @@ zoomedit-Triangle.obj: Triangle.cpp
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(zoomedit_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o zoomedit-Triangle.obj `if test -f 'Triangle.cpp'; then $(CYGPATH_W) 'Triangle.cpp'; else $(CYGPATH_W) '$(srcdir)/Triangle.cpp'; fi`
+zoomedit-IdManager.o: IdManager.cpp
+@am__fastdepCXX_TRUE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(zoomedit_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT zoomedit-IdManager.o -MD -MP -MF $(DEPDIR)/zoomedit-IdManager.Tpo -c -o zoomedit-IdManager.o `test -f 'IdManager.cpp' || echo '$(srcdir)/'`IdManager.cpp
+@am__fastdepCXX_TRUE@ mv -f $(DEPDIR)/zoomedit-IdManager.Tpo $(DEPDIR)/zoomedit-IdManager.Po
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='IdManager.cpp' object='zoomedit-IdManager.o' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(zoomedit_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o zoomedit-IdManager.o `test -f 'IdManager.cpp' || echo '$(srcdir)/'`IdManager.cpp
+
+zoomedit-IdManager.obj: IdManager.cpp
+@am__fastdepCXX_TRUE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(zoomedit_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT zoomedit-IdManager.obj -MD -MP -MF $(DEPDIR)/zoomedit-IdManager.Tpo -c -o zoomedit-IdManager.obj `if test -f 'IdManager.cpp'; then $(CYGPATH_W) 'IdManager.cpp'; else $(CYGPATH_W) '$(srcdir)/IdManager.cpp'; fi`
+@am__fastdepCXX_TRUE@ mv -f $(DEPDIR)/zoomedit-IdManager.Tpo $(DEPDIR)/zoomedit-IdManager.Po
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='IdManager.cpp' object='zoomedit-IdManager.obj' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(zoomedit_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o zoomedit-IdManager.obj `if test -f 'IdManager.cpp'; then $(CYGPATH_W) 'IdManager.cpp'; else $(CYGPATH_W) '$(srcdir)/IdManager.cpp'; fi`
+
ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES)
list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \
unique=`for i in $$list; do \