Space Growth in Ini Files

Propose new features and changes
User avatar
lmstearn
Posts: 154
Joined: 11 Aug 2016, 02:32
GitHub: lmstearn

Space Growth in Ini Files

Post by lmstearn » 06 Jan 2017, 07:42

To curb the growth of spaces when adding/removing records, the following can be done user end, but I think less cost if in-house:
The wish is for the IniWrite function when writing a value, to check for the next character in the writeloc.
If it is a space, replace it with a null.
Thanks.
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
User avatar
lmstearn
Posts: 154
Joined: 11 Aug 2016, 02:32
GitHub: lmstearn

Re: Space Growth in Ini Files

Post by lmstearn » 02 Feb 2018, 00:32

As it turned out, just a simple solution required:

Code: Select all

IniSpaceCleaner(IniFile)
{
; https://autohotkey.com/boards/viewtopic.php?f=13&t=26556&p=124630#p124630
retVal := 0, temp := "", e := ""
Thread, NoTimers
try
{
	FileRead, retVal, %IniFile%
	retVal := RegExReplace(retVal, "m) +$", " ") ;m multilineselect; " +" one or more spaces; $ only at EOL
	FileDelete, %Inifile%
	sleep, 100
	FileAppend, %retVal%, %IniFile%
	sleep, 100
}
catch e
{
if (e)
MsgBox, 8208, IniSpaceCleaner, Error with Ini file! `nSpecifically: %e%
}
Thread, NoTimers, false
}
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
guest3456
Posts: 2419
Joined: 09 Oct 2013, 10:31

Re: Space Growth in Ini Files

Post by guest3456 » 02 Feb 2018, 11:56

i've never noticed space growth, and i utilize iniread/write all the time

can you post a script reproducing the problem first? before offering your solution?

User avatar
lmstearn
Posts: 154
Joined: 11 Aug 2016, 02:32
GitHub: lmstearn

Re: Space Growth in Ini Files

Post by lmstearn » 02 Feb 2018, 22:24

When the above was posted, the decision was made not to use IniDelete. Can't remember why- perhaps the help didn't provide enough examples- or there were other issues regarding extra spaces in the value field- but recall in using it the ini files got messed up.

So with just IniWrite and IniRead it's convenient to initialize all or most entries in the file with something like:

Code: Select all

IniWrite, %A_Space%, %MyIniName%, Section, Key
Values can then be written to the key, but they will always have a trailing space.
In the IniRead docs there's
Default
The value to store in OutputVar if the requested key is not found. If omitted, it defaults to the word ERROR. To store a blank value (empty string), specify %A_Space%.
When the value wants to be deleted again, we must do this again:

Code: Select all

IniWrite, %A_Space%, %MyIniName%, Section, Key
The value is replaced by the space, but not the trailing space(s) already there.

In hindsight, instead of writing a space to delete, it may just be easier to delete the entire key with IniDelete, (if it works), and rewrite it.
Perhaps there are benefits you see with IniDelete?
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
lexikos
Posts: 6175
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Space Growth in Ini Files

Post by lexikos » 09 Feb 2018, 04:08

The OS preserves trailing spaces in the same way that it preserves line endings and comments, because the trailing space is not part of the value.
IniRead wrote:The operating system automatically omits leading and trailing spaces/tabs from the retrieved string. To prevent this, enclose the string in single or double quote marks. The outermost set of single or double quote marks is also omitted, but any spaces inside the quote marks are preserved.
Virtually all limitations and quirks of the INI functions come directly from the OS. AutoHotkey does not write to or read the file, or modify the results given by the OS in any way, except:
  • Converting between null-delimited lists (required/returned by the API) and newline-delimited lists.
  • If the file does not exist, IniWrite creates the file with a UTF-16 BOM in Unicode builds. It also writes the section header to prevent the OS from inserting an empty line at the top of the file. It does not write anything else to the file.
Note: IniWrite requires its parameter to be non-blank (because the first three parameters are mandatory), but you can pass an empty variable or expression. Also, as implied by the text quoted above, writing two literal quote marks ("") will give the same result; when the value is read back, it will be empty.

An empty value is not the same as a non-existent value, due to IniRead's Default parameter.
Post Reply

Return to “Wish List”