Sign in to follow this  
Followers 0

_IniString Functions (was Ini_InMem)

25 posts in this topic

#1 ·  Posted (edited)

UPDATE: 09-June-08 All functions completed: now complete mimic of standard IniRead/Write Functions with same parameters, returns, and errors. Re-write of some functions: see download in this post

UPDATE: 03-June-08 Now named _IniString_Read() etc. & code cleaned up/simplified more. Thanks MrCreatoR!

UPDATE: 03-June-08 Rewrote some of the code to make better use of RegExp and reduce complexity(?)

UPDATE: 02-June-08 Added _Ini_InMem_Delete(), Renamed UDF to Ini_InMem.au3 as now does more than just read

Over here http://www.autoitscript.com/forum/index.ph...showtopic=72602 CrazyMick was looking for a way to access the contents of an Ini file that's stored on a web server without downloading the file to the hard drive.

Obviously _InetGetSource() is the answer, but then being a noob with AutoIt CrazyMick didn't know how to parse the data once it was stored in a string variable.

Anyway, I put together these _Ini_InMem_Read() functions.

They behave just like the standard IniRead() functions with the same returns and error setting, except that instead of an actual Ini file name, they take a variable containing a string representing an Ini file structure as the first parameter.

Certainly they will be of limited use but I thought I would post them anyway.

Can also be used with reading an Ini file into memory; $var = FileRead("some.ini").

Notes: Haven't cleaned up all the code yet, may be some stray references to variables that aren't actually used.

Some brighter coder could probably simplify these functions, and if so, I'm open to suggestions.

Edit: Some brighter coder did... see MrCreatoR's post below: _IniString() Functions

Update: this attachement contains the latest version and replaces MrCreatoR's in post # 9 below

IniString_UDF.zip ( 3.18K ) Number of downloads: 7

IniString_UDF.zip

Edited by ResNullius

Share this post


Link to post
Share on other sites



#2 ·  Posted

Awesome, I had thought about adding a string option to these http://www.autoitscript.com/forum/index.ph...st&p=427420 just last night.


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#3 ·  Posted

Awesome, I had thought about adding a string option to these http://www.autoitscript.com/forum/index.ph...st&p=427420 just last night.

Golly! Thanks SmOke_N :)

I haven't bothered with the rest of the Ini functions yet, but am planning to add them, so it'll be more of an "_Ini_InMem" UDF.

Or should I call them "_Ini_AsStr"? Or something else?

Naming and other suggestions welcome!

Share this post


Link to post
Share on other sites

#4 ·  Posted

Wow, really awesome.

I have to say, that I don´t understand everything, written in the _Ini_InMem_Read.au3 so I´ve to read some parts more than once or twice but it helps me to get a better understanding how it works.

Thank you very much for helping me with that problem.

Greets

Mick

Share this post


Link to post
Share on other sites

#5 ·  Posted

Wow, really awesome.

I have to say, that I don´t understand everything, written in the _Ini_InMem_Read.au3 so I´ve to read some parts more than once or twice but it helps me to get a better understanding how it works.

Thank you very much for helping me with that problem.

Greets

Mick

Your welcome Mick. If you have any questions, just ask.

Share this post


Link to post
Share on other sites

#6 ·  Posted

Updated: Added _Ini_InMem_Delete(), Renamed UDF to Ini_InMem.au3 as now does more than just read :)

Will add _Ini_InMem_RenameSection, _Ini_InMem_Write, and _Ini_InMem_WriteSection just for the sake of completeness.

Share this post


Link to post
Share on other sites

#7 ·  Posted

a name suggestion :)

Firstly - well done - good idea..

_Ini_InMem_xxx sounds quite awkward - I was trying to think of a name that reflected that the ini file was a String at this point -

so I was trying names along the lines

_iniStr_Read(

_iniStr_ReadSection(

etc...

just a thought..

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

a name suggestion :)

Firstly - well done - good idea..

_Ini_InMem_xxx sounds quite awkward - I was trying to think of a name that reflected that the ini file was a String at this point -

so I was trying names along the lines

_iniStr_Read(

_iniStr_ReadSection(

etc...

just a thought..

Thanks steve8tch.

Yeah, I'm not terribly happy with the name either. I'm leaning towards something like _Ini_AsStr_Read() etc.

Edit: Actually maybe not such a good name for the all lower case users: _Ini_asstr_read()

But still open to suggestions...

Also, updated the code to use RegExp better. See 1st post

[grumble] I hate how you can't use the "autoit" code tags when you edit a post, always get a TAG error message :)[/grumble]

Edited by ResNullius

Share this post


Link to post
Share on other sites

#9 ·  Posted

Really Nice, i like the idea (Btw, i also thought about it some time ago) :) .

I modified the UDF a little(?)...

* Renamed «_Ini_InMem_...» to «_IniString_.....».

* Added 2 functions from the "to do"; _IniString_RenameSection() and _IniString_Write() - But they needs more testing i think...

* Changed the examples.

Modified version: IniString_UDFs.zip


Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Really Nice, i like the idea (Btw, i also thought about it some time ago) :) .

I modified the UDF a little(?)...

* Renamed «_Ini_InMem_...» to «_IniString_.....».

* Added 2 functions from the "to do"; _IniString_RenameSection() and _IniString_Write() - But they needs more testing i think...

* Changed the examples.

Modified version: IniString_UDFs.zip

Great! Thanks for cleaning up the code: I was going to do it once I got around to getting all the functions complete.

Also, I like your naming convention, so that's what it will be.

I should be able to complete the WriteSection() function today.

Until then, I'm editing the first post to point to your zip download.

Cheers :)

Edited by ResNullius

Share this post


Link to post
Share on other sites

#11 ·  Posted

All functions now complete! See 1st post for updated zip containing udf and examples.

Many thanks to co-author MrCreatoR :)

Fixed/re-wrote some existing functions.

Please provide feedback if you use. Thanks.

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Excelent :).

One remark... imo, this:

While StringRight($sTempIni, 2) = @CRLF
    $sTempIni = StringTrimRight($sTempIni, 2)
WEnd

not really neccessary, the same can be done using StringStripWS($sTempIni, 2).

http://www.autoitscript.com/autoit3/docs/functions/StringStripWS.htm

Strips the white space in a string.

....

Whitespace includes Chr(9) thru Chr(13) which are HorizontalTab, LineFeed, VerticalTab, FormFeed, and CarriageReturn.

Edited by MrCreatoR

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Excelent :).

One remark... imo, this:

While StringRight($sTempIni, 2) = @CRLF
    $sTempIni = StringTrimRight($sTempIni, 2)
WEnd

not really neccessary, the same can be done using StringStripWS($sTempIni, 2).

Whoops! It's always the simple things we overlook. Code updated. Thanks again MrCreatoR!

PS: When did you change from MsCreatoR to MrCreatoR (from a Ms. to a Mr. ) ?

Edited by ResNullius

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

When did you change from MsCreatoR to MrCreatoR (from a Ms. to a Mr. ) ?

A few month ago or so (Edit: 3rd May 2008 - 12:01 AM :) )... at the begining my english was not so good, i thought Ms. it's like Mister :) , But today i realize that Mister it's with Mr... or it's not? :)

Btw: you can see the «Display name history» in my profile (under the "Options"), or here (the same).

Edited by MrCreatoR

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

#15 ·  Posted

Will be very useful.


Don't bother, It's inside your monitor!------GUISetOnEvent should behave more like HotKeySet()

Share this post


Link to post
Share on other sites

#16 ·  Posted

great work, I will try it.

Share this post


Link to post
Share on other sites

#17 ·  Posted

A non-array approach.

Where an array is not absolutely necessary, why not just get the required string from the ini string.

I might be missing something, but this example does look simpler - to me anyway.

Just a thought, for your consideration.

;
#include <Inet.au3>

$sSource_Mem_Ini = _INetGetSource("http://www.autoitscript.com/autoit3/files/beta/update.dat")
;or
;$sSource_Mem_Ini = FileRead(@ScriptDir & "\AutoIt.dat") ;read in ini from saved file

;============ _IniString_ReadValue - Example ============

$sIniRead = _IniString_ReadValue($sSource_Mem_Ini, "AutoIt", "FileSize", "NotFound")
MsgBox(4096 + 64, "_IniReadValue: [code] - filesize", $sIniRead)

Func _IniString_ReadValue($sIni, $sSection, $sKey, $sDefault = "")
    Local $sRet
    If StringInStr($sIni, "[" & $sSection & "]", 2) <> 0 Then ; Check if $sSection section exists.
        Local $sSect = StringRegExpReplace($sIni, "(?i)(?s)(?:.*)(?-s)(\[" & $sSection & "\]\v+(.+=.*\v)*)(?s)(?:.*)", "\1") ; Get entire $sSection section as string.
        ;ConsoleWrite($sSect & @CRLF)
        If StringLower(StringRegExpReplace($sSect, "(?i)(?m)(?:.*\v+)*(^" & $sKey & ")(=.*)\v+(?s)(.*)\z", "\1")) = StringLower($sKey) Then ; Check if $sKey exists.
            $sRet = StringRegExpReplace($sSect, "(?i)(?m)(?:.*\v+)*(?:^" & $sKey & "=)(.*)\v+(?s)(.*)\z", "\1") ; Get corresponding value of $sKey.
        Else
            $sRet = $sDefault
        EndIf
    Else
        $sRet = $sDefault
    EndIf
    Return $sRet
EndFunc   ;==>_IniString_ReadValue

;============ _IniString_ReadValue - Example ============
;

Share this post


Link to post
Share on other sites

#18 ·  Posted

A non-array approach.

Where an array is not absolutely necessary, why not just get the required string from the ini string.

I might be missing something, but this example does look simpler - to me anyway.

Just a thought, for your consideration.

Thanks for the input Malkey.

I've been meaning to revisit these functions and tighten them up; they are after all a year old and I've learned a lot more about RegExp in that time.

I think the array approach just carried through from the original learning of using RegExp to return matches.

PS: But really now, who ever said simpler is better?

Especially when showing off our ninja coder skilz >_<

:(

Share this post


Link to post
Share on other sites

#19 ·  Posted

A non-array approach.

Where an array is not absolutely necessary, why not just get the required string from the ini string.

I might be missing something, but this example does look simpler - to me anyway.

Just a thought, for your consideration.

;
#include <Inet.au3>

$sSource_Mem_Ini = _INetGetSource("http://www.autoitscript.com/autoit3/files/beta/update.dat")
;or
;$sSource_Mem_Ini = FileRead(@ScriptDir & "\AutoIt.dat") ;read in ini from saved file

;============ _IniString_ReadValue - Example ============

$sIniRead = _IniString_ReadValue($sSource_Mem_Ini, "AutoIt", "FileSize", "NotFound")
MsgBox(4096 + 64, "_IniReadValue:
- filesize", $sIniRead)

Func _IniString_ReadValue($sIni, $sSection, $sKey, $sDefault = "")
    Local $sRet
    If StringInStr($sIni, "[" & $sSection & "]", 2) <> 0 Then ; Check if $sSection section exists.
        Local $sSect = StringRegExpReplace($sIni, "(?i)(?s)(?:.*)(?-s)(\[" & $sSection & "\]\v+(.+=.*\v)*)(?s)(?:.*)", "\1") ; Get entire $sSection section as string.
        ;ConsoleWrite($sSect & @CRLF)
        If StringLower(StringRegExpReplace($sSect, "(?i)(?m)(?:.*\v+)*(^" & $sKey & ")(=.*)\v+(?s)(.*)\z", "\1")) = StringLower($sKey) Then ; Check if $sKey exists.
            $sRet = StringRegExpReplace($sSect, "(?i)(?m)(?:.*\v+)*(?:^" & $sKey & "=)(.*)\v+(?s)(.*)\z", "\1") ; Get corresponding value of $sKey.
        Else
            $sRet = $sDefault
        EndIf
    Else
        $sRet = $sDefault
    EndIf
    Return $sRet
EndFunc   ;==>_IniString_ReadValue

;============ _IniString_ReadValue - Example ============
;
Hi,indeed this is faster but it is bugged ...it takes the last caracter; so @cri've changed
If StringLower(StringRegExpReplace($sSect, "(?i)(?m)(?:.*v+)*(^" & $sKey & ")(=.*)v+(?s)(.*)z", "1")) = StringLower($sKey) Then ; Check if $sKey exists.            $sRet = StringRegExpReplace($sSect, "(?i)(?m)(?:.*v+)*(?:^" & $sKey & "=)(.*)v+(?s)(.*)z", "1") ; Get corresponding value of $sKey.with

If StringLower(StringRegExpReplace($sSect, "(?i)(?m)(?:.*\v+)*(^" & $sKey & ")(=.*)\v+(?s)(.*)\n", "\1")) = StringLower($sKey) Then ; Check if $sKey exists.
            $sRet = StringRegExpReplace($sSect, "(?i)(?m)(?:.*\v+)*(?:^" & $sKey & "=)(.*)\v+(?s)(.*)\n", "\1") ; Get corresponding value of $sKey.

so \z by \n. It seems to work but not sure if it's the best solution.


-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

Faster than everything. Previous = 30ms

Mine is around 1ms, and no more bugged.

Used in a recent project for reading a "database" stored as an inifile.

Func _IniString_Read($sIni, $sSection, $sKey, $sDefault = "")
    ;by Arcker
    ; On 08/04/2011
    ; Only one regexp to get the value !
    ; execution < 1 ms
    Local $sSect = StringRegExp($sIni, "(?i)(?s)(?m)(?:.*)(\["& $sSection &"\].*?^"&$sKey&"=)(.+?$)",1) ; Get entire $sSection section as string.
    if @error then
        return $sDefault
    Else
        return StringStripCR($sSect[1])
    EndIf

endfunc
Edited by arcker

-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]

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