Implement include guards for mkclass

fixes #7033
This commit is contained in:
Gunnar Beutner 2014-09-09 13:30:54 +02:00
parent 3c0833429c
commit 292d9ee740
2 changed files with 43 additions and 0 deletions

View 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;
}

View 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);
};
}