WriteINILargeStr: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
Line 21: Line 21:
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:<br />''!include WriteINILargeStr.nsh''
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:<br />''!include WriteINILargeStr.nsh''
<highlight-nsis>
<highlight-nsis>
Function WriteINILargeStr
!define WriteINILargeStr_Open '!insertmacro WriteINILargeStr_Open'
!define WriteINILargeStr_Open '!insertmacro WriteINILargeStr_Open'
!macro WriteINILargeStr_Open File Section Key
!macro WriteINILargeStr_Open File Section Key


StrCpy $R6 "${File}"
!define g_UniqueID "${__LINE__}"
StrCpy $R7 "${Section}"
!define g_File    "${File}"
StrCpy $R8 "${Key}"
 
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}


Push $R0
  StrCmp $R4 1 Write${g_UniqueID}
Push $R1
  StrCpy $R5 $R3 $R6
Push $R2
  StrCmp $R7 -1 +4
Push $R3
  StrCmp $R5 "[${Section}]" 0 Write${g_UniqueID}
Push $R4
  StrCpy $R7 -1
Push $R5
  Goto Write${g_UniqueID}
Push $R6
  StrCpy $R5 $R3 $R8
Push $R7
  StrCmp $R5 "${Key}=" 0 Write${g_UniqueID}
Push $R8
Push $R9
Push $0


Push STOP
  FileWrite $R0 $R5
  Goto +3
  Write${g_UniqueID}:
  FileWrite $R0 $R3
  Goto Loop${g_UniqueID}


!macroend
!macroend


!define WriteINILargeStr '!insertmacro WriteINILargeStr'
!macro WriteINILargeStr Str
FileWrite $R0 "${Str}"
!macroend
!define WriteINILargeStr_Close '!insertmacro WriteINILargeStr_Close'
!define WriteINILargeStr_Close '!insertmacro WriteINILargeStr_Close'
!macro WriteINILargeStr_Close
!macro WriteINILargeStr_Close


  Call WriteINILargeStr
  FileWrite $R0 $\r$\n
  Pop $0
  StrCpy    $R4 1
  Pop $R9
 
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 $R8
  Pop $R7
  Pop $R7
Line 61: Line 99:
  Pop $R1
  Pop $R1
  Pop $R0
  Pop $R0
!undef g_File
!undef g_UniqueID


!macroend
!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
</highlight-nsis>
</highlight-nsis>


[[Category:String Functions]]
[[Category:String Functions]]

Revision as of 20:36, 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

!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