WriteINILargeStr: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
No edit summary
 
Line 40: Line 40:
Push $R8
Push $R8
Push $R9
Push $R9
Push $0


Push STOP
Push STOP
Line 49: Line 50:


  Call WriteINILargeStr
  Call WriteINILargeStr
Pop $0
  Pop $R9
  Pop $R9
  Pop $R8
  Pop $R8
Line 71: Line 73:
StrLen $R9 $R7
StrLen $R9 $R7
IntOp $R9 $R9 + 2
IntOp $R9 $R9 + 2
StrLen $0 $R8


Loop:
Loop:
Line 83: Line 86:
  StrCpy $R9 -1
  StrCpy $R9 -1
  Goto Write
  Goto Write
  StrCpy $R5 $R3 4
  StrCpy $R5 $R3 $0
  StrCmp $R5 $R8= 0 Write
  StrCmp $R5 $R8= 0 Write



Revision as of 19:48, 1 March 2006

Author: Afrow UK (talk, contrib)


Description

This function is used to write a large amount of data to an INI file. It by-passes NSIS_MAX_STRLEN (1024 by default, or 8192 when using the NSIS special build) by writing pieces of data individually rather than all at once as one sring.

Usage

${WriteINILargeStr_Open} "$INSTDIR\file.ini" "Section" "Key"
Push " Stuart!"
Push " my name is"
Push "Hello"
${WriteINILargeStr_Close}

The data is written into the file in seperate pieces (per Push instruction) and must be Pushed in reverse order to how they must be written to the file (due to stack function limitations). This will write to file.ini with the following data:

[Section]
Key=Hello my name is Stuart!

The Function

Copy the following code into Notepad and save it as WriteINILargeStr.nsh under Include. Make sure you select All files (*.*) as the Save as type first. You can then use the function after using:
!include WriteINILargeStr.nsh

Function WriteINILargeStr
 
!define WriteINILargeStr_Open '!insertmacro WriteINILargeStr_Open'
!macro WriteINILargeStr_Open File Section Key
 
StrCpy $R6 "${File}"
StrCpy $R7 "${Section}"
StrCpy $R8 "${Key}"
 
Push $R0
Push $R1
Push $R2
Push $R3
Push $R4
Push $R5
Push $R6
Push $R7
Push $R8
Push $R9
Push $0
 
Push STOP
 
!macroend
 
!define WriteINILargeStr_Close '!insertmacro WriteINILargeStr_Close'
!macro WriteINILargeStr_Close
 
 Call WriteINILargeStr
 Pop $0
 Pop $R9
 Pop $R8
 Pop $R7
 Pop $R6
 Pop $R5
 Pop $R4
 Pop $R3
 Pop $R2
 Pop $R1
 Pop $R0
 
!macroend
 
WriteINIStr $R6 $R7 $R8 ""
 
GetTempFileName $R1
FileOpen $R0 $R1 w
FileOpen $R2 $R6 r
StrCpy $R4 0
 
StrLen $R9 $R7
IntOp $R9 $R9 + 2
StrLen $0 $R8
 
Loop:
ClearErrors
 FileRead $R2 $R3
IfErrors Done
 
 StrCmp $R4 1 Write
 StrCpy $R5 $R3 $R9
 StrCmp $R9 -1 +4
 StrCmp $R5 [$R7] 0 Write
 StrCpy $R9 -1
 Goto Write
 StrCpy $R5 $R3 $0
 StrCmp $R5 $R8= 0 Write
 
  FileWrite $R0 $R5
  Goto +3
  Write:
  FileWrite $R0 $R3
  Goto Loop
 
 Pop $R5
 StrCmp $R5 STOP +3
 FileWrite $R0 $R5
 Goto -3
 
 FileWrite $R0 $\r$\n
 StrCpy    $R4 1
 
Goto Loop
Done:
FileClose $R2
FileClose $R0
 
SetDetailsPrint none
Delete /rebootok $R6
Rename /rebootok $R1 $R6
SetDetailsPrint both
 
FunctionEnd