Advertisement
Emmennater

JSON Directory Substitution

May 31st, 2023
851
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.06 KB | Source Code | 0 0
  1.  
  2. // Program written by EmmettDJ
  3. // A tool used to mass replace hashes in
  4. // a directory using a json lookup table
  5.  
  6. #include <iostream>
  7. #include <fstream>
  8. #include <string>
  9. #include <sstream>
  10. #include <functional>
  11. #include <windows.h>
  12. #include <map>
  13. #include <regex>
  14. #include <vector>
  15. #include <iomanip>
  16. using namespace std;
  17.  
  18. int filesReplaced = 0;
  19. vector<string> filenames;
  20. vector<string> directories;
  21. vector<string> paths;
  22. map<string, string> jsonReplace;
  23.  
  24. void iterateDirectory(const std::string& directoryUrl) {
  25.     std::string searchPath = directoryUrl + "\\*.*";
  26.     WIN32_FIND_DATAA findData;
  27.     HANDLE hFind = FindFirstFileA(searchPath.c_str(), &findData);
  28.  
  29.     if (hFind != INVALID_HANDLE_VALUE) {
  30.         do {
  31.             std::string filename = findData.cFileName;
  32.             if (filename != "." && filename != "..") {
  33.                 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  34.                     // Recursive call for subdirectories
  35.                     std::string subdirectoryUrl = directoryUrl + "\\" + filename;
  36.                     iterateDirectory(subdirectoryUrl);
  37.                 } else {
  38.                     // Invoke the callback function with the file path
  39.                     std::string filePath = directoryUrl + "\\" + filename;
  40.                     filenames.push_back(filename);
  41.                     directories.push_back(directoryUrl);
  42.                     paths.push_back(filePath);
  43.                 }
  44.             }
  45.         } while (FindNextFileA(hFind, &findData) != 0);
  46.  
  47.         FindClose(hFind);
  48.     } else {
  49.         std::cerr << "Failed to iterate directory: " << directoryUrl << std::endl;
  50.     }
  51. }
  52.  
  53. void copyFile(const std::string& sourcePath, const std::string& destinationPath) {
  54.     if (!CopyFileA(sourcePath.c_str(), destinationPath.c_str(), FALSE)) {
  55.         std::cerr << "Failed to copy file: " << sourcePath << std::endl;
  56.     }
  57. }
  58.  
  59. void copyDirectory(const std::string& sourceDirectory, const std::string& destinationDirectory) {
  60.     CreateDirectoryA(destinationDirectory.c_str(), nullptr);
  61.  
  62.     std::string searchPath = sourceDirectory + "\\*.*";
  63.     WIN32_FIND_DATAA findData;
  64.     HANDLE hFind = FindFirstFileA(searchPath.c_str(), &findData);
  65.  
  66.     if (hFind != INVALID_HANDLE_VALUE) {
  67.         do {
  68.             std::string filename = findData.cFileName;
  69.             if (filename != "." && filename != "..") {
  70.                 std::string sourceFilePath = sourceDirectory + "\\" + filename;
  71.                 std::string destinationFilePath = destinationDirectory + "\\" + filename;
  72.  
  73.                 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  74.                     // Recursive call for subdirectories
  75.                     copyDirectory(sourceFilePath, destinationFilePath);
  76.                 } else {
  77.                     // Copy the file to the destination directory
  78.                     copyFile(sourceFilePath, destinationFilePath);
  79.                 }
  80.             }
  81.         } while (FindNextFileA(hFind, &findData) != 0);
  82.  
  83.         FindClose(hFind);
  84.     } else {
  85.         std::cerr << "Failed to copy directory: " << sourceDirectory << std::endl;
  86.     }
  87. }
  88.  
  89. std::string readFile(const std::string& filePath) {
  90.     std::ifstream in(filePath);
  91.  
  92.     if (in) {
  93.         using BufIt = std::istreambuf_iterator<char>;
  94.         std::string oldData( BufIt( in.rdbuf() ), BufIt() );
  95.         in.close();
  96.         return oldData;
  97.     } else {
  98.         throw std::runtime_error("Failed to open file: " + filePath);
  99.     }
  100. }
  101.  
  102. std::map<std::string, std::string> parseJSON(const std::string& json) {
  103.     std::map<std::string, std::string> parsedJSON;
  104.  
  105.     // Remove leading and trailing whitespaces from the JSON string
  106.     std::string trimmedJSON;
  107.     for (char c : json) {
  108.         if (!std::isspace(c))
  109.             trimmedJSON += c;
  110.     }
  111.  
  112.     // Check if the JSON object is properly enclosed in curly braces
  113.     if (trimmedJSON.front() != '{' || trimmedJSON.back() != '}') {
  114.         std::cout << "Invalid JSON object." << std::endl;
  115.         return parsedJSON;
  116.     }
  117.  
  118.     // Remove the outer curly braces
  119.     std::string innerJSON = trimmedJSON.substr(1, trimmedJSON.size() - 2);
  120.  
  121.     // Split the JSON object into key-value pairs
  122.     size_t pos = 0;
  123.     while (pos != std::string::npos) {
  124.         // Find the next key-value pair
  125.         size_t keyStart = innerJSON.find('"', pos);
  126.         size_t keyEnd = innerJSON.find('"', keyStart + 1);
  127.         size_t valueStart = innerJSON.find(':', keyEnd);
  128.         size_t valueEnd = innerJSON.find(',', valueStart);
  129.  
  130.         // Check if a key-value pair was found
  131.         if (keyStart == std::string::npos || keyEnd == std::string::npos ||
  132.             valueStart == std::string::npos || valueEnd == std::string::npos)
  133.             break;
  134.  
  135.         // Extract the key and value strings
  136.         std::string key = innerJSON.substr(keyStart + 1, keyEnd - keyStart - 1);
  137.         std::string value = innerJSON.substr(valueStart + 2, valueEnd - valueStart - 3);
  138.  
  139.         // Add the key-value pair to the parsed JSON map
  140.         parsedJSON[key] = value;
  141.  
  142.         // Update the position to continue searching for more key-value pairs
  143.         pos = valueEnd + 1;
  144.     }
  145.  
  146.     return parsedJSON;
  147. }
  148.  
  149. std::string replaceFoundHash(const std::smatch& match) {
  150.     string str = match.str();
  151.     try {
  152.         str = str.substr(2, str.length() - 4);
  153.         // std::cout << "Match: " << str << endl;
  154.         str = jsonReplace[str];
  155.         // std::cout << "Replace: " << str << endl;
  156.     } catch (exception e) {
  157.         std::cout << "Failed to replace hash: " << str << endl;
  158.         std::cout << "Exception: " << e.what() << endl;
  159.     }
  160.     return str;
  161. }
  162.  
  163. void processFile(const std::string& filePath) {
  164.     std::cout << left;
  165.     std::cout << to_string(filesReplaced) + " / " + to_string(filenames.size()) << " : " << setw(40) << filenames[filesReplaced] << " " << directories[filesReplaced] << std::endl;
  166.  
  167.     // Sweep data history
  168.     int mode = 0;
  169.     int count = 0;
  170.     string input = readFile(filePath);
  171.     string output;
  172.     string history;
  173.     output.reserve(input.size());
  174.     history.reserve(20);
  175.  
  176.     // Identifying and replacing hashes
  177.     for (unsigned int i = 0; i < input.size(); ++i) {
  178.         char c = input[i];
  179.         history += c;
  180.        
  181.         switch (mode) {
  182.         case 0: // First brace
  183.             if (c == '{') { mode = 1; continue; }
  184.             break;
  185.         case 1: // First exclam
  186.             if (c == '!') { mode = 2; continue; }
  187.             break;
  188.         case 2: // Hash and last exclam
  189.             ++count;
  190.             if (count == 17) {
  191.                 if (c == '!') mode = 3;
  192.                 else break;
  193.             }
  194.             continue;
  195.         case 3: // Last brace
  196.             if (c == '}') {
  197.                 string hash = history.substr(2, 16);
  198.                 if (jsonReplace.find(hash) != jsonReplace.end()) {
  199.                     string replacement = jsonReplace[hash];
  200.                     // std::cout << "Replaced " << history << " with " << replacement << endl;
  201.                     output += replacement;
  202.                     history = "";
  203.                 }
  204.             }
  205.             break;
  206.         }
  207.  
  208.         // Default
  209.         output += history;
  210.         history = "";
  211.         mode = 0;
  212.         count = 0;
  213.     }
  214.  
  215.     ofstream out(filePath);
  216.     out << output;
  217.     out.close();
  218.  
  219.     filesReplaced++;
  220. }
  221.  
  222. int main() {
  223.  
  224.     // Input
  225.     string JSONpath, inputDir, outputDir;
  226.     std::cout << "JSON path: ";
  227.     getline(cin, JSONpath);
  228.     std::cout << "Input directory: ";
  229.     getline(cin, inputDir);
  230.     std::cout << "Output directory: ";
  231.     getline(cin, outputDir);
  232.  
  233.     // Program
  234.     string JSONdata = readFile(JSONpath);
  235.     jsonReplace = parseJSON(JSONdata);
  236.  
  237.     std::cout << "Copying files...\n";    
  238.     copyDirectory(inputDir, outputDir);
  239.     std::cout << "Copying complete!\n";
  240.  
  241.     std::cout << "Substituting keys...\n";
  242.     iterateDirectory(outputDir);
  243.     for (string path : paths) {
  244.         processFile(path);
  245.     }
  246.     std::cout << "Substitution complete!\n";
  247.  
  248.     return 0;
  249. }
  250.  
Tags: file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement