diff --git a/src/HFileGen.cpp b/src/HFileGen.cpp index 25f2ff1..f7daf24 100644 --- a/src/HFileGen.cpp +++ b/src/HFileGen.cpp @@ -17,7 +17,7 @@ void HFileGen::Create(ConfigFile& conf) if(extensionPos != std::string::npos) { std::string filename = it->substr(path.length()); - if(it->substr(extensionPos+1) == "h" && filename != conf.GetConfigPath() + conf.GetSettingString(ConfigSetting::HFileName)) + if(Utils::IsHeaderFile(filename) && filename != conf.GetConfigPath() + conf.GetSettingString(ConfigSetting::HFileName)) { // Make files sorted in alphabetical order hFiles.emplace(filename); diff --git a/src/IncludeDeps.cpp b/src/IncludeDeps.cpp index 8e90cf7..2f97345 100755 --- a/src/IncludeDeps.cpp +++ b/src/IncludeDeps.cpp @@ -12,7 +12,7 @@ IncludeDeps::IncludeDeps(const std::string& filename, const std::string& dir, co IncludeDeps::IncludeDeps(const std::string& filename, const std::string& dir, bool projectHFile, const std::set& files, std::map& allDeps) : filepath(dir+filename), projectHFile{projectHFile} { - if(filename[filename.length() - 1] =='h') + if(Utils::IsHeaderFile(filename)) { allDeps.emplace(filepath, this); } diff --git a/src/Utils.cpp b/src/Utils.cpp index d28674f..6cb5c7b 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -3,6 +3,34 @@ #include "ConfigFile.h" #include "FileUtils.h" +bool Utils::IsSourceFile(const std::string& filepath) +{ + std::string_view extension(filepath); + size_t pSlash = filepath.find_last_of('/'); + size_t pDot = filepath.find_last_of('.'); + if(pDot == std::string::npos || (pSlash != std::string::npos && pSlash > pDot)) + { + LOG_ERROR("No file extension for file: ", filepath); + return false; + } + extension.remove_prefix(pDot + 1); + return extension == "cpp" || extension == "c" || extension == "cxx" || extension == "cc"; +} + +bool Utils::IsHeaderFile(const std::string& filepath) +{ + std::string_view extension(filepath); + size_t pSlash = filepath.find_last_of('/'); + size_t pDot = filepath.find_last_of('.'); + if(pDot == std::string::npos || (pSlash != std::string::npos && pSlash > pDot)) + { + LOG_ERROR("No file extension for file: ", filepath); + return false; + } + extension.remove_prefix(pDot + 1); + return extension == "hpp" || extension == "h" || extension == "hxx"; +} + std::string Utils::CommonPrefix(const std::string& s1, const std::string& s2) { size_t n = 0; @@ -26,19 +54,14 @@ void Utils::GetCppFiles(ConfigFile& conf, std::set& cppFiles) for(auto it = files.begin(); it!=files.end();++it) { - size_t extensionPos = it->find_last_of("."); - if(extensionPos != std::string::npos) + std::string filename = it->substr(path.length()); + if(IsSourceFile(filename)) { - std::string extension = it->substr(extensionPos+1); - std::string filename = it->substr(path.length()); - if(extension == "cpp" || extension == "c") + std::string sourceFile =conf.GetSettingString(ConfigSetting::SourceDir) + filename; + auto it = std::find(excludeSources.begin(), excludeSources.end(), sourceFile); + if(it == excludeSources.end()) { - std::string sourceFile =conf.GetSettingString(ConfigSetting::SourceDir) + filename; - auto it = std::find(excludeSources.begin(), excludeSources.end(), sourceFile); - if(it == excludeSources.end()) - { - cppFiles.emplace(filename); - } + cppFiles.emplace(filename); } } } @@ -54,27 +77,20 @@ void Utils::GetCppAndHFiles(ConfigFile& conf, std::set& hFiles, std::set< // For example src/graphics/Window.h -> graphics/Window.h if src is a src folder for(auto it = files.begin(); it!=files.end();++it) { - size_t extensionPos = it->find_last_of("."); - if(extensionPos != std::string::npos) + std::string filename = it->substr(path.length()); + if(IsSourceFile(filename)) { - std::string extension = it->substr(extensionPos+1); - std::string filename = it->substr(path.length()); - if(extension == "cpp" || extension == "c") + std::string sourceFile =conf.GetSettingString(ConfigSetting::SourceDir) + filename; + auto it = std::find(excludeSources.begin(), excludeSources.end(), sourceFile); + if(it == excludeSources.end()) { - std::string sourceFile =conf.GetSettingString(ConfigSetting::SourceDir) + filename; - auto it = std::find(excludeSources.begin(), excludeSources.end(), sourceFile); - if(it == excludeSources.end()) - { - cppFiles.emplace(filename); - } - else - LOG_INFO("Excluding: ", sourceFile); - } - else if(extension == "hpp" || extension == "h") - { - hFiles.emplace(HFile{filename,path,false}); + cppFiles.emplace(filename); } } + else if(IsHeaderFile(filename)) + { + hFiles.emplace(HFile{filename,path,false}); + } } std::vector& dependencies = conf.GetSettingVectorString(ConfigSetting::Dependency); @@ -100,15 +116,10 @@ void Utils::GetHFiles(const std::string& dependencyDir, ConfigFile& conf, std::s FileUtils::GetAllFiles(depSrcDir, files); for(auto it = files.begin(); it!=files.end();++it) { - size_t extensionPos = it->find_last_of("."); - if(extensionPos != std::string::npos) + if(IsHeaderFile(*it)) { - std::string extension = it->substr(extensionPos+1); - if(extension == "hpp" || extension == "h") - { - std::string filename = it->substr(depSrcDir.length()); - hFiles.emplace(HFile{filename, depSrcDir, conf.GetSettingBool(ConfigSetting::GenerateHFile) && filename == conf.GetSettingString(ConfigSetting::HFileName)}); - } + std::string filename = it->substr(depSrcDir.length()); + hFiles.emplace(HFile{filename, depSrcDir, conf.GetSettingBool(ConfigSetting::GenerateHFile) && filename == conf.GetSettingString(ConfigSetting::HFileName)}); } } } diff --git a/src/Utils.h b/src/Utils.h index 85e1ee4..b7ada5f 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -26,6 +26,8 @@ class ConfigFile; struct Utils { + static bool IsSourceFile(const std::string& filepath); + static bool IsHeaderFile(const std::string& filepath); static std::string CommonPrefix(const std::string& s1, const std::string& s2); static void GetCppFiles(ConfigFile& conf, std::set& cppFiles); static void GetCppAndHFiles(ConfigFile& conf, std::set& hFiles, std::set& cppFiles);