############################################################################ # # Name: readfile.icn # # Title: read Bibleref files # # Author: Richard L. Goerwitz # # Version: 1.3 # ############################################################################ # # Contains readfile(), which reads a file from disk into memory. # Detects automatically whether the file contains passages or only # passage references. Converts both into Bibleref-format bitmap # lists. Files may be in one of two formats: 1) ::key + text format # (the format used when you write a passage to disk from within # Bibleref), and 2) a list of references: # # 1. Gen 1:3 # 2. Exod 2:4 # 3. Lev 3:5 # etc. # # This is the format Bibleref uses when you write one of its # internally generated passage lists to disk. The numbers+period # are optional. # ############################################################################ # # Links: ./bibleref.icn ./ref2bmap.icn # # See also: # ############################################################################ procedure readfile() local intext, fname, bitmap_list, firstline, line, firstchar until \intext do { fname := snarf_input("Enter filename (! for shell; q to quit): ") fname ? { firstchar := move(1) | "" case firstchar of { "" : fail "!" : push_shell() & initialize_screen() "q" : pos(0) & fail default : { intext := open(fname) | { err_message("Can't open " || fname || ".") } } } } } message("Reading.") bitmap_list := [] until firstline := ("" ~== trim(read(intext) | { err_message("Empty file.") fail })) firstline ? { # If the first nonblank line shows the file to be in gettext # format, then it was written by write_passage(), and we need # only read in keys. if ="::" & put(bitmap_list, ref_2_bitmap(tab(0), kjv_filename)) then { while line := trim(read(intext)) do { if ="::" then { put(bitmap_list, ref_2_bitmap(tab(0), kjv_filename)) | { err_message("Garbled ::key: "|| line ||".") fail } } else next } # If the file is not in gettext format, then try reading it in as # a pure list of passages. } else { tab(many(&digits ++ ' ')) & tab(match(".")) & tab(many(' ')) if put(bitmap_list, ref_2_bitmap(tab(0), kjv_filename)) then { while line := trim("" ~== !intext) do { line ? { tab(many(&digits ++ ' ')) & ="." & tab(many(' ')) put(bitmap_list, ref_2_bitmap(tab(0), kjv_filename)) | { err_message("Garbled line: "|| line ||".") fail } } } } else { err_message("Unrecognized file format. Aborting.") fail } } } message("Done.") put(lists, lst(bitmap_list, 0, &null, "(read from disk)")) return lists[-1] end