Sign in to follow this  
Followers 0
Sea

[UDF] Structure Parser 1.2

1 post in this topic

Hello everybody, I'm currently working on structure for make it like in C.
 
For the moment the UDF is able to
  •  
  • Read some structure from a file
  • Read with a given structure
  • Get structure by the name or by GUID,it's automaticly detect if it's a GUID or name
  • Create our datatype
 
 
What will be coming?
  •  
  • Write a file with a given structure
 
 
 
UDF
#include <WinAPI.au3>
#include <Array.au3>
#include <String.au3>
 
 
 
Global $nBytes
; #FUNCTION# =============================================================================
; Name...........: getStruct
; Description ...: Get Autoit structure and the size of it
; Syntax.........: getStruct($sPath,$sStructName,$iType)
; Parameters ....: $sPath - Path of the file who contening the structure.
;                          $sStructName - String contening the name of the structure of his GUID (8bit-4bit-4bit-4bit-12bit)
;                          $iRecursivelvl - Int egual to the level of the recusivity of the script.
; Optional.......:
; Return values .: Success - Returns an array [0] = Structure
;                                             [1] = Size of structure
;                                             [2] = Number of Data
;                  Failure - Returns a 0
;                       @Error - 0 = No error.
;                                1 = Path is not a string
;                                2 = Can't open file
;                                3 = Can't find the structure name
; Author ........: sea78
; ========================================================================================
Func getStruct($sPath,$sStructName,$iRecursivelvl = 1)
    If Not IsString($sPath) Then
        SetError(1)
        Return 0
    EndIf
 
    if ($iRecursivelvl = 1) Then
        Global $text[1]
        Global $data[3]
        $data[2] = 0
    Else
        ReDim $text[$iRecursivelvl]
    EndIf
 
    $file = FileOpen($sPath)
    If $file = -1 Then
        SetError(2)
        Return 0
    EndIf
    $text[$iRecursivelvl-1] = FileRead($file)
    FileClose($file)
 
    $text[$iRecursivelvl-1] = StringReplace($text[$iRecursivelvl-1],@CR,"")
    $text[$iRecursivelvl-1] = StringReplace($text[$iRecursivelvl-1],@LF,"")
 
    If (StringRegExp($sStructName, '^(\{){0,1}[[:xdigit:]]{8}\-[[:xdigit:]]{4}\-[[:xdigit:]]{4}\-[[:xdigit:]]{4}\-[[:xdigit:]]{12}(\}){0,1}$')) Then
        $text[$iRecursivelvl-1] = StringRegExpReplace($text[$iRecursivelvl-1], "(.*)"&$sStructName&"}{", "", 0)
        $text[$iRecursivelvl-1] = StringRegExpReplace($text[$iRecursivelvl-1], "}(.*)", "", 0)
    Else
        $text[$iRecursivelvl-1] = StringRegExpReplace($text[$iRecursivelvl-1], "(.*)struct "&$sStructName&"({)(.{8})-(.{4})-(.{4})-(.{4})-(.{12})}{", "", 0)
        $text[$iRecursivelvl-1] = StringRegExpReplace($text[$iRecursivelvl-1], "}(.*)", "", 0)
    EndIf
    $textExplosed = _StringExplode($text[$iRecursivelvl-1],';')
    If (StringRegExp($text[0], "struct", 0)) Then
        SetError(3)
        Return 0
    EndIf
 
    Redim $textExplosed[UBound($textExplosed)-1]
    for $i = 0 to UBound($textExplosed)-1
        If CheckType($textExplosed[$i]) = 1 Then
            $data[0] = $data[0]&$textExplosed[$i]&';'
            $data[1] = $data[1]+getSize($textExplosed[$i])
            $data[2] = $data[2]+1
        ElseIf CheckType($textExplosed[$i]) = 2 Then
            $nb = StringRegExpReplace($textExplosed[$i], "(\D)", "", 0)
            if $nb <> "" Then
                for $y = 0 to $nb
                    getStruct($sPath,StringRegExpReplace($textExplosed[$i], "\[(.*)\]", "", 0),$iRecursivelvl+1)
                Next
            Else
                getStruct($sPath,StringRegExpReplace($textExplosed[$i], "\[(.*)\]", "", 0),$iRecursivelvl+1)
            EndIf
        EndIf
    Next
 
    Return $data
EndFunc
 


; #FUNCTION# =============================================================================
; Name...........: CheckType
; Description ...: Check if the data is knowed datatype
; Syntax.........: CheckType($sData)
; Parameters ....: $sData - String contening the DataType
; Optional.......:
; Return values .: Success - Returns an int
;                                           1 => DataType is Know
;                                           2 => DataType is Home Maid
;                  Failure - Returns a 0
;                       @Error - 1 = Data is not a string.
; Author ........: sea78
; ========================================================================================

Func CheckType($sData)
    If Not IsString($sData) Then
        SetError(1)
        Return 0
    EndIf
 
    If StringRegExp($sData,"char") Then
        Return 1
    ElseIf StringRegExp($sData,"byte") Then
        $sData = StringRegExpReplace($sData, "\D", "", 0)
        Return 1
    ElseIf StringRegExp($sData,"boolean") Then
        return 1
    ElseIf StringRegExp($sData,"wchar") Then
        $sData = StringRegExpReplace($sData, "\D", "", 0)
        Return 1
    ElseIf StringRegExp($sData,"short") Then
        Return 1
    ElseIf StringRegExp($sData,"ushort") Then
        Return 1
    ElseIf StringRegExp($sData,"word") Then
        Return 1
    ElseIf StringRegExp($sData,"int") Then
        Return 1
    ElseIf StringRegExp($sData,"long") Then
        Return 1
    ElseIf StringRegExp($sData,"bool") Then
        Return 1
    ElseIf StringRegExp($sData,"uint") Then
        Return 1
    ElseIf StringRegExp($sData,"ulong") Then
        Return 1
    ElseIf StringRegExp($sData,"dword") Then
        Return 1
    ElseIf StringRegExp($sData,"float") Then
        Return 1
    ElseIf StringRegExp($sData,"double") Then
        Return 1
    ElseIf StringRegExp($sData,"double") Then
        Return 1
    Else
        Return 2
    EndIf
EndFunc
 
 
 
 
; #FUNCTION# =============================================================================
; Name...........: getSize
; Description ...: Get size of type.
; Syntax.........: getSize($sData)
; Parameters ....: $sData - String contening the DataType
; Optional.......:
; Return values .: Success - Returns an int egal to the size of the DataType
;                  Failure - Returns a 0
;                       @Error - 0 = No error.
;                                1 = Data is not a string
;                                2 = Undefine DataType
; Author ........: sea78
; ========================================================================================
Func getSize($sData)
    If Not IsString($sData) Then
        SetError(1)
        Return 0
    EndIf
 
    If StringRegExp($sData,"char") Then
        $sData = StringRegExpReplace($sData, "\D", "", 0)
        Return $sData
    ElseIf StringRegExp($sData,"byte") Then
        $sData = StringRegExpReplace($sData, "\D", "", 0)
        Return $sData*2
    ElseIf StringRegExp($sData,"boolean") Then
        return 1
    ElseIf StringRegExp($sData,"wchar") Then
        $sData = StringRegExpReplace($sData, "\D", "", 0)
        Return $sData*2
    ElseIf StringRegExp($sData,"short") Then
        return 2
    ElseIf StringRegExp($sData,"ushort") Then
        return 2
    ElseIf StringRegExp($sData,"word") Then
        return 2
    ElseIf StringRegExp($sData,"int") Then
        return 4
    ElseIf StringRegExp($sData,"long") Then
        Return 4
    ElseIf StringRegExp($sData,"bool") Then
        Return 4
    ElseIf StringRegExp($sData,"uint") Then
        Return 4
    ElseIf StringRegExp($sData,"ulong") Then
        Return 4
    ElseIf StringRegExp($sData,"dword") Then
        Return 4
    ElseIf StringRegExp($sData,"float") Then
        Return 4
    ElseIf StringRegExp($sData,"double") Then
        Return 8
    ElseIf StringRegExp($sData,"double") Then
        Return 8
    Else
        SetError(2)
        Return 0
    EndIf
EndFunc
 
 
; #FUNCTION# =============================================================================
; Name...........: readStruct
; Description ...: Read data with structure.
; Syntax.........: readStruct($sFile,$aData)
; Parameters ....: $sFile - path of the file to read
;                  $aData - Array with structure & Size [0] = Autoit string struct
;                                                       [1] = Size of the structure
;                                                       [2] = Number of data
; Optional.......:
; Return values .: Success - Returns an array who contening the data read
;                  Failure - Returns a 0
;                       @Error - 0 = No error.
;                                1 = sFile is not a string
;                                2 = aData is not an array
;                                3 = aData[0] is not a string
;                                4 = aData[1] is not an int
;                                5 = aData[2] is not an int
;                                6 = Can't open file
;                  Failure - Returns a 1
;                       @Error - Look the error code for DllStructCreate
; Author ........: sea78
; ========================================================================================
Func readStruct($sFile,$aData)
    If Not IsString($sFile) Then
        SetError(1)
        Return 0
    EndIf
    If Not IsArray($aData) Then
        SetError(2)
        Return 0
    EndIf
    If Not IsString($aData[0]) Then
        SetError(3)
        Return 0
    EndIf
    If Not IsInt($aData[1]) Then
        SetError(4)
        Return 0
    EndIf
    If Not IsInt($aData[2]) Then
        SetError(5)
        Return 0
    EndIf
    Dim $aBuffer[$aData[2]+1]
    $hFile = _WinAPI_CreateFile($sFile, 2, 2)
    If $hFile = 0 Then
        SetError(6)
        Return 0
    EndIf
    $tBuffer = DllStructCreate($aData[0])
    If $tBuffer = 0 Then
        Return 1
    EndIf
    _WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer), $aData[1], $nBytes)
    For $i = 0 to $aData[2]
        $aBuffer[$i] = DllStructGetData($tBuffer, $i+1)
    Next
    _WinAPI_CloseHandle($hFile)
    Return $aBuffer
EndFunc
 
 
An exemple of use.
#include <Structure.au3> ;See the UDF ^^
 
$sFile = 'C:\DataFileToBeRead.data'
$test = readStruct($sFile,getStruct('Structure.txt',"MyName"))
_ArrayDisplay($test)
 
$test = readStruct($sFile,getStruct('Structure.txt',"C817F7B0-E4C7-40FB-97B3-2B97CC000521"))
_ArrayDisplay($test)
 
$test = readStruct($sFile,getStruct('Structure.txt',"test"))
_ArrayDisplay($test)
 
 
Exemple of structure

Be carefull the syntaxe of the structure must be like that
struct Name
{GUID}
{
DataType1;
DataType2;
}
The space between struc and the name of it are obligatory.The newline too.
Each data must be separate by a semicolon..
 
struct MyName
{C817F7B0-E4E7-40FB-97B3-2B97CC000521}
{
char[4];
char[4];
int;
int;
int;
char[108];
}
 
struct hey
{C817F7B0-E4C7-40FB-97B3-2B97CC000521}
{
char[10];
char[4];
int;
int;
int;
char[108];
}
 
struct test
{C817FAD0-E4E7-40FB-97B3-2B97CC000521}
{
MyName;
MyName[10];
}
 
 
 
Version Note
Legend: +: Addition, -: Remouved/Deprecated, *: Modified, !: Bug correction
V1.1
+Read some structure from a file
+Get structure by her name
V1.2
+Get structure by the name or by GUID,it's automaticly detect if it's a GUID or name
 
V1.3
+Create our datatype
 
 
2 people like this

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  
Followers 0