WriteINILargeStr: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
No edit summary
 
 
(3 intermediate revisions by the same user not shown)
Line 7: Line 7:
<highlight-nsis>
<highlight-nsis>
${WriteINILargeStr_Open} "$INSTDIR\file.ini" "Section" "Key"
${WriteINILargeStr_Open} "$INSTDIR\file.ini" "Section" "Key"
Push " Stuart!"
${WriteINILargeStr} "Hello"
Push " my name is"
${WriteINILargeStr} " my name is"
Push "Hello"
${WriteINILargeStr} " Stuart!"
${WriteINILargeStr_Close}
${WriteINILargeStr_Close}
</highlight-nsis>
</highlight-nsis>
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:
This will write to file.ini with the following data:
 
<p>'''[Section]'''<br />
'''[Section]'''<br />
'''Key=Hello my name is Stuart!'''</p>
'''Key=Hello my name is Stuart!'''


==The Function==
==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:<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 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 $R9
  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 $R8
  Pop $R7
  Pop $R7
Line 59: Line 97:
  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
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 4
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]]

Latest revision as of 20:37, 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"
${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