• Top
    • Documentation
    • Books
    • Recursion-and-induction
    • Boolean-reasoning
    • Debugging
    • Projects
    • Std
      • Std/lists
      • Std/alists
      • Obags
      • Std/util
      • Std/strings
      • Std/io
        • Open-channel-lemmas
        • Std/io/read-char$
        • Std/io/read-object
        • Std/io/open-output-channel
        • Unsound-read
        • Read-string
        • Read-bytes$
        • File-measure
        • Read-bytes$-n
        • Std/io/read-byte$
        • Std/io/open-input-channel
        • Print-compressed
        • Read-file-lines-no-newlines
          • Read-file-lines-no-newlines-aux
        • Nthcdr-bytes
        • Read-file-lines
        • Std/io/close-output-channel
        • Read-file-characters
        • Read-file-bytes
        • Print-legibly
        • Std/io/close-input-channel
        • Read-file-objects
        • Logical-story-of-io
        • Take-bytes
        • Std/io/peek-char$
        • Read-file-characters-rev
        • Read-file-as-string
        • Std/io/write-byte$
        • Std/io/set-serialize-character
        • Std/io/print-object$
        • Std/io/princ$
        • Std/io/read-file-into-string
        • *file-types*
      • Std/osets
      • Std/system
      • Std/basic
      • Std/typed-lists
      • Std/bitsets
      • Std/testing
      • Std/typed-alists
      • Std/stobjs
      • Std-extensions
    • Proof-automation
    • Macro-libraries
    • ACL2
    • Interfacing-tools
    • Hardware-verification
    • Software-verification
    • Testing-utilities
    • Math
  • Std/io

Read-file-lines-no-newlines

Read an entire file into a list of lines (strings), omitting newline characters from the resuting strings

Signature: (read-file-lines-no-newlines filename state) returns (mv contents state).

On success, contents is a string-listp that contains each line of the file, with all newline characters removed (unlike read-file-lines). A line of the file that contains only a newline character will be represented by an empty string in this list.

On failure, e.g., perhaps filename does not exist, contents will be a stringp saying that we failed to open the file.

BOZO This currently just looks for individual newline characters, i.e., #\Newline, sometimes called \n. It might be desirable to change how it works to somehow support \r\n or whatever other carriage-return stuff people use on platforms like Windows.

Definitions and Theorems

Function: read-file-lines-no-newlines

(defun
     read-file-lines-no-newlines
     (filename state)
     "Returns (MV ERRMSG/LINES STATE)"
     (declare (xargs :guard (stringp filename)
                     :stobjs state))
     (b* ((filename (mbe :logic (if (stringp filename) filename "")
                         :exec filename))
          ((mv channel state)
           (open-input-channel filename
                               :byte state))
          ((unless channel)
           (mv (concatenate 'string
                            "Error opening file " filename)
               state))
          ((mv data state)
           (read-file-lines-no-newlines-aux nil nil channel state))
          (state (close-input-channel channel state)))
         (mv (reverse data) state)))

Theorem: state-p1-of-read-file-lines-no-newlines

(defthm
 state-p1-of-read-file-lines-no-newlines
 (implies
  (force (state-p1 state))
  (state-p1 (mv-nth 1
                    (read-file-lines-no-newlines filename state)))))

Theorem: string-listp-of-read-file-lines-no-newlines

(defthm
 string-listp-of-read-file-lines-no-newlines
 (equal
  (string-listp
       (mv-nth 0
               (read-file-lines-no-newlines filename state)))
  (not
      (stringp
           (mv-nth 0
                   (read-file-lines-no-newlines filename state))))))

Subtopics

Read-file-lines-no-newlines-aux
Tail recursive implementation of read-file-lines-no-newlines.