Jump to content

Recommended Posts

Posted

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

Posted
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.
autoit_scripter_blue_userbar.png

  • Developers
Posted

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

Posted (edited)

Here my take on it :

; 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 by Nine
Posted (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 :D ) 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 by argumentum
more

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted

Here is my approach,

taking into account @argumentum  concerns about using ';'  :D

:huh2:  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

 

I know that I know nothing

Posted
18 minutes ago, ioa747 said:

:huh2:  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 :D 
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 :) 

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

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
  • Recently Browsing   0 members

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