#include "stdafx.h"
#include "Creader.h"

CReader::CReader()
{
	hWnd = NULL;
	bufferPos = 0;
}

bool CReader::readFile(CString fileName)
{
	CReader::fileName = fileName;
	if(!file.Open(fileName,CFile::modeRead))
	{
		::MessageBox(hWnd,fileName+"文件打开错误！","",MB_OK);
		return false;
	}

	file.SeekToBegin();
	int readCount;
	long fileLength;
	fileLength = file.GetLength();
	char *bf = new char[fileLength+1]; //!!!
	readCount =	file.ReadHuge(bf,fileLength);
	if( readCount != fileLength)
	{
		::MessageBox(hWnd,fileName+"文件读取错误！","",MB_OK);
		return false;
	}
	bf[readCount] = 0;

	//检测读出的字符串是否符合要求
	for(int i=0;i<=readCount;i++)
	{
		if(bf[i] == 0)
			break;
	}
	if(i != readCount)//不符合要求，返回，buffer为上一次读取的值
	{
		::MessageBox(hWnd,fileName+"文件格式错误！\n使用ANSI文本格式,\n不支持Unicode文本格式！","",MB_OK);
		file.Close();
		return false;
	}
	//符合要求，清空buffer，赋以新值
	buffer.Empty();
	buffer = bf;

	delete bf;
	file.Close();
	return true;
}

bool CReader::getText(CString chapterNumber,CString problemType,CString problemNumber)
{
	int strsCount,ifSuccess,nowPos,nextPos;
	CString problemHeader;
	bufferPos = 0;
	while(1)
	{
		problemHeader = getInnerStrIter("$[","]",ifSuccess);
		if(ifSuccess) //在buffer中从bufferPos开始找下去
		{
			CString *pStr = getStrsDevidedBy(' ',problemHeader,strsCount);
			if(strsCount >= 3)
			if((chapterNumber == pStr[0]) && (problemType == pStr[1]) && (problemNumber == pStr[2]) )
			//找到了所需的题目
			{
				nowPos = bufferPos; //当前题目的开始位置
				nextPos = buffer.Find("$[",bufferPos); //找出下一题的开始位置
				if(nextPos == -1)
					nextPos = buffer.GetLength();
				else
					nextPos += 2;

				text.problem = getInnerStrIter("$题目","$",ifSuccess);
				if((bufferPos > nextPos) || !ifSuccess)
					text.problem = "无题目";
				bufferPos = nowPos;

				text.explanation = getInnerStrIter("$解释","$",ifSuccess);
				if((bufferPos > nextPos) || !ifSuccess)
					text.explanation = "无解释";
				bufferPos = nowPos;

				text.answer = getInnerStrIter("$答案","$",ifSuccess);
				if((bufferPos > nextPos) || !ifSuccess)
					text.answer = "无答案";
				bufferPos = nowPos;

				text.program = getInnerStrIter("$程序","$",ifSuccess);
				if((bufferPos > nextPos) || !ifSuccess)
					text.program = "无程序";
				removeSubStr(text.program,"\15\12");
				bufferPos = nowPos;

				text.picture = getInnerStrIter("$图片","$",ifSuccess);
				if((bufferPos > nextPos) || !ifSuccess)
					text.picture = "无图片";
				removeSubStr(text.picture,"\15\12");
				bufferPos = nowPos;
				return true;
			}
		}
		else
			break;
	}
	return false;
}

CString CReader::getInnerStrIter(CString beginStr,CString endStr,int &ifSuccess)
{
	long beginPos,endPos,count;
	beginPos = buffer.Find(beginStr,bufferPos);
	if(beginPos != -1)
	{
		beginPos += beginStr.GetLength();
		endPos = buffer.Find(endStr,beginPos);
		if(endPos != -1)
		{
			count = endPos - beginPos;
			bufferPos = endPos + endStr.GetLength();
			ifSuccess = 1;
			return buffer.Mid(beginPos,count);
		}
	}
	ifSuccess = 0;
	return "";

}

CString *CReader::getStrsDevidedBy(char c,CString str,int &count)
{
	long i,beginPos,endPos,num;
	CString *pStr,temp;
	pStr = new CString[50];
	beginPos = 0;
	for(i=0;true;i++)
	{
		endPos = str.Find(c,beginPos);
		if(endPos != -1)
		{
			num = endPos - beginPos;
			pStr[i] = str.Mid(beginPos,num);
			beginPos = endPos + 1;
		}
		else
		{
			num = str.GetLength() - beginPos;
			pStr[i] = str.Mid(beginPos,num);
			count = i + 1;
			return pStr;
		}
	}
}

void CReader::removeSubStr(CString &sourceStr,CString subStr)
{
	long beginPos,endPos,num;
	CString temp;
	beginPos = 0;
	while(true)
	{
		endPos = sourceStr.Find(subStr,beginPos);
		if(endPos != -1)
		{
			num = endPos - beginPos;
			temp += sourceStr.Mid(beginPos,num);
			beginPos = endPos + subStr.GetLength();
		}
		else
		{
			num = sourceStr.GetLength() - beginPos;
			temp += sourceStr.Mid(beginPos,num);
			break;
		}
	}
	sourceStr = temp;
}

void CReader::getInnerStrs(CString &s, CString begin, CString end, CString* &dest, int &num)
{
	int pos1,pos2,beginLen,endLen;
	pos1 = s.Find(begin);
	beginLen = begin.GetLength();
	endLen = end.GetLength();
	CString *p = new CString[10];
	for(int i=0;1;i++)
	{
		if(pos1 != -1)
		{
			pos2 = s.Find(end,pos1);
			if(pos2 != -1)
			{
				p[i] = s.Mid(pos1+beginLen,pos2-pos1-beginLen);
				pos1 = pos2 + endLen;
				pos1 =s.Find(begin,pos1);
			}
			else
				break;
		}
		else
			break;
	}
	dest = p;
	num = i;
}

int CReader::getProblemNum(CString chapter, CString type)
{
	int strsCount,problemNum,ifSuccess;
	CString problemHeader;
	bufferPos = 0;
	problemNum = 0;
	while(1)
	{
		problemHeader = getInnerStrIter("$[","]",ifSuccess);
		if(ifSuccess) //在buffer中从bufferPos开始找下去
		{
			CString *pStr = getStrsDevidedBy(' ',problemHeader,strsCount);
			if(strsCount >= 3)
			if((chapter == pStr[0]) && (type == pStr[1]))
			//找到了所需的题目
			{
				problemNum++;
			}
		}
		else
			break;
		bufferPos += 2;//"$["
	}
	return problemNum;
}
