Menu

INPUT Bugs

Anonymous
2014-08-10
2014-08-13
  • Anonymous

    Anonymous - 2014-08-10

    I have found a couple of input bugs:

    1. Spurious spaces when reading unquoted strings.

    Test program:

    10 INPUT Z$
    20 PRINT Z$
    

    Run the program and when prompted enter:
    AAA BBB

    The program responds:
    AAA B B B

    Note the extra spaces introduced between the Bs.

    The fix is a one liner in routine "input_entry" in file "representation.py":

    def input_entry(text_file, allow_quotes, end_all=(), end_not_quoted=(',',)):
        word, blanks = '', ''
        # skip leading spaces and line feeds and NUL. 
        c = text_skip(text_file, ascii_white)
        if c in end_all + end_not_quoted:
            return ''
        quoted = (c == '"' and allow_quotes)
        if quoted:
            text_file.read_chars(1)
        while True:
            # read entry
            if text_file.end_of_file():
                break
            c = ''.join(text_file.read_chars(1))
            if c in end_all or (c in end_not_quoted and not quoted):
                # on KYBD: text file, this will do nothing - comma is swallowed
                text_file.seek(-len(c), 1)
                break
            elif c == '"' and quoted:
                quoted = False
                # ignore blanks after the quotes
                c = text_skip(text_file, ascii_white)
                break
            elif c in ascii_white and not quoted:
                blanks += c    
            else:
                word += blanks + c
                blanks = '' # WJB - clear blanks after reading a non-blank character
            if len(word)+len(blanks) >= 255:
                text_file.seek(-len(c), 1)
                break
        return word
    

    2. Error reading numeric value on one line, followed by text string on the next.

    Test program:

    10 OPEN "A:TEST.DAT" FOR OUTPUT AS 1
    20 PRINT#1, 314
    30 PRINT#1, "Test String"
    40 CLOSE#1
    50 OPEN "A:TEST.DAT" FOR INPUT AS 1
    60 INPUT#1, N
    70 PRINT N
    80 INPUT#1, A$
    90 PRINT A$
    100 CLOSE#1
    110 PRINT "END"
    

    This displays:

    314

    END

    The "Test String" is not read. Dumping the test data file shows:

    00000000  20 33 31 34 20 0d 0a 54  65 73 74 20 53 74 72 69  | 314 ..Test Stri|
    00000010  6e 67 0d 0a 1a                                    |ng...|
    

    The numeric value is followed by <space><cr><lf>. The existing code just skips the trailing space, leaving the file pointer at the end of line. So the following text input just reads an empty string. It is necessary to skip all trailing white space and possibly one end of line after reading a value. The fix is in routine "input_vars_file", again in "representation.py":

    def input_vars_file(readvar, text_file):
        for v in readvar:
            typechar = v[0][-1]
            if typechar == '$':
                valstr = input_entry(text_file, allow_quotes=True, end_all = ('\r', '\x1a'), end_not_quoted = (',', '\n'))
                print ('Read string "%s" from file' % valstr)
            else:
                valstr = input_entry(text_file, allow_quotes=False, end_all = ('\r', '\x1a', ',', '\n', ' '))
                print ('Read value %s from file' % valstr)
            value = str_to_type(valstr, typechar)    
            if value == None:
                value = vartypes.null[typechar]
            # process the ending char (this may raise FIELD OVERFLOW but should avoid INPUT PAST END)
            if not text_file.end_of_file() and text_file.peek_char() not in ('', '\x1a'):
                text_file.read_chars(1)
            # WJB - Skip trailing space and an end of linw
            text_skip (text_file, ascii_white)
            if not text_file.end_of_file() and text_file.peek_char() == '\r':
                text_file.read_chars(1)
                if not text_file.end_of_file() and text_file.peek_char() == '\n':
                    text_file.read_chars(1)
            # and then set the value
            v.append(value)
        return readvar    
    

    Regards,

    WJB

    Don't seem to be able to get code formatting to work, sorry.

     
  • Rob Hagemans

    Rob Hagemans - 2014-08-10

    Hi WJB, thanks for reporting the bugs and for your patches!

    I believe bug #1 has been fixed, at least in the development branch, but it's probably still there in the latest package release - I'll check and do a bugfix release.

    I'll have a look at your test case and patch for bug #2.

     
  • Rob Hagemans

    Rob Hagemans - 2014-08-10

    I've implemented your fix, slightly modified to account for comma-separated values. Thanks!

     
  • Rob Hagemans

    Rob Hagemans - 2014-08-13

    These bugs are now fixed with release 14.04.5.