一発ではないが

多少小さくなった。ハードコーディング気味。なぜ?おそらく foreach という発想から抜け出さないとうまくかけないのだろう。私は抜け出せていない。

(asdf:oos 'asdf:load-op 'cl-ppcre)

(defun my-file-input ()
  (with-open-stream (stream (open "yanau_cs.txt" :direction :input))
    (let (output)
      (do ((curr (read-line stream)
		 (read-line stream nil nil)))
	((null curr))
	(setf output 
	      (string-concat (if (null output) "" output) 
			     (string #\newline)
			     curr)))
      output)))

(setf pt0 '("XXX" ("x_A" "x_B" "x_C")))
(setf pt1 '("YYY" ("y_A" "y_B" "y_C")))

(defun my-replace (str org_str rep_str_ln) 
  (mapcar (lambda (x) 
		  (cl-ppcre:regex-replace-all org_str str x))
	  rep_str_ln))

;(defun test () (my-replace "---XXX---" "XXX" '("jgeil" "s" "band")))
;(defun test () (my-replace (my-file-input) "XXX" '("jgeil" "s" "band")))
;(defun test () (my-replace "---XXX---" (car pt0) (cadr pt0)))

(defun do-my-expand (ptn)
  (let ((str-ln (my-replace (my-file-input) (car pt0) (cadr pt0)))
	output)
    (mapcar (lambda (str-x)
	      (setf output
		    (cons 
		      (my-replace str-x (car ptn) (cadr ptn)) output)))
	 str-ln)
    output))

(with-open-stream (*standard-output* (open "output2.cs" :direction :output))
  (setf ln (do-my-expand pt1))
  (mapcar (lambda (x)
	    (princ x))
	  ln))

(with-open-stream (str (open "output3.cs" :direction :output))
  (let ((str-ln (my-replace (my-file-input) (car pt0) (cadr pt0))))
    (format str "~s" str-ln)))