2

I would like to read an .ods file line by line using lua on Windows. The code I used to do that worked on another platform but failed on Windows. So I changed the code to see what is going on:

local fhandler = io.open(path,"r")
print(pcall(fhandler.read,fhandler,"*l"))
print(pcall(fhandler.read,fhandler,"*l"))
print(pcall(fhandler.read,fhandler,"*l"))

The output I get is the following:

true <?xml version="1.0" encoding="UTF-8"?>
false function: 0x003d39e0
true :tc:opendocument:xmlns:script:1.0" [...] office:version="1.2">

So the code fails on the second line of the .ods file. I am not sure why pcall returns a function here but the important part is, that the content of the "third" line starting with ":tc:opendoc..." belongs to the second line of the .ods file. I am pretty sure that it is the 1025 character of the second line.

I guess the following is happening: There must be a lua internal buffer of 1024 Bytes for reading whole lines. So the second line is read only till the 1024th character and then probably the buffer is full. Lua continues with reading the rest of the line. This could also explain why the code works on other platforms where the buffer might be set to another value.

I know that you can read chunks of files by specifying your own buffer like:

fhandler:read(2048,"*l")

but this does not help here because it then imports the file in 2048 Byte chunks and does not stop at the end of a line. So you would need to parse the imported data and look for line feeds etc.

So finally, my question is: Is there a lua internal buffer for the length of a line that can be read at once which may be set to 1024 Bytes? And how can you change the size of this buffer?

greatwolf
  • 20,287
  • 13
  • 71
  • 105
  • If you run the second line without `pcall` what kind of error do you see propogated? – greatwolf Oct 17 '13 at 09:21
  • 4
    `*l` should accept arbritary-length lines. Lua reads in chunks of `BUFSIZ` using `fgets` until an end-of-line is seen and then concats everything into a single string. See http://www.lua.org/source/5.2/liolib.c.html#read_line. – lhf Oct 17 '13 at 10:36
  • Try `local fhandler = io.open(path,"rb")` – Egor Skriptunoff Oct 17 '13 at 16:36
  • Changing to "rb" did unfortunately not solve the problem. I am not running the lua script directly but by LuaInterface. If I execute without pcall I gett the following error: Eine Ausnahme (erste Chance) des Typs "LuaInterface.Exceptions.LuaScriptException" ist in LuaInterface.dll aufgetreten. Ein Ausnahmefehler des Typs "LuaInterface.Exceptions.LuaScriptException" ist in LuaInterface.dll aufgetreten. Zusätzliche Informationen: function – Michael Gerbracht Oct 17 '13 at 19:02
  • What kind of application or framework are you using Lua in? Usually the 2nd return value of `pcall` is a string if the first is `false` - and thats the error message, and actually never a function. – dualed Oct 18 '13 at 10:08
  • another question: if it's xml, why don't you read it with an xml parser? – Dmitry Ledentsov Oct 18 '13 at 18:52
  • I use it with LuaInterface which lets .net programs interact with lua scripts. Until now I did not had much trouble using LuaInterface but maybe LuaInterface has some internal 1024 bytes buffer? - I would also expect an error message and not a function. – Michael Gerbracht Oct 18 '13 at 19:51
  • I am not using an xml parser that loads the whole file at once (one that loads it line by line will probably fail as well) because the system I wrote the code for, had an application space of only 27MB and loading a large .ods file might fill that up very quickly. On windows I could probably change the code - but this would cost me a day or two. Here I could in principle skip those long lines - I do not need them. I just have to find a way to determine the end of file then because it seems that pcall will also return a function when reaching the EOF. – Michael Gerbracht Oct 18 '13 at 19:57

0 Answers0