Unicode FileRead

From NSIS Wiki
Jump to navigationJump to search
Author: lob (talk, contrib)


Description

Read Unicode Files. The Function is very similar to the built in FileRead function. It reads in an ASCII Unicode text files by line. Note that multi byte characters are not read correctly and characters with more than two bytes throw the whole line if not the whole file off.

The Function

Function "FileReadUnicode"
	Push $0
	Exch 
	Pop $0 ; handle
	Push $1
	Push $2
	Push $3
	Push $4
 
	IntOp $4 0 + 0
	IntOp $3 0 + 0
	StrCpy $1 ""
	SetErrorLevel 0
read:	
	FileReadByte $0 $2
	IntCmp $4 0 0 skip skip
	IntCmp $2 0x0a skip 0 0 
	IntCmp $2 0x0d readsecondhalf 0 0 
	IntCmp $2 0 done 0 0
	IntFmt $2 "%c" $2
	StrCpy $1 "$1$2"
skip:	
	IntOp $4 $4 + 1
	IntOp $4 $4 % 2
	Goto read
readsecondhalf:	
	FileReadByte $0 $2
done:	
	Pop $4
	Pop $3
	Pop $2
	Push $1
	Exch 
	Pop $1
	Exch 
	Pop $0
FunctionEnd

Sample Code

	FileOpen $R0 "c:\unicode.txt" r
	IfErrors done
read:	
	Push $R0
	Call FileReadUnicode
	Pop $R1
	IfErrors done
	MessageBox MB_OK "$R1" 
	Goto read
done: