#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
vector split_str(string s);
class fileOperate
{
public:
int md(string name){
mode_t mode = 0775;
if(mkdir(name.c_str(), mode)==0)
{
cout<
return 0;
}
else
{
cout<
return -1;
}
}
int del(string fileName){
if(remove(fileName.c_str()) == 0 )
{
cout<
return 0;
}
else
{
cout<
return -1;
}
}
int cd(string path){
return chdir(path.c_str())==0? 0 : -1;
}
void ls(){
//列出文件夹内容
DIR * curDir;
struct dirent *ent;
struct stat entStat;
if((curDir=opendir("."))==NULL)
{
cout<
}
else
{
while((ent=readdir(curDir))!=NULL)//遍历该文件目录下的内容
{
if(stat(ent->d_name, &entStat) == 0)
{
if(entStat.st_mode & S_IFDIR)//判断是否是文件夹
{
cout<
cout<
cout<
cout<
cout<d_name<
}
else if(entStat.st_mode & S_IFREG)
{
cout<
cout<
cout<
cout<
cout<d_name<
}
}
}
closedir(curDir);
}
}
int cp(string oldfile,string oldfilePath,string newfilePath){//深度拷贝
struct dirent *ent;
struct stat entStat,entStat2;
cd(oldfilePath);
if( stat(oldfile.c_str(),&entStat)==0){
if(entStat.st_mode & S_IFDIR){
cd(newfilePath);
if(md(oldfile)==0){
DIR * curDir;
cd(oldfilePath);
cd(oldfile);
curDir=opendir(".");
while((ent=readdir(curDir))!=NULL)
{
if(stat(ent->d_name, &entStat2) == 0)
{
if(ent->d_name[0]!='.'){//跳过本身和复文件
string newpath=newfilePath+'/'+oldfile;
string oldpath=oldfilePath+'/'+oldfile;
cp(ent->d_name,oldpath,newpath);
cd(oldfilePath);
cd(oldfile);
}
}
}
closedir(curDir);
cd("..");
}else
return -1;
}else{
cd(oldfilePath);
ifstream file(oldfile.c_str(),ios::binary);
if(!file){
cout<
return -1;
}
cd(newfilePath);
ofstream file2(oldfile.c_str(),ios::binary);
if(!file2){
cout<
return -1;
}
file2<
file.close();
file2.close();
return 0;
}
}
return 0;
}
};
vector split_str(string s)//处理输入指令
{
vector tokens;
istringstream iss(s);//以空格为分割符号,把该行分割开
do
{
string sub;
iss >> sub;
tokens.push_back(sub);
} while (iss);
tokens.erase(tokens.end()-1); // 删除最后的换行回车符
return tokens;
}
int main()
{
fileOperate operate;
cout<
cout<
cout<
cout<
cout<
cout<
string commandLine;
vector commandArgs;
getline(cin,commandLine);
commandArgs=split_str(commandLine);
while(commandArgs.size()>0 && "q"!=commandArgs[0])//按回车或者输入q都能退出程序
{
if(commandArgs[0]=="cd"){
if(commandArgs.size()<2)
cout<
else{
if(operate.cd(commandArgs[1])==0){
cout<
}else
cout<
}
}else if(commandArgs[0]=="md"){
if(commandArgs.size()<2)
cout<
else
operate.md(commandArgs[1]);
}else if(commandArgs[0]=="ls"){
operate.ls();
}else if(commandArgs[0]=="del"){
if(commandArgs.size()<2){
cout<
}else{
cout<
if(operate.del(commandArgs[1])==0){
cout<
}else{
cout<
}
}
}else if(commandArgs[0]=="cp"){
if(commandArgs.size()!=4){
cout<
}else{
if(operate.cp(commandArgs[1],commandArgs[2],commandArgs[3])==0)
cout<
else
cout<
}
}
cout<
getline(cin,commandLine);
commandArgs=split_str(commandLine);
}
return 0;
}