Space Growth in Ini Files

Post a reply

Confirmation code
Enter the code exactly as it appears. All letters are case insensitive.
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :| :mrgreen: :geek: :ugeek: :arrow: :angel: :clap: :crazy: :eh: :lolno: :problem: :shh: :shifty: :sick: :silent: :think: :thumbup: :thumbdown: :salute: :wave: :wtf: :yawn: :facepalm: :bravo: :dance: :beard: :morebeard: :xmas: :HeHe: :trollface: :cookie: :rainbow: :monkeysee: :monkeysay: :happybday: :headwall: :offtopic: :superhappy: :terms: :beer:
View more smilies

BBCode is ON
[img] is OFF
[flash] is OFF
[url] is ON
Smilies are ON

Topic review

Expand view Topic review: Space Growth in Ini Files

Re: Space Growth in Ini Files

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.

Re: Space Growth in Ini Files

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

Re: Space Growth in Ini Files

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?

Re: Space Growth in Ini Files

by lmstearn » 02 Feb 2018, 00:32

As it turned out, just a simple solution required:

Code: Select all

retVal := 0, temp := "", e := ""
Thread, NoTimers
	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

Space Growth in Ini Files

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.