ファイルを分割する

説明

巨大なファイルを分割する。unixでいうsplitを目指したもの。あいかわらずオレオレ大域変数を使っている。

ELIS復活祭

[戻る]

実行例

ファイルを分割する。分割単位は指定しなければ10000バイト。ただしメモリに読み込めるサイズまで。文字列のメモリ領域確保をあらかじめしておく。

Tao>(splitfile "input.txt" 32768)
[戻る]

ソースコード


;  ファイルの分割
;
;  TAO mode
;  IDATEN/K.Takeshita
;  91/02/23 雪が降ったよ
;



;
;  read-pool-byte poolにsizeバイト程読み込む
;  91/02/23

(de read-pool-line-n ( fp size &aux i n dat)
    (!dat nil) (!i 0)
    (loop
     (if (eq (!n (read-line fp)) :eof) (return dat) )
	 (!dat (tcons dat n))
	 (if (> (!i (+ i (string-byte-count n) 2)) size) (return dat))
	 )
)


;
;  splitfile
;  91/02/23

(de splitfile ( infile &optn run-factor &aux factor in vers sp block sec)
    ( if (integerp run-factor) (!factor run-factor) (!factor 10000) )
    (!vers 1) (!sp nil) (!size (- (* (!block 1850) (!sec 512)) 2048))
    ( if (probe-file infile) 
     (progn 
      (with-open-file (in infile)
	(progn
	 (loop (&aux r fp outfile) (:init (!r nil))
	       (if (null (!r (read-pool-line-n in size))) (return))
	       (with-open-file 
		(fp (!outfile (namestring (make-pathname
					   :name (pathname-name infile)
					   :type (format nil "#~D" vers))))
		    :direction :output )
		(progn 
		 (mapc (lambda (x) (write-line x fp)) r)
		 (!sp (cons (sconc (pathname-name outfile)
				   "."
				   (pathname-type outfile)) sp ))
		 (inc vers))
		) ) ))
      (with-open-file (fp (namestring (make-pathname
				       :name (pathname-name infile)
				       :type "#ndx" ))
			  :direction :output )
	(progn 
	 (write (cons (namestring (truename infile)) (reverse sp)) fp )
	 (write (file-properties infile t) fp ) 'ok
	 )))
     (nil))
    )

ELIS8130のフロッピーに保存できるサイズに分割する。多分こっちを先に作った。


;
;  split.tao fdデバイスのブロック数より長いファイルを分割する
;  91/02/23 IDATEN/K.Takeshita
;

(de split-file ( infile &optn index-file &aux blocks bytes factor fl )
    (!factor (* (!blocks 1850) (!bytes 512)) )
    (prog (n m in out tag name st ct run) (!ct 0) (!run 1)
     (if (null (!n (probe-file infile))) (return nil))
     (if (< (! fl (file-length (!n (namestring n)))) factor ) (return "分割無用"))
     (with-open-file (tag (namestring (make-pathname
				       :name (!name (pathname-name n))
				       :type "#index#" ))
			  :direction :output)
       (progn (write n tag) (file-properties n t)
	   (with-open-file (in infile)
	     (progn (!st 0)
		 (with-open-file (out 
				  (!m (namestring 
				       (make-pathname
					:name name 
					:type (format nil "#~D",run)))))
		   (loop  (&aux c) (:init (inc run))


[戻る]
inserted by FC2 system