Download displaycode.cc
//cgi which displays source code (C, C++) in html using prims.js and
//prism.css. Source code file is passed in variable filename in get request:
//displaycode?lang=en&filename=loggbok.c ... for example.
#include<iostream>
#include<string>
#include<string.h>
#include<fstream>
#include<sstream>
#include<cgicc/Cgicc.h>
#include<cgicc/HTMLClasses.h>
#include<cgicc/HTTPHTMLHeader.h>
using namespace cgicc;
using namespace std;
//Next two strings contain html to be placed at beginning and end of output.
string header = "<!DOCTYPE html>\n"
"<html lang=\"en\">\n"
"<head>\n"
"<meta charset=\"UTF-8\"/>\n"
"<title>filename</title>"
"<link href=\"https://briansmath.ddns.net/Code/prism.css\" rel=\"stylesheet\">\n"
"<link rel = \"icon\" href = \"https://briansmath.ddns.net/mathicon.png\" type = \"image/x-icon\">"
"</head>\n"
"<body>\n"
"<script src=\"https://briansmath.ddns.net/Code/prism.js\"></script>\n"
"<a href=\"filename\" download>Download filename</a>\n"
"<pre class=\"line-numbers\">\n"
"<code class=\"language-c\">";
//NOTE: 'filenanme' to be replaced by file name given in GET request.
string footer = "</code>\n</pre>\n</body>\n</html>";
//Output will be header + data_from_file + footer.
//MAIN *********************************************************************************************
int main(int argc, char **argv){
Cgicc formData; //File name should be formData("filename")
ifstream myfile;
if(formData("filename").find("/") != string::npos){ //Why do you think this block is here 🤔
if(formData("lang")=="en")
myfile.open("error.en.html");
else
myfile.open("error.html");
cout << cgicc::HTTPHTMLHeader() << endl;
cout << myfile.rdbuf();
myfile.close();
return 0;
}
//Replace 'filename' with actual file name.
header.replace(header.find("filename"), strlen("filename"), formData("filename"));
header.replace(header.find("filename"), strlen("filename"), formData("filename"));
header.replace(header.find("filename"), strlen("filename"), formData("filename"));
// ... ok, that was a lazy way to do that.
myfile.open(formData("filename").c_str()); //Attempt to open the file.
if(!myfile.is_open()){ //Check if file is open: if not display error page and return.
if(formData("lang")=="en")
myfile.open("error.en.html");
else
myfile.open("error.html");
cout << cgicc::HTTPHTMLHeader() << endl;
cout << myfile.rdbuf();
myfile.close();
return 0;
}
ostringstream os;
os << myfile.rdbuf(); //read the file into osstream os.
myfile.close();
string outs = os.str(); //File contents now in string outs.
//We now need to replace html speicial characters with approppriate codes
size_t pos = 0;
while(true){
pos = outs.find("<", pos);
if (pos == string::npos)
break;
outs.replace(pos, 1, "<"); // '<' here is actually &lt in source code.
}
pos = 0;
while(true){
pos = outs.find(">", pos);
if (pos == string::npos)
break;
outs.replace(pos, 1, ">"); // '>' here is actually &lt in source code.
}
//Hopefully the output string has been doctored enough:
//SEND IT DOWN THE WIRE 🙂
cout << cgicc::HTTPHTMLHeader() << endl;
cout << header << outs << footer << endl;
return 0;
}