Regarding the read-a-line at a time discussion. Some few years ago ago I wrote the small attached module to iterate through lines from a "text" file regardless of source, I have seen line endings such as \r\n from Windows, \n on Unix, and \r on Mac. The module allows you to iterate through all such files gracefully with io.lines. I have used this successfully on 10,000's of files from all sorts of places and systems.
This is inspired by David Manura's code here:
http://lua-users.org/wiki/EnhancedFileLines, but takes it a couple of steps further.
It does NOT return the line ending at the end of the line to the calling function. I also treats the line ending on the last line in the file as optional.
Sorry if I am not allowed to attach a file to an email, I have been on this mailing list for ages, and this is the first time I felt I could contribute something meaningful.
I do have some code also for general handling of BOM's which I can supply if anyone is interested.
Regards
George Williams