Okay, this is a great task that involves both file system interaction and string manipulation to generate source code. We'll use C++17's std::filesystem for easy directory listing.
Here's the C++ script that will take a folder path as input and generate the generated_script.cpp file in the format you specified:
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <filesystem> // Requires C++17
namespace fs = std::filesystem;
// Function to get files from a directory
std::vector<std::string> getFilesInFolder(const std::string& folderPath) {
std::vector<std::string> files;
try {
if (!fs::exists(folderPath) || !fs::is_directory(folderPath)) {
std::cerr << "Error: Folder '" << folderPath << "' does not exist or is not a directory.\n";
return files;
}
for (const auto& entry : fs::directory_iterator(folderPath)) {
if (fs::is_regular_file(entry.status())) {
files.push_back(entry.path().filename().string());
}
}
} catch (const fs::filesystem_error& e) {
std::cerr << "Filesystem error: " << e.what() << '\n';
} catch (const std::exception& e) {
std::cerr << "Standard exception: " << e.what() << '\n';
}
return files;
}
// Function to generate the C++ script
void generateCppScript(const std::string& folderPath, const std::vector<std::string>& files) {
std::string outputFileName = "generated_script.cpp";
std::ofstream outFile(outputFileName);
if (!outFile.is_open()) {
std::cerr << "Error: Could not open output file '" << outputFileName << "' for writing.\n";
return;
}
// Ensure the folder path ends with a slash for consistent concatenation in the generated script
std::string formattedFolderPath = folderPath;
if (!formattedFolderPath.empty() && formattedFolderPath.back() != '/' && formattedFolderPath.back() != '\\') {
formattedFolderPath += "/";
}
outFile << "#include <cstdio>\n";
outFile << "#include <cstring>\n\n";
outFile << "const char* filePath = \"" << formattedFolderPath << "\";\n";
outFile << "const char* files[] = {\n";
for (size_t i = 0; i < files.size(); ++i) {
outFile << " \"" << files[i] << "\"";
if (i < files.size() - 1) {
outFile << ",\n";
} else {
outFile << "\n";
}
}
outFile << "};\n\n";
outFile << "int main()\n";
outFile << "{\n";
outFile << " FILE* outFile = fopen(\"prompt.txt\", \"w\");\n";
outFile << " if (!outFile) {\n";
outFile << " printf(\"Error opening output file\\n\");\n";
outFile << " return 1;\n";
outFile << " }\n";
outFile << " \n";
outFile << " char tmp[1 << 16];\n";
outFile << " char fullPath[256];\n";
outFile << " for (const auto& file : files) {\n";
outFile << " snprintf(fullPath, sizeof(fullPath), \"%s%s\", filePath, file);\n";
outFile << " FILE* inFile = fopen(fullPath, \"r\");\n";
outFile << " if (!inFile) {\n";
outFile << " printf(\"Error opening file: %s\\n\", fullPath);\n";
outFile << " continue;\n";
outFile << " }\n";
outFile << " \n";
outFile << " if (fgets(tmp, sizeof(tmp), inFile)) {\n";
outFile << " fprintf(outFile, \"File: %s\\n```\\n\", file);\n";
outFile << " fputs(tmp, outFile);\n";
outFile << " \n";
outFile << " // Read the rest of the file\n";
outFile << " while (fgets(tmp, sizeof(tmp), inFile)) {\n";
outFile << " fputs(tmp, outFile);\n";
outFile << " }\n";
outFile << " \n";
outFile << " fprintf(outFile, \"```\\n\\n\");\n";
outFile << " }\n";
outFile << " \n";
outFile << " fclose(inFile);\n";
outFile << " }\n";
outFile << " \n";
outFile << " fclose(outFile);\n";
outFile << " return 0;\n";
outFile << "}\n";
outFile.close();
std::cout << "Successfully generated '" << outputFileName << "'\n";
}
int main() {
std::string folderPath;
std::cout << "Enter the folder path: ";
std::getline(std::cin, folderPath); // Use getline to allow paths with spaces
std::vector<std::string> files = getFilesInFolder(folderPath);
if (files.empty()) {
std::cout << "No files found in the specified folder or an error occurred. No script generated.\n";
return 1;
}
generateCppScript(folderPath, files);
return 0;
}