drogon/drogon_ctl/create_view.cc

434 lines
15 KiB
C++
Raw Normal View History

2018-05-31 09:00:31 +00:00
/**
*
2018-10-17 05:48:28 +00:00
* create_view.cc
* An Tao
2018-05-31 09:00:31 +00:00
*
* Copyright 2018, An Tao. All rights reserved.
2018-12-07 07:50:18 +00:00
* https://github.com/an-tao/drogon
2018-05-31 09:00:31 +00:00
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
2018-11-16 05:26:14 +00:00
* Drogon
2018-05-31 09:00:31 +00:00
*
*/
#include "create_view.h"
#include "cmd.h"
#include <iostream>
#include <fstream>
#include <string>
2018-05-29 13:24:04 +00:00
#include <algorithm>
2018-12-07 11:08:17 +00:00
#include <regex>
2018-10-14 07:56:54 +00:00
static const std::string cxx_include = "<%inc";
static const std::string cxx_end = "%>";
static const std::string cxx_lang = "<%c++";
static const std::string cxx_view_data = "@@";
static const std::string cxx_output = "$$";
static const std::string cxx_val_start = "[[";
static const std::string cxx_val_end = "]]";
2018-10-17 05:48:28 +00:00
static const std::string sub_view_start = "<%view";
static const std::string sub_view_end = "%>";
using namespace drogon_ctl;
2019-05-18 12:39:57 +00:00
static std::string &replace_all(std::string &str,
const std::string &old_value,
const std::string &new_value)
{
2018-10-14 07:56:54 +00:00
std::string::size_type pos(0);
while (true)
{
2019-05-18 12:39:57 +00:00
// std::cout<<str<<endl;
// std::cout<<"pos="<<pos<<endl;
2018-10-14 07:56:54 +00:00
if ((pos = str.find(old_value, pos)) != std::string::npos)
{
str = str.replace(pos, old_value.length(), new_value);
2018-10-14 07:56:54 +00:00
pos += new_value.length() - old_value.length();
2019-11-21 03:27:47 +00:00
++pos;
}
else
break;
}
2018-10-14 07:56:54 +00:00
return str;
}
2019-05-18 12:39:57 +00:00
static void parseCxxLine(std::ofstream &oSrcFile,
const std::string &line,
const std::string &streamName,
const std::string &viewDataName)
{
2018-10-14 07:56:54 +00:00
if (line.length() > 0)
{
std::string tmp = line;
replace_all(tmp, cxx_output, streamName);
replace_all(tmp, cxx_view_data, viewDataName);
oSrcFile << tmp << "\n";
}
}
2019-05-18 12:39:57 +00:00
static void outputVal(std::ofstream &oSrcFile,
const std::string &streamName,
const std::string &viewDataName,
const std::string &keyName)
{
2018-10-14 07:56:54 +00:00
oSrcFile << "{\n";
2019-05-18 12:39:57 +00:00
oSrcFile << " auto & val=" << viewDataName << "[\"" << keyName
<< "\"];\n";
2018-10-14 07:56:54 +00:00
oSrcFile << " if(val.type()==typeid(const char *)){\n";
2019-05-18 12:39:57 +00:00
oSrcFile << " " << streamName
<< "<<*any_cast<const char *>(&val);\n";
oSrcFile << " }else "
"if(val.type()==typeid(std::string)||val.type()==typeid(const "
"std::string)){\n";
oSrcFile << " " << streamName
<< "<<*any_cast<const std::string>(&val);\n";
2018-10-14 07:56:54 +00:00
oSrcFile << " }\n";
oSrcFile << "}\n";
}
2018-10-17 05:48:28 +00:00
2019-05-18 12:39:57 +00:00
static void outputSubView(std::ofstream &oSrcFile,
const std::string &streamName,
const std::string &viewDataName,
const std::string &keyName)
2018-10-17 05:48:28 +00:00
{
oSrcFile << "{\n";
2019-05-18 12:39:57 +00:00
oSrcFile << " auto templ=DrTemplateBase::newTemplate(\"" << keyName
<< "\");\n";
2018-10-17 05:48:28 +00:00
oSrcFile << " if(templ){\n";
2019-05-18 12:39:57 +00:00
oSrcFile << " " << streamName << "<< templ->genText(" << viewDataName
<< ");\n";
2018-10-17 05:48:28 +00:00
oSrcFile << " }\n";
oSrcFile << "}\n";
}
2019-05-18 12:39:57 +00:00
static void parseLine(std::ofstream &oSrcFile,
std::string &line,
const std::string &streamName,
const std::string &viewDataName,
int &cxx_flag,
int returnFlag = 1)
{
std::string::size_type pos(0);
2018-10-15 14:46:53 +00:00
// std::cout<<line<<"("<<line.length()<<")\n";
2018-10-14 07:56:54 +00:00
if (line.length() == 0)
2018-10-15 14:46:53 +00:00
{
// std::cout<<"blank line!"<<std::endl;
// std::cout<<streamName<<"<<\"\\n\";\n";
2018-10-31 09:33:15 +00:00
if (returnFlag)
oSrcFile << streamName << "<<\"\\n\";\n";
return;
2018-10-15 14:46:53 +00:00
}
2018-10-14 07:56:54 +00:00
if (cxx_flag == 0)
{
2019-05-18 12:39:57 +00:00
// find cxx lang begin
2018-10-14 07:56:54 +00:00
if ((pos = line.find(cxx_lang)) != std::string::npos)
{
2018-10-14 07:56:54 +00:00
std::string oldLine = line.substr(0, pos);
2018-10-31 09:33:15 +00:00
if (oldLine.length() > 0)
2019-05-18 12:39:57 +00:00
parseLine(
oSrcFile, oldLine, streamName, viewDataName, cxx_flag, 0);
2018-10-14 07:56:54 +00:00
std::string newLine = line.substr(pos + cxx_lang.length());
cxx_flag = 1;
2018-10-31 09:33:15 +00:00
if (newLine.length() > 0)
2019-05-18 12:39:57 +00:00
parseLine(oSrcFile,
newLine,
streamName,
viewDataName,
cxx_flag,
returnFlag);
}
else
{
2018-10-14 07:56:54 +00:00
if ((pos = line.find(cxx_val_start)) != std::string::npos)
{
2018-10-14 07:56:54 +00:00
std::string oldLine = line.substr(0, pos);
2019-05-18 12:39:57 +00:00
parseLine(
oSrcFile, oldLine, streamName, viewDataName, cxx_flag, 0);
2018-10-14 07:56:54 +00:00
std::string newLine = line.substr(pos + cxx_val_start.length());
if ((pos = newLine.find(cxx_val_end)) != std::string::npos)
{
2018-10-14 07:56:54 +00:00
std::string keyName = newLine.substr(0, pos);
auto iter = keyName.begin();
while (iter != keyName.end() && *iter == ' ')
2019-11-21 03:27:47 +00:00
++iter;
2018-10-14 07:56:54 +00:00
auto iterEnd = iter;
while (iterEnd != keyName.end() && *iterEnd != ' ')
2019-11-21 03:27:47 +00:00
++iterEnd;
2018-10-14 07:56:54 +00:00
keyName = std::string(iter, iterEnd);
outputVal(oSrcFile, streamName, viewDataName, keyName);
2019-05-18 12:39:57 +00:00
std::string tailLine =
newLine.substr(pos + cxx_val_end.length());
parseLine(oSrcFile,
tailLine,
streamName,
viewDataName,
cxx_flag,
returnFlag);
2018-10-17 05:48:28 +00:00
}
else
{
std::cerr << "format err!" << std::endl;
exit(1);
}
}
else if ((pos = line.find(sub_view_start)) != std::string::npos)
{
std::string oldLine = line.substr(0, pos);
2019-05-18 12:39:57 +00:00
parseLine(
oSrcFile, oldLine, streamName, viewDataName, cxx_flag, 0);
std::string newLine =
line.substr(pos + sub_view_start.length());
2018-10-17 05:48:28 +00:00
if ((pos = newLine.find(sub_view_end)) != std::string::npos)
{
std::string keyName = newLine.substr(0, pos);
auto iter = keyName.begin();
while (iter != keyName.end() && *iter == ' ')
2019-11-21 03:27:47 +00:00
++iter;
2018-10-17 05:48:28 +00:00
auto iterEnd = iter;
while (iterEnd != keyName.end() && *iterEnd != ' ')
2019-11-21 03:27:47 +00:00
++iterEnd;
2018-10-17 05:48:28 +00:00
keyName = std::string(iter, iterEnd);
outputSubView(oSrcFile, streamName, viewDataName, keyName);
2019-05-18 12:39:57 +00:00
std::string tailLine =
newLine.substr(pos + sub_view_end.length());
parseLine(oSrcFile,
tailLine,
streamName,
viewDataName,
cxx_flag,
returnFlag);
}
else
{
2018-10-14 07:56:54 +00:00
std::cerr << "format err!" << std::endl;
exit(1);
}
}
else
{
2018-10-14 07:56:54 +00:00
if (line.length() > 0)
{
replace_all(line, "\\", "\\\\");
replace_all(line, "\"", "\\\"");
oSrcFile << "\t" << streamName << " << \"" << line;
}
2018-10-14 07:56:54 +00:00
if (returnFlag)
oSrcFile << "\\n\";\n";
else
2018-10-14 07:56:54 +00:00
oSrcFile << "\";\n";
}
}
}
else
{
2018-10-14 07:56:54 +00:00
if ((pos = line.find(cxx_end)) != std::string::npos)
{
2018-10-14 07:56:54 +00:00
std::string newLine = line.substr(0, pos);
parseCxxLine(oSrcFile, newLine, streamName, viewDataName);
std::string oldLine = line.substr(pos + cxx_end.length());
cxx_flag = 0;
2018-10-31 09:33:15 +00:00
if (oldLine.length() > 0)
2019-05-18 12:39:57 +00:00
parseLine(oSrcFile,
oldLine,
streamName,
viewDataName,
cxx_flag,
returnFlag);
}
else
{
2018-10-14 07:56:54 +00:00
parseCxxLine(oSrcFile, line, streamName, viewDataName);
}
}
}
void create_view::handleCommand(std::vector<std::string> &parameters)
{
2018-10-14 07:56:54 +00:00
for (auto iter = parameters.begin(); iter != parameters.end(); iter++)
{
2018-10-14 07:56:54 +00:00
auto file = *iter;
if (file == "-o" || file == "--output")
{
2018-10-14 07:56:54 +00:00
iter = parameters.erase(iter);
if (iter != parameters.end())
{
2019-11-21 03:27:47 +00:00
outputPath_ = *iter;
2018-10-14 07:56:54 +00:00
iter = parameters.erase(iter);
}
break;
}
2018-10-14 07:56:54 +00:00
else if (file[0] == '-')
{
2018-10-14 07:56:54 +00:00
std::cout << ARGS_ERROR_STR << std::endl;
return;
}
}
createViewFiles(parameters);
}
void create_view::createViewFiles(std::vector<std::string> &cspFileNames)
{
for (auto const &file : cspFileNames)
{
2018-10-14 07:56:54 +00:00
std::cout << "create view:" << file << std::endl;
createViewFile(file);
}
}
int create_view::createViewFile(const std::string &script_filename)
{
2019-05-18 12:39:57 +00:00
std::cout << "create HttpView Class file by " << script_filename
<< std::endl;
2018-10-14 07:56:54 +00:00
std::ifstream infile(script_filename.c_str(), std::ifstream::in);
if (infile)
{
std::string::size_type pos = script_filename.rfind('.');
2018-10-14 07:56:54 +00:00
if (pos != std::string::npos)
{
2018-10-14 07:56:54 +00:00
std::string className = script_filename.substr(0, pos);
if ((pos = className.rfind('/')) != std::string::npos)
{
2018-10-14 07:56:54 +00:00
className = className.substr(pos + 1);
}
2018-10-14 07:56:54 +00:00
std::cout << "className=" << className << std::endl;
2019-11-21 03:27:47 +00:00
std::string headFileName = outputPath_ + "/" + className + ".h";
std::string sourceFilename = outputPath_ + "/" + className + ".cc";
2018-10-14 07:56:54 +00:00
std::ofstream oHeadFile(headFileName.c_str(), std::ofstream::out);
2019-05-18 12:39:57 +00:00
std::ofstream oSourceFile(sourceFilename.c_str(),
std::ofstream::out);
2018-10-14 07:56:54 +00:00
if (!oHeadFile || !oSourceFile)
return -1;
2018-10-14 07:56:54 +00:00
newViewHeaderFile(oHeadFile, className);
newViewSourceFile(oSourceFile, className, infile);
}
else
return -1;
}
else
{
2018-10-14 07:56:54 +00:00
std::cerr << "can't open file " << script_filename << std::endl;
return -1;
}
return 0;
}
2019-05-18 12:39:57 +00:00
void create_view::newViewHeaderFile(std::ofstream &file,
const std::string &className)
{
2019-05-18 12:39:57 +00:00
file << "//this file is generated by program automatically,don't modify "
"it!\n";
file << "#include <drogon/DrTemplate.h>\n";
2018-10-14 07:56:54 +00:00
file << "using namespace drogon;\n";
2019-05-18 12:39:57 +00:00
file << "class " << className << ":public DrTemplate<" << className
<< ">\n";
file << "{\npublic:\n\t" << className << "(){};\n\tvirtual ~" << className
<< "(){};\n\t"
"virtual std::string genText(const DrTemplateData &) override;\n};";
}
2019-05-18 12:39:57 +00:00
void create_view::newViewSourceFile(std::ofstream &file,
const std::string &className,
std::ifstream &infile)
{
2019-05-18 12:39:57 +00:00
file << "//this file is generated by program(drogon_ctl) "
"automatically,don't modify it!\n";
file << "#include \"" << className << ".h\"\n";
file << "#include <drogon/utils/OStringStream.h>\n";
2018-10-14 07:56:54 +00:00
file << "#include <string>\n";
file << "#include <map>\n";
file << "#include <vector>\n";
file << "#include <set>\n";
file << "#include <iostream>\n";
file << "#include <unordered_map>\n";
file << "#include <unordered_set>\n";
file << "#include <algorithm>\n";
file << "#include <list>\n";
file << "#include <deque>\n";
file << "#include <queue>\n";
2019-06-27 07:37:41 +00:00
// file << "using namespace std;\n";
2018-10-14 07:56:54 +00:00
// file <<"void __attribute__((constructor)) startup()\n";
// file <<"{std::cout<<\"dynamic lib start to load!\"<<std::endl;}\n";
// file <<"void __attribute__((destructor)) shutdown()\n";
// file <<"{std::cout<<\"dynamic lib start to unload!\"<<std::endl;}\n";
std::string buffer;
char line[8192];
2018-10-14 07:56:54 +00:00
int import_flag = 0;
2018-10-14 07:56:54 +00:00
while (infile.getline(line, sizeof(line)))
{
2018-10-14 07:56:54 +00:00
buffer = line;
std::string::size_type pos(0);
2018-10-14 07:56:54 +00:00
if (!import_flag)
{
2018-10-14 07:56:54 +00:00
std::string lowerBuffer = buffer;
2019-05-18 12:39:57 +00:00
std::transform(lowerBuffer.begin(),
lowerBuffer.end(),
lowerBuffer.begin(),
::tolower);
2018-10-14 07:56:54 +00:00
if ((pos = lowerBuffer.find(cxx_include)) != std::string::npos)
{
2019-05-18 12:39:57 +00:00
// std::cout<<"haha find it!"<<endl;
2018-10-14 07:56:54 +00:00
std::string newLine = buffer.substr(pos + cxx_include.length());
import_flag = 1;
if ((pos = newLine.find(cxx_end)) != std::string::npos)
{
2018-10-14 07:56:54 +00:00
newLine = newLine.substr(0, pos);
file << newLine << "\n";
break;
}
2018-10-14 07:56:54 +00:00
else
{
file << newLine << "\n";
}
}
}
else
{
2019-05-18 12:39:57 +00:00
// std::cout<<buffer<<endl;
2018-10-14 07:56:54 +00:00
if ((pos = buffer.find(cxx_end)) != std::string::npos)
{
2018-10-14 07:56:54 +00:00
std::string newLine = buffer.substr(0, pos);
file << newLine << "\n";
break;
}
else
{
2019-05-18 12:39:57 +00:00
// std::cout<<"to source file"<<buffer<<endl;
2018-10-14 07:56:54 +00:00
file << buffer << "\n";
}
}
}
2019-05-18 12:39:57 +00:00
// std::cout<<"import_flag="<<import_flag<<std::endl;
2018-10-14 07:56:54 +00:00
if (import_flag == 0)
{
infile.clear();
2018-10-14 07:56:54 +00:00
infile.seekg(0, std::ifstream::beg);
}
2019-05-18 12:39:57 +00:00
// std::cout<<"file pos:"<<infile.tellg()<<std::endl;
2018-10-14 07:56:54 +00:00
std::string viewDataName = className + "_view_data";
2019-05-18 12:39:57 +00:00
// virtual std::string genText(const DrTemplateData &)
file << "std::string " << className << "::genText(const DrTemplateData& "
<< viewDataName << ")\n{\n";
// std::string bodyName=className+"_bodystr";
2018-10-14 07:56:54 +00:00
std::string streamName = className + "_tmp_stream";
2019-05-18 12:39:57 +00:00
// oSrcFile <<"\tstd::string "<<bodyName<<";\n";
file << "\tdrogon::OStringStream " << streamName << ";\n";
2018-10-14 07:56:54 +00:00
int cxx_flag = 0;
while (infile.getline(line, sizeof(line)))
{
2018-10-14 07:56:54 +00:00
buffer = line;
2018-12-07 11:08:17 +00:00
if (buffer.length() > 0)
{
std::regex re("\\{%[ \\t]*(((?!%\\}).)*[^ \\t])[ \\t]*%\\}");
2018-12-07 11:08:17 +00:00
buffer = std::regex_replace(buffer, re, "<%c++$$$$<<$1;%>");
}
2018-10-14 07:56:54 +00:00
parseLine(file, buffer, streamName, viewDataName, cxx_flag);
}
file << "std::string ret{std::move(" << streamName << ".str())};\n";
file << "return ret;\n}\n";
}