insert into file without overwriting

insert into file without overwriting

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
maackey
Posts: 490
Joined: 02 Jul 2008, 07:11

insert into file without overwriting

Post by maackey »

So, my question isn't directly related to spring, but more of lua in general.

Is there a way I can easily insert eg. a string into a file without overwriting the contents?

I tried something like this:
file:write(abcdefghijklmn)
local bookmark = file:seek()
file:write(stuvwxyz)
file:seek("set", bookmark)
file:write(opqr)

but instead of getting output

Code: Select all

abcdefghijklmn
opqr
stuvwxyz
I get

Code: Select all

abcdefghijklmn
opqrwxyz
I've written spaces to "fill the gap" in the meantime, but since "opqr" is generated dynamically later on in the file it is very hard to predict how long it will be.
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Re: insert into file without overwriting

Post by Forboding Angel »

As food for thought, depending on what you are doing, it might be better to have a config file that gets included wherever the values are needed as a go between. That way you're not risking overwriting important stuff. And having one lua file write directly into another is O_o


That said, couldn't you just use a global variable?
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: insert into file without overwriting

Post by knorke »

Is there a way I can easily insert eg. a string into a file without overwriting the contents?
I dont think such thing is possible because of how files work?
imo read file into array, insert your stuff, write it again.
if you feel like being cautious you could write to a temp file and when everything worked, replace the orginal file with that.

or if you just want to add at the end of the file, "a" parameter is for append.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7049
Joined: 16 Nov 2004, 13:08

Re: insert into file without overwriting

Post by zwzsg »

knorke wrote:imo read file into array
Or read the whole file into a string. I have a widget that read files in the 50kb range into a string, apply some gsub to it, then write it to a file.
User avatar
maackey
Posts: 490
Joined: 02 Jul 2008, 07:11

Re: insert into file without overwriting

Post by maackey »

Thanks guys! I managed to get it working perfectly
Forboding Angel wrote:As food for thought, depending on what you are doing, it might be better to have a config file that gets included wherever the values are needed as a go between. That way you're not risking overwriting important stuff. And having one lua file write directly into another is O_o


That said, couldn't you just use a global variable?
Its a lua file that generates verilog code. I can't use a variable, because I don't know how many wires there are and what they will be named.
knorke wrote:or if you just want to add at the end of the file, "a" parameter is for append.
Can't append to the end of the file as the wires need to be defined before I use them ^^
zwzsg wrote:r read the whole file into a string. I have a widget that read files in the 50kb range into a string, apply some gsub to it, then write it to a file.
Yeah apparently lua is a beast at strings.
Because Lua handles long strings efficiently, a simple technique for writing filters in Lua is to read the whole file into a string, do the processing to the string (typically with gsub), and then write the string to the output:

t = io.read("*all") -- read the whole file
t = string.gsub(t, ...) -- do the job
io.write(t) -- write the file
ended up using this, worked perfectly.

I attached the file if anyone is interested in seeing what it does/how horribly noobish I am at lua.
Attachments
wallace_generator.lua
(8.8 KiB) Downloaded 27 times
Post Reply

Return to “Lua Scripts”