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

📄 dictionary.cpp

📁 一个简单的虚拟机和虚拟操作系统
💻 CPP
字号:
#include "StdAfx.h"
#include "dictionary.h"
#include "disk.h"
#include<memory>
using namespace std;

Dictionary::Dictionary(const CString& name,Dictionary* father)
{
	this->name = name;
	fcount = 0;
	dcount = 0;
	this->father = father;
}
Dictionary::~Dictionary(void)
{
	for(int i = 0;i<fcount;i++)
		delete files[i];
	for(int i = 0;i<dcount;i++)
		delete dics[i];
}
int Dictionary::createFile(const CString& fname,File*&f,char* data,int size)
{	
	if(fcount == 12)
		return DICT_OUT;
	if(!isValidName(fname))return BAD_NAME;

	for(int i = 0;i<fcount;i++)
		if(files[i]->getName() == fname)
			return NAME_EXIST;
	for(int i = 0;i<dcount;i++)
		if(dics[i]->getName() == fname)
			return NAME_EXIST;

	File*nfile = new File(fname,data,size);

	files[fcount++] = nfile;

	f = nfile;

	return 0;
}
int Dictionary::createDic(const CString& dname,Dictionary*& d)
{
	if(dcount == 12)
		return DICT_OUT;
	if(!isValidName(dname))return BAD_NAME;

	for(int i = 0;i<fcount;i++)
		if(files[i]->getName() == dname)
			return NAME_EXIST;
	for(int i = 0;i<dcount;i++)
		if(dics[i]->getName() == dname)
			return NAME_EXIST;

	Dictionary*ndic = new Dictionary(dname,this);

	dics[dcount++] = ndic;

	d = ndic;

	return 0;
}
bool Dictionary::deleteFile(const CString& fname)
{
	for(int i = 0;i<fcount;i++)
		if(files[i]->getName() == fname)
		{
			delete files[i];
			memmove(&files[i],&files[i+1],sizeof(File*)*(fcount-1-i));
			fcount--;
			return true;
		}
	return false;
}
bool Dictionary::deleteDic(const CString& dname)
{
	for(int i = 0;i<dcount;i++)
		if(dics[i]->getName() == dname)
		{
			delete dics[i];
			memmove(&dics[i],&dics[i+1],sizeof(Dictionary*)*(dcount-1-i));
			dcount--;
			return true;
		}
	return false;
}
int Dictionary::enumFiles(File**&f)const
{
	for(int i = 0;i<fcount;i++)
		f[i] = files[i];
	return fcount;
}
int Dictionary::enumDics(Dictionary**&d)const
{
	for(int i = 0;i<dcount;i++)
		d[i] = dics[i];
	return dcount;
}
int Dictionary::addFile(File*f)
{
	if(fcount==12)return DICT_OUT;
	CString n = f->getName();
	if(!this->getValidName(n))
		return NAME_EXIST;
	f->setName(n);
	files[fcount++] = f;
	return 0;
}
int Dictionary::addDic(Dictionary*d)
{
	if(dcount==12)return DICT_OUT;
	Dictionary*fa = this;
	while(fa!=NULL)
	{
		if(fa == d)return PASTE_FATHER;
		fa = fa->father;
	}
	CString n = d->getName();
	if(!this->getValidName(n))
		return NAME_EXIST;
	d->setName(n);
	dics[dcount++] = d;
	return 0;
}
bool Dictionary::removeFile(File*f)
{
	for(int i = 0;i<fcount;i++)
		if(files[i] == f)
		{
			memmove(&files[i],&files[i+1],sizeof(File*)*(fcount-1-i));
			fcount--;
			return true;
		}
	return false;
}
bool Dictionary::removeDic(Dictionary*d)
{
	for(int i = 0;i<dcount;i++)
		if(dics[i] == d)
		{
			memmove(&dics[i],&dics[i+1],sizeof(Dictionary*)*(dcount-1-i));
			dcount--;
			return true;
		}
	return false;
}
Dictionary* Dictionary::getCopy(Dictionary*father)const
{
	Dictionary*dic = NULL;
	dic = new Dictionary(name,father);
	for(int i = 0;i<dcount;i++,dic->dcount++)
		dic->dics[i] = dics[i]->getCopy(dic);
	for(int i = 0;i<fcount;i++,dic->fcount++)
		dic->files[i] = files[i]->getCopy();
	return dic;
}
bool Dictionary::getValidName(CString& s)const
{
	if(!this->isValidName(s))
		return false;
	char append = 'A';
	int length = s.GetLength();
	bool appended = false;
	while(true)
	if(getFile(s) == NULL&&getDic(s) == NULL)
		break;
	else 
	{
		if(!appended)
		{
			appended = true;
			s.AppendChar(append++);
		}
		else
		s.SetAt(length,append++);
	}
	return true;
}
File* Dictionary::getFile(const CString& fname)const
{
	for(int i = 0;i<fcount;i++)
		if(files[i]->getName() == fname)
			return files[i];
	return NULL;
}
Dictionary* Dictionary::getDic(const CString& dname)const
{
	for(int i = 0;i<dcount;i++)
		if(dics[i]->getName() == dname)
			return dics[i];
	return NULL;
}
CString Dictionary::getPath()const
{
	CString path = "";
	Dictionary*dic = father;
	while(dic!=NULL)
	{
		path.Insert(0,dic->name+"\\");
		dic = dic->father;
	}
	return path;
}
int Dictionary::getDicsNum()const
{
	int num = dcount;
	for(int i = 0;i<dcount;i++)
		num += dics[i]->getDicsNum();
	return num;
}
int Dictionary::getFilesNum()const
{
	int num = fcount;
	for(int i = 0;i<dcount;i++)
		num += dics[i]->getFilesNum();
	return num;
}
int Dictionary::getSize()const
{
	int size = 0;
	for(int i = 0;i<fcount;i++)
		size += files[i]->getSize();
	for(int i = 0;i<dcount;i++)
		size += dics[i]->getSize();
	return size;
}
int Dictionary::getSpace()const
{
	int space = 0;
	for(int i = 0;i<fcount;i++)
		space += files[i]->getSpace();
	for(int i = 0;i<dcount;i++)
		space += dics[i]->getSpace();
	return space;
}
int Dictionary::getBlockNum()const
{
	int nBlock = 0;
	for(int i = 0;i<fcount;i++)
		nBlock += files[i]->getBlockNum();
	for(int i = 0;i<dcount;i++)
		nBlock += dics[i]->getBlockNum();
	return nBlock;
}
void Dictionary::save(CArchive&ar)const
{
	ar<<name<<dcount<<fcount;
	for(int i = 0;i<dcount;i++)
		dics[i]->save(ar);
	for(int i = 0;i<fcount;i++)
		files[i]->save(ar);
}
void Dictionary::load(CArchive&ar)
{
	ar>>name>>dcount>>fcount;
	Dictionary* dic = NULL;
	File* file = NULL;
	for(int i = 0;i<dcount;i++)
	{
		dic = new Dictionary("VOID",this);
		dics[i] = dic;
		dic->load(ar);
	}
	for(int i = 0;i<fcount;i++)
	{
		file = new File("VOID",0,0);
		files[i] = file;
		file->load(ar);
	}
}

⌨️ 快捷键说明

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