Tuesday, February 11, 2020

C++11 Architecture ported

All of the old library is ported and there are new libraries coming in, Going back to work on the single header generateter, found out that my recursive algorithm doesnt work anymore because stack memory is overflowed by it. I fixed this by implementing a technique called dynamic programming or memoization. Here is some code snippets

void File::Preprocess(const std::map<std::string, File*>& headerFiles, const std::map<std::string, const File*>& implFiles)
{
auto RetriveFileName = [](const std::string& fileName) -> std::string
{
std::string truncatedName;
size_t index = fileName.find_last_of("/");
// there is a file directory, if thats the case, we just want the name of the file and remove the last "
if (index != std::string::npos)
{
truncatedName = fileName.substr(fileName.find_last_of("/") + 1);
truncatedName.erase(truncatedName.size() - 1);
}
else // there is no directory for this file, remove the #include and ""
{
truncatedName = fileName.substr(fileName.find_first_of("\"") + 1);
truncatedName.erase(truncatedName.size() - 1);
}
return truncatedName;
};

// INCREMENT FILTER PASS IN CASE OF EXTRA DIRECTORY DEPTH
#define FILTER_PASS 6
// 3 passes
// First pass is replace #include library.hpp
// Second pass is replace #include library_platformOS.hpp
// Third pass is replace any #include in library_platformOS.hpp
// rest of the filter is for safety check
for (int i = 0; i < FILTER_PASS; ++i)
{
std::string newRawData = rawData; // i need a place holder for rawData is because regex_iter will become invalid if original content were modified
for (auto iter = std::sregex_iterator(rawData.begin(), rawData.end(), REGEX::PoundInclude); iter != std::sregex_iterator(); ++iter)
{
std::smatch match = *iter;
std::string lineString = match.str(0);
std::string truncatedString = RetriveFileName(lineString);

std::regex regex(lineString);
auto implFilesIter = implFiles.find(truncatedString);
if (implFilesIter != implFiles.end())
newRawData = std::regex_replace(newRawData, regex, implFilesIter->second->GetRawData());
else
newRawData = std::regex_replace(newRawData, regex, "");

auto headerFilesIter = headerFiles.find(truncatedString);
if (headerFilesIter != headerFiles.end())
++headerFilesIter->second->dependencyCount;
}
for (size_t i = 0; i < includeFiles.size(); ++i)
{
auto headerFilesIter = headerFiles.find(includeFiles[i]);
if (headerFilesIter != headerFiles.end())
++headerFilesIter->second->dependencyCount;
}
rawData = std::move(newRawData);
}
#undef FILTER_PASS
}

No comments:

Post a Comment