# L-24 MCS 507 Mon 16 Oct 2023 : scanquotes.py
"""
Opens a file and returns all strings on file
that are between the double quotes.
"""

def update_qstrings(acc, buf, data):
    """
    acc is a list of double quoted strings,
    buf buffers a double quoted string, and
    data is the string to be processed.
    Returns an updated (acc, buf).
    """
    newbuf = buf
    for char in data:
        if newbuf == '':
            if char == '\"':
                newbuf = 'o' # 'o' is for 'opened'
        else:
            if char != '\"':
                newbuf += char
            else:       # do not store 'o'
                acc.append(newbuf[1:len(newbuf)])
                newbuf = ''
    return (acc, newbuf)

def quoted_strings(file):
    """
    Given a file object, this function scans
    the file and returns a list of all strings
    on the file enclosed between double quotes.
    """
    result = []
    buffer = ''
    while True:
        data = file.read(80)
        if data == '':
            break
        (result, buffer) \
            = update_qstrings(result, buffer, data)
    return result

def main():
    """
    Prompts the user for a file name and
    scans the file for double quoted strings.
    """
    print('getting double quoted strings')
    name = input('Give a file name : ')
    file = open(name, 'r')
    strs = quoted_strings(file)
    print(strs)
    file.close()

if __name__ == "__main__":
    main()
