Skip to content

Instantly share code, notes, and snippets.

@cppcooper
Last active April 14, 2021 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cppcooper/73b9ba3c633f0740439a66057dfda89d to your computer and use it in GitHub Desktop.
Save cppcooper/73b9ba3c633f0740439a66057dfda89d to your computer and use it in GitHub Desktop.
[C++] Recurse directory with
#include <filesystem>
#include <iostream>
#include <regex>
#include <vector>
#include <fstream>
#include <sstream>
#pragma warning (disable : 4267)
namespace fs = std::filesystem;
void print_data(const std::vector<std::string> &lines) {
for (auto line : lines) {
printf("%s", line.c_str());
}
}
void process_file(fs::path file) {
//uint32_t newFileSize = 0;
std::fstream source_file(file, std::ios::in | std::ios::out);
std::string line;
std::vector<std::string> lines;
uint16_t lineCount = 0;
std::regex marker1("static uint64 logFlags = make_bitmask.*");
std::regex marker2("//EndDebugBlock");
std::regex debugLine("[ \t]+(debug.*\\(\\d.*\".*\").*");
int16_t info_head = -1;
int16_t info_tail = -1;
std::vector<uint16_t> debugLines;
while (std::getline(source_file, line)) {
line.append("\n");
lines.push_back(line);
if (info_head == -1 && std::regex_search(line, marker1)) {
info_head = lineCount;
} else if (info_tail == -1 && std::regex_search(line, marker2)) {
info_tail = lineCount;
} else if (info_head != -1 && std::regex_search(line, debugLine)) {
debugLines.push_back(lineCount);
}
++lineCount;
}
//begin doing stuff to the data
uint16_t count = 0; //how many lines we've added
uint16_t removed = 0; //how many lines we've removed, or will remove
if (info_tail != -1) {
removed = info_tail - info_head - 1;
lines.reserve(lines.size() + debugLines.size() - removed);
for (int16_t i = debugLines.size() - 1; i >= 0; --i) {
line = lines.at(debugLines.at(i) + count++);
line = std::regex_replace(line, std::regex("^ +"), "");
line = "//" + line;
lines.insert(lines.begin() + info_tail, line);
}
for (int i = 0; i < removed; ++i) {
lines.erase(lines.begin() + info_head + 1);
}
} else if (info_head != -1) {
lines.reserve(lines.size() + debugLines.size());
line = "//EndDebugBlock\n";
lines.insert(lines.begin() + info_head + 1, line);
count = 1;
for (int16_t i = debugLines.size() - 1; i >= 0; --i) {
line = lines.at(debugLines.at(i) + count++);
line = std::regex_replace(line, std::regex("^ +"), "");
line = "//" + line;
lines.insert(lines.begin() + info_head + 1, line);
}
} else {
return;
}
std::stringstream data;
for (auto ln : lines) {
data << ln;
}
source_file.clear();
source_file.seekp(0);
source_file.write(data.str().c_str(), data.str().length());
}
void recurse_directory(fs::path root) {
if (fs::is_directory(root)) {
fs::recursive_directory_iterator recursive_iter(root);
for (auto &entry : recursive_iter) {
if(entry.is_directory()){
continue;
}
std::string ext = entry.path().extension().string();
if (ext == ".cpp") {
process_file(entry.path());
}
}
}
}
int main(int argc, char *argv[]) {
fs::path root;
if (argc != 1) {
root = fs::canonical(fs::path(argv[1]));
} else {
root = fs::current_path();
}
recurse_directory(root);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment