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.
This commit is contained in:
Thulinma 2020-02-28 16:26:40 +01:00
parent 793f6f7809
commit 27a2e596fd

View file

@ -1,25 +1,89 @@
///\file sourcery.cpp ///\file sourcery.cpp
///Utility program used for c-string dumping files. ///Utility program used for c-string dumping files.
#undef DEBUG
#define DEBUG -1
#include <stdint.h> #include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
#include <fstream> #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[]){ int main(int argc, char* argv[]){
if (argc < 4) { 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; 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]); std::ofstream tmp(argv[3]);
//begin the first line //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 i = 0; //Current line byte counter
uint32_t total = 0; //Finished lines so far byte counter uint32_t total = 0; //Finished lines so far byte counter
std::ifstream inFile(argv[1]);
bool sawQ = false; bool sawQ = false;
while (inFile.good()){ for (size_t pos = 0; pos < fullText.size(); ++pos){
unsigned char thisChar = inFile.get(); if (pos == splitPoint){
if (!inFile.good()){break;} 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){ switch (thisChar){
//Filter special characters. //Filter special characters.
case '\n': tmp << "\\n"; break; case '\n': tmp << "\\n"; break;
@ -51,7 +115,7 @@ int main(int argc, char* argv[]){
} }
} }
//end the last line, plus length variable //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(); tmp.close();
return 0; return 0;
} }