-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileManipulation.nsh
More file actions
377 lines (344 loc) · 9.55 KB
/
FileManipulation.nsh
File metadata and controls
377 lines (344 loc) · 9.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
* This file is part of YUMI
*
* YUMI is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* any later version.
*
* YUMI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with YUMI. If not, see <http://www.gnu.org/licenses/>.
*/
; -------- Configuration and Text File Manipulation Stuff! --------
Function FindFiles ; <- FindFiles function written by KiCHik http://nsis.sourceforge.net/Search_For_a_File
Exch $R5 # callback function
Exch
Exch $R4 # file name
Exch 2
Exch $R0 # directory
Push $R1
Push $R2
Push $R3
Push $R6
Push $R0 # first dir to search
StrCpy $R3 1
nextDir:
Pop $R0
IntOp $R3 $R3 - 1
ClearErrors
FindFirst $R1 $R2 "$R0\*.*"
nextFile:
StrCmp $R2 "." gotoNextFile
StrCmp $R2 ".." gotoNextFile
StrCmp $R2 $R4 0 isDir
Push "$R0\$R2"
Call $R5
Pop $R6
StrCmp $R6 "stop" 0 isDir
loop:
StrCmp $R3 0 done
Pop $R0
IntOp $R3 $R3 - 1
Goto loop
isDir:
IfFileExists "$R0\$R2\*.*" 0 gotoNextFile
IntOp $R3 $R3 + 1
Push "$R0\$R2"
gotoNextFile:
FindNext $R1 $R2
IfErrors 0 nextFile
done:
FindClose $R1
StrCmp $R3 0 0 nextDir
Pop $R6
Pop $R3
Pop $R2
Pop $R1
Pop $R0
Pop $R5
Pop $R4
FunctionEnd
!macro CallFindFiles DIR FILE CBFUNC
Push "${DIR}"
StrCpy $SearchDir "${DIR}"
Push "${FILE}"
StrCpy $SearchFile "${FILE}"
Push $0
GetFunctionAddress $0 "${CBFUNC}"
Exch $0
Call FindFiles
!macroend
Function CBFUNC
Exch $0
DetailPrint "Found $SearchFile at $0"
CopyFiles "$PLUGINSDIR\$SearchFile" "$0"
Pop $0
Push "stop"
FunctionEnd
Function WriteToFile ; <- WriteToFile Function originally written by Afrow UK http://nsis.sourceforge.net/Simple_write_text_to_file, and modified by Lance to populate *.cfg file with the distro user installed!
Exch $R0 ;file to write to
Exch
Exch $1 ;text to write
FileOpen $R0 '$BootDir\multiboot\menu\$Config2Use' a
FileSeek $R0 0 END
FileWrite $R0 '$\r$\n$1$\r$\n'
FileClose $R0
Pop $1
Pop $R0
FunctionEnd
!macro WriteToFile String File
Push "${String}"
Push "${File}"
Call WriteToFile
${LineFind} "$BootDir\multiboot\menu\$Config2Use" "$BootDir\multiboot\menu\$Config2Use" "1:-1" "DeleteEmptyLine" ; Remove any left over empty lines
!macroend
!define WriteToFile "!insertmacro WriteToFile"
Function WriteToSysFile ; Write entry to syslinux.cfg
Exch $R0 ;file to write to
Exch
Exch $1 ;text to write
FileOpen $R0 '$BootDir\multiboot\syslinux.cfg' a
FileSeek $R0 0 END
FileWrite $R0 '$\r$\n$1$\r$\n'
FileClose $R0
Pop $1
Pop $R0
FunctionEnd
!macro WriteToSysFile String File
Push "${String}"
Push "${File}"
Call WriteToSysFile
!macroend
!define WriteToSysFile "!insertmacro WriteToSysFile"
Function InstalledList ; Creates a list of installed distros in the multiboot folder on the USB drive (So we can uninstall the distros later)
${IfNot} ${FileExists} "$BootDir\multiboot\$JustISOName\*.*" ; If the installation directory exists user must be reinstalling the same distro, so we won't add a removal entry.
Exch $R0 ;file to write to
Exch
Exch $1 ;text to write
${If} ${FileExists} "$BootDir\multiboot\Installed.txt"
FileOpen $R0 '$BootDir\multiboot\Installed.txt' a
FileSeek $R0 0 END
FileWrite $R0 '$\r$\n$1' ; add subsequent entry on a new line
${Else}
FileOpen $R0 '$BootDir\multiboot\Installed.txt' a
FileSeek $R0 0 END
FileWrite $R0 '$1' ; add first entry without a new line
${EndIf}
FileClose $R0
Pop $1
Pop $R0
${EndIf}
FunctionEnd
!macro InstalledList String File
Push "${String}"
Push "${File}"
Call InstalledList
!macroend
!define InstalledList "!insertmacro InstalledList"
Function Trim ; Remove leading and trailing whitespace from string - function by Iceman_K http://nsis.sourceforge.net/Remove_leading_and_trailing_whitespaces_from_a_string edited for YUMI by Lance of Pendrivelinux.com
Exch $R1 ; Original string
Push $R2
Loop:
StrCpy $R2 "$R1" 1
StrCmp "$R2" " " TrimLeft
StrCmp "$R2" "$\r" TrimLeft
StrCmp "$R2" "$\n" TrimLeft
StrCmp "$R2" "$\t" TrimLeft
GoTo Loop2
TrimLeft:
StrCpy $R1 "$R1" "" 1
Goto Loop
Loop2:
StrCpy $R2 "$R1" 1 -1
StrCmp "$R2" " " TrimRight
StrCmp "$R2" "$\r" TrimRight
StrCmp "$R2" "$\n" TrimRight
StrCmp "$R2" "$\t" TrimRight
GoTo Done
TrimRight:
StrCpy $R1 "$R1" -1
Goto Loop2
Done:
Pop $R2
Exch $R1
FunctionEnd
!macro Trim TrimmedString OriginalString
Push "${OriginalString}"
Call Trim
Pop "${TrimmedString}"
!macroend
!define Trim "!insertmacro Trim"
Function RemovalList ; Lists the distros installed on the select drive.
${NSD_SetText} $LinuxDistroSelection "Step 2: Select a Distribution to remove from $DestDisk"
${If} ${FileExists} "$BootDir\multiboot\Installed.txt" ; Are there distributions on the select drive?
ClearErrors
FileOpen $0 $BootDir\multiboot\Installed.txt r
loop:
FileRead $0 $1
IfErrors done
StrCpy $DistroName $1
${Trim} "$DistroName" "$DistroName" ; Remove spaces, newlines, and carriage return
${NSD_CB_AddString} $Distro "$DistroName" ; Add DistroName to the listbox of removable distros ; was ${NSD_LB_AddString} $Distro "$DistroName" ; Enable for DropBox
Goto loop
done:
FileClose $0
${Else}
;Call SetISOFileName
${EndIf}
FunctionEnd
!include "TextFunc.nsh" ; TextFunc.nsh required for the following DeleteInstall function
Function DeleteInstall ; Deletes Select Entry from Installed.txt
StrLen $0 "$DistroName"
StrCpy $1 "$R9" $0
StrCmp $1 "$DistroName" 0 End
StrCpy $R9 ""
End:
Push $0
FunctionEnd
Function DeleteEmptyLine ; Deletes empty line from Installed.txt
StrLen $0 "$\r$\n"
StrCpy $1 "$R9" $0
StrCmp $1 "$\r$\n" 0 End
StrCpy $R9 ""
End:
Push $0
FunctionEnd
!define StrRep "!insertmacro StrRep"
!macro StrRep ResultVar String SubString RepString
Push "${String}"
Push "${SubString}"
Push "${RepString}"
Call StrRep
Pop "${ResultVar}"
!macroend
Function StrRep ;http://nsis.sourceforge.net/mediawiki/index.php?title=StrRep&direction=next&oldid=4565
/*After this point:
------------------------------------------
$R0 = RepString (input)
$R1 = SubString (input)
$R2 = String (input)
$R3 = RepStrLen (temp)
$R4 = SubStrLen (temp)
$R5 = StrLen (temp)
$R6 = StartCharPos (temp)
$R7 = TempStrL (temp)
$R8 = TempStrR (temp)*/
;Get input from user
Exch $R0
Exch
Exch $R1
Exch
Exch 2
Exch $R2
Push $R3
Push $R4
Push $R5
Push $R6
Push $R7
Push $R8
;Return "String" if "SubString" is ""
${IfThen} $R1 == "" ${|} Goto Done ${|}
;Get "RepString", "String" and "SubString" length
StrLen $R3 $R0
StrLen $R4 $R1
StrLen $R5 $R2
;Start "StartCharPos" counter
StrCpy $R6 0
;Loop until "SubString" is found or "String" reaches its end
${Do}
;Remove everything before and after the searched part ("TempStrL")
StrCpy $R7 $R2 $R4 $R6
;Compare "TempStrL" with "SubString"
${If} $R7 == $R1
;Split "String" to replace the string wanted
StrCpy $R7 $R2 $R6 ;TempStrL
;Calc: "StartCharPos" + "SubStrLen" = EndCharPos
IntOp $R8 $R6 + $R4
StrCpy $R8 $R2 "" $R8 ;TempStrR
;Insert the new string between the two separated parts of "String"
StrCpy $R2 $R7$R0$R8
;Now calculate the new "StrLen" and "StartCharPos"
StrLen $R5 $R2
IntOp $R6 $R6 + $R3
${Continue}
${EndIf}
;If not "SubString", this could be "String" end
${IfThen} $R6 >= $R5 ${|} ${ExitDo} ${|}
;If not, continue the loop
IntOp $R6 $R6 + 1
${Loop}
Done:
;Return output to user
StrCpy $R0 $R2
/*After this point:
------------------------------------------
$R0 = ResultVar (output)*/
Pop $R8
Pop $R7
Pop $R6
Pop $R5
Pop $R4
Pop $R3
Pop $R2
Pop $R1
Exch $R0
FunctionEnd
Function ddProgress
${Do}
Sleep 1
ReadEnvStr $COMSPEC COMSPEC
nsExec::Exec '"$COMSPEC" /C "copy" $PLUGINSDIR\ddlog.txt $PLUGINSDIR\ddlog2.txt'
Push "$PLUGINSDIR\ddlog2.txt"
Call LineCount
Pop $R0
Push $R0 ;line number to read from
Push "$PLUGINSDIR\ddlog2.txt" ;text file to read
Call ReadFileLine
Pop $0 ;output string (read from file.txt)
StrCpy $PERCENT "$0"
GetDlgItem $2 $1 1030
SendMessage $2 ${WM_SETTEXT} 0 "STR:$PERCENT"
${LoopUntil} $R0 >= "$Casper"
FunctionEnd
Function LineCount ; http://nsis.sourceforge.net/Get_number_of_lines_in_text_file
Exch $R0
Push $R1
Push $R2
FileOpen $R0 $R0 r
loop:
ClearErrors
FileRead $R0 $R1
IfErrors +3
IntOp $R2 $R2 + 1
Goto loop
FileClose $R0
StrCpy $R0 $R2
Pop $R2
Pop $R1
Exch $R0
FunctionEnd
Function ReadFileLine ; http://nsis.sourceforge.net/Read_from_text_file_line_number
Exch $0 ;file
Exch
Exch $1 ;line number
Push $2
Push $3
FileOpen $2 $0 r
StrCpy $3 0
Loop:
IntOp $3 $3 + 1
ClearErrors
FileRead $2 $0
IfErrors +2
StrCmp $3 $1 0 loop
FileClose $2
Pop $3
Pop $2
Pop $1
Exch $0
FunctionEnd