]> granicus.if.org Git - icinga2/commitdiff
Implement include guards for mkclass
authorGunnar Beutner <gunnar.beutner@netways.de>
Tue, 9 Sep 2014 11:30:54 +0000 (13:30 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Tue, 9 Sep 2014 11:30:54 +0000 (13:30 +0200)
fixes #7033

tools/mkclass/classcompiler.cpp
tools/mkclass/classcompiler.hpp

index f67a951ba632bab94a99a806dd280174cc916802..203e90f803cd4cbb26a90d80de254380e66e81e1 100644 (file)
@@ -23,6 +23,7 @@
 #include <stdexcept>
 #include <map>
 #include <vector>
+#include <cstring>
 
 using namespace icinga;
 
@@ -482,10 +483,48 @@ void ClassCompiler::CompileFile(const std::string& path)
        return CompileStream(path, &stream);
 }
 
+std::string ClassCompiler::BaseName(const  std::string& path)
+{
+       char *dir = strdup(path.c_str());
+       std::string result;
+
+       if (dir == NULL)
+               throw std::bad_alloc();
+
+#ifndef _WIN32
+       result = basename(dir);
+#else /* _WIN32 */
+       result = PathFindFileName(dir);
+#endif /* _WIN32 */
+
+       free(dir);
+
+       return result;
+}
+
+std::string ClassCompiler::FileNameToGuardName(const std::string& fname)
+{
+       std::string result = fname;
+
+       for (int i = 0; i < result.size(); i++) {
+               result[i] = toupper(result[i]);
+
+               if (result[i] == '.')
+                       result[i] = '_';
+       }
+
+       return result;
+}
+
 void ClassCompiler::CompileStream(const std::string& path, std::istream *stream)
 {
        stream->exceptions(std::istream::badbit);
 
+       std::string guard_name = FileNameToGuardName(BaseName(path));
+
+       std::cout << "#ifndef " << guard_name << std::endl
+                         << "#define " << guard_name << std::endl << std::endl;
+
        std::cout << "#include \"base/object.hpp\"" << std::endl
                          << "#include \"base/type.hpp\"" << std::endl
                          << "#include \"base/debug.hpp\"" << std::endl
@@ -505,4 +544,6 @@ void ClassCompiler::CompileStream(const std::string& path, std::istream *stream)
        std::cout << "#ifdef _MSC_VER" << std::endl
                          << "#pragma warning ( pop )" << std::endl
                          << "#endif /* _MSC_VER */" << std::endl;
+
+       std::cout << "#endif /* " << guard_name << " */" << std::endl;
 }
index 2701d534abf2d592a498437aedacc6ccf602e2a4..fe653eb3d18eb94f17c824418eaf60273a1ea705 100644 (file)
@@ -151,6 +151,8 @@ private:
        void *m_Scanner;
 
        static unsigned long SDBM(const std::string& str, size_t len);
+       static std::string BaseName(const std::string& path);
+       static std::string FileNameToGuardName(const std::string& path);
 };
 
 }