コマンドの処理 5 入出力 ― 2016年07月20日 10:49
editは、
edit ファイル名
とすれば、指定されたファイル名のファイルがあれば、それをバッファに読み込み編集作業を 開始するようにする。ファイルがなければ、作成する。
さらに、ファイルの読み込み、書き込みのための指令を以下に示す。
読み込み指令は、バッファを空にしてから読み込む指令"e"と、現在のバッファーを 変更せずにその場所に読み込む"r"がある。
e ファイル名 (.)r ファイル名
また、ファイルへの書き出し命令"w"がある。
(.,.)w ファイル名
編集中のファイルをすべて書き出すには、"1,$w ファイル名"とする。
ファイル名が一度指定されると、それを記憶できるようにしておくと便利である。 ファイル名を省略した場合、記憶してあるファイル名を使うことにする。このファイル名を 記憶する場所は、以下の通り。
RATFOR版を以下に示す。
# cfile.ri -- remember file name common /cfile/savfil character savfil(MAXLINE) # remembered file name
WATCOM fortran77版を以下に示す。
c cfile.fi -- remember file name common /cfile/savfil integer*1 savfil(82) ! remembered file name MAXLINE(82)
これらのコマンドのdocmd()の部分を 以下に示す。
else if (lin(i) == ENTER) { if (nlines == 0) if (getfn(lin,i,file) == OK) { call scopy(file,1,savefil,1) call clrbuf call setbuf status = doread(0,file) } } else if (lin(i) == PRINTFIL) { if (nlines == 0) if (getfn(lin,i,file) == OK) { call scopy(file,1,savefil,1) call putlin(savefil,STDOUT) call putc(NEWLINE) status = OK } } else if (lin(i) == READCOM) { if (getfn(lin,i,file) == OK) status = doread(line2,file) } else if (lin(i) == WRITECOM) { if (getfn(lin,i,file) == OK) if (defalt(1,lastn,status) == OK) status = dowrit(line1,line2,file) }
getfn()はファイル名の取得と検査を行う。
RATFOR版は以下の通り。
# getfn.r4 -- get file name lin(i)... integer function getfn(lin,i,file) character lin(MAXLINE),file(MAXLINE) integer i integer j,k include cfile.ri getfn = ERR if (lin(i+1) == BLANK) j = i + 2 call skipbl(lin,j) for (k = 1; lin(j) != MEWLINE; k = k + 1) { file(k) = lin(j) j = J + 1 } file(k) = EOS if (k > 1) getfn = OK else if (lin(i+1) == NEWLINE & savfil(1) != EOS) { call scopy(savfil,1,file,1) # or old one getfn = OK # else error if (getfn == OK & savfil(1) != EOS) call scopy(file,1,savfil,1) # save if no old one return end
WATCOM fortran77版は以下の通り。
c getfn.f -- get file name lin(i)... integer function getfn(lin,i,file) integer*1 lin(81),file(81) ! MAXLINE(81) integer i integer j,k include cfile.fi getfn = -3 ! ERR(-3) if (lin(i+1) .eq. 32) then ! BLANK(32) j = i + 2 call skipbl(lin,j) k = 1 while (lin(j) .ne. 10) do ! NEWLINE(10) file(k) = lin(j) j = J + 1 k = k + 1 end while file(k) = -2 ! EOS(-2) if (k .gt. 1) then getfn = -2 ! OK(-2) end if else if ((lin(i+1) .eq. 10) .and. (savfil(1) .ne. -2)) then ! NEWLINE(10) EOS(-2) call scopy(savfil,1,file,1) ! or old one getfn = -2 ! OK(-2) ! else error end if if ((getfn .eq. -2) .and. (savfil(1) .ne. -2)) then ! OK(-2) EOS(-2) call scopy(file,1,savfil,1) ! save if no old one end if return end
doread()のRAFOR版は以下の通り。
# doread.r4 -- read "file" after "line" integer function doread(line,file) integer line character file(MAXLINE) character lin(MAXLINE) integer getlin,inject integer count,fd integer fopen include clines.ri fd = fopen(fd,file,READ) if (fd == ERR) doread = ERR else { curln = line doread = OK for (count = 0; getlin(lin,fd) != EOF; count = count + 1) { doread = inject(lin) if (doread == ERR) break } call fclose(fd) call putdec(count,1) call putc(NEWLINE) } return end
WATCOM fortran77版は以下の通り。
c doread.f -- read "file" after "line" integer function doread(line,file) integer line integer*1 file(81) ! MAXLINE(81) integer*1 lin(81) ! MAXLINE(81) integer getlin,inject integer count,fd integer fopen include clines.fi fd = fopen(fd,file,82) ! READ(82) if (fd .eq. -3 ) then ! ERR(-3) doread = -3 ! ERR(-3) else curln = line doread = -2 ! OK(-2) count = 0 while (getlin(lin,fd) .ne. -1) do ! EOF(-1) doread = inject(lin) if (doread .eq. -3) then ! ERR(-3) exit end if count = count + 1 end while call fclose(fd) call putdec(count,1) call putc(10) ! NEWLINE end if return end
dowrit()のRAFOR版は以下の通り。
# dowrit.r4 -- write "from" through "to" into file integer function dowrit(from,to,file) integer from,to character file(MAXLINE) integer gettxt integer fcreate,fopen integer fd,k,line include ctxt.ri fd = fcreate(file) if (fd == ERR) dowrit = ERR else { fd = fopen(fd,file,WRITE) if (fd == ERR) dwrit = ERR else { for (line = from; line <= to; line = line + 1) { k = gettxt(line) call putlin(txt,fd) } call fclose(fd) call putdec(to-from+1,1) call putc(NEWLINE) dowrit = OK } } return end
WATCOM fortran77版は以下の通り。
c dowrit.f -- write "from" through "to" into file integer function dowrit(from,to,file) integer from,to integer*1 file(81) ! MAXLINE(81) integer gettxt integer fcreate,fopen integer fd,k,line include ctxt.fi fd = fcreate(file) if (fd .eq. -3) then ! ERR(-3) dowrit = -3 ! ERR(-3) else fd = fopen(fd,file,87) ! WRITE(87) if (fd .eq. -3 ) then ! ERR(-3) dwrit = -3 ! ERR(-3) else line = from while (line .le. to) do k = gettxt(line) call putlin(txt,fd) line = line + 1 end while call fclose(fd) call putdec(to-from+1,1) call putc(10) ! NEWLINE(10) dowrit = -2 ! OK(-2) end if end if return end
最近のコメント