Multiline .INI strings

Post your working scripts, libraries and tools for AHK v1.1 and older
burque505
Posts: 1734
Joined: 22 Jan 2017, 19:37

Multiline .INI strings

14 Jan 2020, 12:19

Recently I've been jumping through hoops to get multiline strings into .INI key values, so I've tried to make my life a little easier in that regard.

This multiline problem has been visited before, at least here and here.

My primary concern was finding a replacement delimiter that wouldn't come back to haunt me. So far so good, but you never know.
(IniMulti.ahk)

Code: Select all

; ┌───────────────────────────────────────────────────────────────────────────┐
; │  After much frustration trying to include multiline values in             │
; │  an .ini file, I came up with this. Much credit to those posts below.     │
; │  This still needs work.                                                   │
; │  In particular, I'm having trouble coming up with a delimiter that        │
; │  won't choke some future input. At first I picked "|n" but decided after  │
; │  some consideration that it was an accident waiting to happen.            │
; │  Thus ╬ below, whichappears as '╬' in my ini files.                                                 │
; │  Probably also an accident waiting to happen. Murphy's Law.               │
; │  Change that if you manage to give your                                   │
; │  script indigestion.                                                      │
; └───────────────────────────────────────────────────────────────────────────┘

IniMultiRead(IniFileName, Section:="", Key:="" , Default:="") {
		Iniread, Value, %IniFileName%, %Section%, %Key%
		StringReplace, Value, Value, ╬, `n, All
		return Value
}

IniMultiWrite(IniVar, IniFileName, Section:="", Key:="") {
		StringReplace, IniVar, IniVar, `n, ╬, All
		IniWrite, %IniVar%, %IniFileName%, %Section%, %Key%
}

/*
https://autohotkey.com/board/topic/91849-how-to-save-multi-line-user-input-to-ini/
https://autohotkey.com/board/topic/33601-solved-multi-line-tooltip-from-ini-file/
π is alt+995
╬ is alt+1230
*/
Here's a test script if you feel like taking it for a spin.
Spoiler
Regards,
burque505
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Multiline .INI strings

14 Jan 2020, 14:27

note:

https://www.autohotkey.com/docs/commands/IniRead.htm
Unicode: IniRead and IniWrite rely on the external functions GetPrivateProfileString and WritePrivateProfileString to read and write values. These functions support Unicode only in UTF-16 files; all other files are assumed to use the system's default ANSI code page.

burque505
Posts: 1734
Joined: 22 Jan 2017, 19:37

Re: Multiline .INI strings

14 Jan 2020, 14:46

Thanks, I did know that, but I appreciate the reference.
If I'm not mistaken, this accounts for the different display of the delimiter in the .ini files, which is fine for my purposes.
ahk7
Posts: 575
Joined: 06 Nov 2013, 16:35

Re: Multiline .INI strings

14 Jan 2020, 17:31

You could base64 encode your multiline string as it will result in one long encoded line, it will run into the max ini key limit sooner as your solution of course. Just a thought. ( https://github.com/ahkscript/libcrypt.ahk has base64 encode/decode )
burque505
Posts: 1734
Joined: 22 Jan 2017, 19:37

Re: Multiline .INI strings

14 Jan 2020, 18:16

@ahk7, thank you, excellent thought. Let me investigate that. Perhaps an advantage would be less possible interference with the content of the "victim" string.
Regards,
burque505
User avatar
Relayer
Posts: 160
Joined: 30 Sep 2013, 13:09
Location: Delaware, USA

Re: Multiline .INI strings

15 Jan 2020, 11:13

I simply replace carriage returns with "<CR>" and the equal sign with "<EQ>" going in and coming out of the ini file.
burque505
Posts: 1734
Joined: 22 Jan 2017, 19:37

Re: Multiline .INI strings

15 Jan 2020, 11:58

Sounds good also! Nice tip.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 204 guests