Jump to content

read/write ini data directly from/to text variables


Guest
 Share

Recommended Posts

hello,

i wrote functions that let you to read and write data in ini format directly from or to text variables.

This means that if you have for example a text page in server that store your ini settings then you don't have to download the text data into file and then read the data from the file with the normal iniread functions.

For example you can create a text post in http://pastebin.com/ ,

then read the text data with a function that designed for that( like inetread() for example)

and finally you can use the text variable that you created (by reading the text from the page) with my ini read/write functions.

This option also let you to store the text data with encryption on file or on server and read/write the data to the file/server without to create tmp file with decrypted data.

to do this first you need to read your encrypted data. then you will have to decrypt your text variable (so you will have text variable unencrypted data). then all you need to do is to use the final text variable with my functions!

my functions + the examples:

;================= Creating INI data: ===================
$inidata = IniWriteText("","TEST1","Key1","Keytest1")
MsgBox(0,"Test1",$inidata)
$inidata = IniWriteText($inidata,"TEST1","Key2","Keytest2")
MsgBox(0,"Test2",$inidata)
$inidata = IniWriteText($inidata,"TEST1","Key3","Keytest3")
MsgBox(0,"Test3",$inidata)

$inidata = IniWriteText($inidata,"TEST2","Key1","Keytest1")
MsgBox(0,"Test4",$inidata)
$inidata = IniWriteText($inidata,"TEST2","Key2","Keytest2")
MsgBox(0,"Test5",$inidata)
$inidata = IniWriteText($inidata,"TEST2","Key3","Keytest3")
MsgBox(0,"Test6",$inidata)

;================= changing INI keys: ====================
$inidata = IniWriteText($inidata,"TEST1","Key1","TEST1")
MsgBox(0,"Test5",$inidata)
$inidata = IniWriteText($inidata,"TEST2","Key3","TEST2")
MsgBox(0,"Test6",$inidata)

;================ reading the keys: =====================
$key1 = IniReadText($inidata,"TEST1","key2","Not found")
MsgBox(0,"key test1",$key1)
$key2 = IniReadText($inidata,"TEST2","key3","Not found")
MsgBox(0,"key test2",$key2)
$key3 = IniReadText($inidata,"TEST1","key4","Not found")
MsgBox(0,"key test3",$key3)

; =================== reading only the Sections names =========
$Sections = IniReadSectionNamesText($inidata)
_ArrayDisplay($Sections,"Sections in $inidata")

; ================== reading only the keys in Section Test1 ======
$test = IniReadSectionText($inidata,"TEST1")
_ArrayDisplay($test,"keys in Section TEST1 in $inidata")

#include <Array.au3>
Func IniReadText($Text,$Section,$Key,$Default = "")
    Local $Output = $Default
    $s_text = StringSplit($Text,@CRLF,1)
    For $a = 1 To $s_text[0]
        ;_ArrayAdd($aURLfile,$s_urltext[$a])
        If $s_text[$a] = "["&$Section&"]" Then
            If $a+1 <= $s_text[0] Then
                For $a2 = $a+1 To $s_text[0]
                    $var = StringSplit($s_text[$a2],"=",1)
                    If $var[0] > 1 Then
                        If $var[1] = $Key Then
                            $KeyFound = StringTrimLeft($s_text[$a2],StringLen($var[1])+1)
                            If $KeyFound <> "" Then $Output = $KeyFound
                            ExitLoop 2
                        EndIf
                    EndIf
                    If StringLeft($var[1],1) = "[" And StringRight($var[1],1) = "]" Then ExitLoop 2
                Next
            EndIf
            ExitLoop
        EndIf
    Next
    Return $Output
EndFunc

Func IniWriteText($Text,$Section,$Key,$Value = "")
    $Output = -1
    If $Text = "" Then
        $Output = "["&$Section&"]"&@CRLF&$Key&"="&$Value
    Else
        $s_text = StringSplit($Text,@CRLF,1)
        $SecLine = $s_text[0]
        $FoundSec = 0
        For $a = 1 To $s_text[0]
            If $s_text[$a] = "["&$Section&"]" Then
                $SecLine = $a
                $FoundSec = 1
                ExitLoop
            EndIf
        Next
        If $FoundSec = 1 Then
            If $SecLine = $s_text[0] Then
                ;MsgBox(0,"",26)
                $Output = $Text&@CRLF&$Key&"="&$Value
            Else
                ;MsgBox(0,"",26)
                Local $FoundKey = 0 , $EndSec = $SecLine
                For $a = $SecLine+1 To $s_text[0]
                    ;MsgBox(0,"33",$s_text[$a])
                    $s2_text = StringSplit($s_text[$a],"=",1)
                    If $s2_text[0] > 1 Then
                        If $s2_text[1] = $Key Then
                            ;MsgBox(0,"","37")
                            $s_text[$a] = $Key&"="&$Value
                            $FoundKey = 1
                            ExitLoop
                        EndIf
                    EndIf
                    If StringLeft($s_text[$a],1) = "[" And StringRight($s_text[$a],1) = "]" Or $a = $s_text[0] Then
                        ;MsgBox(0,"",$s_text[$a])
                        If $a < $s_text[0] Then
                            For $a2 = $a-1 To 1 Step -1
                                If $s_text[$a2] <> "" Then
                                    $EndSec = $a2
                                    ExitLoop
                                EndIf
                            Next
                        Else
                            $EndSec = $a
                        EndIf
                        ExitLoop
                    EndIf
                Next
                If $FoundKey = 0 Then
                    ;MsgBox(0,"","")
                    _ArrayInsert($s_text,$EndSec+1,$Key&"="&$Value)
                    $s_text[0] = $s_text[0]+1
                EndIf
                $tmp = $s_text[1]
                For $a = 2 To $s_text[0]
                    $tmp = $tmp&@CRLF&$s_text[$a]
                Next
                $Output = $tmp
            EndIf
        Else
            $Output = $Text&@CRLF&"["&$Section&"]"&@CRLF&$Key&"="&$Value
        EndIf
    EndIf
    Return $Output
EndFunc


Func IniReadSectionText($Text,$Section)
    Local $Output[1]
    $s_text = StringSplit($Text,@CRLF,1)
    For $a = 1 To $s_text[0]
        If $s_text[$a] = "["&$Section&"]" Then
            If $a+1 <= $s_text[0] Then
                For $a2 = $a+1 To $s_text[0]
                    $var = StringSplit($s_text[$a2],"=",1)
                    If $var[0] > 1 Then _ArrayAdd($Output,StringTrimLeft($s_text[$a2],StringLen($var[1])+1))
                    If StringLeft($var[1],1) = "[" And StringRight($var[1],1) = "]" Then ExitLoop 2
                Next
                ExitLoop
            EndIf
        EndIf
    Next
    If UBound($Output) = 1 Then $Output = 1
    Return $Output
EndFunc

Func IniReadSectionNamesText($Text)
    Local $Output[1]
    $s_text = StringSplit($Text,@CRLF,1)
    For $a = 1 To $s_text[0]
        If StringInStr($s_text[$a],"=") = 0 And StringLeft($s_text[$a],1) = "[" And StringRight($s_text[$a],1) = "]"Then
            _ArrayAdd($Output,StringTrimLeft(StringTrimRight($s_text[$a],1),1))
        EndIf
    Next
    If UBound($Output) = 1 Then $Output = @error
    Return $Output
EndFunc

Enjoy!

Link to comment
Share on other sites

I only looked through your code briefly, but what sets this apart from the native INI read/write functions?

with the native INI read/write functions first you will must to store a INI file (in the disk) because the native functions require a file to read from.

with my functions you don't must to store ini file to read the data.

It sounds not important but actually it is important if your INI settings are store in a page. in this case you don't have to download the page to the disk and then read the page from the disk. you can read the page into variable (stored in the memory) and then you read the data from the variable.

but you might say it's not so matter because download the page(file), then read the page and then delete the page is not big deal.

so here is another good point - if you want to store the INI data with encryption(so the ini data will look like this for example: JHFE37GD458FS.......)

so if you will use the native INI read/write functions then you can't read from such data. first you will have to create somthing like "temp.txt" with decrypted data for the nativeINI read/write functions because they need file. this can be a security hole because you create a file with decrypted data and the user can read the unncrypted data from the temp file.

with my functions you don't have to create a temp file just for reading the data. the temp file in my case is variable that containing the decrypted data. so nothing is saved to the disk while you read the encrypted data.

Edited by Guest
Link to comment
Share on other sites

  • 3 years later...

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...