Wednesday, March 12, 2008

4test - Search and Replace

One project required to replace certain tags at every build. I written this function SearchAndReplaceInFile. This code snippet to search and replace strings in a file.

Code:



[+] public void SearchAndReplaceInFile (STRING sFileInput, STRING sFileOutput, STRING sSearch, STRING sReplace )
HFILE hInFile, hOutFile
STRING sTemp, sLine, sValue
INTEGER iPos1, iPos2

hInFile = FileOpen (sFileInput,FM_READ)
hOutFile = FileOpen (sFileOutput, FM_WRITE)

[-] while (FileReadLine (hInFile, sLine))
sTemp = sLine
[-] if !(Trim(sSearch) == Trim (sReplace))
ReadLine:
iPos1 = StrPos (sSearch,sTemp)
//Print ("Position {sTemp} - {sSearch} : {iPos1}")
[-] if (iPos1 > 0)
sValue = StrTran (sTemp, sSearch, sReplace)
//Print ("got value: {sValue}")
sTemp = sValue
goto ReadLine
FileWriteLine(hOutFile,sTemp)
FileClose (hInFile)
FileClose (hOutFile)

No comments: