WriteINILargeStr

From NSIS Wiki
Jump to navigationJump to search
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"
${WriteINILargeStr} "Hello"
${WriteINILargeStr} " my name is"
${WriteINILargeStr} " Stuart!"
${WriteINILargeStr_Close}

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

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