Unicode FileRead: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Adding new author and category links.)
(Supported saving and restoring $0 register variable.)
 
Line 10: Line 10:
<highlight-nsis>
<highlight-nsis>
Function "FileReadUnicode"
Function "FileReadUnicode"
Push $0
Exch
Pop $0 ; handle
Pop $0 ; handle
Push $1
Push $1
Line 15: Line 17:
Push $3
Push $3
Push $4
Push $4
 
IntOp $4 0 + 0
IntOp $4 0 + 0
IntOp $3 0 + 0
IntOp $3 0 + 0
Line 41: Line 43:
Exch  
Exch  
Pop $1
Pop $1
Exch
Pop $0
FunctionEnd
FunctionEnd
</highlight-nsis>
</highlight-nsis>

Latest revision as of 15:39, 6 October 2010

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: