Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Program written by EmmettDJ
- // A tool used to mass replace hashes in
- // a directory using a json lookup table
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <sstream>
- #include <functional>
- #include <windows.h>
- #include <map>
- #include <regex>
- #include <vector>
- #include <iomanip>
- using namespace std;
- int filesReplaced = 0;
- vector<string> filenames;
- vector<string> directories;
- vector<string> paths;
- map<string, string> jsonReplace;
- void iterateDirectory(const std::string& directoryUrl) {
- std::string searchPath = directoryUrl + "\\*.*";
- WIN32_FIND_DATAA findData;
- HANDLE hFind = FindFirstFileA(searchPath.c_str(), &findData);
- if (hFind != INVALID_HANDLE_VALUE) {
- do {
- std::string filename = findData.cFileName;
- if (filename != "." && filename != "..") {
- if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
- // Recursive call for subdirectories
- std::string subdirectoryUrl = directoryUrl + "\\" + filename;
- iterateDirectory(subdirectoryUrl);
- } else {
- // Invoke the callback function with the file path
- std::string filePath = directoryUrl + "\\" + filename;
- filenames.push_back(filename);
- directories.push_back(directoryUrl);
- paths.push_back(filePath);
- }
- }
- } while (FindNextFileA(hFind, &findData) != 0);
- FindClose(hFind);
- } else {
- std::cerr << "Failed to iterate directory: " << directoryUrl << std::endl;
- }
- }
- void copyFile(const std::string& sourcePath, const std::string& destinationPath) {
- if (!CopyFileA(sourcePath.c_str(), destinationPath.c_str(), FALSE)) {
- std::cerr << "Failed to copy file: " << sourcePath << std::endl;
- }
- }
- void copyDirectory(const std::string& sourceDirectory, const std::string& destinationDirectory) {
- CreateDirectoryA(destinationDirectory.c_str(), nullptr);
- std::string searchPath = sourceDirectory + "\\*.*";
- WIN32_FIND_DATAA findData;
- HANDLE hFind = FindFirstFileA(searchPath.c_str(), &findData);
- if (hFind != INVALID_HANDLE_VALUE) {
- do {
- std::string filename = findData.cFileName;
- if (filename != "." && filename != "..") {
- std::string sourceFilePath = sourceDirectory + "\\" + filename;
- std::string destinationFilePath = destinationDirectory + "\\" + filename;
- if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
- // Recursive call for subdirectories
- copyDirectory(sourceFilePath, destinationFilePath);
- } else {
- // Copy the file to the destination directory
- copyFile(sourceFilePath, destinationFilePath);
- }
- }
- } while (FindNextFileA(hFind, &findData) != 0);
- FindClose(hFind);
- } else {
- std::cerr << "Failed to copy directory: " << sourceDirectory << std::endl;
- }
- }
- std::string readFile(const std::string& filePath) {
- std::ifstream in(filePath);
- if (in) {
- using BufIt = std::istreambuf_iterator<char>;
- std::string oldData( BufIt( in.rdbuf() ), BufIt() );
- in.close();
- return oldData;
- } else {
- throw std::runtime_error("Failed to open file: " + filePath);
- }
- }
- std::map<std::string, std::string> parseJSON(const std::string& json) {
- std::map<std::string, std::string> parsedJSON;
- // Remove leading and trailing whitespaces from the JSON string
- std::string trimmedJSON;
- for (char c : json) {
- if (!std::isspace(c))
- trimmedJSON += c;
- }
- // Check if the JSON object is properly enclosed in curly braces
- if (trimmedJSON.front() != '{' || trimmedJSON.back() != '}') {
- std::cout << "Invalid JSON object." << std::endl;
- return parsedJSON;
- }
- // Remove the outer curly braces
- std::string innerJSON = trimmedJSON.substr(1, trimmedJSON.size() - 2);
- // Split the JSON object into key-value pairs
- size_t pos = 0;
- while (pos != std::string::npos) {
- // Find the next key-value pair
- size_t keyStart = innerJSON.find('"', pos);
- size_t keyEnd = innerJSON.find('"', keyStart + 1);
- size_t valueStart = innerJSON.find(':', keyEnd);
- size_t valueEnd = innerJSON.find(',', valueStart);
- // Check if a key-value pair was found
- if (keyStart == std::string::npos || keyEnd == std::string::npos ||
- valueStart == std::string::npos || valueEnd == std::string::npos)
- break;
- // Extract the key and value strings
- std::string key = innerJSON.substr(keyStart + 1, keyEnd - keyStart - 1);
- std::string value = innerJSON.substr(valueStart + 2, valueEnd - valueStart - 3);
- // Add the key-value pair to the parsed JSON map
- parsedJSON[key] = value;
- // Update the position to continue searching for more key-value pairs
- pos = valueEnd + 1;
- }
- return parsedJSON;
- }
- std::string replaceFoundHash(const std::smatch& match) {
- string str = match.str();
- try {
- str = str.substr(2, str.length() - 4);
- // std::cout << "Match: " << str << endl;
- str = jsonReplace[str];
- // std::cout << "Replace: " << str << endl;
- } catch (exception e) {
- std::cout << "Failed to replace hash: " << str << endl;
- std::cout << "Exception: " << e.what() << endl;
- }
- return str;
- }
- void processFile(const std::string& filePath) {
- std::cout << left;
- std::cout << to_string(filesReplaced) + " / " + to_string(filenames.size()) << " : " << setw(40) << filenames[filesReplaced] << " " << directories[filesReplaced] << std::endl;
- // Sweep data history
- int mode = 0;
- int count = 0;
- string input = readFile(filePath);
- string output;
- string history;
- output.reserve(input.size());
- history.reserve(20);
- // Identifying and replacing hashes
- for (unsigned int i = 0; i < input.size(); ++i) {
- char c = input[i];
- history += c;
- switch (mode) {
- case 0: // First brace
- if (c == '{') { mode = 1; continue; }
- break;
- case 1: // First exclam
- if (c == '!') { mode = 2; continue; }
- break;
- case 2: // Hash and last exclam
- ++count;
- if (count == 17) {
- if (c == '!') mode = 3;
- else break;
- }
- continue;
- case 3: // Last brace
- if (c == '}') {
- string hash = history.substr(2, 16);
- if (jsonReplace.find(hash) != jsonReplace.end()) {
- string replacement = jsonReplace[hash];
- // std::cout << "Replaced " << history << " with " << replacement << endl;
- output += replacement;
- history = "";
- }
- }
- break;
- }
- // Default
- output += history;
- history = "";
- mode = 0;
- count = 0;
- }
- ofstream out(filePath);
- out << output;
- out.close();
- filesReplaced++;
- }
- int main() {
- // Input
- string JSONpath, inputDir, outputDir;
- std::cout << "JSON path: ";
- getline(cin, JSONpath);
- std::cout << "Input directory: ";
- getline(cin, inputDir);
- std::cout << "Output directory: ";
- getline(cin, outputDir);
- // Program
- string JSONdata = readFile(JSONpath);
- jsonReplace = parseJSON(JSONdata);
- std::cout << "Copying files...\n";
- copyDirectory(inputDir, outputDir);
- std::cout << "Copying complete!\n";
- std::cout << "Substituting keys...\n";
- iterateDirectory(outputDir);
- for (string path : paths) {
- processFile(path);
- }
- std::cout << "Substitution complete!\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement