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

📄 book.cpp

📁 一个帮助你学习英语的软件~~很不错的咯~~ 对功能又做了改进~大家支持下哈~
💻 CPP
字号:
//PK 2006/08/28 - 2007/03/10
//PK Recite

#include "Book.h"
#include "records.h"
#include "item.h"
#include "utility.h"
#include "configurations.h"

CBook::CBook() : _items(0), _recorders(new CRecords())
{	//PK 2007/02/26 - 03/07
	_is_active = false;
};
CBook::~CBook()
{	//PK 2006/10/06 - 2007/03/07
	save();
	_clear_items();
	if (NULL != _recorders) delete _recorders;
}
void CBook::_clear_items()
{	//PK 2007/02/12
	t_items::iterator itr, itr_end = _items.end();
	for (itr = _items.begin(); itr != itr_end; ++itr)
		delete (*itr);
	_items.clear();
}
bool CBook::_load_info()
{	//PK 2007/02/05 - 03/10

	//PK load id and name from info.xml
	fs::path fn(_dir / "info.xml");
	if (fs::exists(fn)) {
		TiXmlDocument doc(fn.native_file_string().c_str());
		if (doc.LoadFile()) {
			TiXmlHandle hDoc(&doc);
			TiXmlHandle root = hDoc.FirstChildElement();
			TiXmlElement * id = root.FirstChild("id").Element();	
			if (NULL != id) _id_str = id->GetText();
			TiXmlElement * name = root.FirstChild("name").Element();
			if (NULL != name) _book_name = name->GetText();
			TiXmlElement * contributor = root.FirstChild("contributor").Element();
			if (NULL != contributor) _contributor = contributor->GetText();
		}
	}

	//PK if there are no id, create one, if no name, use the directory name. and save them.
	bool need_save = false;
	if (!_id_str.empty()) {
		if (RPC_S_OK != UuidFromString((unsigned char *)_id_str.c_str(), &_id)) {
			//PK fail to recover UUID from string
			_id_str.clear();
		}
	}
	if (_id_str.empty()) {
		UuidCreateSequential(&_id);
		_id_str = _id2str(_id);
		need_save = true;
	}
	if (_book_name.empty()) {
		_book_name = _dir.leaf();
		need_save = true;
	}
	if (_contributor.empty()) {
		_contributor = "Noname";
		need_save = true;
	}

	if (need_save) _save_info();

	return true;
}
bool CBook::_save_info()
{	//PK 2007/03/06 - 10
	assert(!_id_str.empty());
	TiXmlDocument doc;
	TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "", "" );
	doc.LinkEndChild( decl );

	TiXmlElement * root = new TiXmlElement("Book");
	doc.LinkEndChild(root);

	TiXmlElement * id = new TiXmlElement("id");
	root->LinkEndChild(id);
	TiXmlText * txt = new TiXmlText(_id_str.c_str());
	id->LinkEndChild(txt);

	TiXmlElement * name = new TiXmlElement("name");
	root->LinkEndChild(name);
	txt = new TiXmlText(_book_name.c_str());
	name->LinkEndChild(txt);

	TiXmlElement * contributor = new TiXmlElement("contributor");
	root->LinkEndChild(contributor);
	txt = new TiXmlText(_contributor.c_str());
	contributor->LinkEndChild(txt);

	fs::path fn(_dir / "info.xml");
	doc.SaveFile(fn.string().c_str());
	return true;
}
bool CBook::_load_items()
{	//PK 2007/02/09 - 22
	fs::directory_iterator itr_end;
	string txt_extension(".txt");
	_clear_items();
	for (fs::directory_iterator itr(_dir); itr != itr_end; ++itr) {
		if (!fs::is_directory(*itr)) {
			if (fs::extension(*itr) == txt_extension) {
				CItem * item = new CItem(this);
				item->file_name((*itr).leaf());
				if (item->load_title())	_items.push_back(item);
				else delete item;
			}
		}
	}
	return _items.empty() ? false : true;
}
bool CBook::_load_records()
{	//PK 2007/02/12 - 03/09
	//PK get the string format id
	unsigned char * psz_id = NULL;
	if (RPC_S_OK != UuidToString(&_id, &psz_id)) {
		//PK fail to convert UUID to string
		return false;
	}
	string id_str((char *)psz_id);
	RpcStringFree(&psz_id);

	//PK if the file is existed, load it
	fs::path fn = g_root_dir / g_record_dir / id_str;
	_recorders->file_name(fn);
	if (fs::exists(fn)) _recorders->load();

	return true;
}
bool CBook::_restore_item_info()
{	//PK 2007/02/09 - 26
	//PK restore the item's info from records
	t_items::iterator itr = _items.begin(), itr_end = _items.end();
	for (; itr != itr_end; ++itr)
		(*itr)->info();
	return true;
}
bool CBook::load(const fs::path & dir)
{	//PK 2006/09/02 - 2007/03/05
	if (!fs::exists(dir)) return false;
	_dir = dir;

	if (!_load_info()) return false;
	_load_items();
	if (!_load_records()) return false;
	if (!_restore_item_info()) return false;
	return true;
}
bool CBook::save()
{	//PK 2006/09/18 - 2007/03/07
	if (NULL != _recorders) _recorders->save();
	return true;
}
bool CBook::add_item(CItem * item)
{	//PK 2006/09/18 - 2007/02/20
	item->info(_recorders->get_info(item->title()));
	_items.push_back(item);
	return true;
}
bool CBook::delete_item(unsigned int index)
{	//PK 2006/10/08 - 18
/*	t_items::iterator itr, itr_end = _items.end();
	for (itr = _items.begin(); itr != itr_end; ++itr) {
		if (id == (*itr)->id()) {
			fs::path file(ulong2c((*itr)->id()));
			if (fs::exists(file)) fs::remove(file);
			delete *itr;
			_items.erase(itr);
			_is_dirty = true;
			return true;
		}
	}*/
	return false;
}
bool CBook::delete_item(CItem * item)
{	//PK 2007/03/05
	t_items::iterator itr, itr_end = _items.end();
	for (itr = _items.begin(); itr != itr_end; ++itr) {
		if (*(*itr) == *item) {
			item->remove_article();
			_recorders->remove_info(item->title());
			delete item;
			_items.erase(itr);
			return true;
		}
	}
	return false;
}
CItem * CBook::get_item(unsigned int index)
{	//PK 2006/10/10
	assert(index < size());
	return _items[index];
}
void CBook::dirty()
{	//PK 2007/02/26
	_recorders->dirty(); 
}
bool CBook::is_recorders_matched()
{	//PK 2007/02/26
	return _recorders->size() == size();
}
void CBook::trim_recorders()
{	//PK 2007/02/26
	CRecords::t_names names;
	t_items::const_iterator itr = _items.begin(), itr_end = _items.end();
	for (; itr != itr_end; ++itr)
		names.insert((*itr)->title());
	_recorders->trim(names);
}
string CBook::_id2str(UUID & id)
{	//PK 2007/03/06
	unsigned char * psz_id;
	if (RPC_S_OK != UuidToString(&id, &psz_id)) {
		//PK fail to convert UUID to string
		return string();
	}
	string id_str = string((char *)psz_id);
	RpcStringFree(&psz_id);
	return id_str;
}
bool CBook::delete_self()
{	//PK 2007/03/07
	assert(!_dir.empty());
	fs::remove(_recorders->file_name());
	delete _recorders;
	_recorders = NULL;
	fs::remove_all(_dir);
	return true;
}
void CBook::change_record_name(const string & src, const string & tar)
{	//PK 2007/03/08
	_recorders->change_record_name(src, tar);
}
void CBook::book_info_changed(const string & name, const string & contributor)
{	//PK 2007/03/10
	if (_book_name == name && _contributor == contributor) return;
	_book_name = name;
	_contributor = contributor;
	_save_info();
}

⌨️ 快捷键说明

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