Sign in to follow this  
Followers 0
cppman

DataTag UDFs

1 post in this topic

I wrote this "library" a long time ago, and it was crap. lol, So I completely rewrote it, and it came out alot better than last time.

Basically it is a series of UDFs for securely reading/writing files, such as Game Saves, Configuration Saves and stuff like that:

Here are the functions:

_DataSetPassword($sPwd) - Sets the password.

_DataTagExists($sFile, $sTag) - Checks if a tag exists

_DataTagWrite($sFile, $sTag, $sValue) - Writes the tag to the file.

_DataTagRead($sFile, $sTag) - Reads the specified tag.

_DataTagsClearAll($sFile) - Clears ALL data in the file.

_DataTagsGetAll($sFile) - Returns ALL tags and values in that file.

and... the code:

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

Global $__PASSWORD__DATA__TAG   =   "gxYYzaDSs"
;===============================================================================
;
; Description:      Sets the password to be used with the encryption.
; Parameter(s):     $sPwd - The password you would like to use.
; Requirement(s):   None
; Return Value(s):  On Success - Sets the password.
;                   On Failure - NONE
; Author(s):        CHRIS95219
; Note(s):          DO NOT Change the password during/after writing to a file, as it will not return correct results.
;===============================================================================
Func _DataSetPassword($sPwd)
    $__PASSWORD__DATA__TAG = $sPwd
EndFunc

;===============================================================================
;
; Description:      Returns the encrypted string for use with _DataTag functions.
; Parameter(s):     $sData - The data to be encrypted.
; Requirement(s):   None
; Return Value(s):  On Success - Returns encrypted string.
;                   On Failure - see _StringEncrypt()
; Author(s):        CHRIS95219
;===============================================================================
Func _DataEncode($sData)
    Return _StringEncrypt(1, $sData, $__PASSWORD__DATA__TAG)
EndFunc

;===============================================================================
;
; Description:      Returns the decrypted string for use with _DataTag functions.
; Parameter(s):     $sData - The data to be decrypted.
; Requirement(s):   None
; Return Value(s):  On Success - Returns decrypted string.
;                   On Failure - see _StringEncrypt()
; Author(s):        CHRIS95219
;===============================================================================
Func _DataDecode($sData)
    Return _StringEncrypt(0, $sData, $__PASSWORD__DATA__TAG)
EndFunc

;===============================================================================
;
; Description:      Returns whether or not the specified tag exists.
; Parameter(s):     $sFile - The file which contains your "DataTags".
;                   $sTag  - The tag you wish to check.
; Requirement(s):   None
; Return Value(s):  On Success - Returns true
;                   On Failure - Returns false
; Author(s):        CHRIS95219
;===============================================================================
Func _DataTagExists($sFile, $sTag)
    Local $aDataLines
    _FileReadToArray($sFile, $aDataLines)
    if (UBound($aDataLines) <= 0) Then Return False ;Its a new file.
    For $i = 1 to $aDataLines[0]
        $sCurLine = _DataDecode($aDataLines[$i])
        if (StringInStr($sCurLine, $sTag)) Then Return True
    Next
    
    Return False
EndFunc

;===============================================================================
;
; Description:      Writes the specified tag to the DataTag file.
; Parameter(s):     $sFile  - The file that contains the DataTags you wish to write to.
;                   $sTag   - The tag name you wish to write.
;                   $sValue - The tag's value.
; Requirement(s):   None
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0
; Author(s):        CHRIS95219
;===============================================================================
Func _DataTagWrite($sFile, $sTag, $sValue)
    if (_DataTagExists($sFile, $sTag)) Then Return 0 ;Error occurred. - The tag already exists, so prevent rewriting.
    ;First delete tag, and then rewrite it. Prevent unexpected errors.
    Local $aDataLines
    _FileReadToArray($sFile, $aDataLines)
    _ArrayAdd($aDataLines, _DataEncode($sTag & " = " & $sValue) & @CR)
    FileDelete($sFile)
    _FileWriteFromArray($sFile, $aDataLines, 1)
    if (@error == 2) Then
        $hFile = FileOpen($sFile, 1)
        FileWriteLine($hFile, _DataEncode($sTag & " = " & $sValue) & @CR)
        FileClose($hFile)
    EndIf
    Return 1
EndFunc

;===============================================================================
;
; Description:      Reads the specified tag in the file.
; Parameter(s):     $sFile  - The file containing the "DataTags".
;                   $sTag   - The tag you wish to read.
; Requirement(s):   None
; Return Value(s):  On Success - Returns the value of the tag
;                   On Failure - Returns -1
; Author(s):        CHRIS95219
;===============================================================================
Func _DataTagRead($sFile, $sTag)
    if (_DataTagExists($sFile, $sTag) == False) Then return -1
    ;Only do the searching if the tag exists...
    Local $aDataLines
    _FileReadToArray($sFile, $aDataLines)
    For $i = 1 to $aDataLines[0]
        $sCurLine = _DataDecode($aDataLines[$i])
        $sCurSplit = StringSplit($sCurLine, " = ")
        if (StringLower($sCurSplit[1]) ==  StringLower($sTag)) Then Return $sCurSplit[$sCurSplit[0]]
    Next
    Return -1
EndFunc

;===============================================================================
;
; Description:      Erases/Deletes ALL  "DataTags" inside your file.
; Parameter(s):     $sFile  - File containing your datatags.
; Requirement(s):   None
; Return Value(s):  On Success - Clears File.
;                   On Failure - Nothing - File stays the same. (Check size of file)
; Author(s):        CHRIS95219
; Note(s):          This WILL completely erase all data inside the file, whether they are tags or not.
;===============================================================================
Func _DataTagsClearAll($sFile)
    FileDelete($sFile)
    $hFile = FileOpen($sFile, 1)
    FileClose($hFile)
EndFunc

;===============================================================================
;
; Description:      Returns an Array of EVERY tag and value
; Parameter(s):     $sFile  - The file containing all your tags.
; Requirement(s):   None
; Return Value(s):  On Success - Returns a 2-D Array of Tags and Values.
;                   On Failure - Returns -1
; Author(s):        CHRIS95219
; Note(s):          The Array information is stored as follows:
;                   $aVar[$n][0] = Tag Name
;                   $aVar[$n][1] = Tag Value
;===============================================================================
Func _DataTagsGetAll($sFile)
    Local $aDataLines
    _FileReadToArray($sFile, $aDataLines)
    Local $aResults[$aDataLines[0]+1][2]
    $aResults[0][0] = $aDataLines[0]
    For $i = 1 to $aDataLines[0]
        $sCurLine = _DataDecode($aDataLines[$i])
        $sCurSplit = StringSplit($sCurLine, " = ")
        $aResults[$i][0] = $sCurSplit[1]
        $aResults[$i][1] = $sCurSplit[$sCurSplit[0]]
    Next
    if (IsArray($aResults)) Then
        Return $aResults
    Else
        Return -1
    EndIf
EndFunc
oÝ÷ Ù©ÝêÞƧ{¦¦WºÚ"µÍÒ[ÛYHXÝHÛÙIÌÌÎÂÚ[ÛYH  ][ÝÑ]UYÜË]LÉ][ÝÂÑ]TÙ]ÜÝÛÜ
    ][ÝÓ^TÙXÜ]ÜÝÛÜ  ][ÝÊBÑ]UYÕÜ]J  ][ÝÝÝ]   ][ÝË  ][ÝÓ^S[YI][ÝË   ][ÝÐÚÉ][ÝÊBÌÍÓ^S[YHHÑ]UYÔXY
    ][ÝÝÝ]   ][ÝË  ][ÝÓ^S[YI][ÝÊBÑ]UYÜÐÛX[
    ][ÝÝÝ]   ][ÝÊBÙÐÞ
    ][ÝÉ][ÝË    ][ÝÒ[Ë   ][ÝÈ  [È ÌÍÓ^S[YJB

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