Ticket #1900: StringRegExpGUI_.au3

File StringRegExpGUI_.au3, 9.3 KB (added by FichteFoll <fichtefoll2@…>, 11 years ago)

StringRegExp modified by FichteFoll

Line 
1; Notes by FichteFoll ###########################
2; -   Fixed: $h_Out showing 1 randomly; now shows nothing if nothing found
3; -   Fixed: $s_lgth can be higher than 6 using StringLen and will not be 2 if array has 10 indexes (having 9 as last)
4; -   Fixed: WaitMessage had fixed height and width, multiple lines or too long lines would not be shown. Now generates them dynamically
5; - Changed: WaitMessage() or WaitMessage('') disables the SplashText (just as ToolTip); required _Max (Misc.au3)
6; - Changed: Using global var $h_Wnd instead of window title for WinGetPos
7; - Changed: Seperator character ($c_Sep) is 0x0B instead of "|" because patterns were splitted while writing into the combo control
8; - Changed: Tries to evaluate helpfilepath using AutoIt's install dir (RegRead64 from "AutoUpdateIt.au3") if @AutoItExe failed
9;            Also renamed $helppath to $autoitpath
10; - Changed: Removed X and Y parameters for GUICreate because it's positioned in the middle of the screen by default
11; -   Added: $h_Err will be colored red if @error <> 0
12; ###############################################
13
14#include <GUIConstantsEx.au3>
15#include <EditConstants.au3>
16#include <WindowsConstants.au3>
17
18Opt('MustDeclareVars', 1)
19
20Global $s_Pattern = "(.*)", $h_Wnd, $c_Sep = Chr(11) ; 0x0B = VT
21Global $h_Radio_0, $h_Radio_1, $h_Radio_3
22
23Opt('GUIDataSeparatorChar', $c_Sep)
24Example()
25
26Func Example()
27
28        Local $initialDir = "C:\", $h_Indummy, $h_tab, $h_tabitem1, $h_In1, $msg
29        Local $h_tabitem2, $h_Brwse, $h_fileIn, $h_In2, $h_Out, $h_Pattern, $v_Reg_Old
30        Local $h_Pattern_add, $h_Pattern_del, $h_test, $h_Err, $h_Ext, $h_Help, $h_Exit
31        Local $filepath, $str2, $autoitpath, $h_In
32
33        Readini()
34        $h_Wnd = GUICreate("StringRegExp Original Design GUI -by w0uter, modified Steve8tch and FichteFoll", 550, 570);, (@DesktopWidth - 550) / 2, (@DesktopHeight - 570) / 2)
35        GUICtrlCreateGroup("The pattern   -  $ptn", 10, 210, 530, 60)
36        GUICtrlCreateGroup("Output", 140, 280, 400, 280)
37        GUICtrlCreateGroup("Return", 10, 280, 120, 100)
38        GUICtrlCreateGroup("@Error   @Extended", 10, 390, 120, 50)
39        GUICtrlCreateGroup("StringRegExp Help", 10, 450, 120, 50)
40        $h_Radio_0 = GUICtrlCreateRadio("True/False", 20, 300, 100, 20)
41        $h_Radio_1 = GUICtrlCreateRadio("Array with the text", 20, 321, 100, 27)
42        $h_Radio_3 = GUICtrlCreateRadio("Array of all results", 20, 350, 100, 20)
43        GUICtrlSetState($h_Radio_3, $GUI_CHECKED)
44        $h_Indummy = GUICtrlCreateEdit("", 1020, 1040, 510, 150, BitOR($ES_WANTRETURN, $WS_VSCROLL, $WS_HSCROLL, $ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
45        $h_tab = GUICtrlCreateTab(10, 10, 530, 190)
46        $h_tabitem1 = GUICtrlCreateTabItem("Copy and Paste the text to check - $str")
47        $h_In1 = GUICtrlCreateEdit("", 20, 40, 510, 150, BitOR($ES_WANTRETURN, $WS_VSCROLL, $WS_HSCROLL, $ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
48
49        $h_tabitem2 = GUICtrlCreateTabItem("Load text from File")
50        $h_Brwse = GUICtrlCreateButton("Browse for file", 20, 40, 100, 20)
51        $h_fileIn = GUICtrlCreateEdit("", 130, 40, 400, 20, BitOR($ES_WANTRETURN, $WS_HSCROLL, $ES_AUTOHSCROLL))
52        $h_In2 = GUICtrlCreateEdit("", 20, 70, 510, 120, BitOR($ES_WANTRETURN, $WS_VSCROLL, $WS_HSCROLL, $ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
53
54        GUICtrlCreateTabItem("");
55        $h_Out = GUICtrlCreateEdit("", 150, 296, 380, 262, BitOR($ES_WANTRETURN, $WS_VSCROLL, $WS_HSCROLL, $ES_AUTOVSCROLL, $ES_AUTOHSCROLL))
56        $h_Pattern = GUICtrlCreateCombo("", 70, 230, 430, 30)
57        GUICtrlSetFont($h_Pattern, 14)
58        GUICtrlSetData($h_Pattern, $s_Pattern, "(.*)")
59        $h_Pattern_add = GUICtrlCreateButton("Add", 504, 225, 30, 18)
60        $h_Pattern_del = GUICtrlCreateButton("Del", 504, 245, 30, 18)
61        $h_test = GUICtrlCreateButton("Test", 20, 235, 40, 20)
62        $h_Err = GUICtrlCreateInput("", 20, 410, 40, 20, $ES_READONLY)
63        $h_Ext = GUICtrlCreateInput("", 70, 410, 50, 20, $ES_READONLY)
64        $h_Help = GUICtrlCreateButton("HELP", 20, 468, 100, 24)
65        $h_Exit = GUICtrlCreateButton("Exit", 10, 510, 120, 50)
66        $v_Reg_Old = 0
67        $h_In = $h_In1
68        GUISetState()
69        While 1
70                $msg = GUIGetMsg()
71                Select
72                        Case $msg = $GUI_EVENT_CLOSE
73                                Exit
74                        Case $msg = $h_test
75                                _Valid($h_Out, $h_Pattern, $h_Err, $h_Ext, $v_Reg_Old, $h_In)
76                        Case $msg = $h_Exit
77                                Exit
78                        Case $msg = $h_Brwse
79                                $filepath = FileOpenDialog("Select text file to test", $initialDir, "Text files (*.*)", 1)
80                                $initialDir = StringTrimRight($filepath, StringInStr($filepath, "\", "-1"))
81                                GUICtrlSetData($h_fileIn, $filepath)
82                                WaitMessage("Loading file..")
83                                $str2 = FileRead($filepath)
84                                WaitMessage("File loaded.." & @CRLF & "updating display")
85                                GUICtrlSetData($h_In2, $str2)
86                                WaitMessage()
87                        Case $msg = $h_tab
88                                If GUICtrlRead($h_tab) = 0 Then
89                                        $h_In = $h_In1
90                                Else
91                                        $h_In = $h_In2
92                                EndIf
93                        Case $msg = $h_Pattern_add
94                                Pattern_Add($h_Pattern)
95
96                        Case $msg = $h_Pattern_del
97                                Pattern_del($h_Pattern)
98
99                        Case $msg = $h_Help
100                                $autoitpath = StringLeft(@AutoItExe, StringInStr(@AutoItExe, "\", 0, -1))
101                                Run($autoitpath & "Autoit3Help.exe StringRegExp")
102                                If @error = 1 Then
103                                        $autoitpath = RegRead64('HKLM\Software\AutoIt v3\AutoIt', 'InstallDir')
104                                        Run($autoitpath & "\Autoit3Help.exe StringRegExp")
105                                        If @error Then MsgBox(0, "error", "Cannot find help file - sorry")
106                                EndIf
107                        Case Else
108                                ;;
109                EndSelect
110        WEnd
111EndFunc   ;==>Example
112
113Func _Valid(ByRef $h_Out, ByRef $h_Pattern, ByRef $h_Err, ByRef $h_Ext, ByRef $v_Reg_Old, ByRef $h_In)
114        Local $v_Reg, $v_Check, $h_output, $x, $s_lgth
115        WaitMessage("Performing test..")
116        $v_Reg = StringRegExp(GUICtrlRead($h_In), GUICtrlRead($h_Pattern), _Option())
117        Dim $v_EE[2] = [@error, @extended]
118        If $v_EE[0] = 2 Then
119                GUICtrlSetColor($h_Pattern, 0xFF0000)
120        Else ; 1, 0
121                GUICtrlSetColor($h_Pattern, 0)
122        EndIf
123        GUICtrlSetData($h_Err, $v_EE[0])
124        GUICtrlSetData($h_Ext, $v_EE[1])
125
126        WaitMessage("Test complete.." & @CRLF & "updating display")
127
128        If $v_EE[0] = 0 Then
129                GUICtrlSetColor($h_Err, 0)
130                $v_Check = 0
131                If UBound($v_Reg) <> UBound($v_Reg_Old) Then
132                        $v_Check = 1
133                Else
134                        For $i = 0 To UBound($v_Reg) - 1
135                                If $v_Reg[$i] <> $v_Reg_Old[$i] Then $v_Check = 1
136                        Next
137                EndIf
138                If $v_Check = 1 Then
139                        GUICtrlSetData($h_Out, "")
140                        $h_output = ""
141                        $x = UBound($v_Reg)
142                        $s_lgth = StringLen(String($x - 1))
143
144                        If $x Then
145                                For $i = 0 To $x - 1
146                                        $h_output &= StringFormat("%0" & $s_lgth & "i", $i) & ' => ' & $v_Reg[$i] & @CRLF
147                                Next
148                                GUICtrlSetData($h_Out, $h_output)
149                        Else
150                                GUICtrlSetData($h_Out, $v_Reg)
151                        EndIf
152                EndIf
153        Else ; $v_EE[0] = 1 or 2
154                GUICtrlSetData($h_Out, "")
155                GUICtrlSetColor($h_Err, 0xFF0000)
156        EndIf
157
158        WaitMessage()
159        $v_Reg_Old = $v_Reg
160        StringRegExp('', Random(0x80000000, 0x7FFFFFFF), 1)
161EndFunc   ;==>_Valid
162
163Func _Option()
164        Switch $GUI_CHECKED
165                Case GUICtrlRead($h_Radio_0)
166                        Return 0
167                Case GUICtrlRead($h_Radio_1)
168                        Return 1
169                Case GUICtrlRead($h_Radio_3)
170                        Return 3
171        EndSwitch
172EndFunc   ;==>_Option
173
174Func Readini()
175        Local $h_x
176
177        If FileExists(@ScriptDir & "\StringRegExpGUIPattern.ini") = 0 Then
178                $h_x = FileOpen(@ScriptDir & "\StringRegExpGUIPattern.ini", 1)
179                FileWriteLine($h_x, "[do not delete the file - Patterns are listed below]")
180                FileWriteLine($h_x, "(.*)##~##")
181                FileClose($h_x)
182        Else
183                $s_Pattern = FileRead(@ScriptDir & "\StringRegExpGUIPattern.ini")
184                $s_Pattern = StringTrimLeft($s_Pattern, StringInStr($s_Pattern, @CRLF) + 1)
185                $s_Pattern = StringReplace($s_Pattern, "##~##" & @CRLF, $c_Sep)
186        EndIf
187EndFunc   ;==>Readini
188
189Func Pattern_del($h_Pattern)
190        Local $s_ini, $h_x
191
192        $s_ini = FileRead(@ScriptDir & "\StringRegExpGUIPattern.ini")
193        $h_x = FileOpen(@ScriptDir & "\StringRegExpGUIPattern.ini", 2)
194        If GUICtrlRead($h_Pattern) = "" Then
195                $s_ini = StringReplace($s_ini, "##~##" & @CRLF & "##~##", "##~##")
196                $s_ini = StringReplace($s_ini, @CRLF & @CRLF, @CRLF)
197        Else
198                $s_ini = StringReplace($s_ini, GUICtrlRead($h_Pattern) & "##~##", "")
199                $s_ini = StringReplace($s_ini, @CRLF & @CRLF, @CRLF)
200        EndIf
201        FileWrite($h_x, $s_ini)
202        FileClose($h_x)
203        Readini()
204        GUICtrlSetData($h_Pattern, $c_Sep & $s_Pattern, "(.*)")
205EndFunc   ;==>Pattern_del
206
207Func Pattern_Add($h_Pattern)
208        Local $h_x
209
210        $h_x = FileOpen(@ScriptDir & "\StringRegExpGUIPattern.ini", 1)
211        FileWriteLine($h_x, GUICtrlRead($h_Pattern) & "##~##")
212        FileClose($h_x)
213        Readini()
214        GUICtrlSetData($h_Pattern, "|" & $s_Pattern, GUICtrlRead($h_Pattern))
215EndFunc   ;==>Pattern_Add
216
217Func WaitMessage($s_txt = '')
218        If $s_txt = '' Then Return SplashOff()
219        Local $a_pos = WinGetPos($h_Wnd), $i_height, $i_width = 10, $a_Reg
220
221        $a_Reg = StringRegExp($s_txt, "(.*)", 3)
222        $i_height = UBound($a_Reg) * 13 + 20
223        For $i = 0 To UBound($a_Reg) - 1
224                $i_width = __Max($i_width, StringLen($a_Reg[$i]))
225        Next
226        $i_width = $i_width * 8 + 30
227        SplashTextOn("Please wait", $s_txt, $i_width, $i_height, $a_pos[0] + ($a_pos[2] - $i_width) / 2, $a_pos[1] + ($a_pos[3] - $i_height) / 2, 32)
228EndFunc   ;==>WaitMessage
229
230Func __Max($nNum1, $nNum2)
231        If $nNum1 > $nNum2 Then Return $nNum1
232        Return $nNum2
233EndFunc   ;==>__Max
234
235Func RegRead64($sKeyname, $sValue)
236                Local $res = RegRead($sKeyname, $sValue)
237                If @error And @AutoItX64 Then
238                        $sKeyname = StringReplace($sKeyname, "HKEY_LOCAL_MACHINE", "HKLM")
239                        $sKeyname = StringReplace($sKeyname, "HKLM\SOFTWARE\", "HKLM\SOFTWARE\Wow6432Node\")
240                        $res = RegRead($sKeyname, $sValue)
241                        If @error Then
242                                SetError(1)
243                                Return ""
244                        EndIf
245                EndIf
246
247        SetError(0)
248        Return $res
249EndFunc