Old 11-16-2023, 03:13 AM   #1
smandrap
Human being with feelings
 
smandrap's Avatar
 
Join Date: Feb 2021
Location: Right here
Posts: 1,322
Default 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)
__________________
My Scripts
smandrap is online now   Reply With Quote
Old 11-16-2023, 04:09 AM   #2
F1308
Human being with feelings
 
F1308's Avatar
 
Join Date: Nov 2022
Location: It is season dependant.
Posts: 746
Default

Quote:
Originally Posted by smandrap View Post
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.
F1308 is offline   Reply With Quote
Old 11-16-2023, 04:53 AM   #3
smandrap
Human being with feelings
 
smandrap's Avatar
 
Join Date: Feb 2021
Location: Right here
Posts: 1,322
Default

Thanks for your input, I figured out why my code doesn’t work (as always because I’m dumb)
__________________
My Scripts
smandrap is online now   Reply With Quote
Old 11-16-2023, 05:03 AM   #4
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 4,937
Default

Why not just use BR_Win32_WritePrivateProfileString from SWS?
cfillion is offline   Reply With Quote
Old 11-16-2023, 05:16 AM   #5
smandrap
Human being with feelings
 
smandrap's Avatar
 
Join Date: Feb 2021
Location: Right here
Posts: 1,322
Default

Because I didn’t know about it. Thanks
__________________
My Scripts
smandrap is online now   Reply With Quote
Old 11-16-2023, 05:17 AM   #6
F1308
Human being with feelings
 
F1308's Avatar
 
Join Date: Nov 2022
Location: It is season dependant.
Posts: 746
Default

Quote:
Originally Posted by smandrap View Post
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.

😀😀😀😀
🎹🎹🎹🎹
F1308 is offline   Reply With Quote
Old 11-16-2023, 05:20 AM   #7
F1308
Human being with feelings
 
F1308's Avatar
 
Join Date: Nov 2022
Location: It is season dependant.
Posts: 746
Default

Quote:
Originally Posted by smandrap View Post
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

😀😀😀😀
🎹🎹🎹🎹
F1308 is offline   Reply With Quote
Old 11-16-2023, 05:22 AM   #8
smandrap
Human being with feelings
 
smandrap's Avatar
 
Join Date: Feb 2021
Location: Right here
Posts: 1,322
Default

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
__________________
My Scripts
smandrap is online now   Reply With Quote
Old 11-16-2023, 05:27 AM   #9
F1308
Human being with feelings
 
F1308's Avatar
 
Join Date: Nov 2022
Location: It is season dependant.
Posts: 746
Default

Quote:
Originally Posted by smandrap View Post
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.

😀😀😀😀
🎹🎹🎹🎹
F1308 is offline   Reply With Quote
Old 11-16-2023, 05:51 AM   #10
smandrap
Human being with feelings
 
smandrap's Avatar
 
Join Date: Feb 2021
Location: Right here
Posts: 1,322
Default

Quote:
Originally Posted by F1308 View Post
I see, thanks.

It means to avoid having to click on that option every time we save a project.

😀😀😀😀
🎹🎹🎹🎹
Yessir
__________________
My Scripts
smandrap is online now   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:26 PM.


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