|
Forum Index : Microcontroller and PC projects : Bug - LINE INPUT and INPUT
| Author | Message | ||||
| panky Guru Joined: 02/10/2012 Location: AustraliaPosts: 1116 |
A couple of recent posts dealt with LINE INPUT and INPUT commands and while I was playing around with the issues mentioned I came across the following which I believe to be a bug. Both LINE INPUT and LINE deal with a single line at a time either from the Console or from a file. In both cases, if the last line in the file does NOT have a terminating CR or CR LF, the last character in the file is NOT read. Both LINE INPUT and INPUT make calls to MMgetline in SerialFileIO.c and the code there around line 199 appears to be the problem (I think, I am NOT at all proficient in C but it appears the may be an inadvertant duplication of a couple of lines). What appears to be happening is that as the line is read in by either of the commands above, if EOF is reached and the is no terminating CR in the file, the last character in the file is not read and passed back by MMgetline. The demo code below illustrates the problem. As you will see, the last character in the second (last) line, a "z" is not returned by either LINE INPUT or LINE. This was tested in both MMBasic Plus 5.04.10b3 and 5.03.04 and also in MMBasic Extreme 5.04.17. Interestingly, both commands work correctly in MMBasic for DOS (I guess this may be because file IO is done by Windows ?) The hexdump sub at the bottom below is purely to show that the missing character is actually correct in the file. Please excuse me if I am off the track here but I have tested thoroughly and think I have it correct. Doug. ... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it! |
||||
| matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 10570 |
Geoff can comment further but I would argue that the bug is that the code returns anything at all. I think it should return an empty string as the input does not meet the characteristics of a "line" i.e. it is unterminated. |
||||
| panky Guru Joined: 02/10/2012 Location: AustraliaPosts: 1116 |
... or preferrably indicate some form of error as the last CSV field in an INPUT command or the last line in an INPUT LINE command would be incorrect but the user would not be aware. ... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it! |
||||
| panky Guru Joined: 02/10/2012 Location: AustraliaPosts: 1116 |
There does not appear to be a finalised and formal definition for CSV but I found the following in Wikipedia ... Taking the above into consideration, maybe an 'unterminated' final field or line should be acceptable? At least that way, no data would be'lost'. panky ... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it! |
||||
| panky Guru Joined: 02/10/2012 Location: AustraliaPosts: 1116 |
Thinking about it a bit further, I would prefer MMB to send ALL data back and leave the validity checking to the user. For example, if the final field in an INPUT command was missing the CR and MMB made the decision it had an incorrect structure (ie. missing terminator) and just sent back an empty field, I the user program, would have no way of determining if the field was valid or not? It could have been an intentionally empty ( and thus still valid ) field. My feeling is that data validity is a user program responibility while data integrity is MMB's responsibility. panky ... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it! |
||||
| Geoffg Guru Joined: 06/06/2011 Location: AustraliaPosts: 3308 |
Thanks Doug (panky), that's a great find and thanks for even spotting the issue in the source (I think that you are right). My opinion is that MMBasic should treat end of file the same as a CR/LF - ie, return the chars collected up to that point as a line. I should have the fix in the next beta, Geoff Geoff Graham - http://geoffg.net |
||||
MicroBlocks![]() Guru Joined: 12/05/2012 Location: ThailandPosts: 2209 |
Other BASICs also treat the end of file as the end of the input. The difference between windows and Linux having different line endings was solved by using the LF as the end of the line and ignoring the CR. This required my own input that read character by character. For MMBasic i would assume it is pretty safe to ignore the CR and just use the LF as a end of line. A difficulty is that when text is read there might be a LF inside double quotes. CSV handles this by reading from one double quote to the next and everything in between is the string value. If you have control about the file layout this all is a moot point. You just make it as MMBasic processes it. Saves a lot of testing for conditions that might never occur. Microblocks. Build with logic. |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |