Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

_IniString Functions (was Ini_InMem)


  • Please log in to reply
24 replies to this topic

#1 ResNullius

ResNullius

    Drink Deep, or Taste Not the Pierian Spring

  • Active Members
  • PipPipPipPipPipPip
  • 1,037 posts

Posted 02 June 2008 - 09:48 AM

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

Attached Files


Edited by ResNullius, 10 June 2008 - 12:16 AM.








#2 SmOke_N

SmOke_N

    It's not what you know ... It's what you can prove!

  • Moderators
  • 16,006 posts

Posted 02 June 2008 - 12:37 PM

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

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.


#3 ResNullius

ResNullius

    Drink Deep, or Taste Not the Pierian Spring

  • Active Members
  • PipPipPipPipPipPip
  • 1,037 posts

Posted 02 June 2008 - 01:56 PM

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!

#4 CrazyMick

CrazyMick

    Seeker

  • Active Members
  • 5 posts

Posted 02 June 2008 - 08:13 PM

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

#5 ResNullius

ResNullius

    Drink Deep, or Taste Not the Pierian Spring

  • Active Members
  • PipPipPipPipPipPip
  • 1,037 posts

Posted 03 June 2008 - 01:12 AM

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.

#6 ResNullius

ResNullius

    Drink Deep, or Taste Not the Pierian Spring

  • Active Members
  • PipPipPipPipPipPip
  • 1,037 posts

Posted 03 June 2008 - 01:14 AM

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.

#7 steve8tch

steve8tch

    Universalist

  • Active Members
  • PipPipPipPipPip
  • 291 posts

Posted 03 June 2008 - 03:45 AM

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..

#8 ResNullius

ResNullius

    Drink Deep, or Taste Not the Pierian Spring

  • Active Members
  • PipPipPipPipPipPip
  • 1,037 posts

Posted 04 June 2008 - 12:40 AM

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, 04 June 2008 - 12:42 AM.


#9 MrCreatoR

MrCreatoR

    Must AutoIt!

  • MVPs
  • 3,251 posts

Posted 04 June 2008 - 04:00 AM

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: Attached File  IniString_UDFs.zip   2.29KB   541 downloads
Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

Posted Image AutoIt Russian CommunityPosted Image Projects: 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 ProgramPosted Image UDFs: 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 UDFPosted Image Examples: 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 DemoLike the examples/UDFs? Please rate the topic (up-right corner of the post header: Rating Posted Image)* === My topics === *

==========================================================Posted Image==========================================================

AutoIt is simple, subtle, elegant. © AutoIt Team


#10 ResNullius

ResNullius

    Drink Deep, or Taste Not the Pierian Spring

  • Active Members
  • PipPipPipPipPipPip
  • 1,037 posts

Posted 04 June 2008 - 01:51 PM

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: Attached File  IniString_UDFs.zip   2.29KB   541 downloads

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, 04 June 2008 - 02:08 PM.


#11 ResNullius

ResNullius

    Drink Deep, or Taste Not the Pierian Spring

  • Active Members
  • PipPipPipPipPipPip
  • 1,037 posts

Posted 09 June 2008 - 05:57 PM

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.

#12 MrCreatoR

MrCreatoR

    Must AutoIt!

  • MVPs
  • 3,251 posts

Posted 09 June 2008 - 07:49 PM

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, 15 May 2011 - 11:42 PM.

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

Posted Image AutoIt Russian CommunityPosted Image Projects: 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 ProgramPosted Image UDFs: 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 UDFPosted Image Examples: 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 DemoLike the examples/UDFs? Please rate the topic (up-right corner of the post header: Rating Posted Image)* === My topics === *

==========================================================Posted Image==========================================================

AutoIt is simple, subtle, elegant. © AutoIt Team


#13 ResNullius

ResNullius

    Drink Deep, or Taste Not the Pierian Spring

  • Active Members
  • PipPipPipPipPipPip
  • 1,037 posts

Posted 10 June 2008 - 12:04 AM

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, 10 June 2008 - 12:05 AM.


#14 MrCreatoR

MrCreatoR

    Must AutoIt!

  • MVPs
  • 3,251 posts

Posted 10 June 2008 - 12:15 AM

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, 10 June 2008 - 12:16 AM.

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

Posted Image AutoIt Russian CommunityPosted Image Projects: 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 ProgramPosted Image UDFs: 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 UDFPosted Image Examples: 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 DemoLike the examples/UDFs? Please rate the topic (up-right corner of the post header: Rating Posted Image)* === My topics === *

==========================================================Posted Image==========================================================

AutoIt is simple, subtle, elegant. © AutoIt Team


#15 WeMartiansAreFriendly

WeMartiansAreFriendly

    Where's the kaboom?

  • Active Members
  • PipPipPipPipPipPip
  • 1,245 posts

Posted 22 June 2009 - 08:18 PM

Will be very useful.
Posted ImageDon't bother, It's inside your monitor!------GUISetOnEvent should behave more like HotKeySet()

#16 autoall

autoall

    Seeker

  • Active Members
  • 21 posts

Posted 10 August 2009 - 03:34 AM

great work, I will try it.

#17 Malkey

Malkey

  • MVPs
  • 1,547 posts

Posted 13 August 2009 - 12:22 AM

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.

AutoIt         
; #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 ============ ;


#18 ResNullius

ResNullius

    Drink Deep, or Taste Not the Pierian Spring

  • Active Members
  • PipPipPipPipPipPip
  • 1,037 posts

Posted 13 August 2009 - 12:45 AM

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 >_<

:(

#19 arcker

arcker

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 581 posts

Posted 04 May 2010 - 02:28 PM

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.

AutoIt         
; #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: <pre class="prettyprint lang-nocode"> - 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 [code=auto:0] 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 :

  • Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here

#20 arcker

arcker

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 581 posts

Posted 29 July 2011 - 08:21 AM

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, 29 July 2011 - 08:23 AM.

-- Arck System _ Soon -- Ideas make everything

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

 

Projects :

  • Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users