Jump to content

Replace the GUIConstants include file


GEOSoft
 Share

Recommended Posts

Try this if you are like me and don't like using include files.

#cs
   AutoIt Version:          3.0.103.0 (Beta)
   Language:                English (US)
   Platform:                    All
   Author:                      GEOSoft ©
   Script Function:         Replace GUIConstant strings with values

********* Note>> The $AUTOIT_PATH refers to a key that you probably don't have.  I'm running
parallel installations of AutoIt3 and my installer for the beta version creates the key.
You can either change the line to read the appropriate key for your system or just
place the GUIConstants.au3 or GUIConstants.au3.txt file in the same folder as this script. (Simplest)
#ce

Opt ("TrayIconHide", 1)
Opt("TrayIconDebug", 0)
$AUTOIT_PATH = RegRead('HKLM\Software\HiddenSoft\AutoIt3_Beta', 'InstallDir') & 'Include'
If @Error Then $AUTOIT_PATH = @ScriptDir
$S_NAME = ''
$SLINE_NUM = ''
$S_NAME = ''
$SLINE_NUM = ''
$KEY = ''
$VALUE = ''
$TTL = 'String to Value'
$CONSTANTS_FILE = $AUTOIT_PATH & '\GUIConstants.au3'
$EXT = 4
If FileExists($CONSTANTS_FILE) <> 1 Then
   $CONSTANTS_FILE = $AUTOIT_PATH & '\GUIConstants.au3.txt'
   $EXT = 8
   If FileExists($CONSTANTS_FILE) <> 1 Then Error('locate the GUIConstants')
EndIf
$INI_FILE = @ScriptDir & '\GUIConstants.ini'

#cs
   ************************************************
   <Compare File Dates and if the $Constants_FILE  is newer than the $INI_FILE write a new INI>
   < If the $INI_FILE file does not exist then create it. >
   ************************************************
#ce

$DATE1 = FileGetTime($CONSTANTS_FILE)
$DATE1 = StringRight($DATE1[0], 1) & $DATE1[1] & $DATE1[2]
$DATE2 = FileGetTime($INI_FILE)
If @Error = 1 Then
   $DATE2 = @Error
Else
   $DATE2 = StringRight($DATE2[0], 1) & $DATE2[1] & $DATE2[2]
EndIf
GLOBAL $TEMPFILE = ''
GLOBAL $ORIG_FILE = ''
Global $W_File = ''
Global $T_File = ''
If FileExists($INI_FILE) <> 1 OR $DATE1 > $DATE2 Then Convert_2_INI()
Set_Files()
$I_FILE = FileOpen($INI_FILE, 0)
Get_Sections()
Read_Data()
FileClose($I_FILE)
Fix_File()
FileCopy($ORIG_FILE, StringReplace($ORIG_FILE, '.au3', '.bak'), 1); <== create a backup file
FileCopy($TEMPFILE, $ORIG_FILE, 1)
FileDelete($TEMPFILE)
MsgBox(4096, $TTL, $TTL & ' has finished replacing the GUI Constants strings to actual values and has removed the line ''#Include GUIConstants,au3" if it existed.')
Exit

Func Set_Files(); <== Open the original file for reading and the temporary file for writing
   
   If $CMDLINE[0] = 1 Then
      $ORIG_FILE = FileGetLongName($CMDLINE[1])
   Else
      While NOT FileExists($ORIG_FILE) OR StringRight($ORIG_FILE, 4) <> '.au3'
         $ORIG_FILE = FileGetLongName(FileOpenDialog( "What file do you want to change?", '', "AutoIt3 Script(*.au3)", 1))
         If @Error Then Exit
      Wend

      $TEMPFILE = StringReplace($ORIG_FILE, StringRight($ORIG_FILE, 4), '.tmp')
   EndIf
   $T_FILE = FileOpen($ORIG_FILE, 0)
   $W_FILE = FileOpen($TEMPFILE, 2)
EndFunc  ;<==> Set_Files()

Func Fix_File()

   While 1
      Global $LINE = FileReadLine($T_FILE)
      If @Error = -1 Then ExitLoop
      If StringInStr($LINE, 'GUIConstants.au3') > 0 Then ContinueLoop
      If StringInStr($LINE, '_') > 0 AND StringInStr($LINE, '$') > 0 Then
         For$N = $KEY[0] To 1 Step - 1
            If StringInStr($LINE, $KEY[$N]) Then $LINE = StringReplace($LINE, $KEY[$N], $VALUE[$N])
         Next
      EndIf
   FileWriteLine($W_FILE, $LINE)
   Wend

FileClose($T_FILE)
FileClose($W_FILE)
EndFunc  ;<==> Fix_File()

Func Get_Sections() ; <== Read the INI File Sections and their respective line numbers to arrays.

   While 1
      LOCAL $LINE = FileReadLine($I_FILE)
      If @Error = -1 Then ExitLoop
      LOCAL $LINE_NUM = $LINE_NUM + 1
      If StringLeft($LINE, 1) = '[' Then
         $NAME = StringMid($LINE, 2, StringLen($LINE) - 2)
         $S_NAME = $S_NAME & $NAME & '>'
         $SLINE_NUM = $SLINE_NUM & $LINE_NUM & '/'
      EndIf
   Wend

   $S_NAME = StringSplit(StringTrimRight($S_NAME, 1), '>')
   $SLINE_NUM = StringSplit(StringTrimRight($SLINE_NUM, 1), '/')
EndFunc  ;<==> Get_Sections()

Func Read_Data(); <== Get the key names and their values
   For $I = 1 To $SLINE_NUM[0]
      LOCAL $DATA_COUNT = $SLINE_NUM[$I] + 1

      While 1
         GLOBAL $READ_SEC = FileReadLine($I_FILE, $DATA_COUNT)
         If $READ_SEC = '' Then ExitLoop
         $POS = StringInStr($READ_SEC, '=')
         $KEY = $KEY & StringLeft($READ_SEC, $POS - 1) & '/'
         $VALUE = $VALUE & StringMid($READ_SEC, $POS + 1) & '/'
         $DATA_COUNT = $DATA_COUNT + 1
      Wend

   Next
   $KEY = StringSplit(StringTrimRight($KEY, 1), '/')
   $VALUE = StringSplit(StringTrimRight($VALUE, 1), '/')
EndFunc  ;<==> Read_Data()

Func Convert_2_INI()
   $LINE_COUNT = 0
   GLOBAL $CHANGED_LINES = 0
   GLOBAL $DELETED_LINES = 0
   $TEMP_FILE = StringReplace($CONSTANTS_FILE, StringRight($CONSTANTS_FILE, $EXT), '.tmp')
   Count_Lines($CONSTANTS_FILE)
   GLOBAL $IN_FILE = FileOpen($CONSTANTS_FILE, 0)
   GLOBAL $OUT_FILE = FileOpen($TEMP_FILE, 2)
   If $IN_FILE = -1 Then
      Error('open the GUIConstants')
   ElseIf $OUT_FILE = -1 Then
      Error('open the output')
   EndIf

   While 1
      GLOBAL $LINE = FileReadLine($IN_FILE)
      If @Error = -1 Then ExitLoop
      $LINE_COUNT = $LINE_COUNT + 1
      If $LINE_COUNT < 3 OR $LINE = '' Then; <Remove the first 2 lines and any blank lines>
         $DELETED_LINES = $DELETED_LINES + 1
         ContinueLoop
      ElseIf $LINE_COUNT < 10 Then; <Leave the next 7 lines alone>
         FileWriteLine($OUT_FILE, $LINE)
         ContinueLoop
      Else
         $CHK = StringIsAlpha(StringLeft($LINE, 1)) & StringIsSpace(StringMid($LINE, 2, 1))
         If $CHK = 01 Then
            $LINE = @LF & StringReplace($LINE, StringLeft($LINE, 2), '[')
            $DELETED_LINES = $DELETED_LINES - 1
            Write_INI(2)
         Else
            Write_INI(8)
         EndIf
      EndIf
   Wend

   FileClose($IN_FILE)
   FileClose($OUT_FILE)
   FileCopy($TEMP_FILE, $INI_FILE, 1)
   FileDelete($TEMP_FILE)
   Do
      $CONSTANTS_FILE = StringTrimLeft($CONSTANTS_FILE, StringInStr($CONSTANTS_FILE, '\'))
   Until StringInStr($CONSTANTS_FILE, '\') = 0
   MsgBox(0, 'Finished', 'Your INI file has been created from ' & $CONSTANTS_FILE & @LF & 'Of the original ' & $LINES & ' lines;' & @LF & $CHANGED_LINES & ' were converted and' & @LF & $DELETED_LINES & ' were removed')
EndFunc  ;<==> Convert_2_INI()

Func Write_INI($STRIP)
   If StringInStr($LINE, 'Global') Then $LINE = StringReplace($LINE, 'Global', '')
   $LINE = StringStripWS($LINE, $STRIP)
   If $STRIP = 2 Then $LINE = $LINE & ']'
   $FIND_SEMI = StringInStr($LINE, ';')
   If $FIND_SEMI > 10 Then $LINE = StringLeft($LINE, $FIND_SEMI - 1)
   FileWriteLine($OUT_FILE, $LINE)
   $CHANGED_LINES = $CHANGED_LINES + 1
EndFunc  ;<==> Write_INI($STRIP)

Func Count_Lines($C_FILE)
   GLOBAL $LINES = 0
   $CFILE = FileOpen($C_FILE, 0)
   If $CFILE <> - 1 Then

      While 1
         FileReadLine($CFILE)
         If @Error = -1 Then ExitLoop
         $LINES = $LINES + 1
      Wend

      FileClose($CFILE)
   EndIf
EndFunc  ;<==> Count_Lines($C_FILE)

Func Error($MSG)
   MsgBox(262160, "ERROR", 'Unable to ' & $MSG & ' file.')
   Exit
EndFunc  ;<==> Error($MSG)

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Why don't you like using include files? It is exactly the same code (if not better) that you could write already written for you. Code reuability is very important in modern development languages. You can write a whole script just by including the correct files.

*** Matt @ MPCS

EDIT: Made message smaller to please ezzetabi

Edited by Matt @ MPCS
Link to comment
Share on other sites

Matt, as general advice, can you avoid quote so large messages?

Moreover there is totally no need to quote if your answer is just below the one that refer to or it is general... Use the quote only for 'away' answers or to point to specific part of the message...

Edit: thanks Matt.

About the topic, it sound a good idea. Overall for small scripts.

I mean, I understand the point of Matt, but also the 'a single file, everything you need' is an appelling idea.

Edited by ezzetabi
Link to comment
Share on other sites

Idon't use includes because there is no sense in including an entire file for the sake of half a dozen lines' Try it with the multi GUI code of Jon's. It's a very small script but take a look at the size if the original and then run it through the script I posted and then take another look at the size. Think of what can happen with a large gui. The only time I ever use a script file if it's one I wrote that includes no extras or If I take sections of an include file and just put the required code right in my script

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Some would argue there is no sense copy/pasting code you re-use into each individual script because first and foremost, it creates a maintenance nightmare should you change the implementation of the function due to bug-fixes or a nice performance improvement because of a better algorithm. You've signifcantly improved the odds that you will forget to do something. Although not quite as critical with just a performance boost, what happens when you let a bug into production code that you fixed, just forgot to update in that file?

And honestly, do a couple KB at most matter (Any more than that and you should re-think your include file organization).

In short, it really boils down to this. When you script, do you think about tomorrow, or do you hold the naive belief that in the future you'll never touch a script again. If the latter, then your method will suit you fine until you get tired of it and it bites you in the ass (and it will happen). If the former, then now is the time to re-think your strategy and realize maintainability of code is often worth more than the price you pay for it.

Edit: Fixed typos.

Edited by Valik
Link to comment
Share on other sites

You could create a preprocessor that enumerates #includes and searches for the Function therein's uses and extracts the functions used into another temporary include.... yadda yadda... I may like to try this one...

Lar.

<{POST_SNAPBACK}>

There is one problem, Larry. Callbacks. GUI's, , AdLib, HotKeySet, et cetera all use them. I have a callback in a library called _OnClose() which just calls Exit. I bind it to $GUI_EVENT_CLOSE in GUI scripts. I put it in a library which also includes other GUi-related bits of code since I use them in about all my GUIs. It's possible that a pre-processor thingy might strip things like that resulting in a run-time crash.
Link to comment
Share on other sites

In short, it really boils down to this. When you script, do you think about tomorrow, or do you hold the naive belief that in the future you'll never touch a script again. If the latter, then your method will suit you fine until you get tired of it and it bites you in the ass (and it will happen). If the former, then now is the time to re-think your strategy and realize maintainability of code is often worth more than the price you pay for it.

I agree with all of this.

What I disagree with is including a lot of code that I don't need and it's becoming even more critical than ever right now. the majority of my scripts only use about 3 lines out of the GUIInclude file. On top of that is the fact that AutoIt3.exe is looking for the file in the same folder as the script file is in. That means that with the 18 current projects that I have on the go that are using the Beta GUI, I have to have a separate copy of the file in each of the project folders. I know that I can change my template file to point to a single copy of it but I don't want to change the template at this point. I've started the new (3rd full re-write) of Project Express and I'm including a incarnation of that code in one of the option settings. It's no different with the other includes. For example I have only used 1 function out of the "Files" include. Why would I want to include the whole file when it's easier to just create a separate include file that contains only the function that I use or if I seldon use it I can just write the function into my code. This is exasperated further if I using one function out of each of 4 of the standard includes. I would have to include 4 files instead of 4 functions. I might get around to working on that pre-processor that Larry got bored with. I could just code in the name of the functions that are required for that script and then it parse all of the standard includes and if it found the functions that are called it could write them to a new include file that would then be included in the project. By the way that code was left intentionally long and can easily be shortened down. I left it as is so that I can more easily see where I am at when I port it over to Project Express. For example some of the file names will probably be duplicated as well as a function or two. The new Project Express Is A huge Muilti GUI X Multi GUI and I don't reall need to have it continually checking back to see what a string value is when the actual value can be declared where it's needed.

For example, which is better

$String_1 = 1
<insert 200 lines of code>
While String_1
<insert whatever>
Wend

OR

<insert 200 lines of code>
While 1
<insert whatever>
Wend
Why would I want to jump back to see what the value of $String_1 was or why would I want the system to remember that value when I can declare the value 1 only at the point where it is required. There is good value in declaring string values but it tends to be used to often. In the above example If the string was $String_1 = $Four + $twenty + $My_string +1 Then it makes sense to declare it as a string and just call While $String_1.

No different with includes.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...