//	M061 : Read the text file and display the line containing the specified character string
option unsigned;

//--------------------------------------------------------------------------------------//
//	Read the text file and display the line containing the specified character string	//
//																						//
//	Return	: Number of lines in which the character string was actually found			//
//--------------------------------------------------------------------------------------//
function main()
{
	define	FoundLines = 0;
	define	finp;
	define	LineNo = 0;
	define	$InputFile, $SearchString, $TextLine;

	//	Profile section name setting
	_SetProfileSect("M04Sect");

	//	Read 'InputFile' and 'SearchString' from profile
	$InputFile	  = $_GetProfileStr("InpFile", "");
	$SearchString = $_GetProfileStr("InpStr" , "");

	do {
		//	Enter search file
		$InputFile = $_GetOpenFile("Please select the file to search",
									  "All(*.*)/*.*/Text(*.txt)/*.txt",
									  "txt",
									  $InputFile);
		if ($InputFile == "") break;

		//	Input 'SearchString'
		$SearchString = $_Input("Please enter the search string", $SearchString);
		if ($SearchString == "") break;

		if (finp = _FOpen($InputFile, 'rt')) {
			while ($TextLine = $_FGetS(finp)) {
				LineNo++;
				if (ShowString($TextLine, $SearchString)) {
					ShowLineText(LineNo, $TextLine, $SearchString);
					FoundLines++;
				}
			}
			_FClose(finp);
		}
		_Print("FoundLines = ", FoundLines, "\n");
	}

	//	Save 'InputFile' and 'SearchString' to Profile
	if ($InputFile	  != "") _SetProfileStr("InpFile", $InputFile	);
	if ($SearchString != "") _SetProfileStr("InpStr" , $SearchString);
}

//------------------------------------------------------------------------------//
//	Check if there is a SearchString in line text								//
//																				//
//	ArgumentF	$Line - Line text												//
//				$str  - SearchString											//
//																				//
//	Return	F	_TRUE FThere is a 'SearchString' in the line text				//
//				_FALSEFThere is no 'SearchString' in the line text				//
//------------------------------------------------------------------------------//
function ShowString($Line, $str)
{
	define	rc = _FALSE;

	if (_StrStr($Line, $str) != -1) rc = _TRUE;

	return rc;
}

//------------------------------------------------------------------------------//
//	ShowLineTexti'SearchString' is displayed in redj							//
//																				//
//	ArgumentF	$Line - Line text												//
//				$str  - Search String											//
//																				//
//	Return	F	None															//
//------------------------------------------------------------------------------//
function ShowLineText(lno, $Line, $str)
{
	define len = _StrLen($Line);
	define stl = _StrLen($str);
	define ix, ixStr;
	define $part, $left;

	_Pd(lno, 5);
	_Pd(" : ");

	for ($part = $Line;
		 (ixStr = _StrStr($part, $str)) != -1;
		 $part = $_StrPart($part, ix, len - ix)) {

		if (ixStr != 0) $left = $_StrPart($part, 0, ixStr);
		else			$left = "";
		_Print($left, "\x1B[31m", $str, "\x1B[0m");
		ix = ixStr + stl;
	}
	_Print($part);
}
