Jump to content
Sign in to follow this  
Mat

Find a constant by its value.

Recommended Posts

Mat

I hate using includes, I prefer to write completely from scratch, and so my GUI's look like this:

$hGUI = GUICreate ("A GUI", 450, 390, -1, -1, 0x00000080 + 0x00080000 + 0x00C00000, 0x00000001)
This function takes the value (0x00C00000) and returns an array of possible names. In that case it would return $WS_CAPTION, as well as its copy from GUIEdit.au3. It also returns the file where you might be able to find it in (WindowsConstants.au3). This just helps make code readable, because lets face it, imagining what that GUI looks like is hard.

#include<array.au3>

$sInp = InputBox ("Enter...", "Please enter a value or a name for a valid constant...", "$GUI_EVENT_CLOSE", " M")
If @Error Then Exit

If StringLeft ($sInp, 1) = "$" Then ; Is a name!
   $temp = _FindValByConst ($sInp)
Else
   $temp = _FindConstByVal ($sInp)
EndIf
_ArrayDisplay ($temp)

; { FUNCTION }====================================================================================================================
;
; Name         : _FindConstByVal
; Description  : Finds the name of a constant given its value.
; Syntax       : _FindConstByVal ( $sFind )
; Variables    : $sFind         - The value of the constant to find
; Returns      : Success        - Returns an 2d array where [][0] = The variable name and [][1] = the file found in.
;                Failure          Returns 0 and sets @Error to 1
; Author       : Mat
; Notes        :
; Related      :
; Example      : Yes
;
; ================================================================================================================================

Func _FindConstByVal ($sFind)
   Local $aFolders = StringSplit (RegRead ("HKEY_CURRENT_USER\Software\AutoIt v3\AutoIt", "include"), ";"), $aRet[1][2]
      $aRet[0][0] = 0
      $aRet[0][1] = $sFind
   If $aFolders[1] = "" Then ReDim $aFolders[1]
   $aFolders[0] = StringTrimRight (@AutoitEXE, 11) & "include"

   For $i = 0 to UBound ($aFolders) - 1
      If StringRight ($aFolders[$i], 1) <> "\" Then $aFolders[$i] &= "\"
      $hSearch = FileFindFirstFile ($aFolders[$i] & "*.au3")
      If $hSearch = -1 Then ContinueLoop
      While 1
         $sFile = FileFindNextFile ($hSearch)
         If @Error Then ExitLoop
         $aReg = StringRegExp (FileRead ($aFolders[$i] & $sFile), "(?x)(?i)(?:\s+Global\s+const\s+)(\$.*?)(?:\s*)?=(?:\s*)?(\Q" & $sFind & "\E)", 3)
         If Not IsArray ($aReg) Then ContinueLoop 1
         For $x = 1 to UBound ($aReg) - 1 step + 2
            ReDim $aRet[UBound ($aRet) + 1][2]
            $aRet[UBound ($aRet) - 1][0] = $aReg[$x - 1]
            $aRet[UBound ($aRet) - 1][1] = $sFile
            $aRet[0][0] += 1
         Next
      WEnd
      FileClose ($hSearch)
   Next
   If UBound ($aRet) = 1 Then Return SetError (1, 0, 0)
   Return $aRet
EndFunc ; ==> _FindConstByVal

; { FUNCTION }====================================================================================================================
;
; Name         : _FindValByConst
; Description  : Finds the value and file for a given constant.
; Syntax       : _FindIncludeByConstName ( $sFind )
; Variables    : $sFind         - The value of the constant to find
; Returns      : Success        - Returns an 2d array where [][0] = The value and [][1] = the file found in.
;                Failure          Returns 0 and sets @Error to 1
; Author       : Valery
; Notes        : Mat
; Related      :
; Example      : Yes
;
; ================================================================================================================================

Func _FindValByConst ($sFindName)
   Local $aFolders = StringSplit (RegRead ("HKEY_CURRENT_USER\Software\AutoIt v3\AutoIt", "include"), ";"), $aRet[1][2]
   $aRet[0][0] = 0
   $aRet[0][1] = $sFindName
   If $aFolders[1] = "" Then ReDim $aFolders[1]
   $aFolders[0] = StringTrimRight (@AutoitEXE, 11) & "include"

   For $i = 0 to UBound ($aFolders) - 1
      If StringRight ($aFolders[$i], 1) <> "\" Then $aFolders[$i] &= "\"
      $hSearch = FileFindFirstFile ($aFolders[$i] & "*.au3")
      If $hSearch = -1 Then ContinueLoop
      While 1
         $sFile = FileFindNextFile ($hSearch)
         If @Error Then ExitLoop
         $aReg = StringRegExp (FileRead ($aFolders[$i] & $sFile), "(?x)(?i)(?:\s+Global\s+const\s+)?(?:\Q" & $sFindName & "\E)(?:\s+=\s+)(.+?)\s", 3)
         If Not IsArray ($aReg) Then ContinueLoop 1
         For $x = 0 to UBound ($aReg) - 1
            ReDim $aRet[UBound ($aRet) + 1][2]
            $aRet[UBound ($aRet) - 1][0] = $aReg[$x]
            $aRet[UBound ($aRet) - 1][1] = $sFile
            $aRet[0][0] += 1
         Next
      WEnd
      FileClose ($hSearch)
   Next
   If UBound ($aRet) = 1 Then Return SetError (1, 0, 0)
   Return $aRet
EndFunc ; ==> _FindValByConst

Mat

Edited by Mat

Share this post


Link to post
Share on other sites
ValeryVal

Thanx. One problem exists, though.

(?:Global\sconst\s) - It will match any single whitespace character, only.

Try this lines (with many whitespaces):

Global Const $xxx = 4096

Global Const $xxx = 4096


The point of world view

Share this post


Link to post
Share on other sites
Mat

All you need is a few stars :)

I'll add it now thanks.

Mat

Edited by Mat

Share this post


Link to post
Share on other sites
ValeryVal

Often there is other problem - to find include file which has some constants.

I've rewritten your example to do it with func _FindIncludeByConstName.

And it has the same problem with whitespaces, too.

#include<array.au3>

$sInp = InputBox ("Enter...", "Please enter a constant name ...", "$ACS_TIMER", " M")
If @Error Then Exit

$temp = _FindIncludeByConstName ($sInp)
_ArrayDisplay ($temp)

Func _FindIncludeByConstName ($sFindName)
   Local $aFolders = StringSplit (RegRead ("HKEY_CURRENT_USER\Software\AutoIt v3\AutoIt", "include"), ";"), $aRet[1][2]
   $aRet[0][0] = 0
   $aRet[0][1] = $sFindName
   If $aFolders[1] = "" Then ReDim $aFolders[1]
   $aFolders[0] = StringTrimRight (@AutoitEXE, 11) & "include"

   For $i = 0 to UBound ($aFolders) - 1
      If StringRight ($aFolders[$i], 1) <> "\" Then $aFolders[$i] &= "\"
      $hSearch = FileFindFirstFile ($aFolders[$i] & "*.au3")
      If $hSearch = -1 Then ContinueLoop
      While 1
         $sFile = FileFindNextFile ($hSearch)
         If @Error Then ExitLoop
         $aReg = StringRegExp (FileRead ($aFolders[$i] & $sFile), "(?x)(?i)(?:Global\sconst\s)?(\Q" & $sFindName & "\E)", 3)
         If Not IsArray ($aReg) Then ContinueLoop 1
         For $x = 1 to UBound ($aReg) - 1 step + 2
            ReDim $aRet[UBound ($aRet) + 1][2]
            $aRet[UBound ($aRet) - 1][0] = $aReg[$x - 1]
            $aRet[UBound ($aRet) - 1][1] = $sFile
            $aRet[0][0] += 1
         Next
      WEnd
      FileClose ($hSearch)
   Next
   If UBound ($aRet) = 1 Then Return SetError (1, 0, 0)
   Return $aRet
EndFunc ; ==> _FindIncludeByConstName

The point of world view

Share this post


Link to post
Share on other sites
danielkza

I can't find any possible reason not to use includes when you can use Obfuscator to trim all the extra code they have. I'm curious to hear your reasoning for it.

Share this post


Link to post
Share on other sites
Mat

Fixed the whitespace problem, tidied your code, rewrote the example, made your code return the value in the 0th element.

Mat

Share this post


Link to post
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
Sign in to follow this  

×