BiggerRadius

Auto Build Tool

Apr 4th, 2025 (edited)
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | Source Code | 0 0
  1. #include <stdexcept>
  2. #include <regex>
  3. #include <string>
  4. #include <fstream>
  5. #include <vector>
  6. #include <iostream>
  7. #include <filesystem>
  8. #include <deque>
  9.  
  10. std::string trimLeadingTrailing(std::string input){
  11.     std::deque<char> inputQueue(input.begin(),input.end());
  12.     for(char character:inputQueue){
  13.         if(std::isspace(character)){
  14.             inputQueue.pop_front();
  15.         }else{
  16.             break;
  17.         }
  18.     }
  19.     //stolen from Stack Overflow
  20.     for (auto it = inputQueue.rbegin(); it != inputQueue.rend(); ++it){
  21.         if((std::isspace(*it))){
  22.             inputQueue.pop_back();
  23.         }else{
  24.             break;
  25.         }
  26.     }
  27.     std::string finalString;
  28.     for(char character: inputQueue){
  29.         finalString += character;
  30.     }
  31.     return finalString;
  32.     }
  33.  
  34. int main(int argc, char** argv){
  35.     std::regex checkInput(".*\\.cpp$");
  36.     if(argc != 2){
  37.         throw std::invalid_argument("only enter 1 file as args");
  38.     }
  39.     if(!std::regex_match(argv[1],checkInput)){
  40.         throw std::invalid_argument("only .cpp files");
  41.     }
  42.     std::ifstream file(argv[1]);
  43.     std::vector<std::string> file_contents;
  44.     std::string str;
  45.     if (!file.is_open()){
  46.         throw std::runtime_error("file can't be opened");
  47.  
  48.     }
  49.     while (std::getline(file, str)){
  50.         file_contents.push_back(str);
  51.     }  
  52.     file.close();
  53.     std::vector<std::string> includeList;
  54.     for(std::string line: file_contents){
  55.         //std::regex includeUserHeader("#include \"[A-Za-z0-9_\\\\/]+\\.h\"");
  56.         std::regex includeUserHeader("#include \"([A-Za-z0-9_\\\\/\\.-]+)\\.(h|hpp)\"");
  57.         if (std::regex_match(line,includeUserHeader)){
  58.             includeList.push_back(trimLeadingTrailing(line.substr(8)));
  59.         }
  60.     }
  61.    std::filesystem::path directory = std::filesystem::absolute(__FILE__).remove_filename();
  62.    std::vector<std::string> subDirectories;
  63.    //const std::filesystem::directory_entry& stolen from the cppreference.com
  64.     for (const auto& dir_entry:std::filesystem::recursive_directory_iterator(directory)){
  65.         subDirectories.push_back(dir_entry.path().string());
  66.     }
  67.     std::vector<std::string> relativeSubDirectories;
  68.     for(std::string& subDirectory:subDirectories){
  69.         std::filesystem::path path = std::filesystem::path(subDirectory);
  70.         std::filesystem::path relativePath = std::filesystem::proximate(path, directory);
  71.         relativeSubDirectories.push_back(relativePath.string());
  72.     }
  73.     std::vector<std::string> ImplementationFiles;
  74.     int i = 0;
  75.     for(std::string path:relativeSubDirectories){
  76.         bool foundImplementation = false;
  77.             std::string ImplementationPath = includeList[i].substr(1,includeList[i].length() - 4);
  78.             for(std::string path:relativeSubDirectories){
  79.                 if (path == includeList[i].substr(1,includeList[i].length() - 4).append(".cpp")){
  80.                     foundImplementation = true;
  81.                     break;
  82.                 }
  83.                 }
  84.                 if (!foundImplementation){
  85.                     throw std::runtime_error("no implementation file for: " + includeList[i]);
  86.                 }
  87.             ImplementationFiles.push_back(ImplementationPath.append(".cpp"));
  88.         i++;
  89.         if (i == includeList.size()){
  90.             break;
  91.         }
  92.     }
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment