2018-05-31 09:00:31 +00:00
|
|
|
/**
|
|
|
|
*
|
2020-10-12 13:51:39 +00:00
|
|
|
* @file create_view.cc
|
|
|
|
* @author 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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-05-29 09:21:37 +00:00
|
|
|
#include "create_view.h"
|
|
|
|
#include "cmd.h"
|
2020-10-09 13:41:27 +00:00
|
|
|
#include <drogon/utils/Utilities.h>
|
2018-05-29 09:21:37 +00:00
|
|
|
#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 = "$$";
|
2019-04-25 06:36:42 +00:00
|
|
|
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 = "%>";
|
2018-05-29 09:21:37 +00:00
|
|
|
|
|
|
|
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-05-29 09:21:37 +00:00
|
|
|
{
|
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)
|
|
|
|
{
|
2018-05-29 09:21:37 +00:00
|
|
|
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;
|
2018-05-29 09:21:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2018-10-14 07:56:54 +00:00
|
|
|
return str;
|
2018-05-29 09:21:37 +00:00
|
|
|
}
|
2023-08-23 03:33:30 +00:00
|
|
|
|
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-05-29 09:21:37 +00:00
|
|
|
{
|
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";
|
2018-05-29 09:21:37 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-23 03:33:30 +00:00
|
|
|
|
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-05 11:17:12 +00:00
|
|
|
{
|
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
|
2023-08-23 03:33:30 +00:00
|
|
|
<< "<<*(std::any_cast<const char *>(&val));\n";
|
2019-05-18 12:39:57 +00:00
|
|
|
oSrcFile << " }else "
|
|
|
|
"if(val.type()==typeid(std::string)||val.type()==typeid(const "
|
|
|
|
"std::string)){\n";
|
|
|
|
oSrcFile << " " << streamName
|
2023-08-23 03:33:30 +00:00
|
|
|
<< "<<*(std::any_cast<const std::string>(&val));\n";
|
2018-10-14 07:56:54 +00:00
|
|
|
oSrcFile << " }\n";
|
|
|
|
oSrcFile << "}\n";
|
2018-10-05 11:17:12 +00:00
|
|
|
}
|
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)
|
2018-05-29 09:21:37 +00:00
|
|
|
{
|
|
|
|
std::string::size_type pos(0);
|
2018-10-15 14:46:53 +00:00
|
|
|
// std::cout<<line<<"("<<line.length()<<")\n";
|
2020-02-03 00:39:48 +00:00
|
|
|
if (line.length() > 0 && line[line.length() - 1] == '\r')
|
|
|
|
{
|
|
|
|
line.resize(line.length() - 1);
|
|
|
|
}
|
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";
|
2021-11-06 03:15:46 +00:00
|
|
|
if (returnFlag && !cxx_flag)
|
2018-10-31 09:33:15 +00:00
|
|
|
oSrcFile << streamName << "<<\"\\n\";\n";
|
2018-05-29 09:21:37 +00:00
|
|
|
return;
|
2018-10-15 14:46:53 +00:00
|
|
|
}
|
2018-10-14 07:56:54 +00:00
|
|
|
if (cxx_flag == 0)
|
2018-05-29 09:21:37 +00:00
|
|
|
{
|
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-05-29 09:21:37 +00:00
|
|
|
{
|
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);
|
2018-05-29 09:21:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-14 07:56:54 +00:00
|
|
|
if ((pos = line.find(cxx_val_start)) != std::string::npos)
|
2018-10-05 11:17:12 +00:00
|
|
|
{
|
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-05 11:17:12 +00:00
|
|
|
{
|
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);
|
2018-10-05 11:17:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-14 07:56:54 +00:00
|
|
|
std::cerr << "format err!" << std::endl;
|
2018-10-05 11:17:12 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2018-05-29 09:21:37 +00:00
|
|
|
else
|
2018-10-05 11:17:12 +00:00
|
|
|
{
|
2018-10-14 07:56:54 +00:00
|
|
|
if (line.length() > 0)
|
2018-10-21 06:52:41 +00:00
|
|
|
{
|
|
|
|
replace_all(line, "\\", "\\\\");
|
|
|
|
replace_all(line, "\"", "\\\"");
|
|
|
|
oSrcFile << "\t" << streamName << " << \"" << line;
|
|
|
|
}
|
2018-10-14 07:56:54 +00:00
|
|
|
if (returnFlag)
|
|
|
|
oSrcFile << "\\n\";\n";
|
2018-10-05 11:17:12 +00:00
|
|
|
else
|
2018-10-14 07:56:54 +00:00
|
|
|
oSrcFile << "\";\n";
|
2018-10-05 11:17:12 +00:00
|
|
|
}
|
2018-05-29 09:21:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-14 07:56:54 +00:00
|
|
|
if ((pos = line.find(cxx_end)) != std::string::npos)
|
2018-05-29 09:21:37 +00:00
|
|
|
{
|
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);
|
2018-05-29 09:21:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-14 07:56:54 +00:00
|
|
|
parseCxxLine(oSrcFile, line, streamName, viewDataName);
|
2018-05-29 09:21:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void create_view::handleCommand(std::vector<std::string> ¶meters)
|
|
|
|
{
|
2020-10-09 13:41:27 +00:00
|
|
|
for (auto iter = parameters.begin(); iter != parameters.end();)
|
2018-05-29 09:21:37 +00:00
|
|
|
{
|
2020-10-09 13:41:27 +00:00
|
|
|
auto &file = *iter;
|
2018-10-14 07:56:54 +00:00
|
|
|
if (file == "-o" || file == "--output")
|
2018-06-14 10:51:26 +00:00
|
|
|
{
|
2018-10-14 07:56:54 +00:00
|
|
|
iter = parameters.erase(iter);
|
|
|
|
if (iter != parameters.end())
|
2018-06-14 10:51:26 +00:00
|
|
|
{
|
2019-11-21 03:27:47 +00:00
|
|
|
outputPath_ = *iter;
|
2018-10-14 07:56:54 +00:00
|
|
|
iter = parameters.erase(iter);
|
2018-06-14 10:51:26 +00:00
|
|
|
}
|
2020-10-09 13:41:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (file == "-n" || file == "--namespace")
|
|
|
|
{
|
|
|
|
iter = parameters.erase(iter);
|
|
|
|
if (iter != parameters.end())
|
|
|
|
{
|
|
|
|
namespaces_ = utils::splitString(*iter, "::");
|
|
|
|
iter = parameters.erase(iter);
|
|
|
|
}
|
|
|
|
continue;
|
2018-06-14 10:51:26 +00:00
|
|
|
}
|
2020-10-12 13:51:39 +00:00
|
|
|
else if (file == "--path-to-namespace")
|
|
|
|
{
|
|
|
|
iter = parameters.erase(iter);
|
|
|
|
pathToNamespaceFlag_ = true;
|
|
|
|
continue;
|
|
|
|
}
|
2018-10-14 07:56:54 +00:00
|
|
|
else if (file[0] == '-')
|
2018-05-29 09:21:37 +00:00
|
|
|
{
|
2018-10-14 07:56:54 +00:00
|
|
|
std::cout << ARGS_ERROR_STR << std::endl;
|
2018-05-29 09:21:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-10-09 13:41:27 +00:00
|
|
|
++iter;
|
2018-05-29 09:21:37 +00:00
|
|
|
}
|
|
|
|
createViewFiles(parameters);
|
|
|
|
}
|
2023-08-23 03:33:30 +00:00
|
|
|
|
2018-05-29 09:21:37 +00:00
|
|
|
void create_view::createViewFiles(std::vector<std::string> &cspFileNames)
|
|
|
|
{
|
2019-01-21 09:32:49 +00:00
|
|
|
for (auto const &file : cspFileNames)
|
2018-05-29 09:21:37 +00:00
|
|
|
{
|
2018-10-14 07:56:54 +00:00
|
|
|
std::cout << "create view:" << file << std::endl;
|
2021-07-08 23:58:03 +00:00
|
|
|
if (createViewFile(file) != 0)
|
|
|
|
exit(1);
|
2018-05-29 09:21:37 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-23 03:33:30 +00:00
|
|
|
|
2018-05-29 09:21:37 +00:00
|
|
|
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;
|
2020-10-12 13:51:39 +00:00
|
|
|
if (pathToNamespaceFlag_)
|
|
|
|
{
|
|
|
|
std::string::size_type pos1 = 0, pos2 = 0;
|
|
|
|
if (script_filename.length() >= 2 && script_filename[0] == '.' &&
|
|
|
|
(script_filename[1] == '/' || script_filename[1] == '\\'))
|
|
|
|
{
|
|
|
|
pos1 = pos2 = 2;
|
|
|
|
}
|
|
|
|
else if (script_filename.length() >= 1 &&
|
|
|
|
(script_filename[0] == '/' || script_filename[0] == '\\'))
|
|
|
|
{
|
|
|
|
pos1 = pos2 = 1;
|
|
|
|
}
|
|
|
|
while (pos2 < script_filename.length() - 1)
|
|
|
|
{
|
|
|
|
if (script_filename[pos2] == '/' || script_filename[pos2] == '\\')
|
|
|
|
{
|
|
|
|
if (pos2 > pos1)
|
|
|
|
{
|
|
|
|
namespaces_.push_back(
|
|
|
|
script_filename.substr(pos1, pos2 - pos1));
|
|
|
|
}
|
|
|
|
pos1 = ++pos2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++pos2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::string npPrefix;
|
|
|
|
for (auto &np : namespaces_)
|
|
|
|
{
|
|
|
|
npPrefix += np;
|
|
|
|
npPrefix += "_";
|
|
|
|
}
|
2018-10-14 07:56:54 +00:00
|
|
|
std::ifstream infile(script_filename.c_str(), std::ifstream::in);
|
|
|
|
if (infile)
|
2018-05-29 09:21:37 +00:00
|
|
|
{
|
2019-09-01 04:06:19 +00:00
|
|
|
std::string::size_type pos = script_filename.rfind('.');
|
2018-10-14 07:56:54 +00:00
|
|
|
if (pos != std::string::npos)
|
2018-05-29 09:21:37 +00:00
|
|
|
{
|
2018-10-14 07:56:54 +00:00
|
|
|
std::string className = script_filename.substr(0, pos);
|
2019-09-01 04:06:19 +00:00
|
|
|
if ((pos = className.rfind('/')) != std::string::npos)
|
2018-05-29 09:21:37 +00:00
|
|
|
{
|
2018-10-14 07:56:54 +00:00
|
|
|
className = className.substr(pos + 1);
|
2018-05-29 09:21:37 +00:00
|
|
|
}
|
2018-10-14 07:56:54 +00:00
|
|
|
std::cout << "className=" << className << std::endl;
|
2020-10-12 13:51:39 +00:00
|
|
|
std::string headFileName =
|
|
|
|
outputPath_ + "/" + npPrefix + className + ".h";
|
|
|
|
std::string sourceFilename =
|
|
|
|
outputPath_ + "/" + npPrefix + 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)
|
2020-02-16 09:19:16 +00:00
|
|
|
{
|
|
|
|
std::cerr << "Can't open " << headFileName << " or "
|
|
|
|
<< sourceFilename << "\n";
|
2018-05-29 09:21:37 +00:00
|
|
|
return -1;
|
2020-02-16 09:19:16 +00:00
|
|
|
}
|
2018-05-29 09:21:37 +00:00
|
|
|
|
2018-10-14 07:56:54 +00:00
|
|
|
newViewHeaderFile(oHeadFile, className);
|
2020-10-12 13:51:39 +00:00
|
|
|
newViewSourceFile(oSourceFile, className, npPrefix, infile);
|
2018-05-29 09:21:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-14 07:56:54 +00:00
|
|
|
std::cerr << "can't open file " << script_filename << std::endl;
|
2018-05-29 09:21:37 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2023-08-23 03:33:30 +00:00
|
|
|
|
2019-05-18 12:39:57 +00:00
|
|
|
void create_view::newViewHeaderFile(std::ofstream &file,
|
|
|
|
const std::string &className)
|
2018-05-29 09:21:37 +00:00
|
|
|
{
|
2019-05-18 12:39:57 +00:00
|
|
|
file << "//this file is generated by program automatically,don't modify "
|
|
|
|
"it!\n";
|
2018-10-17 02:50:51 +00:00
|
|
|
file << "#include <drogon/DrTemplate.h>\n";
|
2020-10-09 13:41:27 +00:00
|
|
|
for (auto &np : namespaces_)
|
|
|
|
{
|
|
|
|
file << "namespace " << np << "\n";
|
|
|
|
file << "{\n";
|
|
|
|
}
|
2020-10-12 13:51:39 +00:00
|
|
|
file << "class " << className << ":public drogon::DrTemplate<" << className
|
2019-05-18 12:39:57 +00:00
|
|
|
<< ">\n";
|
|
|
|
file << "{\npublic:\n\t" << className << "(){};\n\tvirtual ~" << className
|
|
|
|
<< "(){};\n\t"
|
2020-10-12 13:51:39 +00:00
|
|
|
"virtual std::string genText(const drogon::DrTemplateData &) "
|
2020-10-09 13:41:27 +00:00
|
|
|
"override;\n};\n";
|
2021-06-09 11:14:15 +00:00
|
|
|
for (std::size_t i = 0; i < namespaces_.size(); ++i)
|
2020-10-09 13:41:27 +00:00
|
|
|
{
|
|
|
|
file << "}\n";
|
|
|
|
}
|
2018-05-29 09:21:37 +00:00
|
|
|
}
|
|
|
|
|
2019-05-18 12:39:57 +00:00
|
|
|
void create_view::newViewSourceFile(std::ofstream &file,
|
|
|
|
const std::string &className,
|
2020-10-12 13:51:39 +00:00
|
|
|
const std::string &namespacePrefix,
|
2019-05-18 12:39:57 +00:00
|
|
|
std::ifstream &infile)
|
2018-05-29 09:21:37 +00:00
|
|
|
{
|
2019-05-18 12:39:57 +00:00
|
|
|
file << "//this file is generated by program(drogon_ctl) "
|
|
|
|
"automatically,don't modify it!\n";
|
2020-10-12 13:51:39 +00:00
|
|
|
file << "#include \"" << namespacePrefix << className << ".h\"\n";
|
2020-01-01 08:15:11 +00:00
|
|
|
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";
|
2020-10-12 13:51:39 +00:00
|
|
|
|
2020-06-18 13:44:54 +00:00
|
|
|
// Find layout tag
|
2020-02-16 09:19:16 +00:00
|
|
|
std::string layoutName;
|
|
|
|
std::regex layoutReg("<%layout[ \\t]+(((?!%\\}).)*[^ \\t])[ \\t]*%>");
|
2020-05-07 10:57:03 +00:00
|
|
|
for (std::string buffer; std::getline(infile, buffer);)
|
2020-06-18 13:44:54 +00:00
|
|
|
{
|
|
|
|
std::smatch results;
|
|
|
|
if (std::regex_search(buffer, results, layoutReg))
|
|
|
|
{
|
|
|
|
if (results.size() > 1)
|
|
|
|
{
|
|
|
|
layoutName = results[1].str();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
infile.clear();
|
|
|
|
infile.seekg(0, std::ifstream::beg);
|
|
|
|
bool import_flag{false};
|
|
|
|
for (std::string buffer; std::getline(infile, buffer);)
|
2018-05-29 09:21:37 +00:00
|
|
|
{
|
|
|
|
std::string::size_type pos(0);
|
|
|
|
|
2018-10-14 07:56:54 +00:00
|
|
|
if (!import_flag)
|
2018-05-29 09:21:37 +00:00
|
|
|
{
|
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(),
|
2022-04-22 03:08:40 +00:00
|
|
|
[](unsigned char c) { return tolower(c); });
|
2018-10-14 07:56:54 +00:00
|
|
|
if ((pos = lowerBuffer.find(cxx_include)) != std::string::npos)
|
2018-05-29 09:21:37 +00:00
|
|
|
{
|
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());
|
2020-06-18 13:44:54 +00:00
|
|
|
import_flag = true;
|
2018-10-14 07:56:54 +00:00
|
|
|
if ((pos = newLine.find(cxx_end)) != std::string::npos)
|
2018-05-29 09:21:37 +00:00
|
|
|
{
|
2018-10-14 07:56:54 +00:00
|
|
|
newLine = newLine.substr(0, pos);
|
|
|
|
file << newLine << "\n";
|
2018-05-29 09:21:37 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-10-14 07:56:54 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
file << newLine << "\n";
|
|
|
|
}
|
2018-05-29 09:21:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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-05-29 09:21:37 +00:00
|
|
|
{
|
2018-10-14 07:56:54 +00:00
|
|
|
std::string newLine = buffer.substr(0, pos);
|
|
|
|
file << newLine << "\n";
|
2018-05-29 09:21:37 +00:00
|
|
|
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";
|
2018-05-29 09:21:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-18 12:39:57 +00:00
|
|
|
// std::cout<<"import_flag="<<import_flag<<std::endl;
|
2020-06-18 13:44:54 +00:00
|
|
|
if (!import_flag)
|
2018-05-29 09:21:37 +00:00
|
|
|
{
|
|
|
|
infile.clear();
|
2018-10-14 07:56:54 +00:00
|
|
|
infile.seekg(0, std::ifstream::beg);
|
2018-05-29 09:21:37 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 13:51:39 +00:00
|
|
|
if (!namespaces_.empty())
|
|
|
|
{
|
|
|
|
file << "using namespace ";
|
2021-06-09 11:14:15 +00:00
|
|
|
for (std::size_t i = 0; i < namespaces_.size(); ++i)
|
2020-10-12 13:51:39 +00:00
|
|
|
{
|
|
|
|
if (i != namespaces_.size() - 1)
|
|
|
|
{
|
|
|
|
file << namespaces_[i] << "::";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
file << namespaces_[i] << ";";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
file << "\n";
|
|
|
|
}
|
|
|
|
file << "using namespace drogon;\n";
|
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";
|
2018-05-29 09:21:37 +00:00
|
|
|
|
2019-05-18 12:39:57 +00:00
|
|
|
// oSrcFile <<"\tstd::string "<<bodyName<<";\n";
|
2020-01-01 08:15:11 +00:00
|
|
|
file << "\tdrogon::OStringStream " << streamName << ";\n";
|
2020-02-16 09:19:16 +00:00
|
|
|
file << "\tstd::string layoutName{\"" << layoutName << "\"};\n";
|
2018-10-14 07:56:54 +00:00
|
|
|
int cxx_flag = 0;
|
2020-05-07 10:57:03 +00:00
|
|
|
for (std::string buffer; std::getline(infile, buffer);)
|
2018-05-29 09:21:37 +00:00
|
|
|
{
|
2018-12-07 11:08:17 +00:00
|
|
|
if (buffer.length() > 0)
|
|
|
|
{
|
2020-02-16 09:19:16 +00:00
|
|
|
std::smatch results;
|
|
|
|
if (std::regex_search(buffer, results, layoutReg))
|
|
|
|
{
|
|
|
|
if (results.size() > 1)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 04:55:14 +00:00
|
|
|
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);
|
2018-05-29 09:21:37 +00:00
|
|
|
}
|
2020-02-16 09:19:16 +00:00
|
|
|
file << "if(layoutName.empty())\n{\n";
|
2020-01-01 08:15:11 +00:00
|
|
|
file << "std::string ret{std::move(" << streamName << ".str())};\n";
|
2020-02-16 09:19:16 +00:00
|
|
|
file << "return ret;\n}else\n{\n";
|
2020-03-12 02:26:07 +00:00
|
|
|
file << "auto templ = DrTemplateBase::newTemplate(layoutName);\n";
|
2020-02-16 09:19:16 +00:00
|
|
|
file << "if(!templ) return \"\";\n";
|
2020-05-09 06:13:20 +00:00
|
|
|
file << "HttpViewData data = " << viewDataName << ";\n";
|
2020-02-16 09:19:16 +00:00
|
|
|
file << "auto str = std::move(" << streamName << ".str());\n";
|
|
|
|
file << "if(!str.empty() && str[str.length()-1] == '\\n') "
|
|
|
|
"str.resize(str.length()-1);\n";
|
|
|
|
file << "data[\"\"] = std::move(str);\n";
|
|
|
|
file << "return templ->genText(data);\n";
|
|
|
|
file << "}\n}\n";
|
2018-05-29 09:21:37 +00:00
|
|
|
}
|