Old 12-31-2018, 09:15 AM   #1
reapero
Human being with feelings
 
Join Date: Aug 2011
Posts: 121
Default Insert strings and variable at certain position of a file

I am reading stats from reaper´s takes and items and trying to paste them on an xml file.

I used the append mode on io.open but unfortunately pasting at the very end wotn work, since i have to respect some of the formatting options on this xml. I need all the reaper stuff to be pasted 17 cursor positions before the ending of the file.

I saw the seek method and thought it could work. Tried something with:

Code:
file:seek("end",-17)
Then trying to write using append but that wouldnt work. Any ideas?
Thank you guys
reapero is offline   Reply With Quote
Old 12-31-2018, 09:28 AM   #2
reapero
Human being with feelings
 
Join Date: Aug 2011
Posts: 121
Default

So do i have to manually read everything, modify, then write everything again? Looks more steps than i thought at first.
reapero is offline   Reply With Quote
Old 12-31-2018, 09:32 AM   #3
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 7,577
Default

Quote:
Originally Posted by reapero View Post
So do i have to manually read everything, modify, then write everything again? Looks more steps than i thought at first.
That's the standard way to insert/change something in the middle of a file. (You don't necessarily need to read in everything at once, though. You could read in smaller blocks of data and when you come to your insertion/change point, write out the insert/modified data, else write out the original data.)
__________________
For info on SWS Reaper extension plugin (including Xenakios' previous extension/actions) :
http://www.sws-extension.org/
https://github.com/Jeff0S/sws
--
Xenakios blog (about HourGlass, Paul(X)Stretch and λ) :
http://xenakios.wordpress.com/
Xenakios is online now   Reply With Quote
Old 12-31-2018, 09:35 AM   #4
reapero
Human being with feelings
 
Join Date: Aug 2011
Posts: 121
Default

Thanks Xenakios!

How do i know when i came to my insertion/change point though?
reapero is offline   Reply With Quote
Old 12-31-2018, 09:38 AM   #5
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 7,577
Default

Quote:
Originally Posted by reapero View Post
Thanks Xenakios!

How do i know when i came to my insertion/change point though?
If you have xml, then it's probably best to use a proper xml library to deal with it. Also it's probably easiest to read in everything at once, do the changes and save the modified xml back.
__________________
For info on SWS Reaper extension plugin (including Xenakios' previous extension/actions) :
http://www.sws-extension.org/
https://github.com/Jeff0S/sws
--
Xenakios blog (about HourGlass, Paul(X)Stretch and λ) :
http://xenakios.wordpress.com/
Xenakios is online now   Reply With Quote
Old 01-01-2019, 05:48 AM   #6
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 5,483
Default

This is a nice article about FIle I/O in lua:

https://www.tutorialspoint.com/lua/lua_file_io.htm
X-Raym is offline   Reply With Quote
Old 01-01-2019, 09:17 AM   #7
reapero
Human being with feelings
 
Join Date: Aug 2011
Posts: 121
Default

Thx for the link X-Raym,

Thats the tutorial where i got the info about the seek method. My idea was to use seek to place the cursor at the right point, then use io.open in append mode. Apparenty you cant combine both and append is always gonna insert stuff at the very end of the file.
reapero is offline   Reply With Quote
Old 01-01-2019, 10:03 AM   #8
reapero
Human being with feelings
 
Join Date: Aug 2011
Posts: 121
Default

Mm..having trouble displaying the contents of a file in the console. Can anyone tell me whats wrong with this code please?

Code:
file = io.open(xml_filenanme, "r")
io.input(file)
content = io.read("*all")
reaper.ShowConsoleMsg(content)
Why is content nil here?
reapero is offline   Reply With Quote
Old 01-01-2019, 10:28 AM   #9
reapero
Human being with feelings
 
Join Date: Aug 2011
Posts: 121
Default

Nevermind, i had to update JS extension on this machine.
reapero is offline   Reply With Quote
Old 01-02-2019, 01:33 PM   #10
mespotine
Human being with feelings
 
mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig, Germany
Posts: 810
Default

There are functions for inserting strings into files in the Ultraschall-API, as I was too lazy to code that time and again.

These should do the job

Normal Textfiles, using linenumbers as the insert-position:
https://mespotin.uber.space/Mespotin...eToFile_Insert

The same for binary-files, using characteroffsets:
https://mespotin.uber.space/Mespotin...e_InsertBinary
__________________
Ultraschall-API - a Lua-functions-library4Reaper: https://forum.cockos.com/showthread....98#post2067798
Reaper Internals - Developerdocs4Reaper: https://forum.cockos.com/showthread.php?t=207635
mespotine 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 04:08 AM.


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