Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdexcept>
- #include <regex>
- #include <string>
- #include <fstream>
- #include <vector>
- #include <iostream>
- #include <filesystem>
- #include <deque>
- std::string trimLeadingTrailing(std::string input){
- std::deque<char> inputQueue(input.begin(),input.end());
- for(char character:inputQueue){
- if(std::isspace(character)){
- inputQueue.pop_front();
- }else{
- break;
- }
- }
- //stolen from Stack Overflow
- for (auto it = inputQueue.rbegin(); it != inputQueue.rend(); ++it){
- if((std::isspace(*it))){
- inputQueue.pop_back();
- }else{
- break;
- }
- }
- std::string finalString;
- for(char character: inputQueue){
- finalString += character;
- }
- return finalString;
- }
- int main(int argc, char** argv){
- std::regex checkInput(".*\\.cpp$");
- if(argc != 2){
- throw std::invalid_argument("only enter 1 file as args");
- }
- if(!std::regex_match(argv[1],checkInput)){
- throw std::invalid_argument("only .cpp files");
- }
- std::ifstream file(argv[1]);
- std::vector<std::string> file_contents;
- std::string str;
- if (!file.is_open()){
- throw std::runtime_error("file can't be opened");
- }
- while (std::getline(file, str)){
- file_contents.push_back(str);
- }
- file.close();
- std::vector<std::string> includeList;
- for(std::string line: file_contents){
- //std::regex includeUserHeader("#include \"[A-Za-z0-9_\\\\/]+\\.h\"");
- std::regex includeUserHeader("#include \"([A-Za-z0-9_\\\\/\\.-]+)\\.(h|hpp)\"");
- if (std::regex_match(line,includeUserHeader)){
- includeList.push_back(trimLeadingTrailing(line.substr(8)));
- }
- }
- std::filesystem::path directory = std::filesystem::absolute(__FILE__).remove_filename();
- std::vector<std::string> subDirectories;
- //const std::filesystem::directory_entry& stolen from the cppreference.com
- for (const auto& dir_entry:std::filesystem::recursive_directory_iterator(directory)){
- subDirectories.push_back(dir_entry.path().string());
- }
- std::vector<std::string> relativeSubDirectories;
- for(std::string& subDirectory:subDirectories){
- std::filesystem::path path = std::filesystem::path(subDirectory);
- std::filesystem::path relativePath = std::filesystem::proximate(path, directory);
- relativeSubDirectories.push_back(relativePath.string());
- }
- std::vector<std::string> ImplementationFiles;
- int i = 0;
- for(std::string path:relativeSubDirectories){
- bool foundImplementation = false;
- std::string ImplementationPath = includeList[i].substr(1,includeList[i].length() - 4);
- for(std::string path:relativeSubDirectories){
- if (path == includeList[i].substr(1,includeList[i].length() - 4).append(".cpp")){
- foundImplementation = true;
- break;
- }
- }
- if (!foundImplementation){
- throw std::runtime_error("no implementation file for: " + includeList[i]);
- }
- ImplementationFiles.push_back(ImplementationPath.append(".cpp"));
- i++;
- if (i == includeList.size()){
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment