📄 extract.cpp
字号:
#include<cassert>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<fstream>
#include<iostream>
#include<string>
using namespace std;
#if defined(_GNUC_)||defined(_MWERKS_)
#include<sys/stat.h>
#elif defined(_BORLANDC_)||defined(_MSC_VER)\
||defined(_DMC_)
#include<direct.h>
#else
#error Compiler not support
#endif
bool exists(string fname)
{
size_t len=fname.length();
if(fname[len-1]!='/'&&fname[len-1]!='\\')
fname.append("/");
fname.append("000.tmp");
ofstream outf(fname.c_str());
bool existflag=outf;
if(outf)
{
outf.close();
remove(fname.c_str());
}
return existflag;
}
int main(int argc,char *argv[])
{
if(argc==1){
cerr<<"usage:extractcode file [dir]"<<endl;
exit(EXIT_FAILURE);
}
ifstream inf(argv[1]);
if(!inf){
cerr<<"error opening file:"<<argv[1]<<endl;
exit(EXIT_FAILURE);
}
string root("./");
if(argc==3){
root=argv[2];
if(!exists(root)){
cerr<<"no such directory:"<<root<<endl;
exit(EXIT_FAILURE);
}
size_t rootlen=root.length();
if(root[rootlen-1]!='/'&&root[rootlen-1]!='\\')
root.append("/");
}
string line;
bool incode=false;
bool printdelims=true;
ofstream outf;
while(getline(inf,line)){
size_t finddelim=line.find("//""/:~");
if(finddelim!=string::npos){
if(!incode){
cerr<<"no such directory:"<<root<<endl;
exit(EXIT_FAILURE);
}
assert(outf);
if(printdelims)
outf<<line<<endl;
outf.close();
incode=false;
printdelims=true;
}else{
finddelim=line.find("//"":");
if(finddelim==0){
if(line[3]=='!'){
printdelims=false;
++finddelim;
}
size_t startofsubdir=line.find_first_not_of("\t",finddelim+3);
finddelim=line.find(':',startofsubdir);
if(finddelim==string::npos){
cerr<<"no such directory:"<<root<<endl;
exit(EXIT_FAILURE);
}
string subdir;
if(finddelim>startofsubdir)
subdir=line.substr(startofsubdir,finddelim-startofsubdir);
size_t startoffile=finddelim+1;
size_t endoffile=line.find_first_of("\t",startoffile);
if(endoffile==startoffile){
cerr<<"missing file imformation!"<<endl;
exit(EXIT_FAILURE);
}
string fullpath(root);
if(subdir.length()>0)
fullpath.append(subdir).append("/");
assert(fullpath[fullpath.length()-1]=='/');
if(!exists(fullpath))
#if defined(_GNUC_)||defined(_MWERKS_)
mkdir(fullpath.c_str (),0);
#else
mkdir(fullpath.c_str());
#endif
fullpath.append(line.substr(startoffile,endoffile-startoffile));
outf.open(fullpath.c_str());
if(!outf){
cerr<<"error opening"<<fullpath<<endl;
exit(EXIT_FAILURE);
}
incode=true;
cout<<"processing"<<fullpath<<endl;
if(printdelims)
outf<<line<<endl;
else if(incode){
assert(outf);
outf<<line<<endl;
}
}
}
exit(EXIT_SUCCESS);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -