Sign in to follow this  
Followers 0
cppman

DataWrite,DataRead UDF

3 posts in this topic

#1 ·  Posted (edited)

Updated... Added some extra security...

Here is a udf i wrote that might help with games(dunno, but have'nt really seen any games in autoit) that needs to save the users information. Or use it for programs that needs to save projects.

It writes data to a file in a somewhat of an xml format, and reads the tag.

e.g. DataRead("test.dat", "myname") would return the value of myname in the file test.dat

#include <string.au3>
#include <file.au3>
#include <array.au3>

#include <file.au3>
#include <string.au3>
func DataWrite($file, $tag, $value)
;   fwrite($file, $tag, $value)
;      $file - File to write to.
;      $tag - Tag to store data. e.g.(name, size, length)
;      $value - Value of the tag. e.g.(chris, 200, 5)
;   Return values
;      (0) - No Error.
;      (3) - Value contains invalid characters.
    $tempFile = FileOpen($file, 1)
    $temp = DataRead($file, $tag)
    if not $temp == "" then return 1
    if HasInvalid($tag) or HasInvalid($value) then return 3
    if TagExists($file, $tag) and TagGetLine($file, $tag, $value) > 1 Then
        _FileWriteToLine($file, TagGetLine($file, $tag, $value), Encrypt("<"&$tag&"="&$value&">")&@LF, 1)
    Else
    FileWrite($tempFile, Encrypt("<"&$tag&"="& $value & ">")&@LF)
    EndIf
    FileClose($tempFile)
    return 0
EndFunc

func DataRead($file, $tag)
;      fread($file, $tag)
;      $file - File to read from.
;      $tag - Tag that contains the data. e.g.(name, size, length)
;   Return values
;      (2) - No data to read.
;      (4) - File does not exist.
;      (5) - Cannot find tag.
    $fs = FileGetSize($file)
    if $fs <= 0 then return 2
    if not FileExists($file) then return 4
    Dim $avData[_FileCountLines($file)]
    _FileReadToArray($file, $avData)
    For $i = 1 to $avData[0]
        $avData[$i] = Decrypt($avData[$i])
    Next
    For $i = 1 to $avData[0]
        $str = StringSplit($avData[$i], "=")
        if $str[1] == "<"&$tag Then
            Return RemoveChar($str[2], ">")
        Else
            ContinueLoop
        EndIf
    Next
    return 5
EndFunc

Func ClearData($file) ;Deletes all Data in the file.
    FileDelete($file)
    FileOpen($file, 1)
    FileClose($file)
EndFunc

Func RemoveChar($string, $char)
    $str = StringSplit($string, "")
    local $str2
    For $i = 1 to $str[0]
        if $str[$i] == $char Then
            $str[$i] = ""
        EndIf
        $str[$i] = $str[$i]
    Next
    For $i = 1 to $str[0]
        $str2 &= $str[$i]
    Next
Return $str2
EndFunc

Func HasInvalid($string)
    ;Check for '<' or '>'.
    $str = StringSplit($string, "")
    for $i = 1 to $str[0]
        if $str[$i] == ">" or $str[$i] = "<" Then
            return True
        Else
            ContinueLoop
        EndIf
    Next
    Return False
EndFunc

Func TagGetLine($file, $tag, $value) ;Returns the line that a certain tag is on.
    $fc = _FileCountLines($file)
    if $fc < 1 Then
        return 0
    EndIf
    Dim $avData[$fc]
    _FileReadToArray($file, $avData)
     For $i = 1 to $avData[0]
         $avData[$i] = Decrypt($avData[$i])
     Next
    For $i = 1 to $avData[0]
        if $avData[$i] = "<"&$tag&"="&$value&">" Then
            Return $i
        Else
            ContinueLoop
        EndIf
    Next
EndFunc

Func TagExists($file, $tag) ;Returns true if the specified tag exists...
    $count = _FileCountLines($file)
    if $count < 1 Then
        Return False
    EndIf
    Dim $avData[_FileCountLines($file)]
    _FileReadToArray($file, $avData)
    For $i = 1 to $avData[0]
        $avData[$i] = Decrypt($avData[$i])
    Next
    
    For $i = 1 to $avData[0]
            $str = StringSplit($avData[$i], "=")
            if $str[1] = "<"&$tag Then
                Return True
        Else
            ContinueLoop
        EndIf
    Next
    Return False
EndFunc
 
        
Func Encrypt($String)
    $sarray=StringSplit($string, "")
    $nums=$sarray[0]+1
    dim $hexvals[$nums]
    
    for $count = 1 to $sarray[0]
        $val=asc($sarray[$count])
        $val=$val+55
        $sarray[$count]=chr($val)
    next
    $string=""
    for $count = 1 to $sarray[0]
        $string=$string&$sarray[$count]
    next
    $string=_StringReverse($string)
    Return $string
EndFunc   ;==>Encrypt

Func Decrypt($string)
    $sarray=StringSplit($string, "")
    $nums=$sarray[0]+1
    dim $hexvals[$nums]
    for $count = 1 to $sarray[0]
        $val=asc($sarray[$count])
        $val=$val-55
        $sarray[$count]=chr($val)
    Next
    $string=""
    for $count = 1 to $sarray[0]
        $string=$string&$sarray[$count]
    Next
    $string=_StringReverse($string)
    Return $string
EndFunc

although there are a few bugs. I was having trouble re-writing a tag so i left that option out, so if u write data to a tag that already exists, it will only append the file. and when you use DataRead($file, $tag, $value) it will read the first tag with the name $tag.

Functions:

DataWrite($file, $tag, $value) - Writes the specified tag with the specified value.

DataRead($file, $tag) - Reads the value from a tag.

ClearData($file) - Deletes all data in the file.

TagExists($file, $tag) - Returns true is the specified tag exists.

Edited by CHRIS95219

Share this post


Link to post
Share on other sites



As a matter a fact, this is very useful for the program im working on right now, and its extremely helpful, Thanks, looks great but i havn't tested it though, (yet).

Share this post


Link to post
Share on other sites

thanks..

to be honest, i think i was bored, and half wasy asleep when i wrote that, cuz now that i look at it, it looks a bit useless to me... I think im going to write a BETTER version of it.

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