C++虚拟二级文件管理系统

#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <ctime>
using namespace std;
const int MAX_NAME_SIZE=256;
const int MAX_PATH_SIZE=1024;
const int MAX_LINE_SIZE=256;
const int MAX_USERNAME=18;
const int MAX_TEXT_LINE_SIZE=1024;
const int MAX_TIME_SIZE=32;
struct Fold;//声明结构体
struct File;
struct user;
fstream control_file_users;//控制文件,账户
fstream data_file_folders;//数据文件,文件夹
fstream data_file_files;///数据文件,文件
char user_access_file[MAX_NAME_SIZE];//用户存储磁盘文件名
char user_access_fold[MAX_NAME_SIZE];//用户存储磁盘文件夹
Fold *currentFold;//当前目录
Fold *rootFlod;//根目录
char currentPath[MAX_PATH_SIZE];//当前路径
char currentUser[MAX_USERNAME];//当前用户名
char currentUserPassWord[MAX_USERNAME];//用户密码
char curTime[MAX_TIME_SIZE];
void init();//初始化
Fold *mkdir(Fold *,char *);//新建文件夹
void rd(Fold *,char *);//删除文件夹
void cd(char *);//打开文件夹
void showList(char *);//显示列表
void createFile(Fold *,char *);//新建文件
void readFile(char *);//读文件内容
void saveToDisk();//退出时,存盘
void readFromDisk();//初始化,读盘
void saveFile(Fold *);//保存所有文件到磁盘
void saveFold(Fold *);//保存所有目录信息磁盘
void formatString(char *);//去掉字符串两端的空格
Fold *findFoldByPoint(Fold *,char *);//查找当前文件夹下的文件夹
Fold *findFoldByUrl(char *);//通过路径,往下找到指定文件夹
File *findFileByPoint(Fold *,char *);//查找当前文件夹下的文件
Fold *createNewFold(Fold *);//在文件夹下新建文件夹
File *createNewFile(Fold *);//在当前文件夹下新建文件
char *stringToCharArray(string);//把字符串转换成字符数组
void returnParentFold();//返回上级目录
bool isNameLegal(char *);//判断文件名是否合法
void delFile(Fold *,File *);//通过指针删除文件
void delFold(Fold *);//通过指针删除文件夹
void updataFoldPath(Fold *);//更新文件下的所有文件夹的路径属性
void updataFilePath(Fold *);//更新文件下的所有文件的路径属性
string strSplit(char * source,char c);//依据[参数2],对[参数1]进行分割
void getTime(char *);//获取当前时间;
void showUserInfo();//显示当前用户@/路径
void showHelpInfo();//帮助
void showAboutInfo();//一些说明

user *userHead;
void createNewUser(char *name,char *password);//用户

user *findUser(char *name,char *password);
void delUser(user *);
void saveUser();

void alert();
struct Fold {
 char name[MAX_NAME_SIZE];
 char path[MAX_PATH_SIZE];
 char time[MAX_TIME_SIZE];
 Fold *father;//指向父文件夹
 Fold *fold_head;//指向子目录的目录链表的头节点
 File *file_head;//指向子目录的文件链表的头节点
 Fold *next;//指向下一个
 Fold();
};
struct File {
 char name[MAX_NAME_SIZE];//文件名
 char path[MAX_PATH_SIZE];//路径
 char text[MAX_TEXT_LINE_SIZE];//文件内容
 char time[MAX_TIME_SIZE];
 File *next;//指向下一个
 Fold *father;//指向父目录
 File();
};
struct user {
 char name[MAX_USERNAME];
 char password[MAX_USERNAME];
 user *next;
 user()
 {
  strcpy(name,"");
  strcpy(password,"");
  next=NULL;
 }
};
void main()
{
 alert();
 int i=0,j=0;
 getTime(curTime);
 init();
 char temp_array[MAX_LINE_SIZE];
 string comd;//读取一个流
 while (true)
 {
  cin>>comd;
  if (comd=="mkdir")
  {// mkdir /data/ddd
   cin.getline(temp_array,MAX_LINE_SIZE,'\n');
   formatString(temp_array);
   Fold *mkdir_fo=mkdir(currentFold,temp_array);
   if (mkdir_fo!=NULL)
   {
    cout<<"文件夹创建成功"<<endl;
   }
   
  }
  else if (comd=="rd")
  {// rd data
   cin.getline(temp_array,MAX_LINE_SIZE,'\n');
   formatString(temp_array);
   rd(currentFold,temp_array);
  }
  else if (comd=="dir")
  {// dir 或者 dir /data/ddd
   cin.getline(temp_array,MAX_LINE_SIZE,'\n');
   formatString(temp_array);
   showList(temp_array);
  }
  else if (comd=="cd")
  {// cd data 或者 cd /data
   cin.getline(temp_array,MAX_LINE_SIZE,'\n');
   formatString(temp_array);
   if (strcmp(temp_array,"..")==0)
   {
    returnParentFold();
   }
   else
   {
    cd(temp_array);
   }
  }
  else if (comd=="create")
  {//create filename
   cin.getline(temp_array,MAX_LINE_SIZE,'\n');
   formatString(temp_array);
   if (strlen(temp_array)>0)
   {
    createFile(currentFold,temp_array);
   }
   else
   {
    cout<<"文件名不能为空!"<<endl;
   }
   
  }
  else if (comd=="read")
  {//read filename
   cin.getline(temp_array,MAX_LINE_SIZE,'\n');
   formatString(temp_array);
   readFile(temp_array);
  }
  else if (comd=="del" || comd=="delete")
  {
   cin.getline(temp_array,MAX_LINE_SIZE,'\n');
   formatString(temp_array);
   File *fi=findFileByPoint(currentFold,temp_array);
   if (fi!=NULL)
   {//找到了
    delFile(currentFold,fi);
   }
   else
   {
    cout<<"未找到文件"<<endl;
   }
  }
  else if (comd=="ren" || comd=="rename")
  {
   cin.getline(temp_array,MAX_LINE_SIZE,'\n');
   formatString(temp_array);
   File *renamefi=findFileByPoint(currentFold,temp_array);
   if (renamefi!=NULL)
   {//找到了文件
    cout<<"请输入新文件名:";
    cin.getline(temp_array,MAX_LINE_SIZE,'\n');
    formatString(temp_array);
    strcpy(renamefi->name,temp_array);
    cout<<"文件重命名完成"<<endl;
   }
   else
   {
    cout<<"未找到指定文件,操作撤销"<<endl;
   }
  }
  else if (comd=="renfold" || comd=="renamefold")
  {
   cin.getline(temp_array,MAX_LINE_SIZE,'\n');
   formatString(temp_array);
   Fold *renamefold=findFoldByUrl(temp_array);
   if (renamefold==NULL)
   {//路径或者文件名无效
    cout<<"未找到指定文件夹,操作撤销"<<endl;
   }
   else
   {
    cout<<"请输入新文件夹名:";
    cin.getline(temp_array,MAX_LINE_SIZE,'\n');
    formatString(temp_array);
    strcpy(renamefold->name,temp_array);
    strcpy(renamefold->path,renamefold->father->path);//把父路径拿来
    if (strcmp(renamefold->father->path,"/")!=0)
    {//若父路径不是根目录
     strcat(renamefold->path,"/");
    }
    strcat(renamefold->path,renamefold->name);
    updataFoldPath(renamefold);//更新文件下的所有[文件夹]的路径属性
    updataFilePath(renamefold);//更新文件下的所有[文件]的路径属性
   }
  }
  else if (comd=="cls")
  {//清屏
   system("cls");
  }
  else if (comd=="save")
  {//保存
   saveToDisk();
  }
  else if (comd=="exit" || comd=="quit")
  {//退出
   break;
  }
  else if (comd=="help")
  {
   showHelpInfo();
  }
  else if (comd=="info" ||comd=="about" ||comd=="version")
  {
   showAboutInfo();
  }
  else if (comd=="alter")
  {
   char input_user[MAX_USERNAME];
   char input_password[MAX_USERNAME];
   string action;
   cout<<"输入操作:";
   cin>>action;
   if (action=="delete" || action=="del")
   {
    cout<<"输入用户名:";
    cin>>input_user;
    cout<<"输入密码:";
    cin>>input_password;
    user *findResult=findUser(input_user,input_password);
    if (findResult!=NULL)
    {
     delUser(findResult);
     cout<<"用户["<<input_user<<"]已删除"<<endl;
    }
    else
    {
     cout<<"未找到指定用户\n";
    }
    saveUser();
   }
   if (action=="new")
   {
    cout<<"输入用户名:";
    cin>>input_user;
    cout<<"输入密码:";
    cin>>input_password;
    user *r=findUser(input_user,input_password);
    if (r!=NULL)
    {
     cout<<"用户已存在"<<endl;
    }
    else
    {
     createNewUser(input_user,input_password);
     cout<<"用户创建成功\n";
    }
    saveUser();
   }
   if (action=="show")
   {
    cout<<"所有用户:"<<endl;
    user*p=userHead->next;
    while(p!=NULL)
    {
     cout<<p->name<<endl;
     p=p->next;
    }
   }
  }
  else
  {
   cout<<"无法识别的命令!"<<endl;
  }
  showUserInfo();//提示信息
 }
 system("pause");
}
Fold::Fold()
{//构造函数
 father=NULL;
 fold_head=NULL;
 file_head=NULL;
 next=NULL;
 strcpy(name,"");
 strcpy(path,"");
 getTime(curTime);
 strcpy(time,curTime);
}
File::File()
{//构造函数
 strcpy(text,"");
 next=NULL;
 father=NULL;
 strcpy(name,"");
 strcpy(path,"");
 getTime(curTime);
 strcpy(time,curTime);
}
Fold *mkdir(Fold *currentFold,char *name)
{//新建文件夹cout<<"name="<<name<<endl;
 if (strcmp(name,"")==0)
 {
  cout<<"文件夹名不能为空!"<<endl;
  return NULL;
 }
 if (isNameLegal(name)==false)
 {
  cout<<"文件夹名不合法!"<<endl;
  return NULL;
 }
 if (findFoldByPoint(currentFold,name)!=NULL)
 {
  cout<<"文件夹已存在!"<<endl;
  return NULL;
 }
 Fold *fo=createNewFold(currentFold);
 strcpy(fo->name,name);
 //下面,用于产生路径
 strcpy(fo->path,currentFold->path);
 if (strcmp(currentFold->path,"/")!=0)
 {//若当前不是根目录
  strcat(fo->path,"/");
 }
 strcat(fo->path,name);
 return fo;
}
void rd(Fold *currentFold,char *url)
{//删除文件夹
 if (!isNameLegal(url))
 {
  cout<<"参数不合法"<<endl;
  return ;
 }
 Fold *fo=findFoldByUrl(url);
 if (fo==NULL)
 {//没找到
  cout<<"未找到文件夹! 操作终止"<<endl;
  return;
 }
 if (fo->file_head->next!=NULL || fo->fold_head->next!=NULL)
 {//文件夹不空
  string judge;
  cout<<"文件夹不为空!\n确定要删除该文件夹下的所有文件和文件夹吗?[Y/N]"<<endl; 
  cin>>judge;
  if (judge!="Y" && judge!="y")
  {
   cout<<"操作终止"<<endl;
   return;
  }
 }
 Fold *pre=currentFold->fold_head;
 Fold *p=pre->next;
 while (p!=NULL)
 {
  if (p==fo)
  {//调整前后节点
   pre->next=p->next;
   delFold(fo);
   break;
  }
  pre=pre->next;
  p=p->next;
 }
}
void cd(char *url)
{//打开文件夹,url可以是绝对路径,或者文件夹名
 Fold *fo=findFoldByUrl(url);
 if (fo==NULL)
 {
  cout<<"无法打开指定目录"<<endl;
  return;
 }
 else
 {
  currentFold=fo;
 }
}
void createFile(Fold *currentFold,char *name)
{//创建文件,并输入内容
 if (isNameLegal(name)==false)
 {
  cout<<"文件名不合法!"<<endl;
  return;
 }
 File *f=NULL;
 char input[MAX_TEXT_LINE_SIZE];
 f=findFileByPoint(currentFold,name);
 if (f!=NULL)
 {//已存在文件
  cout<<"文件已存在,是否覆盖[Y/N]?"<<endl;
  cin.getline(input,MAX_TEXT_LINE_SIZE,'\n');
  if (strcmp(input,"Y")==0 || strcmp(input,"y")==0)
  {
   strcpy(f->text,"");//清空
   cout<<"请输入文件新内容"<<endl;
   cin.getline(input,MAX_TEXT_LINE_SIZE,'\n');
   strcpy(f->text,input);
  }
  else
  {
   cout<<"操作取消!"<<endl;
   return;
  }
 }
 else
 {//新建文件
  f=createNewFile(currentFold);
  strcpy(f->name,name);
  strcpy(f->path,currentFold->path);
  cout<<"请输入文件内容"<<endl;
  cin.getline(input,MAX_TEXT_LINE_SIZE,'\n');
  strcpy(f->text,input);
  f->father=currentFold;
 }
 cout<<"文件已保存"<<endl;
}
void delFile(Fold *fo,File *f)
{//通过指针删除文件
 File * pre=fo->file_head;
 File * p=pre->next;
 while (p!=NULL)
 {
  if (p==f)
  {
   pre->next=p->next;
   delete f;
   cout<<"成功删除文件"<<endl;
   return ;
  }
  pre=p;
  p=p->next;
 }
 cout<<"删除过程中出现错误!"<<endl;
}
void delFold(Fold *fo)//通过指针删除文件夹
{//cout<<"递归"<<endl;
 if (fo->fold_head->next!=NULL)
 {//有子文件夹//cout<<"有子文件夹"<<endl;
  Fold *fopre=fo->fold_head;
  Fold *fop=fopre->next;
  while (fop!=NULL)
  {//cout<<"#2"<<endl;
   fopre->next=fop->next;
   delFold(fop);
   fop=fopre->next;
  }
 }
 if (fo->file_head->next!=NULL)
 {//有子文件cout<<"有子文件"<<endl;
  File *fipre=fo->file_head;
  File *fip=fo->file_head->next;
  while (fip!=NULL)
  {
   fipre->next=fip->next;
   delete fip;
   fip=fipre->next;
  }
 }
 delete fo->fold_head;
 delete fo->file_head;
 delete fo;//删除自己
}
void returnParentFold()
{//返回上级目录
 currentFold=currentFold->father;
}
void showList(char *url)//显示列表
{//[参数可选]
 Fold *dirfold=NULL;//需要显示的目录
 if (strcmp(url,"")==0)
 {//若无参数
  dirfold=currentFold;
 }
 else
 {//绝对路径
  dirfold=findFoldByUrl(url);
  if (dirfold==NULL)
  {//路径无效
   cout<<"路径无效!"<<endl;
   return ;
  }
 }
 Fold * fo=dirfold->fold_head->next;
 while (fo!=NULL)
 {//输出文件夹
  cout<<"<DIR>\t"<<fo->name<<"\t["<<fo->time<<"]"<<endl;
  fo=fo->next;
 }
 File *fi=dirfold->file_head->next;
 while (fi!=NULL)
 {//输出文件
  cout<<"<FILE>\t"<<fi->name<<"\t["<<fi->time<<"]"<<endl;
  fi=fi->next;
 }
}
void readFile(char *name)
{//读取当前目录下的文件内容
 File *f=NULL;
 f=findFileByPoint(currentFold,name);
 if (f==NULL)
 {
  cout<<"当前文件夹下,未找到指定文件!"<<endl;
 }
 else
 {
  cout<<"文件内容如下:"<<endl;
  cout<<f->text<<endl;
 }
}
void updataFoldPath(Fold *fo)
{//更新[参数指定文件夹]下的所有文件夹的path属性
 if (fo==NULL)
 {//递归结束的条件之一
  return ;
 }
 strcpy(fo->path,fo->father->path);//把父路径拿来
 if (strcmp(fo->father->path,"/")!=0)
 {//若父路径不是根目录
  strcat(fo->path,"/");
 }
 strcat(fo->path,fo->name);
 updataFoldPath(fo->next);
 updataFoldPath(fo->fold_head->next);
}
void updataFilePath(Fold *p)
{//更新[参数指定文件夹]下的path属性,因为上级目录可能更改了路径
 if (p->file_head->next!=NULL)
 {//有子文件cout<<"有子文件"<<endl;
  File *pf=p->file_head->next;
  while (pf!=NULL)
  {//更新文件的path信息
   strcpy(pf->path,pf->father->path);
   pf=pf->next;
  }
 }
 if (p->fold_head->next!=NULL)
 {//有子文件夹cout<<"有子文件夹"<<endl;
  p=p->fold_head->next;
  while (p!=NULL)
  {//子文件夹的兄弟节点
   updataFilePath(p);//递归
   p=p->next;
  }
 }
}
void saveToDisk()
{//存盘[trunc]
 data_file_folders.open(user_access_fold,ios::binary|ios::out|ios::trunc);
 data_file_folders.seekg(0,ios::beg);
 saveFold(rootFlod);
 data_file_folders.close();
 data_file_files.open(user_access_file,ios::binary|ios::out|ios::trunc);
 data_file_files.seekg(0,ios::beg);
 saveFile(rootFlod);
 data_file_files.close();
 cout<<"已保存"<<endl;
}
void readFromDisk()
{//从磁盘全部读取
 control_file_users.open("control_file_users.dat",ios::binary|ios::in);
 char tempUser[MAX_USERNAME],tempUserPassWord[MAX_USERNAME];
 bool flag=false;

 control_file_users.seekg(ios::beg);
 while (control_file_users.peek()!=EOF)
 {
  control_file_users.read(currentUser,MAX_USERNAME);
  control_file_users.read(currentUserPassWord,MAX_USERNAME);
  createNewUser(currentUser,currentUserPassWord);
 }

 while (!flag)
 {
  cout<<"请登录,输入[用户名] [密码]"<<endl;
  cin>>tempUser>>tempUserPassWord;

  user *findUserResult=findUser(tempUser,tempUserPassWord);
  if (findUserResult!=NULL)
   //(
   //strcmp(currentUser,tempUser)==0 && strcmp(currentUserPassWord,tempUserPassWord)==0
   //)
  {
   strcpy(currentUser,tempUser);
   strcpy(currentUserPassWord,tempUserPassWord);
   flag=true;
   cout<<"登录成功"<<endl;
   strcpy(user_access_file,currentUser);
   strcat(user_access_file,"_data_file_files.dat");
   strcpy(user_access_fold,currentUser);
   strcat(user_access_fold,"_data_file_folders.dat");
   break;
  }

  control_file_users.clear();
 }
 control_file_users.close();
 //接下来,读取文件夹路径并创建文件夹
 ifstream infold(user_access_fold,ios::binary|ios::in);
 if (infold)
 {
  string s;
  //infold.seekg(MAX_PATH_SIZE,ios::beg);//第一个路径是/,不读
  char inpath[MAX_PATH_SIZE];
  char temp_time[MAX_TIME_SIZE];
  infold.seekg(0,ios::beg);
  Fold *fo;
  while(infold.peek()!=EOF)
  {
   infold.read(inpath,MAX_PATH_SIZE);
   infold.read(temp_time,MAX_TIME_SIZE);//读取时间
   //接下来,根据读取的url,创建文件夹,以及其链表
   s=strSplit(inpath,'/');//把第一个'/'剔除
   currentFold=rootFlod;//因为是绝对路径,借用全局变量
   while (strlen(inpath)>0)
   {//该级目录
    s=strSplit(inpath,'/');//取一个文件名
    fo=findFoldByPoint (currentFold,stringToCharArray(s));
    if(fo!=NULL)
    {//文件夹已存在
     currentFold=fo;
    }
    else
    {
     currentFold=mkdir(currentFold,stringToCharArray(s));
     strcpy(currentFold->time,temp_time);
    }
   }
  }
  currentFold=rootFlod;
 }
 infold.close();
 //接下来,读取文件属性,并创建文件;
 ifstream infile(user_access_file,ios::binary|ios::in);
 if (infile)
 {//输入文件存在
  char temp_path[MAX_PATH_SIZE];
  char temp_name[MAX_NAME_SIZE];
  char temp_time[MAX_TIME_SIZE];
  char temp_text[MAX_TEXT_LINE_SIZE];
  infile.seekg(0,ios::beg);
  Fold *fo=NULL;
  while (infile.peek()!=EOF)
  {//开始读了
   infile.read(temp_path,MAX_PATH_SIZE);
   infile.read(temp_name,MAX_NAME_SIZE);
   infile.read(temp_time,MAX_TIME_SIZE);
   infile.read(temp_text,MAX_TEXT_LINE_SIZE);
   fo=findFoldByUrl(temp_path);
   File *f=createNewFile(fo);//文件建好了
   strcpy(f->name,temp_name);
   strcpy(f->path,fo->path);
   strcpy(f->time,temp_time);
   strcpy(f->text,temp_text);
   //cout<<"初始化文件:"<<f->name<<endl;
  }
 }
 infile.close();
 showUserInfo();
}
void saveFile(Fold *p)
{//保存所有文件信息->到磁盘cout<<"path="<<p->path <<endl;
 if (p->file_head->next!=NULL)
 {//有子文件cout<<"有子文件"<<endl;
  File *pf=p->file_head->next;
  int i=0;
  while (pf!=NULL)
  {//写入文件的各种信息cout<<"写入文件:"<<pf->name<<endl;
   data_file_files.write(pf->path,MAX_PATH_SIZE);//文件所在路径
   data_file_files.write(pf->name,MAX_NAME_SIZE);//文件名
   data_file_files.write(pf->time,MAX_TIME_SIZE);//时间戳
   data_file_files.write(pf->text,MAX_TEXT_LINE_SIZE);//文件内容
   pf=pf->next;
  }
 }
 if (p->fold_head->next!=NULL)
 {//有子文件夹cout<<"有子文件夹"<<endl;
  p=p->fold_head->next;
  while (p!=NULL)
  {//子文件夹的兄弟节点
   saveFile(p);//递归
   p=p->next;
  }
 }
}
void saveFold(Fold *p)
{//保存所有目录信息->到磁盘
 if (p==NULL)
 {
  return ;
 }
 data_file_folders.write(p->path,MAX_PATH_SIZE);//先写当前路径
 data_file_folders.write(p->time,MAX_TIME_SIZE);//时间戳
 //cout<<"写当前路径: "<<p->path<<endl;
 saveFold(p->next);//递归
 saveFold(p->fold_head->next);
}
void showUserInfo()
{//格式 [userName@/path]>
 cout<<endl;
 if (strcmp(currentUser,"")!=0 && currentFold!=NULL)
 {
  cout<<"["<<currentUser<<"@"<<currentFold->path<<">]";
 }
}
Fold *findFoldByPoint(Fold *pp,char *name)
{//查找[参数指定文件夹]下的文件夹
 if (pp->fold_head->next==NULL)
 {//当前为空文件夹
  return NULL;
 }
 else
 {
  Fold *p=pp->fold_head->next;
  while (p!=NULL)
  {
   if (strcmp(p->name,name)==0)
   {//找到了
    return p;
   }
   p=p->next;
  }
  return NULL;
 }
}
Fold *findFoldByUrl(char *url)
{//依据[绝对||相对]路径找到指定文件夹
 if (strcmp(url,"")==0)
 {//若参数为空
  return NULL;
 }
 Fold *p=NULL;
 string s;
 if (url[0]=='/')
 {// cd /path1/path2/path3
  p=rootFlod;
  s=strSplit(url,'/');//把第一个'/'剔除
  for (;p!=NULL && strlen(url)>0;)
  {//该级目录已找到 && 文件名不空
   s=strSplit(url,'/');//取一个文件名
   p=findFoldByPoint(p,stringToCharArray(s));
  }
  return p;
 }
 else
 {// cd path2/path3
  p=currentFold;
  for (;p!=NULL && strlen(url)>0;)
  {//cout<<"s="<<s<<endl;
   s=strSplit(url,'/');//取一个文件名
   p=findFoldByPoint(p,stringToCharArray(s));
  }
  return p;
 }
}

File *findFileByPoint(Fold * pp,char *name)
{//查找[指定参数文件夹]下的文件
 if (pp->file_head->next==NULL)
 {
  return NULL;
 }
 else
 {
  File *p=pp->file_head->next;
  while (p!=NULL)
  {
   if (strcmp(p->name,name)==0)
   {//找到了
    return p;
   }
   p=p->next;
  }
  return NULL;
 }
}
Fold * createNewFold(Fold *fatherFold)
{//在文件夹下新建文件夹
 Fold *fo=new Fold();//new 一个文件夹
 fo->father=fatherFold;
 fo->next=fatherFold->fold_head->next;
 fatherFold->fold_head->next=fo;//插入
 Fold *foo=new Fold();//new 一个文件夹头结点
 foo->father=fo;	//头结点不带数据
 fo->fold_head=foo;
 File *fii=new File();//new 一个文件头结点
 fii->father=fo;
 fo->file_head=fii;
 return fo;
}
File * createNewFile(Fold *fatherFold)
{//在参数指定文件夹下新建文件
 File *fi=new File();
 fi->father=fatherFold;
 strcpy(fi->path,fatherFold->path);
 fi->next=fatherFold->file_head->next;
 fatherFold->file_head->next=fi;//插入
 return fi;
}

string strSplit(char * source,char c)
{//字符数组分割,每次返回一个String,被分割字符串也变化了
 int i=0,j=0;
 string result;
 for (i=0;source[i]!='\0';i++)
 {
  if (source[i]==c)
  {//cout<<"标记 1"<<endl;
   break;
  }
  else
  {//cout<<"标记 2"<<" source[i]="<<source[i]<<" result="<<result<<endl;
   result=result+source[i];
  }
 }//cout<<"标记3 "<<" i="<<i<<" j="<<j<<endl;
 if (i==0)
 {//第一个就是目标字符
  for (j=1;source[j]!='\0';j++)
  {//前移一位
   source[j-1]=source[j];
  }
  source[j-1]='\0';
  result=result+c;
  return result;
 }
 if (i==strlen(source))
 {//剩余都是
  source[0]='\0';//置空
  return result;
 }
 for (j=0,i++;source[i]!='\0';i++)
 {//cout<<"循环次数"<<" i="<<i<<" j="<<j<<endl;
  source[j]=source[i];//前移,覆盖
  j++;
 }
 source[j]='\0';
 return result;//cout<<"result="<<result<<endl;
}
void formatString(char *name)
{//去掉字符串两端的空格
 int i=0,j=0,k=0,s=0;
 s=strlen(name);
 if (s<=0)
 {
  return;
 }
 for (i=0;i<s;i++)
 {
  if (name[i]!=' ')
  {
   break;
  }
 }
 for (j=s-1;j>=0;j--)
 {
  if (name[j]!=' ')
  {
   name[j+1]='\0';
   break;
  }
 }//cout<<"s="<<s<<";i="<<i<<";j="<<j<<endl;
 for (k=0;i<=j;i++,k++)
 {//i,j的值,不确定
  name[k]=name[i];
 }
 name[k]='\0';
}
char * stringToCharArray(string s)
{//把string转换为字符数组
 char *c=new char[s.length()+1];
 for (int i=0;i<s.length();i++)
 {
  c[i]=s[i];
 }
 c[i]='\0';
 return c;
}
bool isNameLegal(char * name)
{//判断参数是否合法(合法参数中没有'/')
 for (int i=0;name[i]!='\0';i++)
 {
  if (name[i]=='/')
  {
   return false;
  }
 }
 return true;
}
void getTime(char *t)
{//获取当前时间,返回char[]
 tm *timeinfo;
 time_t nowtime;
 time(&nowtime);
 timeinfo=localtime(&nowtime);
 strftime(t,MAX_TIME_SIZE,"%Y-%m-%d %H:%M:%S",timeinfo);
}
void showHelpInfo()
{
 cout<<"温馨提示:所有操作都在内存中进行,如需存入磁盘,请用save命令!"<<endl;
 cout<<"命令说明:"<<endl;
 cout<<"新建文件夹:\tmkdir [\"文件夹名\"]"<<endl;
 cout<<"删除文件夹:\trd [\"文件夹名\"]"<<endl;
 cout<<"显示文件列表:\tdir [可选参数:\"绝对路径\"]"<<endl;
 cout<<"打开文件夹:\tcd [\"文件夹名\"或\"绝对路径\"]"<<endl;
 cout<<"返回上级目录:\tcd .."<<endl;
 cout<<"新建文件:\tcreate [\"文件名\"]"<<endl;
 cout<<"读取文件:\tread [\"文件名\"]"<<endl;
 cout<<"删除文件:\tdel或delete [\"文件名\"]"<<endl;
 cout<<"重命名文件:\tren或rename [\"当前目录下原文件名\"]"<<endl;
 cout<<"重命名文件夹:\trenfold或renamefold [\"当前目录下的目录文件名\"]"<<endl; 
 cout<<"显示所有用户:\talter show"<<endl;
 cout<<"删除用户:\talter del或delete [userName] [userPassWord]"<<endl;
 cout<<"增加用户:\talter new [userName] [userPassWord]"<<endl;
 cout<<"清屏:\t\tcls"<<endl;
 cout<<"保存:\t\tsave"<<endl;
 cout<<"退出不保存:\texit或quit"<<endl;
 cout<<"关于/说明:\tinfo或about或version"<<endl;
 cout<<"帮助:\t\thelp"<<endl;
}
void alert()
{
 cout<<"2014 操作系统课程设计 FileSystem ";
 cout<<"[version 5.0 beta]"<<endl;
 cout<<"Designed by zhang_xiao_bai"<<endl<<endl;
}
void showAboutInfo()
{
 cout<<"\nversion 5.0 beta\n";
 cout<<"1.支持 新建 文件/文件夹.\n";
 cout<<"2.支持 重命名 文件/文件夹.\n";
 cout<<"3.支持 删除 文件/文件夹.\n";
 cout<<"4.文件/文件夹 名,支持空格等字符,不支持'/'.\n";
 cout<<"5.由于CMD的局限性,不支持文件编辑,只能替换文件内容.\n";
 cout<<"6.鉴于输入密码为*的效果不好(无法回退).并不是重点,所以输入密码为明文.\n";
 cout<<"7.内置3组用户,分别是:root admin guest\n";
 cout<<"8.默认[用户名=密码].每个用户之间 文件系统相互独立.\n\n";
 cout<<"修复/更新:\n";
 cout<<"1.修复删除文件夹错误的情况.\n";
 cout<<"2.新增时间戳功能.\n";
 cout<<"3.优化细节,优化保存和退出功能.\n";
 cout<<"4.修复文件名可能为空的情况.\n";
 cout<<"version 5.0 新增:\n";
 cout<<"1.增加 删除用户 增加用户的功能\n";
}
void init()
{//cout<<"init...\n"<<endl;//初始化

 userHead=new user();

 Fold *rootDirFold =new Fold();
 rootDirFold->fold_head=new Fold();
 rootDirFold->fold_head->father=rootDirFold;
 rootDirFold->file_head=new File();
 rootDirFold->file_head->father=rootDirFold;
 strcpy(rootDirFold->name,"/");
 strcpy(rootDirFold->path,"/");
 rootDirFold->father=rootDirFold;//根目录的父目录还是根目录.
 rootFlod=currentFold=rootDirFold;//cout<<"根目录是"<<rootDirFold->name<<endl;


 control_file_users.open("control_file_users.dat",ios::binary|ios::in);
 control_file_users.seekg(0,ios::end);//指针移到源文件尾,经过测试>必须2个参数
 int data_length=control_file_users.tellg();//得到源文件长度
 control_file_users.close();

 //cout<<"文件长度是"<<data_length<<endl;
 if (data_length<=0)
 {//初始化根目录cout<<"标记\n";
  control_file_users.open("control_file_users.dat",ios::binary|ios::out|ios::trunc);
  strcpy(currentUser,"root");
  strcpy(currentUserPassWord,"root");
  control_file_users.write(currentUser,MAX_USERNAME);
  control_file_users.write(currentUserPassWord,MAX_USERNAME);
  strcpy(currentUser,"admin");
  strcpy(currentUserPassWord,"admin");
  control_file_users.write(currentUser,MAX_USERNAME);
  control_file_users.write(currentUserPassWord,MAX_USERNAME);
  strcpy(currentUser,"guest");
  strcpy(currentUserPassWord,"guest");
  control_file_users.write(currentUser,MAX_USERNAME);
  control_file_users.write(currentUserPassWord,MAX_USERNAME);
  control_file_users.close();
  readFromDisk();
 }
 else//读取控制文件以及data
 {//cout<<"002"<<endl;
  readFromDisk();
 }
 cout<<"如需帮助,请用命令help"<<endl;
 cout<<"温馨提示:所有操作都在内存中进行,如需存入磁盘,请用save命令!"<<endl;
 showUserInfo();
}

void createNewUser(char *name,char *password)
{
 user *newUser=new user();
 strcpy(newUser->name,name);
 strcpy(newUser->password,password);
 newUser->next=userHead->next;
 userHead->next=newUser;
}
user * findUser(char *name,char *password)
{//cout<<"到了findUser()";
 user *pre=userHead;
 user *p=userHead->next;
 while(p!=NULL)
 {
  if(strcmp(p->name,name)==0 && strcmp(p->password,password)==0)
  {
  //	strcpy(currentUser,p->name);
   //strcpy(currentUserPassWord,p->password);
   return p;
  }
  p=p->next;
 }
 return NULL;
}
void delUser(user *findResult)
{
 user *pre=userHead;
 user *p=pre->next;
 while(p!=NULL)
 {
  if (p==findResult)
  {
   pre->next=p->next;
   delete p;
   return ;
  }
  pre=pre->next;
  p=p->next;
 }
}

void saveUser()
{
 char temp_user[MAX_USERNAME];
 char temp_password[MAX_USERNAME];
 fstream userfile;
 userfile.open("control_file_users.dat",ios::binary|ios::out|ios::trunc);
 userfile.seekg(0,ios::beg);//指针移到源文件尾,经过测试>必须2个参数
 user *p=userHead->next;
 while(p!=NULL)
 {//cout<<"--count--"<<endl;
  strcpy(temp_user,p->name);
  strcpy(temp_password,p->password);
  userfile.write(temp_user,MAX_USERNAME);
  userfile.write(temp_password,MAX_USERNAME);
  p=p->next;
 }
 userfile.close();
 cout<<"用户账户已保存\n";
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值