[Function] - IniValue() - Write\Read New Lines To\From ".ini" Files! (Carriage Return - Line Feed)

Post your working scripts, libraries and tools
User
Posts: 407
Joined: 26 Jun 2017, 08:12

[Function] - IniValue() - Write\Read New Lines To\From ".ini" Files! (Carriage Return - Line Feed)

27 Sep 2018, 21:19

Hi,

Just to share IniValue() function that allows CarriageReturn(`r) and LineFeed(`n) characters to be stored in keys-Values of ".ini" files!

Consequently, the function allows windows new lines "`r`n" to be stored too!

Code: Select all

	; "`r" and "`n" from keys values must be converted before storing in ".ini" files!
	;Write values enclosed between " " in order to preserve leading and trailing "spaces" characters while reading!
	

File := "#_ IniWrite - IniRead _#.ini"

String := ""
. "                                                                                `r`n"
. "       Test1  "" '   # ## ### _ #R#N  Test #R#N _ ### ## #    ' ""  Test1       `r`n"
. "       Test2  "" '   # ## ### _ #R#N  Test #R#N _ ### ## #    ' ""  Test2       `r`n"
. "       Test3  "" '   # ## ### _ #R#N  Test #R#N _ ### ## #    ' ""  Test3       `r`n"
. "                                                                                    "

gui, add, edit, w400 h100 +HwndControlId,
gui, add, edit, w400 h100 +HwndControlId2, #R #N #R#N `r`n#R #N #R#N `r`n#R #N #R#N
gui, add, edit, w400 h100 +HwndControlId3, 123 `r`n456 `n789

ControlSetText , , % String, % "ahk_id" ControlId

gui, add, Button, gSave, Save
gui, add, Button,x+5 gLoad, Load

gui, show

return

Save:	;_____________ Save ________________

ControlGetText, UserText, , % "ahk_id" ControlId
ControlGetText, UserText2, , % "ahk_id" ControlId2
ControlGetText, UserText3, , % "ahk_id" ControlId3

IniWrite, % IniValue(UserText), % File, section, key
IniWrite, % IniValue(UserText2), % File, section, key2
IniWrite, % IniValue(UserText3), % File, section, key3

ControlSetText , , % "Open ''" File "'' file, then ''Load'' ! ", % "ahk_id" ControlId
ControlSetText , , % "Open ''" File "'' file, then ''Load'' ! ", % "ahk_id" ControlId2
ControlSetText , , % "Open ''" File "'' file, then ''Load'' ! ", % "ahk_id" ControlId3

return

Load:	;_____________ Load ________________

IniRead, KeyValue, % File, section, key
IniRead, KeyValue2, % File, section, key2
IniRead, KeyValue3, % File, section, key3

IniValue(KeyValue,  "R")	;"R" option, Converts "#R" to "`r", "#N" to "`n" and "#_" to "#"
IniValue(KeyValue2, "R")
IniValue(KeyValue3, "R")

ControlSetText , , % KeyValue, % "ahk_id" ControlId
ControlSetText , , % KeyValue2, % "ahk_id" ControlId2
ControlSetText , , % KeyValue3, % "ahk_id" ControlId3

return

guiclose:	;___________ gui close ____________
exitapp


IniValue(ByRef Value, Options := "")	;__________ IniValue(Function) - v1.0 ___________
{
	if (Options = "R")
	{
	Value := RegExReplace(Value, "#R", "`r")
	Value := RegExReplace(Value, "#N", "`n")

	Value := RegExReplace(Value, "#_", "#")

	return		;no need to return anything, because the caller variable value will be the same as the ByRef "Value" var value!
	}

TValue := RegExReplace(Value, "#", "#_")

TValue := RegExReplace(TValue, "`r", "#R")
TValue := RegExReplace(TValue, "`n", "#N")

TValue := """" TValue """"	;enclose btween " " in order to preserve leading and trailing "spaces" characters while reading!

return, TValue
}

Return to “Scripts and Functions”

Who is online

Users browsing this forum: sancarn and 48 guests