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

📄 ttsdlg.cpp

📁 TTS语音开发示例
💻 CPP
📖 第 1 页 / 共 2 页
字号:

	//this is the index data file
	ifstream file("D:/TTS-Data/index.dat");
	string line;
	
	//初始化文件索引、阅读标记以及存储标记
	::getline(file, line);
	//get the readTeg
	istringstream temp(line);
	temp >> m_lastReadBook >> m_lastSaveBook;

	int bookNum = 0;
	while(::getline(file, line))
	{
		istringstream input(line);
		int readTag;
		int saveTag;
		string content;

		input >> readTag >> saveTag >> content;
		m_bookIndex[bookNum] = content;
		m_readTag[bookNum] = readTag;
		m_saveTag[bookNum] = saveTag;
		bookNum++;
	}

	//初始化中间变量, Book从0开始计数
	m_currentBook = m_lastReadBook;
	m_currentPage = m_readTag[m_lastReadBook];
	m_curSaveBook = m_lastSaveBook;
	m_curSavePage = m_saveTag[m_lastSaveBook];
}

void CTTSDlg::NextBook()
{
	ostringstream readStr;
	if(m_isSave)		//当前为录制状态
	{
		if(m_curSaveBook != 999)
		{		
			m_curSaveBook++;
			m_curSavePage = m_saveTag[m_curSaveBook];

			readStr << "录制当前阅读的内容到第" << m_curSaveBook+1 << "本书。"
				<< m_bookIndex[m_curSaveBook] << "第" << m_saveTag[m_curSaveBook]+1
				<< "页。按录制键确认录制或选择其他书页";			
		}
		else
			readStr << "已经到达最后一本书.";
	}
	else if(m_isDelete)		//当前为删除状态
	{
		if(m_currentBook != 999)
		{
			m_currentBook++;
			m_currentPage = 0;
			m_isDelBook = true;

			readStr << "清除当前整书, 第"  << m_currentBook+1 << "本书"
				<< m_bookIndex[m_currentBook] << "是否确认" 
				<< "或通过前后页选择要清除的页码";
		}
		else
			readStr << "已经到达最后一本书.";
	}
	else	//当前为阅读状态
	{
		m_isLoad = true;
		if(m_currentBook != 999)	//如果不是最后一本书
		{
			m_currentBook++;
			m_currentPage = m_readTag[m_currentBook];
			
			if(m_bookIndex[m_currentBook].length() == 0)
				readStr << "第" << m_currentBook+1 << "本书,该书为空白。";
			else
			{
				readStr << "第" << m_currentBook+1 << "本书。" << m_bookIndex[m_currentBook]
						<< "当前读到第" << m_readTag[m_currentBook]+1 << "页";
			}						
		}
		else
			readStr << "已经到达最后一本书.";
	}

	STTSPlayString(m_hTTSInstance, (char*)readStr.str().c_str(), 1);
}

void CTTSDlg::PreBook()
{
	ostringstream readStr;

	if(m_isSave)				//当前状态为录制
	{
		if(m_curSaveBook != 0)
		{	
			m_curSaveBook--;
			m_curSavePage = m_saveTag[m_curSaveBook];

			readStr << "录制当前阅读的内容到第" << m_curSaveBook+1 << "本书。"
				<< m_bookIndex[m_curSaveBook] << "第" << m_saveTag[m_curSaveBook]+1
				<< "页。按录制键确认录制或选择其他书页";
		}
		else
			readStr << "已经到达第一本书";
	}
	else if(m_isDelete)
	{
		if(m_currentBook != 0)
		{	
			m_currentBook--;		
			m_currentPage = 0;
			m_isDelBook = true;

			readStr << "清除当前整书, 第"  << m_currentBook+1 << "本书"
				<< m_bookIndex[m_currentBook] << "是否确认" 
				<< "或通过前后页选择要清除的页码";
		}
		else
			readStr << "已经到达第一本书";
	}
	else		//当前状态为阅读
	{
		m_isLoad = true;
		if(m_currentBook != 0)	//如果不是第一本书
		{
			m_currentBook--;
			m_currentPage = m_readTag[m_currentBook];

			if(m_bookIndex[m_currentBook].length() == 0)
				readStr << "第" << m_currentBook+1 << "本书,该书为空白。";
			else
			{
				readStr << "第" << m_currentBook+1 << "本书。"
					<< m_bookIndex[m_currentBook] << "当前读到第" 
					<< m_readTag[m_currentBook]+1 << "页";
			}
		}
		else
			readStr << "已经到达第一本书";
	}

	STTSPlayString(m_hTTSInstance, (char*)readStr.str().c_str(), 1);
}

void CTTSDlg::NextPage()
{
	ostringstream readStr;
	if(m_isSave)		//当前状态为录制状态
	{	
		//该书为空白
		//if(m_bookIndex[m_curSaveBook].length() == 0)
		//{
		//	readStr << "该书为空白,录制当前阅读的内容到第" << m_curSavePage+1 << "页"
		//		<< "请按录制键开始录制";
		//	STTSPlayString(m_hTTSInstance, (char*)readStr.str().c_str(), 1);
		//	return;
		//}
		//if(m_curSavePage != 0)
		//	m_curSavePage++;
		m_curSavePage++;
		readStr << "录制当前阅读的内容到第" << m_curSavePage+1 << "页"
			<< "按录制键确认录制或选择其他书页";
	}
	else if(m_isDelete)		//当前状态为删除状态
	{	
		m_isDelBook = false;
		m_currentPage++;	
		ostringstream pageName;
		pageName << "D:/TTS-Data/" << m_currentBook << "/" << m_currentPage;

		string line;
		ifstream file(pageName.str().c_str());
		if(!file.is_open())		//该页为空白页
		{
			readStr << "该页为空白页";
		}
		else
		{
			::getline(file, line);
			::getline(file, line);
			file.close();
			readStr << "清除当前的内容, 第" << m_currentPage+1 << "页" << line
				<< "请按清除键确认清除";
		}
	}
	else	//当前状态为阅读状态
	{
		//判断该书是否有内容
		m_isLoad = true;
		if(m_bookIndex[m_currentBook].length() == 0)
		{
			STTSPlayString(m_hTTSInstance, (char*)"该书为空白", 1);
			return;
		}	
		m_currentPage++;	
		
		//当前页的名字
		ostringstream pageName;
		pageName << "D:/TTS-Data/" << m_currentBook << "/" << m_currentPage;
		ifstream file(pageName.str().c_str());

		if(!file.is_open())	//如果该页为空
			readStr << "第" << m_currentPage+1 << "页。本页为空白页。";
		else				//如果该页不为空
		{
			string line;
			//得到该页的第一行
			::getline(file, line);
			::getline(file, line);
			readStr << "第" << m_currentPage+1 << "页。" << line << "请按朗读暂停键开始阅读";
			file.close();
		}
	}


	STTSPlayString(m_hTTSInstance, (char*)readStr.str().c_str(), 1);
}

void CTTSDlg::PrePage()
{
	ostringstream readStr;
	if(m_isSave)			//当前状态为录制状态
	{
		if(m_curSavePage == 0)
			readStr << "已到达首页";
		else
		{			
			m_curSavePage--;
			readStr << "录制当前阅读的内容到第" << m_curSavePage+1 << "页"
				<< "按录制键确认录制或选择其他书页";
		}
	}
	else if(m_isDelete)		//当前状态为删除 
	{
		m_isDelBook = false;
		if(m_currentPage == 0)
			readStr << "已到达首页";
		else
		{
			m_currentPage--;
			ostringstream pageName;
			pageName << "D:/TTS-Data/" << m_currentBook << "/" << m_currentPage;

			string line;
			ifstream file(pageName.str().c_str());
			if(!file.is_open())		//该页为空白页
				readStr << "该页为空白页";
			else
			{
				::getline(file, line);
				::getline(file, line);
				file.close();
				readStr << "清除当前的内容, 第" << m_currentPage+1 << "页" << line
					<< "请按清除键确认清除";
			}
		}
	}
	else	//当前状态为阅读状态
	{
		//判断该书是否有内容
		m_isLoad = true;
		if(m_bookIndex[m_currentBook].length() == 0)
		{
			STTSPlayString(m_hTTSInstance, (char*)"该书为空白", 1);
			return;
		}
		
		if(m_currentPage == 0)
			readStr << "已到达首页";
		else
		{	
			m_currentPage--;
			ostringstream pageName;
			pageName << "D:/TTS-Data/" << m_currentBook << "/" << m_currentPage;
			ifstream file(pageName.str().c_str());

			if(!file.is_open())		//this page is empty
				readStr << "第" << m_currentPage+1 << "页。本页为空白页。";
			else					//this page is not empty
			{
				string line;
				//得到该页的第一行
				::getline(file, line);
				::getline(file, line);
				readStr << "第" << m_currentPage+1 << "页。" << line << "请按朗读暂停键开始阅读";
				file.close();
			}
		}
	}

	STTSPlayString(m_hTTSInstance, (char*)readStr.str().c_str(), 1);
}

void CTTSDlg::LoadSavedPage()
{
	ostringstream pageName;
	pageName << "D:/TTS-Data/" << m_currentBook << "/"
		<< m_currentPage;

	ifstream file(pageName.str().c_str());

	if(!file.is_open())
	{
		STTSPlayString(m_hTTSInstance, "该页为空白页", 1);
		return;
	}

	if(!m_page.empty())
		m_page.clear();

	//装载存储的文件
	string line;
	while(::getline(file, line))
		m_page.push_back(line);

	file.close();
	m_senPos = m_page.begin();

	//更新阅读标记
	m_lastReadBook = m_currentBook;
	m_readTag[m_currentBook] = m_currentPage;
}

void CTTSDlg::CheckSavePage()
{
	//当第二次进行确认时,保存该页内容
	if(m_isSave)
	{
		SavePage();
		return;
	}

	m_isSave = true;
	ostringstream readStr;
	readStr << "录制当前阅读的内容到第"<< m_lastSaveBook+1 << "本书。"
		<< m_bookIndex[m_lastSaveBook].c_str() << "第" 
		<< m_saveTag[m_lastSaveBook]+1 << "页。" << "按录制键确认录制或选择其他书页";
	m_curSavePage = m_saveTag[m_curSaveBook];

	STTSPlayString(m_hTTSInstance, (char*)readStr.str().c_str(), 1);
}

void CTTSDlg::SavePage()
{
	ostringstream dirName;
	dirName << "D:/TTS-Data/" << m_curSaveBook;

	_finddata_t fileInfo;
	intptr_t handle;
	//如果该书还不存在,创建该书
	handle = _findfirst(dirName.str().c_str(), &fileInfo);
	if(handle == -1)
		_mkdir(dirName.str().c_str());
	_findclose(handle);

	ostringstream pageName;
	pageName << "D:/TTS-Data/" << m_curSaveBook << "/"
			<< m_curSavePage;

	if(m_isCover)		//用户要求覆盖该页内容
	{
		ofstream file(pageName.str().c_str());
		for(list<string>::iterator pos = m_page.begin();
			pos != m_page.end(); pos++)
			file << *pos << endl;
		file.close();

		//更新录制标记
		m_lastSaveBook = m_curSaveBook;
		m_saveTag[m_curSaveBook] = m_curSavePage + 1;

		//表示录制成功
		m_isCover = false;
		m_isSave = false;

		STTSPlayString(m_hTTSInstance, "该页内容已经录制", 1);
		
	}
	else
	{
		//检查该页内是否已经有内容
		ifstream checkFile(pageName.str().c_str());
		if(!checkFile.is_open())	//该页为空白页
		{
			//写入文件
			ofstream file(pageName.str().c_str());
			for(list<string>::iterator pos = m_page.begin();
				pos != m_page.end(); pos++)
				file << *pos << endl;
			file.close();

			//更新录制标记
			m_lastSaveBook = m_curSaveBook;
			m_saveTag[m_curSaveBook] = m_curSavePage + 1;
			m_isSave = false;	//录制成功

			STTSPlayString(m_hTTSInstance, "该页内容已经录制", 1);
		}
		else		//该页已有内容
		{
			STTSPlayString(m_hTTSInstance, "本页已有内容,按录制键覆盖或选择其他页", 1);
			m_isCover = true;
		}
	}

	//更新书目索引
	dirName << "/*.*";
	handle = _findfirst(dirName.str().c_str(), &fileInfo);
	if(handle != -1)
	{
		do
		{
			if(string(fileInfo.name) == "."
				|| string(fileInfo.name) == "..")
				continue;
			else
				break;
		}while(_findnext(handle, &fileInfo) == 0);
	}
	ostringstream firstPage;
	firstPage << "D:/TTS-Data/" << m_curSaveBook << "/"
		<< fileInfo.name;
	ifstream first(firstPage.str().c_str());
	string line;
	::getline(first, line);
	::getline(first, line);
	if(m_bookIndex[m_curSaveBook] != line)
		m_bookIndex[m_curSaveBook] = line;
	
	//if(m_curSavePage == 0)
	//{
	//	//这本书原本为空
	//	list<string>::iterator pos = m_page.begin();
	//	pos++;
	//	if(m_bookIndex[m_curSaveBook].length() == 0)
	//	{
	//		m_bookIndex[m_curSaveBook] = *pos;
	//		m_readTag[m_curSaveBook] = 0;
	//	}
	//	else
	//		m_bookIndex[m_curSaveBook] = *pos;
	//}
}

void CTTSDlg::CheckDelete()
{
	if(m_isDelete)
	{
		Delete();
		return;
	}

	m_isDelete = true;
	m_currentPage = 0;
	ostringstream readStr;
	readStr << "清除当前整书, 第"  << m_currentBook+1 << "本书"
		<< m_bookIndex[m_currentBook] << "是否确认" 
		<< "或通过前后页选择要清除的页码";

	STTSPlayString(m_hTTSInstance, (char*)readStr.str().c_str(), 1);
}

void CTTSDlg::Delete()
{
	ostringstream name;
	ostringstream readStr;
	
	if(m_isDelBook)	//清除整书
	{
		name << "D:/TTS-Data/" << m_currentBook << "/*.*";
		list<string> deleteFiles;

		_finddata_t fileInfo;
		intptr_t handle;

		handle = _findfirst(name.str().c_str(), &fileInfo);

		//删除该目录下所有文件
		if(handle != -1)
		{
			do
			{
				if(string(fileInfo.name) == "."
					|| string(fileInfo.name) == "..")
					continue;
				ostringstream filename;
				filename << "D:/TTS-Data/" << m_currentBook
					<< "/" << fileInfo.name;
				//int res = remove(fileInfo.name);
				deleteFiles.push_back(filename.str());
			}while(_findnext(handle, &fileInfo) == 0);
		}
		_findclose(handle);

		for(list<string>::iterator pos = deleteFiles.begin();
			pos != deleteFiles.end(); pos++)
			int res = remove(pos->c_str());
		ostringstream dirName;
		dirName << "D:/TTS-Data/" << m_currentBook;
		int res = _rmdir(dirName.str().c_str());
		//更新书目索引
		m_bookIndex[m_currentBook] = "";
		m_readTag[m_currentBook] = 0;
		m_saveTag[m_currentBook] = 0;
		m_isDelete = false;

		readStr << "第" << m_currentBook+1 << "本书已经清除";
	}
	else		//清除一页
	{
		name << "D:/TTS-Data/" << m_currentBook << "/"
			<< m_currentPage;

		ifstream checkFile(name.str().c_str());
		if(!checkFile.is_open())	//该页为空白页
			return;
		else
			checkFile.close();
		
		int res = remove(name.str().c_str());
		m_isDelete = false;
		readStr << "第" << m_currentBook + 1 << "本书第" << m_currentPage+1 << "页已经清除";

		//更新书目
		_finddata_t fileInfo;
		intptr_t handle;
		ostringstream dirName;
		dirName << "D:/TTS-Data/" << m_currentBook << "/*.*";
		handle = _findfirst(dirName.str().c_str(), &fileInfo);
		if(handle != -1)
		{
			do
			{
				if(string(fileInfo.name) == "."
					|| string(fileInfo.name) == "..")
					continue;
				else
					break;
			}while(_findnext(handle, &fileInfo) == 0);
		}
		_findclose(handle);

		ostringstream firstPage;
		firstPage << "D:/TTS-Data/" << m_curSaveBook << "/"
			<< fileInfo.name;
		ifstream first(firstPage.str().c_str());
		string line;
		::getline(first, line);
		::getline(first, line);
		if(m_bookIndex[m_curSaveBook] != line)
			m_bookIndex[m_curSaveBook] = line;
		first.close();

		if(line == "")
		{
			m_readTag[m_currentBook] = 0;
			m_saveTag[m_currentBook] = 0;
			ostringstream delDir;
			delDir << "D:/TTS-Data/" << m_currentBook;
			remove(delDir.str().c_str());
		}


	}

	STTSPlayString(m_hTTSInstance, (char*)readStr.str().c_str(), 1);
}

void CTTSDlg::SaveIndex()
{
	ofstream file("D:/TTS-Data/index.dat");

	file << m_lastReadBook << " " << m_lastSaveBook << endl;
	for(unsigned i = 0; i < m_bookIndex.size(); i++)
	{
		file << m_readTag[i] << " "
			<< m_saveTag[i] << " "
			<< m_bookIndex[i] << endl;
	}
	file.close();
}

⌨️ 快捷键说明

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