This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
neptune12100's profile picture

Need Help With Io Api

Started by neptune12100, 10 November 2013 - 02:46 PM
neptune12100 #1
Posted 10 November 2013 - 03:46 PM
I've written a program for pure Lua (without ComputerCraft) that takes a list of files and makes a self-extracting archive.

I am trying to convert it to work in CC, but I'm having trouble with the IO API and the wiki is anything but helpful.

My actual question is: What are the differences between Lua's io library and CC's?

Here's the program: db03MQjR

And an example archive (with the program, gpl, list of files and a readme): aUs44bky
Bomb Bloke #2
Posted 10 November 2013 - 04:16 PM
Current differences are listed here, whereas info about previous versions of CC have to be trawled out of the page history (something I intend to rectify if I ever get around to tweaking the wiki).
Bubba #3
Posted 11 November 2013 - 12:51 PM
Edit: Removing this while I test a few things
Edited on 11 November 2013 - 11:56 AM
neptune12100 #4
Posted 12 November 2013 - 11:13 AM
OK, took the return from io.open("youknowafile", "rb") in CC 1.56, pairs()'d through it and found just write, flush, close, and some random function I've never heard of (Can't remember what it was, not at my computer right now).
theoriginalbit #5
Posted 12 November 2013 - 07:03 PM
What is the actual problem you're encountering? Is in an error? What does it say? 'cause your code looks fine to me.

The "random function" you saw was probably lines, which is an iterator function to loop through all the lines in a generic-for loop.

It should also be pointed out that in CCLua the IO API is actually a wrapper of the FS API.

local iFile = fs.open('name', 'r')
iFile.read() --# reads a byte
iFile.readLine() --# reads a line
iFile.close() --# closes the handle

local oFile = fs.open('named', 'w') --# 'a' is also the same as the following
oFile.write('text') --# writes text to the buffer
oFile.writeLine('text') --# writes text appending a new line to the buffer
oFile.flush() --# flushes the buffer to file
oFile.close() --# flushes the buffer to file and closes the handle
'rb' and 'wb' are also supported and are the same as the above with the omission of the `Line` versions of functions.
distantcam #6
Posted 12 November 2013 - 09:04 PM
It should also be pointed out that in CCLua the IO API is actually a wrapper of the FS API.

Do you mean the other way around? As in the FS API is a wrapper around IO?
theoriginalbit #7
Posted 12 November 2013 - 09:13 PM
Do you mean the other way around? As in the FS API is a wrapper around IO?
No I meant it the way I said it, the IO API is a wrapper of the FS API. The FS API is where the sandboxed file methods are implemented in the mod. The IO API then is a wrapper on these so that Lua scripts can semi-work with CCLua.

Also avoid double posting. You can edit your post and add in extra information including quotes at any time. The people you quote in an edit even still get a notification.
Edited on 12 November 2013 - 08:13 PM
distantcam #8
Posted 12 November 2013 - 09:23 PM
No I meant it the way I said it, the IO API is a wrapper of the FS API. The FS API is where the sandboxed file methods are implemented in the mod. The IO API then is a wrapper on these so that Lua scripts can semi-work with CCLua.

Huh, interesting. I would have thought it would be easier to do it the other way since IO feels like it's lower level than FS.

Also avoid double posting. You can edit your post and add in extra information including quotes at any time. The people you quote in an edit even still get a notification.

Yeah my bad. The moment I did it I realized I should have just edited the post.
theoriginalbit #9
Posted 12 November 2013 - 09:28 PM
Huh, interesting. I would have thought it would be easier to do it the other way since IO feels like it's lower level than FS.
I think it was maybe to avoid that kind of thinking… idk… but I used to think that IO was the lower level, since it is in every other language, including Lua, its just not in CCLua though…

Yeah my bad. The moment I did it I realized I should have just edited the post.
Its ok, we all do it from time to time.
Lyqyd #10
Posted 13 November 2013 - 12:23 AM
OP's code is broken because he's trying to use binary-mode handles as if they were text-mode handles. It doesn't work that way. OP, remove the "b" from all of your handle opening calls, using "r" or "w" instead of "rb" or "wb". Binary-mode handles only allow you to read and write a single byte at a time, which is not what you want here.
neptune12100 #11
Posted 13 November 2013 - 11:17 AM
Is that a CC thing with binary mode? I just don't like the OS messing with my files. Thanks!
Lyqyd #12
Posted 13 November 2013 - 12:32 PM
Yes. It is one of the important differences between Lua in ComputerCraft and Lua elsewhere. Give it a try with text-mode handles. If you aren't trying to transport binary data with it, you should be fine.
neptune12100 #13
Posted 14 November 2013 - 11:06 AM
Well, thanks. I guess universal newlines wouldn't be a problem anyway, since I'm not moving stuff between OSes with it. I just add the "b" out of habit.
You're amazing, Lyqyd!
Perhaps a note on the wiki about binary files would be helpful?