Sign in to follow this  
Followers 0
MilesAhead

comment ini file section

11 posts in this topic

I'm wondering if anyone has a tried and true method of preceding .ini file sections with comments? I've tried just generally sticking comments at the beginning by deleting the file, creating the file, writing comment lines, then closing the file. Then using the standard .ini file functions to put values in sections etc..

But I ran into side effects where even if I flush the file and close the file and delay before adding values using the .ini file functions, the file gets corrupted.

Seems like it would be a useful addition to have something like

IniWriteSectionComment(Filename,Section,string or array of comments)

that would be put in the file just before the named section.

Instead of a readme refering to the .ini file settings it could be documented in place.

Most of my small programs have only one section so in other languages it has worked to stick the comments in, close the file etc..

But I think the comment section function would be cool.

For now a work-around would be great too though. :)

Share this post


Link to post
Share on other sites



Although you can use comments in an ini file they are not a part of the ini file type specification and therefore not allowed using the standard ini* calls.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Although you can use comments in an ini file they are not a part of the ini file type specification and therefore not allowed using the standard ini* calls.

Technically, comments are a part of the ini file specification, they're preceeded by the semicolon. There's just no support for them in the standard INI* functions in AutoIt, although it shouldn't be too hard to create a function that does it.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Well imagine that, correct you are and AutoIt can write an inline comment.

IniWrite("test.ini", "General", "Test", "Some Value ;some comment")

This will also work for comments on a separate line but I can see it becoming a nightmare if there are any additions to the file at some point.

$sComment = @CRLF & @CRLF & ";Some comment goes here"
IniWrite("test.ini", "General", "Test2", "Some Other Value" & $sComment)

It could also be done if you don't specify the key but that requires a small fix after the file is written

$sLine = "=;Some comment goes here"
$sLine = StringRegExpReplace($sLine, "(?m:^)=", "")

EDIT:

I should have mentioned that if you are using the inline method the semi-colon must follow a whitespace.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thanks for the replies. But what I really want to be able to get is this kind of result:

; valid modifier keys include

; ^ for Control

; ! for Alt

; # for WinKey

;

; LButton left mouse button

; MButton middle mouse button

; RButton right mouse button

;

; Example: !^MButton would be

; Alt and Control while pressing

; Middle Mouse Button

;

[settings]

MouseHotkey=^MButton

KeyboardHotkey=!F8

PasteFilename=0

UseGlass=1

DesktopDir=C:\Utils

; Other comment

; Yet another comment

[NewSection]

yadda=yadda

Having the comments all mixed up with the settings just seems confusing. Better for documentation if I could put several lines just above a section.

Edited by MilesAhead
1 person likes this

Share this post


Link to post
Share on other sites

You can do that with a StringReplace() or StringRegExpReplace() after the file has been written but I don't see another way around it. Someone else may come up with an alternative yet.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Actually I just though of another possibility as long as you know all the section names in advance.

Create a template with everything there including the section names then just use the standard functions to add the values

;some comment goes here

;and line 2 here

;just like above

[section1]

;

;

;section 2 comments start here

;more

;and some more

[section2]

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Actually I just though of another possibility as long as you know all the section names in advance.

Create a template with everything there including the section names then just use the standard functions to add the values

Yeah, seems like it would be easier to just write the file using the normal write line functions. It just seems weird that there would be a delay flushing the file. That's the only reason I can think of why the hybrid approach was not dependable. It's only AutoIt3 that I got the corruption. I wonder if there's some issue with file flush in one of the libraries. Especially since I called flush on the file handle before closing, then closed it, then waited, you'd think then running the ini file functions wouldn't be a problem.

edit: now that I think of it, on the settings write side, I already know what's what so it's no big deal to write out the file. Just forces me to load an array in the proper sequence to get what I want into the file. It would just be easier for working code that already writes out a bunch of settings not to make changes.

Edited by MilesAhead

Share this post


Link to post
Share on other sites

I knocked up a function that should do what you're looking for as far as commenting a section in an ini file. See the code below, it includes error checking as well as the ability to add the comments before or after the section name. Note that when adding a comment to the ini file, if you are adding it before the section name, it will start at the line just before the section name then inserts the comments, then adds the section name and the rest of the ini file's contents. If adding a comment to a section after the section name, it will start at the next line after the name, insert the comments, and then the rest of the ini file's contents. The only reason this is mentioned is because you may want to split the comments into easily handled chunks and if you're not aware of how the comments are added, you may end up with the comments in the wrong order.

Also, this function does include the ability to add multi-line comments by separating the lines with the pipe ("|") character in the comment string.

I included a very short example script to demonstrate how it works, it will create a file called Test.ini in the scriptdir folder, populate it, then write the comments into it.

#include-once
#include <File.au3>
#include <array.au3>
;Test function to write comments to an ini file
Global $IniFile = @ScriptDir & "\Test.ini"
FileOpen($IniFile, 2)
IniWrite($IniFile, "Test1", 1, 1)
IniWrite($IniFile, "Test1", 2, 2)
IniWrite($IniFile, "Test1", 3, 3)
IniWrite($IniFile, "Test1", 4, 4)
IniWrite($IniFile, "Test2", 1, 1)
IniWrite($IniFile, "Test2", 2, 2)
IniWrite($IniFile, "Test2", 3, 3)
IniWrite($IniFile, "Test3", 1, 1)
IniWrite($IniFile, "Test3", 2, 2)
IniWrite($IniFile, "Test3", 3, 3)
Global $Test = _IniWriteSectionComment($IniFile, "Test1", "This is a comment that comes before a section name", 1)
$Test = _IniWriteSectionComment($IniFile, "Test2", "This is a comment that comes after a section name", 0)
$Test = _IniWriteSectionComment($IniFile, "Test3", "This is a multi-line comment|that comes after a section name", 0)
$Test = _IniWriteSectionComment($IniFile, "Test4", "This will cause an error by referencing a non-existent section name", 0)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Test = ' & $Test & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
; #FUNCTION# ====================================================================================================================
; Name...........: _IniWriteSectionComment
; Description ...: Writes comment(s) to an .ini file
; Syntax.........: _IniWriteSectionComment($IWSC_FileName, $IWSC_SectionName, $IWSC_Comment[, $IWSC_ForeAft = 1])
; Parameters ....: $IWSC_FileName       - String path of the file to write to.
;                  $IWSC_SectionName    - The section of the ini file to comment.
;                  $IWSC_Comment        - String that contains the comment for the section name.
;                  $IWSC_ForeAft        - Optional: Specifies where to put the comment in relation to the section name
;                                         default is before the section name.
; Return values .: Success - Returns a 1
;                  Failure - Returns a 0
;                  @Error  - 0 = No error.
;                  |1 = file not found
;                  |2 = Could not read/split file
;                  |3 = Not an .ini file
;                  |4 = Section Name not found
; Author ........: Bob Marotte (BrewManNH)
; Modified.......:
; Remarks .......: $IWSC_ForeAft specifies whether to put the comments before or after the section name, 1 = before/0 = after
;                  To write multiline comments, separate the lines with the "|" (pipe) character, see example below.
; Related .......:
; Link ..........:
; Example .......: $Test = _IniWriteSectionComment($IniFile, "Settings", "Now is the time for all good|men to come to the aid of their country", 1)
; ===============================================================================================================================
Func _IniWriteSectionComment($IWSC_FileName, $IWSC_SectionName, $IWSC_Comment, $IWSC_ForeAft = 1)
    Local $aFileRead
    If FileExists($IWSC_FileName) Then
        Local $IWSC_fHnd = FileOpen($IWSC_FileName, 0)
        If $IWSC_fHnd = -1 Then
            Return SetError(2, 0, 0)
        EndIf
        Local $Return = _FileReadToArray($IniFile, $aFileRead)
        If $Return = 0 Then
            Return SetError(2, 0, 0)
        EndIf
        Local $aSectionNames = IniReadSectionNames($IniFile)
        If @error Then
            Return SetError(3, 0, 0)
        EndIf
        If _ArraySearch($aSectionNames, $IWSC_SectionName) < 0 Then
            Return SetError(4, 0, 0)
        EndIf
        Local $aTempArray = StringSplit($IWSC_Comment, "|")
        Local $IWSC_Index = _ArraySearch($aFileRead, "[" & $IWSC_SectionName & "]")
        Local $aHolder[UBound($aFileRead) + UBound($aTempArray) - 1]
        If $IWSC_ForeAft Then
            For $I = 0 To $IWSC_Index - 1
                $aHolder[$I] = $aFileRead[$I]
            Next
            For $I = $IWSC_Index To $aTempArray[0] + $IWSC_Index - 1
                $aHolder[$I] = "; " & $aTempArray[$I - ($IWSC_Index - 1)]
            Next
            For $I = $IWSC_Index To $aFileRead[0]
                $aHolder[$I + $aTempArray[0]] = $aFileRead[$I]
            Next
        Else
            For $I = 0 To $IWSC_Index
                $aHolder[$I] = $aFileRead[$I]
            Next
            For $I = $IWSC_Index + 1 To $aTempArray[0] + $IWSC_Index
                $aHolder[$I] = "; " & $aTempArray[$I - ($IWSC_Index)]
            Next
            For $I = $IWSC_Index + 1 To $aFileRead[0]
                $aHolder[$I + $aTempArray[0]] = $aFileRead[$I]
            Next
        EndIf
        _ArrayDelete($aHolder, 0)
        _FileWriteFromArray($IniFile, $aHolder)
    Else
        Return SetError(1, 0, 0)
    EndIf
    Return SetError(0, 0, 1)
EndFunc   ;==>_IniWriteSectionComment
1 person likes this

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

I just remembered an earlier post () that investigated the whole "comments in an ini file thing". If you run the script in post #5 on that thread with the test ini file that FichteFoll provided, you'll see what is and isn't considered a valid comment as far as AutoIt is concerned. In AutoIt, in-line comments (Key1=Value1 ; comment goes here) are not valid and the value for Key1 will include the whole line including the comment. Quotes around any part of the key/value/Key&value won't affect it either, it sees the quotes as part of the key name and/or the value.

Just thought that this earlier post would help clear some things up, it did for me because I forgot half of it until rereading it. :)


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Thanks very much for the script. I will check out the other thread also. :)

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