// SimpleGrep.cpp : _xZkqqHx_iDfCiDjDiDfMiBfLiDiL_xHqqkZx_ _xZkqqHx_iDeBiDhGiDiKiDfAiBfLiDfGiDiHiDjDjHhAiCmMiDeHiDjDiDgHiDiK_xHqqkZx_ _xZkqqHx_iDhMiDeDiDjDiDgHiCmMjCoIiLgA_xHqqkZx_
//

#include "stdafx.h"
#include <io.h>			// For _topen.etc.
#include <fcntl.h>		// For _O_BINARY.etc.
#include <sys/stat.h>	// For _S_IREAD.etc.

char* LoadTextFile (const char* cpFile)
{
	// _xZkqqHx_iDhEiDeAiDeDiDiLiCmMiNfIjAfGiOjOiKnEiCpAiOoGjDlOiBeC_xHqqkZx_
	struct _stat buff;
	if (_tstat(cpFile, &buff) != 0) return NULL;
	// _xZkqqHx_iDhEiDeAiDeDiDiLiCpA_xHqqkZx_Open_xZkqqHx_iBeC_xHqqkZx_
	int fh = _topen(cpFile, _O_BINARY|_O_RDONLY);
	if (fh == -1) return NULL;
	// _xZkqqHx_iDgGiBfLiDfOiCpAiOoGjDlOiBeC_xHqqkZx_
	char* cpBuff = new char[buff.st_size + 10];
	// _xZkqqHx_iDhEiDeAiDeDiDiLiCkJiCoHiDgGiBfLiDfOjDmHiCnNiNjOiCnNiBeC_xHqqkZx_
	int iCount = _read(fh, cpBuff, buff.st_size + 9);
	if (iCount == -1 || _close(fh) == -1)
	{
		delete[] cpBuff;
		return NULL;
	}
	cpBuff[iCount] = '\0';
	return cpBuff;
} // LoadTextFile.

void ReleaseTextFileBuff (char* cpBuff)
{
	delete[] cpBuff;
} // ReleaseTextFileBuff.

void StringToWString (wstring& wstr, const char* cpText)
{
	// UNICODE_xZkqqHx_iCmJjFmPiKlHiClHiCoJiBeC_xHqqkZx_
	int iBuffSz = strlen(cpText) + 1;
	wstr.assgin(iBuffSz, L'\0');
	int iTextSz = MultiByteToWideChar(wCodePage, MB_PRECOMPOSED, cpText, -1, wstr.begin(), iBuffSz);
	wstr.resize(iTextSz);
}

int main (int argc, char* argv[])
{
	if (argc != 3) return 1;

	// _xZkqqHx_jAlDiLeLjFfMiMlLiDeJiDhFiDfHiDeGiDeOiDgHjAlGjAkMiBeC_xHqqkZx_
	FmRegExp regexp;
	// _xZkqqHx_iMjPiNpFiOkOiBgJiDgHiBfLiDeOiDjDjFlGiOjKjHpBiBgKiCmMiDhMiDeDiDjDiDfOiCpAjAnNjCoIiBeC_xHqqkZx_
	wstring wstrExp;
	StringToWString(wstrExp, argv[1]);

	char* cpText = LoadTextFile(argv[2]);
	wstring wsrText;
	StringToWString(wstrText, cpText);
	WStringConst wscTextConst(wsrText.begin(), wsrText.end());
	for (;;)
	{
		int iIndex = wscTextConst.Find(L'\n');
		if (iIndex != -1)
			wscTextConst.SetLength(iIndex + 1);
		if (regexp.IsMatch(wscTextConst, wstrExp.c_str(), L'\n'))


	const char* cpCurrent = cpText;
	char buff[1000];
	long lSize;
	for (;;)
	{
		if (*cpCurrent == '\0') break;
		const char* cpMemo = cpCurrent;
		if (pRegEx->IsMatch(wPreChar, cpCurrent))
		{
			lSize = long(cpCurrent - cpMemo);
			if (900 <= lSize) lSize = 900;
			strncpy(buff, cpMemo, lSize);
			buff[lSize] = '\0';
			printf("%s\r\n", buff);

			for (long lIdx = 0; lIdx < pRegEx->GetMatchStrSize(); lIdx++)
			{
				const char* cpFrom;
				const char* cpTo;
				pRegEx->GetMatchStr(cpFrom, cpTo, lIdx);
				lSize = long(cpTo - cpFrom);
				if (900 <= lSize) lSize = 900;
				strncpy(buff, cpFrom, lSize);
				buff[lSize] = '\0';
				printf("%s\r\n", buff);
			}
		} else
		{
			cpCurrent = CharNext(cpCurrent);
			wPreChar = 0;
			while (cpMemo != cpCurrent)
			{
				wPreChar = (wPreChar << 8) + _TUCHAR(*cpMemo);
				cpMemo++;
			}
		}
	}
	ReleaseTextFileBuff(cpText);
	return 0;
}

