aboutsummaryrefslogtreecommitdiff
path: root/challenge-072/jeongoon/common-lisp/ch-2.lsp
blob: c1e960d8fd0fb98aacc28b58291a17569b4fedbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
;; Ref:
;; http://www.gigamonkeys.com/book/files-and-file-io.html
;; http://cl-cookbook.sourceforge.net/files.html#open
;; http://www.ai.sri.com/pkarp/loop.html

(defun get-command-line ()
  (or
   #+CLISP *args*
   #+SBCL *posix-argv*
   #+LISPWORKS system:*line-arguments-list*
   #+CMU extensions:*command-line-words*
   nil))

(defparameter *cmdline* (get-command-line))

(defun print-usage ()
  (format t "Usage: sbcl --script ch-2.lsp <file-path> <from> <to>~%" (car *cmdline*)))

(when (not (= (length *cmdline*) 4)) ;; not 3
  (format t "Wrong Number of arguments got ~d: not 3~%" (length *cmdline*))
  (print-usage)
  (quit))

(defparameter *file-path* (nth 1 *cmdline*))
(defparameter *from*      (parse-integer (nth 2 *cmdline*)))
(defparameter *to*        (parse-integer (nth 3 *cmdline*)))

(when (not (probe-file *file-path*))
  (format t "Could not read the file ~a~%" *file-path*)
  (quit))

(when (not (and (numberp *from*) (numberp *to*)))
  (format t "given numbers are not in the range: from ~a, to ~a" *from* *to*)
  (quit))

(format t "[INF] File: ~a: from: ~d to: ~d~%" *file-path* *from* *to*)

(with-open-file
 (in *file-path* :if-does-not-exist nil) ;; return nil instead of error
 (loop for line = (read-line in nil)
       for cnt from 1  ;; cool auto counter

       while line do   ;; I don't know whay parenthesis is not used here.
                       ;; look like a macro for something
       (progn
         (when (and (<= *from* cnt) (<= cnt *to*))
           (format t "~a~%" line))
         (when (= cnt *to*)
           (format t "[INF] Done.~%")
           (return "[done]"))
         (setq cnt (+ 1 cnt)))))