|
|
|
11-16-2023, 03:13 AM
|
#1
|
Human being with feelings
Join Date: Feb 2021
Location: Right here
Posts: 1,322
|
How to make this script "safer"?
Hi,
Need some help here. How do i make sure this script doesn't change any other line which may contain "saveFlags=" in reaper.ini?
I tried "^saveFlags=%d+" but it seems the ^ anchor doesn't work in gsub.
From this thread :
Code:
--USE AT YOUR OWN RISK
local function ChangeFileSeek(path, pattern, substitution, bufsize, maxchunks)
local f = io.open(path, 'r+')
if f == nil then return false end
bufsize = bufsize or 2048
maxchunks = maxchunks or 10
f:setvbuf("full", bufsize)
local buf = ''
local readchunks = 0
local reaper_chunk_found = false
local pattern_found = false
while readchunks < maxchunks do
buf = f:read(bufsize)
if not buf then break end
readchunks = readchunks + 1
if not reaper_chunk_found and buf:find('%[reaper%]') then
reaper_chunk_found = true
end
if reaper_chunk_found and buf:find(pattern) then
buf = buf:gsub(pattern, substitution)
pattern_found = true
break
end
end
if pattern_found then
f:seek("set", bufsize * (readchunks - 1))
f:write(buf)
end
f:close()
return pattern_found
end
local path = reaper.get_ini_file()
if not path then return end
ChangeFileSeek(path, 'saveFlags=%d+', 'saveFlags=1', 4096, 1000)
reaper.Main_OnCommand(40026, 0)
|
|
|
11-16-2023, 04:09 AM
|
#2
|
Human being with feelings
Join Date: Nov 2022
Location: It is season dependant.
Posts: 746
|
Quote:
Originally Posted by smandrap
Hi,
Need some help here. How do i make sure this script doesn't change any other line which may contain "saveFlags=" in reaper.ini?
I tried "^saveFlags=%d+" but it seems the ^ anchor doesn't work in gsub.
From this thread :
Code:
--USE AT YOUR OWN RISK
local function ChangeFileSeek(path, pattern, substitution, bufsize, maxchunks)
local f = io.open(path, 'r+')
if f == nil then return false end
bufsize = bufsize or 2048
maxchunks = maxchunks or 10
f:setvbuf("full", bufsize)
local buf = ''
local readchunks = 0
local reaper_chunk_found = false
local pattern_found = false
while readchunks < maxchunks do
buf = f:read(bufsize)
if not buf then break end
readchunks = readchunks + 1
if not reaper_chunk_found and buf:find('%[reaper%]') then
reaper_chunk_found = true
end
if reaper_chunk_found and buf:find(pattern) then
buf = buf:gsub(pattern, substitution)
pattern_found = true
break
end
end
if pattern_found then
f:seek("set", bufsize * (readchunks - 1))
f:write(buf)
end
f:close()
return pattern_found
end
local path = reaper.get_ini_file()
if not path then return end
ChangeFileSeek(path, 'saveFlags=%d+', 'saveFlags=1', 4096, 1000)
reaper.Main_OnCommand(40026, 0)
|
See if this helps... AI assisted.
Code:
local function ChangeFileSeek(path, pattern, substitution, bufsize, maxchunks)
local f = io.open(path, 'r+')
if f == nil then return false end
bufsize = bufsize or 2048
maxchunks = maxchunks or 10
f:setvbuf("full", bufsize)
local buf = ''
local readchunks = 0
local reaper_chunk_found = false
local pattern_found = false
while readchunks < maxchunks do
buf = f:read(bufsize)
if not buf then break end
readchunks = readchunks + 1
if not reaper_chunk_found and buf:find('%[reaper%]') then
reaper_chunk_found = true
end
if reaper_chunk_found and buf:find(pattern) then
buf = buf:gsub(pattern, substitution)
pattern_found = true
break
end
end
if pattern_found then
f:seek("set", bufsize * (readchunks - 1))
f:write(buf)
end
f:close()
return pattern_found
end
local path = reaper.get_ini_file()
if not path then return end
ChangeFileSeek(path, '(%s-)saveFlags=%d+', '%1saveFlags=1', 4096, 1000)
reaper.Main_OnCommand(40026, 0)
...at your own risk.
😀😀😀😀
🎹🎹🎹🎹
Last edited by F1308; 11-16-2023 at 05:15 AM.
|
|
|
11-16-2023, 04:53 AM
|
#3
|
Human being with feelings
Join Date: Feb 2021
Location: Right here
Posts: 1,322
|
Thanks for your input, I figured out why my code doesn’t work (as always because I’m dumb)
|
|
|
11-16-2023, 05:03 AM
|
#4
|
Human being with feelings
Join Date: May 2015
Location: Québec, Canada
Posts: 4,937
|
Why not just use BR_Win32_WritePrivateProfileString from SWS?
|
|
|
11-16-2023, 05:16 AM
|
#5
|
Human being with feelings
Join Date: Feb 2021
Location: Right here
Posts: 1,322
|
Because I didn’t know about it. Thanks
|
|
|
11-16-2023, 05:17 AM
|
#6
|
Human being with feelings
Join Date: Nov 2022
Location: It is season dependant.
Posts: 746
|
Quote:
Originally Posted by smandrap
Thanks for your input, I figured out why my code doesn’t work (as always because I’m dumb)
|
My pleasure.
May I know, please, what does your script do ?
Thanks.
😀😀😀😀
🎹🎹🎹🎹
|
|
|
11-16-2023, 05:20 AM
|
#7
|
Human being with feelings
Join Date: Nov 2022
Location: It is season dependant.
Posts: 746
|
Quote:
Originally Posted by smandrap
Because I didn’t know about it. Thanks
|
You have discovered the reason to go AI...
One gets what needs much sooner than searching or even asking and waiting for someone´s goodwill...
https://forum.cockos.com/showthread....21#post2676621
😀😀😀😀
🎹🎹🎹🎹
|
|
|
11-16-2023, 05:22 AM
|
#8
|
Human being with feelings
Join Date: Feb 2021
Location: Right here
Posts: 1,322
|
Enables “create subdirectory for project” in the save project dialog. The function ChangeFileSeek is part of another script that I’m developing which needs to replace stuff in big files in a fast way. I copy pasted that in the script above, but it’s a very very idiotic solution
|
|
|
11-16-2023, 05:27 AM
|
#9
|
Human being with feelings
Join Date: Nov 2022
Location: It is season dependant.
Posts: 746
|
Quote:
Originally Posted by smandrap
Enables “create subdirectory for project” in the save project dialog. The function ChangeFileSeek is part of another script that I’m developing which needs to replace stuff in big files in a fast way. I copy pasted that in the script above, but it’s a very very idiotic solution
|
I see, thanks.
It means to avoid having to click on that option every time we save a project.
😀😀😀😀
🎹🎹🎹🎹
|
|
|
11-16-2023, 05:51 AM
|
#10
|
Human being with feelings
Join Date: Feb 2021
Location: Right here
Posts: 1,322
|
Quote:
Originally Posted by F1308
I see, thanks.
It means to avoid having to click on that option every time we save a project.
😀😀😀😀
🎹🎹🎹🎹
|
Yessir
|
|
|
Thread Tools |
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -7. The time now is 02:26 PM.
|