代码拉取完成,页面将自动刷新
#pragma once
#include "compiler.hpp"
#include "runner.hpp"
namespace ns_compile_and_run
{
using namespace ns_log;
using namespace ns_util;
using namespace ns_compiler;
using namespace ns_runner;
class CompileAndRun
{
public:
// 获取报错代码对应的错误原因
// code > 0:进程收到了信号导致异常崩溃
// code < 0:整个过程非运行报错(代码为空、编译报错等)
// code = 0:整个过程全部完成
static string CodeToDesc(int code, const string &file_name)
{
string desc;
switch (code)
{
case 0:
desc = "编译运行成功";
break;
case -1:
desc = "提交的代码是空";
break;
case -2:
desc = "未知错误";
break;
case -3:
FileUtil::ReadFile(PathUtil::CompileError(file_name), &desc, true);
break;
case SIGABRT: // 6
desc = "内存超过范围";
break;
case SIGXCPU: // 24
desc = "CPU使用超时";
break;
case SIGFPE: // 8
desc = "浮点数溢出";
break;
default:
desc = "未知: " + std::to_string(code);
break;
}
return desc;
}
// 删除临时文件
static void RemoveTempFile(const string file_name)
{
string _src = PathUtil::Src(file_name);
if (FileUtil::IsFileExists(_src))
unlink(_src.c_str());
string _compile_error = PathUtil::CompileError(file_name);
if (FileUtil::IsFileExists(_compile_error))
unlink(_compile_error.c_str());
string _execute = PathUtil::Exe(file_name);
if (FileUtil::IsFileExists(_execute))
unlink(_execute.c_str());
string _stdin = PathUtil::Stdin(file_name);
if (FileUtil::IsFileExists(_stdin))
unlink(_stdin.c_str());
string _stdout = PathUtil::Stdout(file_name);
if (FileUtil::IsFileExists(_stdout))
unlink(_stdout.c_str());
string _stderr = PathUtil::Stderr(file_name);
if (FileUtil::IsFileExists(_stderr))
unlink(_stderr.c_str());
}
/************************************
* 输入:
* code:用户提交的代码
* input:用户给自己提交的代码对应的输入,不做处理
* cpu_limit:时间要求
* mem_limit:空间要求
*
* in_json: {"code" : "#include...", "input" : "1, 2"....}
*
* 输出:
* 必填:
* status:状态码
* reason:请求结果
* 选填:
* stdout:我的程序运行完的结果
* stderr:我的程序运行完的错误结果
*************************************/
static void Start(const string &in_json, string *out_json)
{
Json::Value in_value; // Value是Json的中间类,可以填充KV值
Json::Reader reader;
reader.parse(in_json, in_value); // 将in_json的序列化数据,反序列化成KV值并读取到in_value中
string code = in_value["code"].asString();
string input = in_value["input"].asString();
int cpu_limit = in_value["cpu_limit"].asInt();
int mem_limit = in_value["mem_limit"].asInt();
int status_code = 0; // 错误码,都为负数,方便和信号区分
Json::Value out_value;
int run_result = 0;
string file_name;
if (code.size() == 0)
{
status_code = -1; // 用户代码为空
goto END;
}
// 形成的文件名具有唯一性,没有目录没有后缀
file_name = FileUtil::UniqueFileName();
// 将代码写入源文件
if (!FileUtil::WriteFile(PathUtil::Src(file_name), code))
{
status_code = -2; // 未知错误(不暴露给用户,内部错误)
goto END;
}
// 编译并运行
if (!Compiler::Compile(file_name))
{
status_code = -3; // 编译错误
goto END;
}
run_result = Runner::Run(file_name, cpu_limit, mem_limit);
if (run_result < 0)
{
status_code = -2; // 未知错误
}
else if (run_result > 0)
{
status_code = run_result; // 用户程序运行错误
}
END:
out_value["status"] = status_code;
out_value["reason"] = CodeToDesc(status_code, file_name);
if (status_code == 0)
{
// 整个过程全部成功
string _stdout;
FileUtil::ReadFile(PathUtil::Stdout(file_name), &_stdout, true);
out_value["stdout"] = _stdout;
string _stderr;
FileUtil::ReadFile(PathUtil::Stderr(file_name), &_stderr, true);
out_value["stderr"] = _stderr;
}
Json::StyledWriter writer;
*out_json = writer.write(out_value);
//删除临时文件
RemoveTempFile(file_name);
}
};
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。