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:
parent
793f6f7809
commit
27a2e596fd
1 changed files with 71 additions and 7 deletions
|
@ -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
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue