#inculde<string.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
//孩子节点链表结构
typedef struct Childnode
{
int childIndex;
struct Childnode *next;
}Childnode;
//文件目录节点
typedef struct
{
char name[10000];
int parentIndex;
Childnode* children;//该结点的孩子头指针
int flag;//文件为0,文件夹为1
char content[10000];
int is_open;//1为文件已经打开,0为文件未打开
}Filenode;
//全局变量
Filenode node[10000];//存储节点的数组
int nodecount=0;//节点数量
int curdir=0;//当前目录下标
//初始化系统
void initsystem()
{
strcpy(node[0].name,"root");
node[0].parentIndex=-1;
node[0].children=NULL;
node[0].flag=1;
node[0].content[0]="\0";
node[0].is_open=0;
nodecount=1;
curdir=0;
}
//查找子节点
int findChild(int i,char *name,int flag)
{
Childnode* cur=node[parentIndex].children;
while(cur!=NULL){
int childIndex=cur->childIndex;
if(strcmp(node[childIndex].name,name)==0&&node[childIndex].flag==flag){
return childIndex;
}
cur=cur->next;
}
return -1;//未找到
}
//创建节点
int createnode(char* name,int i,int flag)
{
if(nodecount>=10000){
cout<<"ERROR:createnode";
return -1;
}
int index=findchild(i,name,flag);
if(index!=-1){
if(flag==1)
printf("ERROR:directory %s already exists",name);
else
printf("ERROR:file %s already exists",name);
return -1
}
//对新结点初始化
int newIndex=nodecount++;
node[newIndex].parentIndex=-1;
node[newIndex].children=NULL;
node[newIndex].flag=flag;
node[newIndex].content[0]="\0";
node[newIndex].is->open=0;
//将新结点添加到孩子链表中
Childenode* newnode=(Childnode*)malloc(sizeof(Childnode));
newnode->childIndex=newIndex;
newnode->next=
}
//主函数
int main()
{
initsystem();//创建根目录
char command[100];
char str1[100];
char str2[100];
while(1)
{
cout<<">>";
cin>>command;
if(strcmp(command,"exit")==0)
break;
else if(strcmp(command,"create_file")==0){
cin>>str1;
create_file(str1);
}
else if(command,"create_dir")==0){
cin>>str1;
create_dir(str1);
}
else if(command,"rename_file")==0){
cin>>str1;
cin>>str2;
rename_file(str1,str2);
}
else if(command,"rename_dir")==0){
cin>>str1;
cin>>str2;
rename_dir(str1,str2);
}
else if(command,"ls")==0){
//输出当前目录下的所有目录和文件
ls(curdir);
}
else if(command,"cd")==0){
cin>>str1;
cd(str1);
}
else if(command,"cd")==0){
cin>>str1;
cd(str1);
}
else if(command,"open")==0){
cin>>str1;
open(str1);
}
else if(command,"close_file")==0){
cin>>str1;
close();
}
else if(command,"read_file")==0){
cin>>str1;
read_file(str1);
}
else if(command,"write_file")==0){
cin>>str1;
write_file(str1);
}
else{
//如果输入命令无效
cout<<"ERROR:Invalid command";
}
}
}