Jump to content

Bartokv

Active Members
  • Posts

    163
  • Joined

  • Last visited

Reputation Activity

  1. Like
    Bartokv got a reaction from guestscripter in Handy Functions   
    Below are a few custom functions that I've created, and use fairly regularly in my scripts. I don't know if anybody else would find them as useful as I, but I thought that I'd post them just in case. Please keep in mind that I'm new to AU3, so if you have any recommendations for code improvement please let me know.

    ...Enjoy!


    ; **************************************************************************** ; Returns the Variable type (STRing, NUMber, FLoaT, ARraY, INTeger, ERRor) ; **************************************************************************** Func TypeOf($TypeOfVariableName) $TypeOfReturnValue = "ERR" ; This value should never be returned If IsString($TypeOfVariableName) Then $TypeOfReturnValue = "STR" If IsNumber($TypeOfVariableName) Then $TypeOfReturnValue = "NUM" If IsFloat($TypeOfVariableName) Then $TypeOfReturnValue = "FLT" If IsArray($TypeOfVariableName) Then $TypeOfReturnValue = "ARY" If IsInt($TypeOfVariableName) Then $TypeOfReturnValue = "INT" Return $TypeOfReturnValue EndFunc ; **************************************************************************** ; Returns a String Conisting of the Specified Character and Length ; **************************************************************************** Func Pad($PadChar, $PadLen) If $PadChar == "" Then $PadChar = " " ; Use space as default padding If $PadLen > 500 Then $PadLen = 500 ; Try to keep limit reasonable If $PadLen < 0 Then $PadLen = 0 ; Idiot-proofing - just in case $PadOutString = "" For $PadCount = 1 To $PadLen $PadOutString = $PadOutString & $PadChar Next Return $PadOutString EndFunc ; **************************************************************************** ; Centers Text within the Specified Width ; **************************************************************************** Func Center($CenterString, $CenterWidth, $CenterSpacer) $CenterStringLen = StringLen($CenterString) If $CenterWidth < $CenterStringLen Then $CenterWidth = $CenterStringLen If $CenterSpacer = "" Then $CenterSpacer = " " $CenterOutString = "" $CenterPadding = ($CenterWidth - $CenterStringLen) / 2 If INT($CenterPadding) <> $CenterPadding Then $CenterLeftPadding = INT($CenterPadding) + 1 $CenterRightPadding = INT($CenterPadding) Else $CenterLeftPadding = $CenterPadding $CenterRightPadding = $CenterPadding EndIf $CenterOutString = Pad($CenterSpacer, $CenterLeftPadding) & $CenterString & Pad($CenterSpacer, $CenterRightPadding) Return $CenterOutString EndFunc ; **************************************************************************** ; Resizes the Specified Array to the Desired Depth ; **************************************************************************** Func ResizeArray(ByRef $ArrayName, $ArraySize) If TypeOf($ArrayName) <> "ARY" Then Return SetError(1) If TypeOf($ArraySize) <> "INT" Then Return SetError(2) If $ArraySize < 0 Then $ArraySize = 0 ; Idiot-proofing - just in case Dim $ArrayTempValues[$ArraySize] ; Create swap array with new size $ArrayOldMax = Ubound($ArrayName) - 1 ; Get size of original array If $ArraySize < $ArrayOldMax Then ; Try to avoid needless bloat $ArrayCountMax = $ArraySize - 1 Else $ArrayCountMax = $ArrayOldMax Endif For $ArrayCounter = 0 to $ArrayCountMax ; Populate swap array with data $ArrayTempValues[$ArrayCounter] = $ArrayName[$ArrayCounter] Next $ArrayName = $ArrayTempValues ; Replace original array with swap $ArrayTempValues = "" ; Dump swap array to free memory EndFunc ; **************************************************************************** ; Retreives Header Block Names from the Specified INI File ; **************************************************************************** Func Get_INI_headers($INI_file, ByRef $INI_ArrayName) $INI_stream = FileOpen($INI_file, 0) If TypeOf($INI_ArrayName) <> "ARY" Then Return SetError(1) $INI_count = 1 ; Verify file is open for reading If $INI_stream == -1 Then MsgBox(1, "Error", "Unable to open configuration file.") Return SetError(1) EndIf Dim $INI_head[1] ; Read in lines of text until the EOF is reached While 1 Sleep(10) ; Release some ticks to CPU $line = FileReadLine($INI_stream) ; Get next line in file If @error == -1 Then ExitLoop If StringLeft($line, 1) == "[" Then ; Header block marker found... $HeadName = StringSplit($line, "[]") If $HeadName[0] > 2 Then ; Verify INI Header name is valid ResizeArray($INI_ArrayName, $INI_count) $INI_ArrayName[$INI_count - 1] = $HeadName[2] $INI_count = $INI_count + 1 EndIf Endif Wend FileClose($INI_stream) EndFunc
×
×
  • Create New...