1; Function
2; FileReadFirstLine Function
3;   Read and returns the first line of a given file
4; Usage :
5;	Push File
6;   Call FileReadFirstLine
7;	Pop  FirstLine
8; Result :
9;   Return first line of the file, or a blank string when an error occured
10
11Function FileReadFirstLine
12;; store register values to stack
13Exch $R0 ;swap file url and R0
14Push $R1 ;filehandle of opened file R1
15Push $R2
16
17StrCpy $R1 0
18
19ClearErrors 		#get a clean start
20FileOpen $R1 $R0 r
21IfErrors Done
22
23FileRead $R1 $0
24
25;remove line endings and spaces
26CheckFinalChar:
27	StrCpy $R2 "$0" 1 -1		;put final char in $R2
28	StrCmp "$R2" " " TrimFinalChar
29	StrCmp "$R2" "$\r" TrimFinalChar
30	StrCmp "$R2" "$\n" TrimFinalChar
31	StrCmp "$R2" "$\t" TrimFinalChar
32	GoTo CloseFile
33TrimFinalChar:
34	StrCpy $0 "$0" -1			;trim final char
35	Goto CheckFinalChar
36
37CloseFile:
38FileClose $R1
39
40Done:
41
42; restore register values from stack
43Pop $R2
44Pop $R1
45Exch $R0 ; restore R0
46
47Push $0 ;put on stack whether searchstring was found
48FunctionEnd
49
50
51
52; Function
53; FileSearch Function
54;   Searches a file for the occurance of a specified search string
55; Usage :
56;	Push FileToSearch
57;   Push StringToSearch
58;   Call FileSearch
59;	Push StringFound
60; Result :
61;   Return 0 when search string was not found in file, or an error occured
62;	return 1 when the search string was found in the file
63
64Function FileSearch
65;;Delete /REBOOTOK '$ExeDir\Dissociation.log'
66
67;; store register values to stack
68Exch $R0 ;swap search string and R0
69Exch	 ;put input file on top of stack
70Exch $R1 ;swap input file with R1
71
72Push $R2 ;filehandle of opened file R1
73Push $R3 ;read line from file
74Push $R4 ;length of search string
75Push $R5 ;offset in string R3
76Push $R6 ;temp string in R3 that contains R4 bytes
77
78StrCpy $0 0 		#by default, set to 0 (not found)
79StrLen $R4 $R0
80
81
82ClearErrors 		#get a clean start
83FileOpen $R2 $R1 r
84IfErrors Done
85
86ReadNextLine:
87  ClearErrors
88  FileRead $R2 $R3
89  IfErrors DoneRead
90  StrCpy $R5 0					#reset LineOffset to zero
91
92  SearchLine:
93    StrCpy $R6 $R3 $R4 $R5
94	StrCmp $R6 "" ReadNextLine 0	#read next line if R6 is empty
95	IntOp $R5 $R5 + 1				#increment offset
96
97	;check if Line with R5 offset is the search string
98    StrCmp $R6 $R0 0 SearchLine	#go back to SearchLine if this was not the string we're looking for
99    StrCpy $0 1 				#searchstring was found
100
101DoneRead:
102  FileClose $R2
103Done:
104
105 ;; restore register values from stack
106Pop $R6
107Pop $R5
108Pop $R4
109Pop $R3
110Pop $R2
111Exch $R1
112Exch
113Exch $R0
114
115Push $0 ;put on stack whether searchstring was found
116FunctionEnd
117
118
119; Macros
120!macro SearchStringInFile FOUND SEARCH_STRING FILE_TO_SEARCH
121  Push `${FILE_TO_SEARCH}`
122  Push `${SEARCH_STRING}`
123  Call FileSearch
124  Pop `${FOUND}`
125!macroend
126
127!macro GetFirstLineOfFile FILE_TO_READ FIRSTLINE
128  Push `${FILE_TO_READ}`
129  Call FileReadFirstLine
130  Pop `${FIRSTLINE}`
131!macroend