Get all section names of INI file: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
(Unicode fixes)
No edit summary
Line 57: Line 57:
</highlight-nsis>
</highlight-nsis>


== The Function Code==
== The Function Code (ANSI/Unicode) ==
If you are using Unicode NSIS, this code will require the input INI file to also be Unicode (with no BOM). If you wish to only use ANSI encoded INI files whether you build using NSIS or Unicode NSIS, see the alternative code further down.


<highlight-nsis>
<highlight-nsis>
Line 119: Line 120:
IntOp $5 $5 + 1
IntOp $5 $5 + 1
!endif
!endif
IntOp $3 $3 + $5
goto enumok
enumex:
System::Free $2
Pop $9
Pop $8
Pop $5
Pop $4
Pop $3
Pop $2
Pop $1
Pop $0
FunctionEnd</highlight-nsis>
== The Function Code (ANSI only) ==
<highlight-nsis>
!define GetSectionNames `!insertmacro GetSectionNamesCall`
!macro GetSectionNamesCall _FILE _FUNC
Push $0
Push `${_FILE}`
GetFunctionAddress $0 `${_FUNC}`
Push `$0`
Call GetSectionNames
Pop $0
!macroend
Function GetSectionNames
Exch $1
Exch
Exch $0
Exch
Push $2
Push $3
Push $4
Push $5
Push $8
Push $9
System::Call *(&m1024)i.r2
        StrCpy $3 $2
        System::Call "kernel32::GetPrivateProfileSectionNamesA(i, i, m)i (r3, 1024, r0).r4"
     
enumok:
        System::Call 'kernel32::lstrlen(m)i (i r3).r5'
StrCmp $5 '0' enumex
System::Call '*$3(&m1024 .r9)'
Push $0
Push $1
Push $2
Push $3
Push $4
Push $5
Push $8
Call $1
Pop $9
Pop $8
Pop $5
Pop $4
Pop $3
Pop $2
Pop $1
Pop $0
        StrCmp $9 'StopGetSectionNames' enumex
enumnext:
IntOp $5 $5 + 1
IntOp $3 $3 + $5
IntOp $3 $3 + $5
goto enumok
goto enumok

Revision as of 13:39, 22 April 2013

Author: nechai (talk, contrib)


Links

If a function is used without header, put the function code in your script before calling it.

The Function Description

Based on the method to loop over a buffer of null-terminated strings in function "GetDrives"
 
Loop over all available sections in an INI file.
 
Syntax:
${GetSectionNames} "File" "Function"
 
"File"     ; name of the initialization file
"Function" ; Callback function
 
Function "Function"
         ; $9    "section name"
         ; $R0-$R9  are not used (save data in them).
 
         ; ...
 
         Push $var    ; If $var="StopGetSectionNames" Then exit from function
FunctionEnd

Example1:

Section
	${GetSectionNames} "c:\test.ini" "Example1"
SectionEnd
 
Function Example1
	MessageBox MB_OK "$9"
 
	Push $0
FunctionEnd

Example2:

Section
	${GetSectionNames} "c:\test.ini" "Example2"
SectionEnd
 
Function Example2
	StrCmp $9 $R0 0 +3
	StrCpy $R1 $9
	StrCpy $0 StopGetSectionNames
 
	Push $0
FunctionEnd

The Function Code (ANSI/Unicode)

If you are using Unicode NSIS, this code will require the input INI file to also be Unicode (with no BOM). If you wish to only use ANSI encoded INI files whether you build using NSIS or Unicode NSIS, see the alternative code further down.

!define GetSectionNames `!insertmacro GetSectionNamesCall`
 
!macro GetSectionNamesCall _FILE _FUNC
	Push $0
	Push `${_FILE}`
	GetFunctionAddress $0 `${_FUNC}`
	Push `$0`
	Call GetSectionNames
	Pop $0
!macroend
 
Function GetSectionNames
	Exch $1
	Exch
	Exch $0
	Exch
	Push $2
	Push $3
	Push $4
	Push $5
	Push $8
	Push $9
 
	System::Call *(&t1024)i.r2
        StrCpy $3 $2
 
        System::Call "kernel32::GetPrivateProfileSectionNames(i, i, t)i (r3, 1024, r0).r4"
 
	enumok:
        System::Call 'kernel32::lstrlen(t)i (i r3).r5' ; (t) is here to trigger A/W detection
	StrCmp $5 '0' enumex
 
	System::Call '*$3(&t1024 .r9)'
 
	Push $0
	Push $1
	Push $2
	Push $3
	Push $4
	Push $5
	Push $8
	Call $1
	Pop $9
	Pop $8
	Pop $5
	Pop $4
	Pop $3
	Pop $2
	Pop $1
	Pop $0
        StrCmp $9 'StopGetSectionNames' enumex
 
	enumnext:
!if "${NSIS_CHAR_SIZE}" > 1
	IntOp $5 $5 * ${NSIS_CHAR_SIZE}
	IntOp $5 $5 + ${NSIS_CHAR_SIZE}
!else
	IntOp $5 $5 + 1
!endif
	IntOp $3 $3 + $5
	goto enumok
 
	enumex:
	System::Free $2
 
	Pop $9
	Pop $8
	Pop $5
	Pop $4
	Pop $3
	Pop $2
	Pop $1
	Pop $0
FunctionEnd

The Function Code (ANSI only)

!define GetSectionNames `!insertmacro GetSectionNamesCall`
 
!macro GetSectionNamesCall _FILE _FUNC
	Push $0
	Push `${_FILE}`
	GetFunctionAddress $0 `${_FUNC}`
	Push `$0`
	Call GetSectionNames
	Pop $0
!macroend
 
Function GetSectionNames
	Exch $1
	Exch
	Exch $0
	Exch
	Push $2
	Push $3
	Push $4
	Push $5
	Push $8
	Push $9
 
	System::Call *(&m1024)i.r2
        StrCpy $3 $2
 
        System::Call "kernel32::GetPrivateProfileSectionNamesA(i, i, m)i (r3, 1024, r0).r4"
 
	enumok:
        System::Call 'kernel32::lstrlen(m)i (i r3).r5'
	StrCmp $5 '0' enumex
 
	System::Call '*$3(&m1024 .r9)'
 
	Push $0
	Push $1
	Push $2
	Push $3
	Push $4
	Push $5
	Push $8
	Call $1
	Pop $9
	Pop $8
	Pop $5
	Pop $4
	Pop $3
	Pop $2
	Pop $1
	Pop $0
        StrCmp $9 'StopGetSectionNames' enumex
 
	enumnext:
	IntOp $5 $5 + 1
	IntOp $3 $3 + $5
	goto enumok
 
	enumex:
	System::Free $2
 
	Pop $9
	Pop $8
	Pop $5
	Pop $4
	Pop $3
	Pop $2
	Pop $1
	Pop $0
FunctionEnd