michelb2 Posted August 12 Posted August 12 I use a extended version of iniread for inserting comment and variable in a normal iniread. 1) after a ; all text are ignored. AND 2) ;variable autoit user commence et fini par $ $bdmount$ => $bdmount ;variable autoit native commence et fini par @ @Tempdir@ => @tempdir ;variable systeme native commence et fini par % %path% => %path% EXEMPLES : bdmount="w:" path_log=e:\Mes documents\HCFR\logs\ ;vrai sauf pour bootime où path_log est redéfini à e:\sna10\xfert\ pour ecrire boot_time.log daemon_mount= /l=$bdmount$ "µisoµ" ;en théorie =w ; ici =w: mais seule la 1ere lettre compte ; de plus =w inutile (mount sur le 1er disque virtuel ?) fichier_resultats=@Tempdir@\result.log ; fichier resultats des actions effectuées ""=pas de sauve all is good except "x" ; commentaire" ==>> x" could you help me ? inireadcustom.au3
argumentum Posted August 12 Posted August 12 Func IniReadOrInit($sFilename, $sSection, $sKey, $sDefault, $iExcludeComments = 1) Local $iErr = 0, $sVal = IniRead($sFilename, $sSection, $sKey, @CRLF) ; IniRead ( "filename", "section", "key", "default" ) If $sVal = @CRLF Then $iErr = Int(Not IniWrite($sFilename, $sSection, $sKey, $sDefault)) ; IniWrite ( "filename", "section", "key", "value" ) $sVal = $sDefault EndIf If $iExcludeComments And StringInStr($sVal, @TAB & ";") Then $sVal = StringStripWS(StringLeft($sVal, StringInStr($sVal, @TAB & ";") - 1), 3) Return SetError($iErr, 0, $sVal) EndFunc ;==>IniReadOrInit This is what I use. @TAB & ";" is not likely to be found by accident. Use that ? Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
michelb2 Posted August 12 Author Posted August 12 (edited) i use {space x N}; or {tab x N}; indifferentially so i would prefere not to be force to use @TAB & ";" but i think is good $sVal = StringStripWS(StringLeft($sVal, StringInStr($sVal, ";") - 1), 3) Edited August 12 by michelb2
michelb2 Posted August 12 Author Posted August 12 it's better to use if stringregexp($out,";") then $out = StringStripWS(StringLeft($out, StringInStr($out, ";") - 1), 3)
Developers Jos Posted August 12 Developers Posted August 12 Moved to the appropriate AutoIt General Help and Support forum, as the AutoIt Example Scripts forum very clearly states: Quote Share your cool AutoIt scripts, UDFs and applications with others. Do not post general support questions here, instead use the AutoIt Help and Support forums. Moderation Team SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
Nine Posted August 12 Posted August 12 (edited) Here my take on it : expandcollapse popup; From Nine #include <Constants.au3> Local $sText = IniReadEx("Test.ini", "Section", "cle1", Null) ConsoleWrite("[" & $sText & "]" & @CRLF) $sText = IniReadEx("Test.ini", "Section", "cle2") ConsoleWrite("[" & $sText & "]" & @CRLF) $sText = IniReadEx("Test.ini", "Section", "heure") ConsoleWrite("[" & $sText & "]" & @CRLF) $sText = IniReadEx("Test.ini", "Section", "boot") ConsoleWrite("[" & $sText & "]" & @CRLF) $sText = IniReadEx("Test.ini", "Section", "Log") ConsoleWrite("[" & $sText & "]" & @CRLF) $sText = IniReadEx("Test.ini", "Section", "New", "$disk$\%username%\@scriptname@") ConsoleWrite("[" & $sText & "]" & @CRLF) Func IniReadEx($sFileName, $sSection, $sKey, $sDefault = Null) Local $sOut = IniRead($sFileName, $sSection, $sKey, $sDefault) If $sOut == $sDefault Then IniWrite($sFileName, $sSection, $sKey, $sDefault) $sOut = StringRegExpReplace($sOut, "\h*;.*", "") ; comment $sOut = StringRegExpReplace($sOut, '^"(.*?)"$', "$1") ; double-quotes Local $aText = StringRegExp($sOut, '@\w+?@', $STR_REGEXPARRAYGLOBALMATCH) ; autoit macro If Not @error Then For $sMacro In $aText $sOut = StringReplace($sOut, $sMacro, Execute(StringTrimRight($sMacro, 1))) Next EndIf $aText = StringRegExp($sOut, '\$\w+?\$', $STR_REGEXPARRAYGLOBALMATCH) ; other key If Not @error Then For $sSecKey In $aText $sOut = StringReplace($sOut, $sSecKey, IniReadEx($sFileName, $sSection, StringReplace($sSecKey, "$", ""))) Next EndIf $aText = StringRegExp($sOut, "%\w+?%", $STR_REGEXPARRAYGLOBALMATCH) ; environment If Not @error Then For $sEnv In $aText $sOut = StringReplace($sOut, $sEnv, EnvGet(StringReplace($sEnv, "%", ""))) Next EndIf Return $sOut EndFunc ;==>IniReadEx With this ini file : [Section] cle1=test1 ; comment cle2="Test2" ; comment with tab heure=@HOUR@:@MIN@:@SEC@ disk=W: boot=$disk$\test\boot\@username@ Log=%ProgramData%\AutoIt\@username@\log ; plus a comment Edited August 12 by Nine “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
argumentum Posted August 12 Posted August 12 (edited) ...at times I make a folder representing "C:\this\here" and since that can not be done as a representation, I name the folder "C;,this,here" because it makes sense to me. Then I figure that this character ";" could be part of a filename. Hence the @tab + ";". I don't like it either, but, how can I make sure ( or less likely ) that the code will not make a mistake 🤷♂️ With the "@tab & ;" I can have an entry like: path=\\share\backups\c;,this,here@TAB; A ";" represents ":" and "," represent "\" as a comment without confusing the code, because is hard to foresee what a user will feel like doing. An inline comment in an ini file was never meant to be there so, get creative PS: When not user editable, I'd use "face delimited fields" ( Chr(1) looks like a face ) but with a user editable, @TAB; is the best I came up with. Do share your brainstorming if you find a nicer way because, I too dislike the solution I came up with. Edit: https://gist.github.com/dk949/88b2652284234f723decaeb84db2576c has good ideas. Edit 2: https://futhark-lang.org/blog/2017-10-10-block-comments-are-a-bad-idea.html sees my point. I think that the common /* comment */ is a good solution 🤔 Edited August 12 by argumentum more Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
ioa747 Posted August 12 Posted August 12 (edited) Here is my approach, taking into account @argumentum concerns about using ';' why not use a rarer character ? like: 👉, 💬, 🚩, ⁉, ⚠, ℹ, ♠, ⋮, ⅈ, © Local $sFile = @ScriptDir & "\test2.ini" Local $sMyComment ; Declare a variable to hold the comment ; Read the INI section labelled 'EXEMPLES'. This will return a 2 dimensional array. Local $aArray = IniReadSection($sFile, "EXEMPLES") If Not @error Then For $i = 1 To $aArray[0][0] Local $sResult = IniReadWithComment($sMyComment, $sFile, "EXEMPLES", $aArray[$i][0]) ConsoleWrite($aArray[$i][0] & " = " & $sResult & @CRLF) If $sMyComment Then ConsoleWrite(@TAB & "Comment: " & $sMyComment & @CRLF) Next EndIf Func IniReadWithComment(ByRef $sComment, $filename, $section, $key, $defaut = "") Opt("ExpandEnvStrings", 1) ;0=don't expand, 1=do expand Opt("ExpandVarStrings", 1) ;0=don't expand, 1=do expand Local $sValue = IniRead($filename, $section, $key, $defaut) Local $sCleanValue, $sRetValue Local $aPart = StringSplit($sValue, "👉", 1) If $aPart[0] = 2 Then $sCleanValue = StringStripWS($aPart[1], 3) $sComment = StringStripWS($aPart[2], 3) $sRetValue = $sCleanValue Else $sRetValue = $aPart[1] EndIf Opt("ExpandEnvStrings", 0) ;0=don't expand, 1=do expand Opt("ExpandVarStrings", 0) ;0=don't expand, 1=do expand Return $sRetValue EndFunc ;==>IniReadWithComment_ByRef test2.ini [EXEMPLES] bdmount="w:" path_log=e:\Mes documents\HCFR\logs\ 👉 vrai sauf pour bootime où path_log est redéfini à e:\sna10\xfert\ pour ecrire boot_time.log daemon_mount= /l=$bdmount$ "µisoµ" 👉 en théorie =w ; ici =w: mais seule la 1ere lettre compte ; de plus =w inutile (mount sur le 1er disque virtuel ?) fichier_resultats=@Tempdir@\result.log 👉 fichier resultats des actions effectuées ""=pas de sauve Time=@HOUR@:@MIN@:@SEC@ 👉 curent time File=$sFile$ 👉 this .ini file SCITE=%SCITE_USERHOME% 👉 On installation this variable is set to "%LOCALAPPDATA%\AutoIt v3\SciTE" for Vista and above and to "%USERPROFILE%\AutoIt v3\SciTE" for WinXP. Edited August 12 by ioa747 add SCITE=%SCITE_USERHOME% I know that I know nothing
argumentum Posted August 12 Posted August 12 18 minutes ago, ioa747 said: why not use a rarer character ? like: 👉, 💬, 🚩, ⁉, ⚠, ℹ, ♠, ⋮, ⅈ, © if that was the case then my "face delimited(TM)" approach using chr(1) is just as good It would have to be a keyboard character ( yes, I know about Win + "." ) so that a user could add a comment. The C style is widely known and should work. But is a pain to remove the last comment using regex. Local $sString = "Some code here /* first comment */ more code /* second comment */ /* /* last comment */" The AI is stuck and I don't know regex. I can do it with StringInStr() but regex is the challenge ioa747 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
michelb2 Posted August 13 Author Posted August 13 @Nine I like your solution ! the only "bad thing" is fault of iniread itself t1=xx yy t2="xx yy" t3=""xx yy"" t1 and t2 report the same thing => xx yy only t3 report "xx yy" ...but i can live with that 🙂
Nine Posted August 13 Posted August 13 oh you mean that only 2 double-quotes should result in single double-quotes ? That is quite easy to modify, try it, and come back if can't find a solution. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
michelb2 Posted August 13 Author Posted August 13 no. i would prefer to find the function equal to the source in the .ini like inireadex ( t1)= xx yy inireadex ( t2)= "xx yy" inireadex ( t3)= ""xx yy""
Nine Posted August 13 Posted August 13 Just remove that line : $sOut = StringRegExpReplace($sOut, '^"(.*?)"$', "$1") ; double-quotes “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
argumentum Posted August 13 Posted August 13 ..I don't know. The ini file is what win v3.1 used to save initialization parameters. DOS may have used it too. You could FileReadToArray() the ini file and do whatever you feel like doing with it and that would avoid the shortcomings of the "standard" ini file. On the left of the first "=" is the key, and on the right the data. Come up with the "MMF" ( @michelb2 Multipurpose File ) and it could replace the INI standard, and JSON too, why not ! Joking aside, you can code whatever you feel like having. No need to use the standard ini functions if they are limiting your needs. Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
michelb2 Posted August 13 Author Posted August 13 (edited) great ! i'll try it tomorrow (22h00 in france) Edited August 13 by michelb2
michelb2 Posted August 14 Author Posted August 14 using _filereadtoarray, its easier BUT i can't create variable assign doesn't create normal variable i need to create one by one (like with iniread) Can i do better ? (section are not handle at the moment) test_inireadex.au3 variable_read_exemple.ini test_inireadex.au3 variable_read_exemple.ini
argumentum Posted August 14 Posted August 14 "We could not locate the item you are trying to view." Can't download the files Just paste the code in a code box. ( the <> button ) Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
michelb2 Posted August 14 Author Posted August 14 expandcollapse popup#include <Array.au3> #include <File.au3> local $a,$r,$out,$tmp _FileReadToArray("c:\util\autoit\sources\variable_read_exemple.ini " ,$a,0) for $i=UBound($a)-1 to 0 step -1 $a[$i]=StringRegExpReplace($a[$i], "\h*;.*", "") ;suppression commentaire if $a[$i]="" then _ArrayDelete($a,$i) ;suppression ligne vide $out=$a[$i] if stringregexp($out,'@.*?@') then ; pour utiliser une variable d'environnement (@xxx "texte") la varialble commence et fini par @ $tmp=stringregexp($out,'(@.*?@)',1)[0] $out=StringReplace($out,$tmp,execute(StringTrimRight ( $tmp,1))) EndIf if stringregexp($out,'\$.*?\$') then ; pour utiliser une variable autoit ($xxx "texte") la varialble commence et fini par $ $tmp=stringregexp($out,'(\$.*?\$)',1)[0] $out=StringReplace($out,$tmp,execute(StringTrimRight ( $tmp,1))) EndIf if stringregexp($out,"%.*?%") then $out= EnvGet(stringregexp($out,"%(.*?)%",1)[0]) & StringRegExpReplace($out,"%.*?%","") ; pour utiliser une variable d'environnement (%xxx%texte) EndIf $a[$i]=$out next _ArrayDisplay($a) ;============================== ;for $i=0 to UBound($a) $i=1 $r=StringRegExp($a[$i],"(.*?)=(.*)",3) if isarray($r) then ;msgbox(0,"",$r[0]&@cr&$r[1]) assign ($r[0],$r[1]) local $t1=eval($r[0]) msgbox(0,"",$t1) EndIf ;next Exit ;================================================================ variable_read_exemple.ini [test] ; ligne commentaire t0= /l=$bdmount$ "ÁisoÁ" ;en thÚorie =w ; ici =w: mais seule la 1ere lettre compte ; de plus =w inutile (mount sur le 1er disque virtuel ?) t1="x y" ; "commentaire" comment2 t2="x y" t3='"x y"' t4=x y t5=""x y"" ;com bdmount="w:" path_log=e:\Mes documents\HCFR\logs\ ;vrai sauf pour bootime o¨ path_log est redÚfini Ó e:\sna10\xfert\ pour ecrire boot_time.log daemon_mount= /l=$bdmount$ "ÁisoÁ" ;en thÚorie =w ; ici =w: mais seule la 1ere lettre compte ; de plus =w inutile (mount sur le 1er disque virtuel ?) fichier_resultats=@Tempdir@\result.log ; fichier resultats des actions effectuÚes ""=pas de sauve
ioa747 Posted August 14 Posted August 14 (edited) expandcollapse popup#include <Array.au3> $ini = _MapIni("test4.ini") ConsoleWrite("$ini.test.t0:" & $ini.test.t0 & @CRLF) ConsoleWrite("$ini.test.daemon_mount:" & $ini.test.daemon_mount & @CRLF) ConsoleWrite("$ini.test2.z2:" & $ini.test2.z2 & @CRLF) ; $iStripWS [optional] default is 3. This is the flag for StringStripWS(). Set to zero to disable. Func _MapIni($sFilePath, $iStripWS = 3) Local $sFileTxt = FileRead($sFilePath) If @error Then Return SetError(1, 0, "") ; Put a @CRLF in start, and convert all @LF, @CR to @CRLF $sFileTxt = @CRLF & StringRegExpReplace($sFileTxt, "(\r\n|\n)", @CRLF) ; split String in (@CRLF & "[") Local $aPart = StringSplit($sFileTxt, @CRLF & "[", 1) If $aPart[0] < 2 Then Return SetError(2, 0, "") Local $aSections[$aPart[0]][2] $aSections[0][0] = $aPart[0] - 1 Local $mMaps[] For $j = 2 To $aPart[0] ; normal first line is the (name & ]) Local $iPos = StringInStr($aPart[$j], "]", 0, -1) If Not $iPos > 0 Or @error Then Return SetError(3, 0, "") ; find the name and the Dimension Local $sName = StringLeft($aPart[$j], $iPos - 1) ; remove leading CRLF or LF from front & back Local $sString = StringRegExpReplace(StringTrimLeft($aPart[$j], $iPos + 2), '(^[\r\n]+|[\r\n]+$)', '') Local $a = StringSplit($sString, @CRLF, 3) Local $out, $m[] For $i = UBound($a) - 1 To 0 Step -1 $a[$i] = StringRegExpReplace($a[$i], "\h*;.*", "") If $a[$i] = "" Then _ArrayDelete($a, $i) $out = $a[$i] If StringRegExp($out, '@.*?@') Then $tmp = StringRegExp($out, '(@.*?@)', 1)[0] $out = StringReplace($out, $tmp, Execute(StringTrimRight($tmp, 1))) EndIf If StringRegExp($out, '\$.*?\$') Then $tmp = StringRegExp($out, '(\$.*?\$)', 1)[0] $out = StringReplace($out, $tmp, Execute(StringTrimRight($tmp, 1))) EndIf If StringRegExp($out, "%.*?%") Then $out = EnvGet(StringRegExp($out, "%(.*?)%", 1)[0]) & StringRegExpReplace($out, "%.*?%", "") EndIf $a[$i] = $out Next For $i = 0 To UBound($a) - 1 Local $result = StringRegExp($a[$i], '^(.*?)=(.*)$', 3) If IsArray($result) Then $m[$result[0]] = StringStripWS($result[1], $iStripWS) Next $mMaps[$sName] = $m Next Return $mMaps EndFunc ;==>_MapIni test4.ini [test] ; ligne commentaire t0= /l=$bdmount$ "ÁisoÁ" ;en thÚorie =w ; ici =w: mais seule la 1ere lettre compte ; de plus =w inutile (mount sur le 1er disque virtuel ?) t1="x y" ; "commentaire" comment2 t2="x y" t3='"x y"' t4=x y t5=""x y"" ;com bdmount="w:" path_log=e:\Mes documents\HCFR\logs\ ;vrai sauf pour bootime o¨ path_log est redÚfini Ó e:\sna10\xfert\ pour ecrire boot_time.log daemon_mount= /l=$bdmount$ "ÁisoÁ" ;en thÚorie =w ; ici =w: mais seule la 1ere lettre compte ; de plus =w inutile (mount sur le 1er disque virtuel ?) fichier_resultats=@Tempdir@\result.log ; fichier resultats des actions effectuÚes ""=pas de sauve [test2] z1="d:" z2="""zz""" ;"test2" Edited August 14 by ioa747 [test2] I know that I know nothing
argumentum Posted August 14 Posted August 14 ...things to keep into consideration in regards to INI behavior: If the line starts with ";" (@CRLF & ";") , that line gets ignored. If the value of an entry has a ";" in it, add one to the end ( " ;" ) to avoid confusion in the code that would ignore part of the data because it fund one of those ";" characters. The key, and the data, are to be StringStripWS() pre-evaluation. The last one found is the one presented to the user ( I think if memory serves ). A key value is a string that can be almost anything, but would not work as map name ? $ini = StringTrimRight(@ScriptFullPath, 4) & ".ini" IniWrite($ini, "s", "k.k", "k.k2") ConsoleWrite( IniRead($ini, "s", "k.k", "failed k.k2") & @CRLF) IniWrite($ini, "s", "k,k", "k,k1") ConsoleWrite( IniRead($ini, "s", "k,k", "failed k,k1") & @CRLF) IniWrite($ini, "s", "k,k3", "k,k3") ConsoleWrite( IniRead($ini, "s", "k,k3", "failed k,k3") & @CRLF) IniWrite($ini, "s", ";k,k4", "k,k4") ConsoleWrite( IniRead($ini, "s", ";k,k4", "failed k,k4") & @CRLF) k.k2 k,k1 k,k3 failed k,k4 So, if a global/static map is going to hold the INI file, all regular INI functions should be replicated as INIishRead(), INIishWrite(), INIish* ( I say INIish but you choose a prefix that sounds better like DatRead, DatWrite, Dat* ) And, if all functions are to be replicated, as the test code above shows, a key named "k,k" or "k.k" is valid but would not be a good name in: ConsoleWrite("$ini.s.k,k:" & $ini.s.k,k & @CRLF) therefore a hash of the name could be used as the internal name, or something ? ( I don't have the time to code what am saying ) Also, am sure that INI replacements have been coded and could be found somewhere in the forum, but will have to search/find them. To not reinvent the wheel. Ok, good chat Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now