From 27a2e596fd6888f3f36041c79469c73e15292c2f Mon Sep 17 00:00:00 2001 From: Thulinma <jaron@vietors.com> Date: Fri, 28 Feb 2020 16:26:40 +0100 Subject: [PATCH] Updated sourcery to be slightly more magical. Now supports replacing script tags with their contents as well as splitting a source file on a given string. --- src/sourcery.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 71 insertions(+), 7 deletions(-) diff --git a/src/sourcery.cpp b/src/sourcery.cpp index 27e34b25..79f1eecc 100644 --- a/src/sourcery.cpp +++ b/src/sourcery.cpp @@ -1,25 +1,89 @@ ///\file sourcery.cpp ///Utility program used for c-string dumping files. +#undef DEBUG +#define DEBUG -1 #include <stdint.h> +#include <string.h> +#include <unistd.h> #include <iomanip> #include <iostream> #include <fstream> +#include <string> +#include "../lib/url.h" +#include "../lib/url.cpp" +#include "../lib/encode.h" +#include "../lib/encode.cpp" + +std::string getContents(const char * fileName){ + std::ifstream inFile(fileName); + std::string fullText; + if (inFile){ + std::ostringstream contents; + contents << inFile.rdbuf(); + inFile.close(); + return contents.str(); + } + return ""; +} + int main(int argc, char* argv[]){ + if (argc < 4) { - std::cerr << "Usage: " << argv[0] << " <inputFile> <variableName> <outputFile>" << std::endl; + std::cerr << "Usage: " << argv[0] << " <inputFile> <variableName> <outputFile> [<splittext>]" << std::endl; return 42; } + const char * splitText = 0; + if (argc >= 5){splitText = argv[4];} + + char workDir[512]; + getcwd(workDir, 512); + HTTP::URL inUri(std::string("file://") + workDir + "/"); + inUri = inUri.link(argv[1]); + + //Read the entire first argument into a string buffer + std::string fullText = getContents(inUri.getFilePath().c_str()); + + //replace every <script src="*"></script> with the contents of the file '*' + while (fullText.find("<script src=\"") != std::string::npos){ + size_t locStart = fullText.find("<script src=\""); + size_t locEnd = fullText.find("\"></script>", locStart); + //Assume we should abort if the strlen of the filename is > 230 chars + if (locEnd - locStart >= 230){break;} + HTTP::URL fileName = inUri.link(fullText.substr(locStart+13, locEnd-locStart-13)); + std::string subText = getContents(fileName.getFilePath().c_str()); + fullText = fullText.substr(0, locStart) + "<script>" + subText + fullText.substr(locEnd+2); + } + + size_t splitPoint = std::string::npos; + size_t splitLen = 0; + if (splitText){ + splitPoint = fullText.find(splitText); + if (splitPoint != std::string::npos){ + splitLen = strlen(splitText); + } + } + std::ofstream tmp(argv[3]); //begin the first line - tmp << "const char *" << argv[2] << " = " << std::endl << " \""; + if (!splitLen){ + tmp << "const char *" << argv[2] << " = " << std::endl << " \""; + }else{ + tmp << "const char *" << argv[2] << "_prefix = " << std::endl << " \""; + } uint32_t i = 0; //Current line byte counter uint32_t total = 0; //Finished lines so far byte counter - std::ifstream inFile(argv[1]); bool sawQ = false; - while (inFile.good()){ - unsigned char thisChar = inFile.get(); - if (!inFile.good()){break;} + for (size_t pos = 0; pos < fullText.size(); ++pos){ + if (pos == splitPoint){ + tmp << "\";" << std::endl << "uint32_t " << argv[2] << "_prefix_len = " << i + total << ";" << std::endl; + tmp << "const char *" << argv[2] << "_suffix = " << std::endl << " \""; + i = 0; + total = 0; + sawQ = false; + pos += splitLen; + } + unsigned char thisChar = fullText.at(pos); switch (thisChar){ //Filter special characters. case '\n': tmp << "\\n"; break; @@ -51,7 +115,7 @@ int main(int argc, char* argv[]){ } } //end the last line, plus length variable - tmp << "\";" << std::endl << "uint32_t " << argv[2] << "_len = " << i + total << ";" << std::endl; + tmp << "\";" << std::endl << "uint32_t " << argv[2] << (splitLen?"_suffix":"") << "_len = " << i + total << ";" << std::endl; tmp.close(); return 0; }