⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filesystem.cpp

📁 简单文件管理系统
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include"stdAfx.h"

//函数描述:创建磁盘文件
FileSystem::FileSystem()
{
  
   for(int count=0;count<10;count++)
   {
	   strcpy(this->currentpath[count],"");
	   strcpy(this->inputpath[count],"");
	   strcpy(this->path[count],"");
   }
    strcpy(this->currentpath[0],"root");       //当前路径
      
  
   strcpy(this->stdorder[0],"cd");
   strcpy(this->stdorder[1],"edit");
   strcpy(this->stdorder[2],"del");
   strcpy(this->stdorder[3],"dir");
   strcpy(this->stdorder[4],"md");
   strcpy(this->stdorder[5],"rd");
   strcpy(this->stdorder[6],"exit");
   

  
   this->i = 0 ;
   this->j = 0 ;
   strcpy(order,"");//输入的命令
   strcpy(newname,"");//新文件或目录的名字
   strcpy(orderline,"");//从键盘上获得命令行,包括命令和路径
   
  
    this->newnode = NULL ;                //创建结点时指针
    this->temp=NULL ;                     //活动的指针
    this->temp2=NULL ;                    //活动的指针
    this->temp3=NULL ;                    //活动的指针
    this->temp4=NULL ;                    //活动的指针
    this->root=NULL ;                    //指向根结点(root)的指针

	
//创建20M的文件目标磁盘
   if((this->pFile=fopen("FileDriver","rb")) != NULL )//检测目标磁盘
   {
	   printf("当前检测到磁盘的存在,可以进行相关的操作\n");
	   fclose(this->pFile);
   }
   else
   {
	    printf("没有检测到磁盘的存在,正在进行磁盘创建工作请稍候.......\n");
		Sleep(1000);    //等待时间为1秒
        if((this->pFile=fopen("FileDriver","wb")) == NULL )
		{
	     printf("尝试创建磁盘分区错误 ,系统要关闭.........\n");
	     exit(1) ;
		}

        for(int i=0;i<20;i++)	//创建20M磁盘
		{
	       for(int j=0;j<1024;j++)
		   {
		      for(int k=0;k<1024;k++)
			  {
			     fputc('#',this->pFile);
			  }
		   }
		}

       fclose(this->pFile) ;
	   printf("磁盘创建完毕,可以进行相关操作.....\n");
   }

   Sleep(1000);
  
}

/////////////函数描述:创建磁盘文件
VRESULT FileSystem::run()
{
	print_title();                     ///打印题头(版本号)
    
	initialization();                  //初始化
	cin.getline(orderline,50);         

	order_path_explain();              //命令和路径解释部分,将命令和路径分离出来,并对命令进行处理
	test();                            //对路径进行分析
	                                   
	while(orderflag!=6)
	{
		switch(orderflag)
		{
		case -1: error(103);
			print_currentpath();break;
		case 0:  cdmodule();break;
		case 1:  editmodule();break;
		case 2:  delmodule();break;
		case 3:  dirmodule();break;
		case 4:  mdmodule();break;
		case 5:  rdmodule();break;
		}
		reclaim();//把指针temp,temp2复位,inputpath,newname,orderline,order,path清空
        cin.getline(orderline,50);
		order_path_explain();                
	    test();                            
	}
	exit_command();
}

///////////////////函数描述:打印版本信息
VRESULT FileSystem::print_title()
{
	printf("欢迎你使用本系统!\n");
	printf("\n");
    cout<<"root>";
}

//////////////////函数描述:申请新的节点空间
VRESULT FileSystem::getnewnode()
{
	if ((newnode=(struct node*)malloc(sizeof(N)))==NULL)
	{
		error(104);
		exit(0);
	}
}

//////////////////函数描述:初始化
VRESULT FileSystem::initialization()        //初始化
{//创建根结点root
	if ((root=(struct node*)malloc(sizeof(N)))==NULL)
	{
		error(104);
		exit(0);
	}
	strcpy((*root).filename,"root");
	(*root).isdir=1; //为目录
	(*root).sibling=NULL;//此时root指向root
    
	//创建目录结点bin
	getnewnode();//申请新的节点空间
	strcpy((*newnode).filename,"bin");
	(*newnode).isdir=1;
    (*newnode).child=NULL;
	(*root).child=newnode;
	temp=newnode;//此时temp newcode (*root).child都指向a
   
	//创建目录结点usr
	getnewnode();
	strcpy((*newnode).filename,"usr");
	(*newnode).isdir=1;//此时newcode指向b,temp指向a
	(*temp).sibling=newnode;//把a的兄弟节点指向b
	temp=newnode;//此时temp newcode都指向b
	temp2=newnode;//此时temp2指向b,暂存,用来以后处理它的子节点(指向lib)

	//创建文件结点c
	getnewnode();
	strcpy((*newnode).filename,"unix");
	(*newnode).isdir=0;//为文件
    (*newnode).child=NULL;//此时temp指向b,newcode指向c
	(*temp).sibling=newnode;//把b的兄弟节点指向c
	temp=newnode;//此时temp newcode都指向c
    
	//创建目录结点d
	getnewnode();
	strcpy((*newnode).filename,"etc");
	(*newnode).isdir=1;
    (*newnode).sibling=NULL;
	(*newnode).child=NULL;//此时temp指向c,newcode指向d
	(*temp).sibling=newnode;//把c的兄弟节点指向d

    temp=root;
	temp2=root;
	temp3=root;
    temp4=root;
}

////////函数描述:打印版本信息
VRESULT FileSystem::order_path_explain()//命令和路径解释部分,将命令和路径分离出来,并对命令进行处理
{
	int z,y,r;
	orderflag=-1;
	for(i=0;i<50;i++)//如果输入的命令行第一个字符就是空格的话还要跳过
	{
		if((orderline[i]!='\0')&&(orderline[i]!=' ')&&(orderline[i]!=(char)32))
		{
			r=i;//第r个字母不为空
			break;
		}
	}
	for(i=0;i<50;i++)
	{
		if((orderline[i+r]!='\0')&&(orderline[r+i]!=' ')&&(orderline[r+i]!=(char)32))
			order[i]=orderline[r+i];
		else 
		{
			z=r+i;//保存输入的命令行中第一个空格的位置
			break;
		}
	}
	for(i=0;i<7;i++)
	{
		if(!strcmp(order,stdorder[i]))
		{
			orderflag=i;//命令的标志
			break;
		}
	}
    //路径的解释
	j=0;
	y=0;
	for(i=z+1;i<50;i++)
	{
		if(orderline[i]==(char)92)
		{
			y=0;
			j++;
		}
		else
			if((orderline[i]!='\0')&&(orderline[i]!=' ')&&(orderline[i]!=(char)32))
			{
				inputpath[j][y]=orderline[i];
				y++;
			}
	}
}

//////////////函数描述:打印版本信息
VRESULT FileSystem::test()//对路径进行分析
{          /*路径的标志,分别用-2、-1、0、1、5表示最后一个名字为新文件或目录、
		   错误或无效的路径、正确的相对路径、正确的完全路径、没有输入路径*/
  
    if(strcmp(this->orderline,"help")==0 && orderflag == -1 )
	{
		orderflag = 7 ;
		return ;
	}

	int x,g;
	temp=root;
	temp2=root;
	temp3=root;
	temp4=root;
  if(!strcmp(inputpath[0],"\0"))       //输入的路径为空
	{
		pathflag=5;
		currentpath_path();           //把当前目录存入PArootH中
	}
  else
  {
	if(!strcmp(inputpath[0],"root"))    //输入路径第一个名字为root,即为完全路径
	{
		for(i=1;i<10;i++)              //注意:i是从1开始的
		{
			if(!strcmp(inputpath[i],"\0"))
				break;                 //加一个跳出控制,节省执行时间
			pathflag=10;               //10为任意一个无所谓的数值
			temp=(*temp).child;        //注意:i是从1开始的,所以从一开始就可以执行一次此语句
			while(temp!=NULL)
			{
				if(!strcmp(inputpath[i],(*temp).filename))
				{
					pathflag=1;//假如是完全路径
					break;
				}
				else
					temp=(*temp).sibling;//指向兄弟节点
			}//结束while
			if(pathflag==1)
				continue;
			else
			{
				x=i;                   //保存当前的i值,即当前的inputpath[i]
				if(strcmp(inputpath[x+1],"\0"))//不匹配的名字不是输入路径的最后一个,即输入的路径是错误的或者无效的
				{
					pathflag=-1;
					currentpath_path();
				}
				else                   //只是最后一个名字不匹配,可能为新的目录或文件名
				{
					pathflag=-2;
					strcpy(newname,inputpath[x]);//把最后一个名字存放在newname中
					for(j=0;j<x;j++)   //注意:j<x
					{
						strcpy(path[j],inputpath[j]);//把最后一个之前的路径存入PArootH中
					}
				break;                 //跳出循环"for(i=1;i<10;i++)注意:i是从1开始的
				}//结束else 

			}//结束else

		}//结束for(i=1;i<10;i++)注意:i是从1开始的
		if(pathflag==1)
			inputpath_path();             //为完全路径,把输入的路径存入path
	}//结束if(!strcmp(inputpath[0],"root"))输入路径第一个名字为root,即为完全路径
	else//为相对路径
	{    
		for(i=0;i<10;i++)
		{
			if(!strcmp(currentpath[i],"\0"))
				break;                 //加一个跳出控制,节省执行时间
			while(temp!=NULL)
			{
				if(!strcmp(currentpath[i],(*temp).filename))
					break;
				else
					temp=(*temp).sibling;
			}
			temp=(*temp).child;
		}//这个FOR循环部分是查找当前路径在目录树中的位置
         //举例:若当前路径为root\usr\lib,最后temp指向的是liu,即此时的temp指向的应是
		         //相对路径的第一个名字或者是通过兄弟节点就可以指向的一个名字
		while(temp!=NULL)
		{
			if(!strcmp(inputpath[0],(*temp).filename))
				break;
			else temp=(*temp).sibling;
		}
		if(temp==NULL)
		{
			temp=temp2;
			g=0;
		}
		else
			g=1;

		if(!strcmp(inputpath[0],(*temp).filename))
		{
			for(i=g;i<10;i++)          //注意:i是从1开始的
			{
				if(!strcmp(inputpath[i],"\0"))
					break;             //加一个跳出控制,节省执行时间
				pathflag=10;
				temp=(*temp).child;
				while(temp!=NULL)
				{
					if(!strcmp(inputpath[i],(*temp).filename))
					{
						pathflag=0;
						break;
					}
					else
						temp=(*temp).sibling;
				}
				if(pathflag==0)
					continue;
				else
				{
					x=i;               //保存当前的i值,即当前的inputpath[i]
					if(strcmp(inputpath[x+1],"\0"))//不匹配的名字不是输入路径的最后一个,即输入的路径是错误的或者无效的
					{
						pathflag=-1;
						currentpath_path();
					}
					else               //只是最后一个名字不匹配,可能为新的目录或文件名
					{
						pathflag=-2;
						strcpy(newname,inputpath[x]);//把最后一个名字存放在newname中
						current_input_1_path();//把当前路径和输入的路径(除掉最后一个)连接起来存入path中
					}//结束else 只是最后一个名字不匹配,可能为新的目录或文件名

					break;             //跳出循环"for(i=1;i<10;i++)注意:i是从1开始的

				}//结束else

			}//结束for(i=1;i<10;i++)注意:i是从1开始的
			if(pathflag==0)
				current_input_path();//把当前路径和输入的路径连接起来存入path中
		}//结束if(!strcmp(inputpath[0],"root"))输入路径第一个名字为root,即为完全路径
		else
		{
			if(!strcmp(inputpath[1],"\0"))//不匹配的路径名是输入路径的最后一个名字,即为新文件或目录
			{
				strcpy(newname,inputpath[0]);
				currentpath_path();
				pathflag=-2;
			}
			else
			{
				pathflag=-1;
				currentpath_path();
			}
		}//结束else
	}//结束else
  }
}//结束对路径进行分析,返回不同的PArootHFLAG及path,如果有的话还有newname

//////////////////////////////////////////////函数描述:打印版本信息
VRESULT FileSystem::error(int wrongnumber)//错误情况报告
{
	printf("    Error");
	printf(" %d",wrongnumber);
	printf(":");
	switch(wrongnumber)
	{
	case 100: printf("无效的或者错误的路径!\n");break;
	case 101: printf("目录或者文件已存在!\n");break;
	case 102: printf("目录名或者文件名不能为空!\n");break;
	case 103: printf("错误的命令!\n");break;
	case 104: printf("申请结点出错,内存溢出!\n"); break;
	case 105: printf("使用cd命令不能进入文件中!\n");break;
	case 106: printf("不能用rd命令删除文件,请指定一个目录名!\n");break;
	case 107: printf("不能用del命令删除目录,请指定一个文件名!\n");break;
	case 108: printf("不能删除上级目录或同级目录!\n");break;
	}
}//结束错误情况报告函数

////////////函数描述:打印版本信息
VRESULT FileSystem::print_currentpath()//打印当前路径
{
	for(i=0;i<10;i++)
	{
		if(strcmp(currentpath[i],"\0"))//不为空的时候打印
		{
			printf("%s",currentpath[i]);
		    if(strcmp(currentpath[i+1],"\0"))
				printf("\\");
		}
		else
			break;
	}
	printf(">");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -