Fix_Includes.au3

Last modified:   Wednesday, 28 May 2008

#cs
  Compiler Version: 		3.2.12.0
  Language:		English (US)
  Platform: 		XP (unless otherwise noted)
  Author: 		George (GeoSoft) Gedye
  Project Description: Change the #Includes to work with later versions of AutoIt
#ce
#include <stringx.au3>
#include <filex.au3>
#include <array.au3>
$aSep = Chr(32)
$tFldr = ""
$cfList = ""
$ini = @ScriptDir & "\Const.ini"
$iPath = StringLeft(@AutoItExe, StringInStr(@AutoItExe,"\", 0, -1)) & "include\"

If NOT FileExists($Ini) Then _CreateINI()
$File = @DeskTopDir & "\Test.au3"
;;  You could build an array of file names (with the path) here and
;;  and pass each element to the next line as $File
_CheckFile($File)
MsgBox(0, "Finished", "The #includes have been fixed")

Func _CreateINI()
  Local $sFile = FileFindFirstFile($iPath & "*constants*.au3"), $tArr, $iArr
  If $sFile <> -1 Then
    While NOT @Error
      $nFile = FileFindNextFile($sFile)
      $cfList &= $nFile & $aSep
    Wend
    $cfList = StringSplit(StringStripWS($cfList, 2), $aSep)
  Else
    Return SetError(1)
  EndIf
  
  For $I = 1 To Ubound($cfList)-1
    $tArr = __FileReadToArray($iPath & $cfList[$I])
    For $C = 1 To Ubound($tArr)-1
      If StringInStr($tArr[$C], "Global Const") Then
        $Cid = _StringGetWord($tArr[$C], 3)
        If StringLeft($Cid, 1) <> "$" Then ContinueLoop
        IniWrite($ini, "Default", $Cid, $cfList[$I])
      EndIf
    Next
  Next
  $iArr = __FileReadToArray($ini)
  _ArraySort($iArr, 0, 2)
  _ArrayReverse($iArr, 2)
  $oFile = FileOpen($Ini,2)
  
  For $I = 1 To Ubound($iArr)-1
    FileWriteLine($oFile, StringReplace($iArr[$I], "=", "= "))
  Next
  FileClose($oFile)
EndFunc   ;<==> _CreateINI()

Func _CheckFile($sFile = "")
  Local $cList = "", $fList = __FileReadToArray($Ini), $I
  If $sFile = "" Then
    $sFile = FileOpenDialog("Select the file to check", "", "AutoIt Files (*.au3)", 3)
    If @Error Then Return SetError(2)
  EndIf
  If $sFile = @ScriptFullPath Then
  MsgBox(0, "Error", "You can not run this script against itself.")
  Exit
  EndIf
  $cFile = FileRead($sFile)
  FileMove($sFile, StringReplace($sFile, ".au3", ".bak")) ;; Create a backup
  ;; Now lets prepare the file
  $cFile = StringReplace($cFile, "<GUIConstants.au3>", "<GUIConstantsEx.au3>")
  
  $cFile = StringReplace($cFile, "< ", "<")
  $cFile = StringReplace($cFile, " >", ">")
  $cFile = StringReplace($cFile, "#Include <", "#Include<")
  
  ;; Now we get down to work by determining what constants were used
  ;; and building the list of constants file that are required
  If _StringGetChrCount($cFile, "guiconstants") Then
    $cList &= "GUIConstantsEx.au3" & $aSep
    For $I = 2 To Ubound($fList) -1
      $var = StringTrimRight(_StringGetWord($fList[$I]), 1)
      If _StringGetChrCount($cFile, $Var) Then
        $Val = IniRead($Ini, "Default", $var, "")
        If $Val  = "" Then ContinueLoop
        If Not StringInStr($cList, $Val & $aSep) Then $cList &= $Val & $aSep
      EndIf
    Next
  EndIf
  
  ;; We have the list of required constants files se we turn it into an array
  ;; and build our output
  $out = ""
    $cList = StringSplit(StringStripWS($cList,2), $aSep)
    For $I = 1 To $cList[0]
      If $I > 1 Then $cFile = StringReplace($cFile, "#Include<" & $cList[$I] & ">", "")
      $out &= "#Include <" & $cList[$I] & ">" & @CRLF
    Next
  $cFile = StringReplace($cFile, "#Include<GUIConstantsEx.au3>", $Out)
  $cFile = StringReplace($cFile, "#Include<", "#Include <")
  $oFile = FileOpen($sFile, 2)
  FileWrite($oFile, StringStripWS($cFile, 2))
  FileClose($oFile)
EndFunc   ;<==> _CheckFile()