内存文件操作系统

模拟实现的一个文件操作系统,头文件如下:
#ifndef _FILE_MANAGER_H_
#define _FILE_MANAGER_H_
#include <map>
#include <string>
class Dir;
//文件节点
class File{
public:
	File() {
		parent = NULL;
	}
	File(const std::string &n) : fileName(n) {}
	bool operator == (const File &other) const{
		if(fileName == other.fileName) {
			return true;
		}
		return false;
	}
public: 
    std::string fileName;  
	Dir *parent;
};
//目录节点
class Dir{
public:
	Dir() {
		parent = NULL;
	}
	Dir(const std::string &n) : dirName(n) {}
	bool operator == (const Dir &other) const{
		if(dirName == other.dirName) {
			return true;
		}
		return false;
	}
public:
	std::string dirName;
	Dir *parent;
	std::map<std::string, Dir *> subDirs;
	std::map<std::string, File *> files;
};
int CreateDir(const char * ParentDirName, const char * DirName);

void DeleteDir(const char * DirName);

int MoveDir(const char * SrcDirName, const char * DestDirName);

int CreateFile(const char * DirName, const char * FileName);

void DeleteFile(const char * FileName);

unsigned int GetFileNum(const char * DirName);

void Clear(void);

#endif

具体实现代码如下:

/******************************************************************************

Copyright (C), 2001-2013, Huawei Tech. Co., Ltd.

******************************************************************************
File Name     :
Version       :
Author        :
Created       : 2013/9
Last Modified :
Description   :
Function List :

History       :
1.Date        : 2013/9
Author      :
Modification: Created file

******************************************************************************/
#include "FileManager.h"
using namespace std;
typedef map<string, Dir>::iterator DirItr;
typedef map<string, File>::iterator FileItr;
typedef map<string, Dir*>::iterator DirPtrItr;
typedef map<string, File*>::iterator FilePtrItr;
//维护一个hash的目录索引
map<string, Dir*> dirs;
map<string, File*> files;
//定义用于表示文件系统的树结构   
Dir *Root=new Dir("root");
Dir *CurrentDir;
File *CurrentFile;
string getString(const char *s) {
	return string(s);
}

Dir* FindDir(const char * DirName)
{
	if(getString(DirName) == "root") {
		return Root;
	}

	DirPtrItr itr = dirs.find(getString(DirName));
	if(itr == dirs.end()) {
		return NULL;
	}
	return itr->second;
}

File* FindFile(const char *FileName) {
	FilePtrItr itr = files.find(getString(FileName));
	if(itr == files.end()) {
		return NULL;
	}
	return itr->second;
}

int CreateDir(const char * ParentDirName, const char * DirName)
{
	CurrentDir=FindDir(ParentDirName);
	if(CurrentDir==NULL){
		return -1;
	}
	Dir *TemptDir=FindDir(DirName);
	if(TemptDir!=NULL){
		return -1;
	}
	Dir *NewDir=new Dir(getString(DirName));
	CurrentDir->subDirs[getString(DirName)]=NewDir;
	NewDir->parent=CurrentDir;
	dirs[getString(DirName)]=NewDir;
	return 0;
}

Dir* RemoveFile(const char *FileName) {
	File *filePtr = FindFile(FileName);
	if(filePtr == NULL) {
		return NULL;
	}
	Dir *parentPtr = filePtr->parent;
	files.erase(getString(FileName));
	return parentPtr;
}

Dir* RemoveDir(const char *DirName) {
	Dir *pDir = FindDir(DirName);
	if(pDir == NULL) {
		return NULL;
	}
	if( !(pDir->subDirs.empty())){
		DirPtrItr sItr = pDir->subDirs.begin();
		//后序删除所有子目录节点
		while(sItr != pDir->subDirs.end()) {
			RemoveDir(sItr->first.c_str());
			sItr++;
		}
	}
		//后序删除所有子文件节点
		FilePtrItr fItr = pDir->files.begin();
		while(fItr != pDir->files.end()) {
			RemoveFile(fItr->first.c_str());
			if( !(pDir->files.empty())) fItr++;
			else break;
		}
		Dir *parentPtr = pDir->parent;
		delete pDir;
		dirs.erase(getString(DirName));
		return parentPtr;
}


void DeleteDir(const char * DirName)
{
	Dir *parentPtr = RemoveDir(DirName);
	if(parentPtr != NULL) {
		parentPtr->subDirs.erase(getString(DirName));
	}
    return;
}

int MoveDir(const char * SrcDirName, const char * DestDirName)
{
    Dir *srcDir = FindDir(SrcDirName);
	Dir *destDir = FindDir(DestDirName);
	if(srcDir == NULL || destDir == NULL) {
		return -1;
	}
	if(srcDir->parent == destDir) {
		return -1;
	}
	Dir *tempDir = destDir;
	while(tempDir != Root) {
		if(tempDir == srcDir) {
			return -1;
		}
		tempDir = tempDir->parent;
	}

	Dir *srcParent = srcDir->parent;
	srcParent->subDirs.erase(srcDir->dirName);
	srcDir->parent = destDir;
	destDir->subDirs[srcDir->dirName] = srcDir;
    return 0;
}

int CreateFile(const char * DirName, const char * FileName)
{
    File *filePtr = FindFile(FileName);
	if(filePtr != NULL) {
		return -1;
	}
	Dir *dirPtr = FindDir(DirName);
	if(dirPtr == NULL) {
		return -1;
	}
	File *f=new File(getString(FileName));
	files[getString(FileName)] = f;
	filePtr = files[getString(FileName)];
	filePtr->parent = dirPtr;
	dirPtr->files[getString(FileName)] = filePtr;
    return 0;
}


void DeleteFile(const char * FileName)
{
    Dir *parentPtr = RemoveFile(FileName);
	if(parentPtr != NULL) {
		parentPtr->files.erase(getString(FileName));
	}
	return;
}

unsigned int GetFileNum(const char * DirName)
{
    Dir *dirPtr = FindDir(DirName);
	if(dirPtr == NULL) {
		return 0;
	}
	unsigned int fileNum = dirPtr->files.size();
	DirPtrItr itr = dirPtr->subDirs.begin();
	while(itr != dirPtr->subDirs.end()) {
		fileNum += GetFileNum(itr->first.c_str());
		itr++;
	}
    return fileNum;
}

void Clear(void)
{
    dirs.clear();
	files.clear();
	Root->files.clear();
	Root->subDirs.clear();
    return;
}





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值