tpl_code
点击查看代码
#include<bits/stdc++.h>
using namespace std;
#ifndef LINUX_HPP
#define LINUX_HPP
#if INT_MAX==RAND_MAX
#include<bits/stdc++.h>
using namespace std;
#ifndef COLORLINUX_H
#define COLORLINUX_H
#if INT_MAX==RAND_MAX
#include<bits/stdc++.h>
using namespace std;
namespace hdk{
class color{
public:
const string NONE= "\e[0m";
const string BLACK= "\e[0;30m";
const string L_BLACK= "\e[1;30m";
const string RED= "\e[0;31m";
const string L_RED= "\e[1;31m";
const string GREEN= "\e[0;32m";
const string L_GREEN= "\e[1;32m";
const string BROWN= "\e[0;33m";
const string YELLOW= "\e[1;33m";
const string BLUE= "\e[0;34m";
const string L_BLUE= "\e[1;34m";
const string PURPLE= "\e[0;35m";
const string L_PURPLE= "\e[1;35m";
const string CYAN= "\e[0;36m";
const string L_CYAN= "\e[1;36m";
const string GRAY= "\e[0;37m";
const string WHITE= "\e[1;37m";
const string BOLD= "\e[1m";
const string UNDERLINE= "\e[4m";
const string BLINK= "\e[5m";
const string REVERSE= "\e[7m";
const string HIDE= "\e[8m";
const string CLEAR= "\e[2J";
}color;
class color_t{
private:string x;
public:
color_t operator =(const std::string y){x=y;return *this;};
color_t(const std::string y){x=y;}
color_t operator +(const color_t&A)const{
color_t ans=*this;ans.x=ans.x+";"+A.x;
return ans;
}
const std::string operator()()const{return x;}
};
class __const_color_t{
private:string x;
public:
__const_color_t operator =(const string y){x=y;return *this;};
__const_color_t(const string y){x=y;}
const string operator()()const{return x;}
};
class __color{
public:
const __const_color_t START=(string)"\e[";
const __const_color_t NONE=(string)"\e[0m";
const color_t EMPTY=(string)"";
const color_t HIGHLIGHT=(string)"1";
const color_t VAGUE=(string)"2";
const color_t ITALIC=(string)"3";
const color_t UNDERLINE=(string)"4";
const color_t TWINKLE=(string)"5";
const color_t FAST_TWINKLE=(string)"6";
const color_t REVERSE_COLOR=(string)"7";
const color_t HIDE=(string)"8";
struct{
const color_t BLACK=(string)"30";
const color_t RED=(string)"31";
const color_t GREEN=(string)"32";
const color_t YELLOW=(string)"33";
const color_t BLUE=(string)"34";
const color_t PERPLE=(string)"35";
const color_t CRAY=(string)"36";
const color_t WHITE=(string)"37";
}FRONTCOLOR;
struct{
const color_t BLACK=(string)"40";
const color_t RED=(string)"41";
const color_t GREEN=(string)"42";
const color_t YELLOW=(string)"43";
const color_t BLUE=(string)"44";
const color_t PERPLE=(string)"45";
const color_t CRAY=(string)"46";
const color_t WHITE=(string)"47";
}BACKGROUND;
};
typedef __color __color_t;
__color_t __color_info;
#define color_print(x) std::printf("%s",((string)""+(x)+hdk::color.NONE).c_str())
#define printc(x,info) std::printf("%s",(hdk::__color_info.START()+(x)()+"m"+info+hdk::__color_info.NONE()).c_str())
}
#endif
#endif
namespace hdk{
bool linux_copyfile(const char*in,const char *out){
ifstream _fin(in);
if(!_fin){
printc(hdk::__color_info.FRONTCOLOR.RED+hdk::__color_info.HIGHLIGHT,"Error");
cout<<": File ";
printc(hdk::__color_info.FRONTCOLOR.CRAY+hdk::__color_info.ITALIC,in);
cout<<" not found"<<endl;
return 0;
}
ofstream _fout(out);
if(!_fout){
printc(hdk::__color_info.FRONTCOLOR.RED+hdk::__color_info.HIGHLIGHT,"Error");
cout<<": File ";
printc(hdk::__color_info.FRONTCOLOR.CRAY+hdk::__color_info.ITALIC,out);
cout<<" not found"<<endl;
return 0;
}
while(!_fin.eof()){
char _a[1000001];_fin.getline(_a,1000000);
_fout<<_a<<"\n";
}
return 1;
}
bool linux_printfile(const char*in){
ifstream _fin(in);
if(!_fin){
printc(hdk::__color_info.FRONTCOLOR.RED+hdk::__color_info.HIGHLIGHT,"Error");
cout<<": File ";
printc(hdk::__color_info.FRONTCOLOR.CRAY+hdk::__color_info.ITALIC,in);
cout<<" not found"<<endl;
return 0;
}
cout<<"File ";
printc(hdk::__color_info.FRONTCOLOR.CRAY+hdk::__color_info.UNDERLINE+hdk::__color_info.ITALIC,in);
cout<<endl;
while(!_fin.eof()){
char _a[1000001];_fin.getline(_a,1000000);
cout<<_a<<"\n";
}
return 1;
}
bool linux_fc(const char *file_A,const char *file_B,bool display=true,bool debug_mode=false){
std::string a,b;
if(display){
printc(hdk::__color_info.FRONTCOLOR.PERPLE,"fc");
cout<<' ';
printc(hdk::__color_info.FRONTCOLOR.CRAY+hdk::__color_info.UNDERLINE+hdk::__color_info.ITALIC,file_A);
cout<<" and ";
printc(hdk::__color_info.FRONTCOLOR.CRAY+hdk::__color_info.UNDERLINE+hdk::__color_info.ITALIC,file_B);
cout<<endl;
}
bool eofA=false,eofB=false;std::ifstream f1,f2;f1.open(file_A);f2.open(file_B);
if(!f1){
printc(hdk::__color_info.FRONTCOLOR.RED+hdk::__color_info.HIGHLIGHT,"Error");
cout<<": File ";
printc(hdk::__color_info.FRONTCOLOR.CRAY+hdk::__color_info.ITALIC,file_A);
cout<<" not found"<<endl;
return true;
}
if(!f2){
printc(hdk::__color_info.FRONTCOLOR.RED+hdk::__color_info.HIGHLIGHT,"Error");
cout<<": File ";
printc(hdk::__color_info.FRONTCOLOR.CRAY+hdk::__color_info.ITALIC,file_B);
cout<<" not found"<<endl;
return true;
}
while(1){
if(f1.eof()){eofA=true;}
else{f1>>a;}
if(f2.eof()){eofB=true;}
else{f2>>b;}
if(eofA and eofB){
if(display){
printc(hdk::__color_info.FRONTCOLOR.GREEN+hdk::__color_info.HIGHLIGHT,"Accept");
cout<<": File is equal.";
cout<<endl;
}
return false;
}
else if(eofA or eofB){
if((eofA and (b[0]=='\n' or b[0]==' ' or b[0]==0)) or (eofB and (a[0]=='\n' or a[0]==' ' or a[0]==0))){
if(display){
printc(hdk::__color_info.FRONTCOLOR.GREEN+hdk::__color_info.HIGHLIGHT,"Accept");
cout<<": File is equal.";
cout<<endl;
}
return false;
}
if(display){
printc(hdk::__color_info.FRONTCOLOR.RED+hdk::__color_info.HIGHLIGHT,"Find Difference");
printf(" Length is not equal.\n");
return true;
}
}
if(debug_mode){
printc(hdk::__color_info.FRONTCOLOR.PERPLE,"Compare");
printf(": '%s' '%s'\n",a.c_str(),b.c_str());
}
if(a!=b){
if(display){
printc(hdk::__color_info.FRONTCOLOR.RED+hdk::__color_info.HIGHLIGHT,"Find Difference");
cout<<endl;
printc(hdk::__color_info.FRONTCOLOR.CRAY+hdk::__color_info.UNDERLINE+hdk::__color_info.ITALIC,file_A);
printf(": %s\n",a.c_str());
printc(hdk::__color_info.FRONTCOLOR.CRAY+hdk::__color_info.UNDERLINE+hdk::__color_info.ITALIC,file_B);
printf(": %s\n",b.c_str());
}
return true;
}
}
}
bool linux_fc(const std::string file_A,const std::string file_B,bool display=true,bool debug_mode=false){
return linux_fc(file_A.c_str(),file_B.c_str(),display,debug_mode);
}
void scroll_line(int size,double nowpercent,const color_t finished_color,const color_t unfinished_color){
int finished=size*nowpercent;
for(int i=1;i<=finished;++i){
printc(finished_color,' ');
}
for(int i=finished+1;i<=size;++i){
printc(unfinished_color,' ');
}
}
void __error(string expression){
printc(hdk::__color_info.HIGHLIGHT+hdk::__color_info.FRONTCOLOR.RED,"Error");
cout<<" "<<expression<<endl;
}
}
#endif
#endif
#ifndef TOOL_H
#ifndef EXT_H
#define TOOL_H
#include<bits/stdc++.h>
using namespace std;
namespace hdk{
namespace tool{
template<typename T>
T floor_sqrt(T x,T l=1,T r=-1){
if(r==-1) r=x;
int ans=-1;
while(l<=r){
int mid=(l+r)/2;
if(mid*mid<=x){
l=mid+1;
ans=mid;
}
else{
r=mid-1;
}
}
return ans;
}
template<typename T>
void print(T x,bool first=true){
if(x<0){
putchar('-');
print(-x,false);
return;
}
if(x==0){
if(first) putchar('0');
return;
}
print(x/10,false);
putchar((int)(x%10)+'0');
}
template<typename T>
std::string to_string(T x){
std::string res;bool f=false;
if(x<0){
f=true;
x*=-1;
}
while(x){
res.push_back((int)(x%10)+'0');
x/=10;
}
reverse(res.begin(),res.end());
if(f) res.push_back('-');
if(res.empty()) res.push_back('0');
return res;
}
long long to_number(std::string x){
long long res=0;bool f=false;
for(int i=0;i<=(int)x.length()-1;++i){
if(x[i]=='-'){
f=true;
}
else{
res=res*10+x[i]-'0';
}
}
return res*(f?-1:1);
}
template<typename T>
std::string to_bit(T x){
std::string ans;
while(x){
ans.push_back((x&1)+'0');
x>>=1;
}
std::reverse(ans.begin(),ans.end());
return ans;
}
std::string fixed_size(std::string x,int size,char fillchar='0'){
std::string ans=x;
std::reverse(ans.begin(),ans.end());
while((int)ans.size()!=size) ans.push_back(fillchar);
std::reverse(ans.begin(),ans.end());
return ans;
}
bool copyfile(const char*in,const char *out){
ifstream _fin(in);
if(!_fin){
cout<<"Error: File '"<<in<<"' not found"<<endl;
return 0;
}
ofstream _fout(out);
if(!_fout){
cout<<"Error: File '"<<out<<"' not found"<<endl;
return 0;
}
while(!_fin.eof()){
char _a[1000001];_fin.getline(_a,1000000);
_fout<<_a<<"\n";
}
return 1;
}
bool printfile(const char*in){
ifstream _fin(in);
if(!_fin){
cout<<"Error: File '"<<in<<"' not found"<<endl;
return 0;
}
cout<<"File '"<<in<<"'"<<endl;
while(!_fin.eof()){
char _a[1000001];_fin.getline(_a,1000000);
cout<<_a<<"\n";
}
return 1;
}
bool fc(const char *file_A,const char *file_B,bool display=false,bool debug_mode=false){
std::string a,b;
if(display){printf("fc %s and %s\n",file_A,file_B);}
bool eofA=false,eofB=false;std::ifstream f1,f2;f1.open(file_A);f2.open(file_B);
if(!f1){
cout<<"File '"<<file_A<<"' not found"<<endl;
return true;
}
if(!f2){
cout<<"File '"<<file_B<<"' not found"<<endl;
return true;
}
while(1){
if(f1.eof()){eofA=true;}
else{f1>>a;}
if(f2.eof()){eofB=true;}
if(eofA and eofB){if(display) printf("File is equal.\n"); return false;}
else if(eofA or eofB){if(display) printf("Length is not equal.\n");return true;}
else{f2>>b;}
if(debug_mode){printf("Compare: '%s' '%s'\n",a.c_str(),b.c_str());}
if(a!=b){if(display){
printf("Find Difference: \n");
printf("%s: %s\n",file_A,a.c_str());
printf("%s: %s\n",file_B,b.c_str());}
return true;
}
}
}
bool fc(const std::string file_A,const std::string file_B,bool display=false,bool debug_mode=false){
return fc(file_A.c_str(),file_B.c_str(),display,debug_mode);
}
const long long clocks_per_sec=CLOCKS_PER_SEC;
#define time_s *clocks_per_sec
#define time_ms *clocks_per_sec/1000.0
class _time{
private:
long long last_clocks=-1;
long long start_clocks=0;
public:
inline void sleep(long long clocks){
int st=clock();
while(clock()-st<=clocks);
}
inline long long record_time(){
if(last_clocks==-1){
last_clocks=clock();
return 0;
}
else{
int res=clock()-last_clocks;
last_clocks=-1;
return res;
}
}
inline long long record_time(bool is_start){
if(is_start){
start_clocks=clock();
return start_clocks;
}
else{
return clock()-start_clocks;
}
}
};
string file_name(const string file){
int pos=(int)file.length()-1;
for(int i=(int)file.length()-1;i>=0;--i){
if(file[i]=='.'){
pos=i-1;break;
}
}
string ans;
for(int i=0;i<=pos;++i){
ans.push_back(file[i]);
}
return ans;
}
int system(const string x){
return std::system(x.c_str());
}
class bad_expression{};
inline const pair<int,int> get_numeraic(const string &x,int start_pos){
int res=0,f=1;
for(int i=start_pos;i<=(int)x.length()-1;++i){
if(x[i]=='-'){
if(i==start_pos) f=-1;
else throw bad_expression();
}
else if(x[i]==':'){
return {res*f,i+1};
}
else if(isdigit(x[i])) res=res*10+x[i]-'0';
}
return {res*f,(int)x.length()};
}
inline const string substr(const string &x,int opt0=0,int opt1=0,int opt2=0){
string ret="";
if(opt0<0){
opt0=(int)x.length()+opt0;
if(opt0<0) throw bad_expression();
}
if(opt1<0){
opt1=(int)x.length()+opt1;
if(opt1<0) throw bad_expression();
}
if(opt2==0){
for(int i=opt0;i<((opt1==0)?(int)x.length():opt1);++i){
ret.push_back(x[i]);
}
}
else if(opt2>0){
for(int i=opt0;i<((opt1==0)?(int)x.length():opt1);i+=opt2){
ret.push_back(x[i]);
}
}
else{
for(int i=((opt1==0)?(int)x.length():opt1)-1;i>=opt0;i+=opt2){
ret.push_back(x[i]);
}
}
return ret;
}
inline const string substr(const string &x,string expr=""){
if(expr.empty()) return x;
for(char&i:expr) if(i=='.' or i==';' or i==',' or i=='/' or i==' ') i=':';
for(char i:expr) if(i!='-' and i!=':' and !isdigit(i)) throw bad_expression();
vector<int>opt;
int pos=0;
while(pos<(int)expr.length()){
if(expr[pos]==':'){
opt.push_back(0);
pos++;
}
else{
auto rt=get_numeraic(expr,pos);
opt.push_back(rt.first);
pos=rt.second;
}
}
if(expr.back()==':') opt.push_back(0);
if(opt.size()>3ul) throw bad_expression();
string ret="";
if(opt[0]<0){
opt[0]=(int)x.length()+opt[0];
if(opt[0]<0) throw bad_expression();
}
if(opt[1]<0){
opt[1]=(int)x.length()+opt[1];
if(opt[1]<0) throw bad_expression();
}
if(opt.size()==1ul){
for(int i=opt[0];i<(int)x.length();++i){
ret.push_back(x[i]);
}
return ret;
}
if(opt.size()==2ul){
for(int i=opt[0];i<((opt[1]==0)?(int)x.length():opt[1]);++i){
ret.push_back(x[i]);
}
return ret;
}
if(opt.size()==3ul){
if(opt[2]==0){
for(int i=opt[0];i<((opt[1]==0)?(int)x.length():opt[1]);++i){
ret.push_back(x[i]);
}
}
else if(opt[2]>0){
for(int i=opt[0];i<((opt[1]==0)?(int)x.length():opt[1]);i+=opt[2]){
ret.push_back(x[i]);
}
}
else{
for(int i=((opt[1]==0)?(int)x.length():opt[1])-1;i>=opt[0];i+=opt[2]){
ret.push_back(x[i]);
}
}
return ret;
}
throw bad_expression();
return "";
}
inline const string substr(const string &x,int start_pos,int length){
return x.substr(start_pos,length);
}
class str_t{
private:string str;
public:str_t(const string &x){str=x;}
inline const string operator[](string x){return substr(str,x);}
inline const string operator[](const vector<int>&opt){return substr(str,opt[1],opt[2],opt[3]);}
inline const string operator()(int opt1,int opt2,int opt3){return substr(str,opt1,opt2,opt3);}
inline const string operator()(string x){return substr(str,x);}
inline const string operator()(const vector<int>&opt){return substr(str,opt[1],opt[2],opt[3]);}
};
const string lowercase(const string&x){
string res;
for(char i:x){
if(i>='A' and i<='Z') res.push_back(i-'A'+'a');
else res.push_back(i);
}
return res;
}
const string uppercase(const string&x){
string res;
for(char i:x){
if(i>='a' and i<='z') res.push_back(i-'a'+'A');
else res.push_back(i);
}
return res;
}
template<typename return_value_type=unsigned long long,return_value_type numer=233,return_value_type mod=0>
class string_hash{
private:return_value_type get_hash(const string &x){
return_value_type res=0;
for(char i:x){
if(mod==0) res=res*numer+i;
else res=(1ll*res*numer%mod+i)%mod;
}
return res;
}
public:return_value_type operator()(const string &x){return get_hash(x);}
return_value_type operator[](const string &x){return get_hash(x);}
};
string_hash<> strhash;
}
}
#endif
#endif
char format_[10001];
string format;
int main(int argc,char**argv){
string fmt;bool isfmt=false;
if(argc>=3){fmt=argv[2];
if(fmt[0]=='-' and fmt[1]=='f' and fmt.size()==2ull){
isfmt=true;fmt.clear();
for(int i=3;i<argc;++i){
fmt.append(argv[i]);fmt.push_back(' ');}}
else fmt=fmt.substr(1);}
else fmt="default";
if(isfmt==false and (argc==1 or argc>3)){
hdk::__error("run: Wrong Input Format: Expect './cpp [CompileFile] (<-[FormatFile] | -d:default>/<-f [Compile Command]>)'");exit(1);}
if(!isfmt and fmt.size()==1ull and fmt[0]=='d') fmt="default";
if(!isfmt){
if(fmt=="14") format="-std=c++14";
else if(fmt=="14O2") format="-std=c++14 -O2";
else if(fmt=="14O2fs") format="-std=c++14 -O2 -Wall -fsanitize=address,undefined,leak -fno-omit-frame-pointer";
else if(fmt=="14fs") format="-std=c++14 -fsanitize=address,undefined,leak -fno-omit-frame-pointer";
else if(fmt=="17") format="-std=c++17";
else if(fmt=="17O2") format="-std=c++17 -O2";
else if(fmt=="17O2fs") format="-std=c++17 -O2 -Wall -fsanitize=address,undefined,leak -fno-omit-frame-pointer";
else if(fmt=="17fs") format="-std=c++17 -fsanitize=address,undefined,leak -fno-omit-frame-pointer";
else{
ifstream in;in.open(fmt);
if(!in){in.close();ofstream ot;ot.open(fmt);ot.close();in.open(fmt);}
in.getline(format_,10000);
format=format_;
in.close();}}
else format=fmt;
if(system(((string)"g++ "+(string)argv[1]+" "+format+" -o "+hdk::tool::file_name(argv[1])).c_str())){
hdk::__error("run: Compile Failed");exit(1);}
printc(hdk::__color_info.HIGHLIGHT+hdk::__color_info.FRONTCOLOR.CRAY,"run: Compile Finished");cout<<endl;
if(system(((string)"./"+hdk::tool::file_name(argv[1])).c_str())){
hdk::__error("run: Unexpected Exit Status");exit(1);}}
用处:编译并运行代码
这玩意在上个版本(Fileheader2.1.0)用起来还不如直接 g++
因此在这个版本加强了
如果你编译出来的可执行文件是 run
那么语法是这样的(在终端内输入)
./run [File]
直接编译 [File] 文件./run [File] -d
效果同上./run [File] -f []
后面接编译参数,如./run [File] -f -std=c++14 -O2 -Wall
./run [File] -[File2]
[File2] 内是提前写好的编译参数,程序将会读取文件内内容并作为编译参数编译./run [File] -[]
直接使用配置好的编译指令
编译指令有以下几个
-14
=-std=c++14
-14O2
=-std=c++14 -O2
-14fs
=-std=c++14 -Wall --fsanitize=address,undefined,leak -fno-omit-frame-pointer
-14O2fs
=-std=c++14 -O2 -Wall -fsanitize=address,undefined,leak -fno-omit-frame-pointer
同理也有 -17
-17O2
-17fs
-17O2fs
程序在编译完成后即刻开始运行(编译完成后有提示)
程序可能出现的报错有以下几种情况
Wrong Input Format
输入参数不符合格式
Compile Failed
程序编译失败
Unexpected Exit Status
返回值异常(非 0,即 RE)
在随后的更新中会加入在默认目录中寻找,目录优先级等功能