Old 04-09-2016, 05:40 PM   #1
FnA
Human being with feelings
 
FnA's Avatar
 
Join Date: Jun 2012
Posts: 2,173
Default Lua Question: Reading from REAPER.ini

Needed to get default layouts because ReaScript function returns "" when you call it on default tracks that have no layout applied. I used this. See any problems? Better way? I guess I'm stumped why I have to use f3-2. string.find(s,"\\n",f1,true) doesn't work.

Code:
function Fn_Get_Default_Layouts()
  local f = io.open(reaper.get_ini_file(), "rb")
  local s = f:read("*all")
  f:close()
  local f1,f2 = string.find(s,"layout_tcp=",1,true)
  local f3 = string.find(s,"\n",f1,true)
  local tcp_str = string.sub(s, f2+1, f3-2)
  f1,f2 = string.find(s,"layout_mcp=",1,true)
  f3 = string.find(s,"\n",f1,true)
  local mcp_str = string.sub(s, f2+1, f3-2)
  return tcp_str, mcp_str
end
FnA is offline   Reply With Quote
Old 04-10-2016, 02:52 AM   #2
heda
Human being with feelings
 
heda's Avatar
 
Join Date: Jun 2012
Location: Spain
Posts: 5,409
Default

if using SWS extensions, there is a function:

Code:
integer retval, string stringOut reaper.BR_Win32_GetPrivateProfileString(string sectionName, string keyName, string defaultString, string filePath)

BR] Equivalent to win32 API GetPrivateProfileString(). For example, you can use this to get values from REAPER.ini
heda is offline   Reply With Quote
Old 04-10-2016, 12:16 PM   #3
FnA
Human being with feelings
 
FnA's Avatar
 
Join Date: Jun 2012
Posts: 2,173
Default

Doh! Should have control Fd ".ini" in there.

Thanks! It works, I think.
Code:
inipath = reaper.get_ini_file()
r1, tcp = reaper.BR_Win32_GetPrivateProfileString("reaper", "layout_tcp", "Error", inipath)
r2, mcp = reaper.BR_Win32_GetPrivateProfileString("reaper", "layout_mcp", "Error", inipath)
Any experience writing with the other one? I was thinking a script to get theme tweak color settings and apply them to a new version/update would save a lot of time. Scared, though.

Also...

"win32"

Does it work on mac?
FnA is offline   Reply With Quote
Old 04-10-2016, 02:19 PM   #4
heda
Human being with feelings
 
heda's Avatar
 
Join Date: Jun 2012
Location: Spain
Posts: 5,409
Default

create a backup of the ini file while testing, just in case. It should work perfectly to write too.
But since it says win32, I'm not sure if it is working in mac. I cannot afford a mac. Maybe someone else can confirm?
heda is offline   Reply With Quote
Old 04-10-2016, 02:37 PM   #5
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 2,768
Default

BR_Win32_GetPrivateProfileString works here on OS X.
cfillion is offline   Reply With Quote
Old 04-10-2016, 07:19 PM   #6
FnA
Human being with feelings
 
FnA's Avatar
 
Join Date: Jun 2012
Posts: 2,173
Default

Thanks a million cfillion! hehe. I am going to use it in a script for Commala's theme thread and want it to be safe and good.

I couldn't resist trying to write new colors. (just for markers and regions as an experiment.) I guess it works, but looks like you have to restart Reaper to get it to take. Maybe it would be better to use copy paste? Maybe that's not even the best thing to do...
FnA is offline   Reply With Quote
Old 12-30-2017, 04:08 PM   #7
tufb
Human being with feelings
 
Join Date: Dec 2017
Location: Germany
Posts: 49
Default

FnA, how is your piece of code, "Fn_Get_Default_Layouts()" in the OP, licensed?


ps: sorry for the bump.. I tried sending you a pm, but it doesn't work for me
tufb is offline   Reply With Quote
Old 12-30-2017, 05:48 PM   #8
FnA
Human being with feelings
 
FnA's Avatar
 
Join Date: Jun 2012
Posts: 2,173
Default

Default?



I don't know much about such things. You can do what ever you like as far as I'm concerned...

I appreciate the question though. Funny about the PMs. Someone else has said they could not get through either, but others have been successful.
FnA is offline   Reply With Quote
Old 01-06-2018, 06:24 AM   #9
tufb
Human being with feelings
 
Join Date: Dec 2017
Location: Germany
Posts: 49
Default

Thank you. I've adapted your code to the following function. Successfully tested on linux.

Code:
-- @param String
-- @return String or nil
function bfut_FetchUserPreference(name)
  local file = io.open(reaper.get_ini_file(),"rb")
  local s = file:read("*all")
  file:close()
  local a,b = string.find(s,name.."=",1,true)
  if a == nil then return nil end
  local c = string.find(s,"\n",a,true)
  return string.sub(s,b+1,c-1)
end
Crucially, it's c-1, instead of c-2 in the last line.
tufb is offline   Reply With Quote
Old 01-06-2018, 07:03 PM   #10
FnA
Human being with feelings
 
FnA's Avatar
 
Join Date: Jun 2012
Posts: 2,173
Default

Quote:
Originally Posted by tufb View Post
Thank you. I've adapted your code to the following function. Successfully tested on linux.

Code:
-- @param String
-- @return String or nil
function bfut_FetchUserPreference(name)
  local file = io.open(reaper.get_ini_file(),"rb")
  local s = file:read("*all")
  file:close()
  local a,b = string.find(s,name.."=",1,true)
  if a == nil then return nil end
  local c = string.find(s,"\n",a,true)
  return string.sub(s,b+1,c-1)
end
Crucially, it's c-1, instead of c-2 in the last line.

I know little of lua string handling. But what happens if you use -2 instead? Here, on Windows 7 x64 I get what appears to be the same thing, but there's an extra invisible something on the end, with -1. I saw something like that when I made the OP, but can't remember what really. Maybe the #str thing.


Code:
-- @param String
-- @return String or nil
function bfut_FetchUserPreference(name)
  local file = io.open(reaper.get_ini_file(),"rb")
  local s = file:read("*all")
  file:close()
  local a,b = string.find(s,name.."=",1,true)
  if a == nil then return nil end
  local c = string.find(s,"\n",a,true)
  return string.sub(s,b+1,c-1)
end

-----------------------

str1 = bfut_FetchUserPreference("prefs_x")
str1len = #str1
str1q = string.format('%q', str1)

str2 = str1:gsub([[%c]],[[]])
str2len = #str2
str2q = string.format('%q', str2)

str3 = str1..str1..str1

reaper.MB(str3,"MB",0)
reaper.ShowConsoleMsg(str3)
I get:

"265"
4
""265\13""

"265"
3
""265""

265265265

in the IDE, for the first 7 things I added.

MB displays
265
265
265

Console shows 265265265
FnA is offline   Reply With Quote
Old 01-07-2018, 07:04 AM   #11
tufb
Human being with feelings
 
Join Date: Dec 2017
Location: Germany
Posts: 49
Default

Ach! That's the carriage return CR / line feed LF issue between Windows and Linux: https://en.wikipedia.org/wiki/Newline

EOL look like this:
linux/osx: \n
win: \r\n

In your script, CR is stripped from str2 by replacing all control characters. Incorporating this in the script as follows checks out on WINE (Win x64 version) and native Linux, for me. It's safer to specifically replace CR only. Also, gsub returns two values, so an additional line is needed.


Code:
-- @param name String
-- @return s String or nil
function bfut_FetchUserPreference(name)
  local f = io.open(reaper.get_ini_file(),"rb")
  local s = f:read("*all")
  f:close() 
  local s1,s2 = string.find(s,name.."=",1,true)
  if s1 == nil then return nil end
  local s3 = string.find(s,"\n",s1,true)
  s = s:sub(s2+1,s3-1):gsub("[\r]","")
  return s
end

Last edited by tufb; 01-07-2018 at 05:36 PM.
tufb is offline   Reply With Quote
Old 01-07-2018, 07:07 PM   #12
FnA
Human being with feelings
 
FnA's Avatar
 
Join Date: Jun 2012
Posts: 2,173
Default

Well, I don't know how to thoroughly test it, but it seems to give numbers that match the string length, and file paths look ok.
FnA is offline   Reply With Quote
Old 01-13-2018, 02:41 PM   #13
tufb
Human being with feelings
 
Join Date: Dec 2017
Location: Germany
Posts: 49
Default

It's in use in one of my scripts, so I'll hear back in case of a mishap. Main purpose is really as fall-back option, to eliminate dependence on SWS.


I'll maintain this as a minimal example for string and number values on my GitHub. Now accepts keyname, section, and ini-path as parameters, thus may be used for all ini-formatted text files. Line comments, and trailing comments are ignored.

https://gist.github.com/bfut/3e738d9...91999d933ec407

Last edited by tufb; 10-17-2018 at 06:49 AM.
tufb is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 02:48 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.