文字バッファの表現と文書行の管理 ― 2016年03月06日 20:30
文字バッファの表現と文書行の管理
編集するテキストは、バッファbuf()にしまい込む。この中には、行と行をリンクさせる 指標も含む。
buf(k+0) PREV 前の行の指標 buf(k+4) NEXT 次の行の指標 buf(k+8) MARK 広域指定の処理に使用 buf(k+9) TEXT 文字列の第一文字 buf(k+10) ... 第二文字・・・
このバッファを操作するルーチンをは、下記の通り。
setbuf() バッファを初期化して、第0行をだけを含む状態にする。 clrbuf() 作業ファイルを捨てる。現状の版では、何もしない。 inject(lin) linのテキストをバッファに転写する。 curlinは、最後に送り込まれた行を指させる。 getind(n) 行番号nをその行を占める指標に変換する。 gettxt() 行の内容を共通領域ctxtのtxtに転写する。 relink(k1,k2,k3,k4) 指標のつなぎ替えを行う。k2の逆方向の指標にk1をささせる。 k3の順方向の指標にはk4を指させる。
文字バッファbufは共通領域cbufに置かれる。 共通領域cbufのRATFOR版は、以下の通り。
# cbuf.ri common /cbuf/buf,lastbf character buf(MAXBUF) # buffer for pointers plus text integer lastbf # last element used in buf
WATCOM Fortran77版は以下の通り。
c cbuf.fi common /cbuf/buf,lastbf integer*1 buf(20000000) ! MAXBUF(20000000) buffer for pointers plus text integer lastbf ! last element used in buf
文字バッファの文書は必要に応じてバッファtxtに転写される。 バッファtxtは共通領域ctxtに置かれる。 共通領域ctxtのRATFOR版は、以下の通り。
# ctxt.ri common /ctxt/txt character txt(MAXLINE) # text line for matching and output
WATCOM Fortran77版は以下の通り。
c ctxt.fi common /ctxt/txt integer*1 txt(81) ! MAXLINE(81) text line for matching and output
setbuf()のRATFOR版は、以下の通り。
# setbuf.r4 (in memory) -- initialize line storage buffer subroutine setbuf logical addset,junk include cbuf.ri include clines.ri call relink(LINE0,LINE0,LINE0,LINE0) lastbf = LINE0 + TEXT junk = addset(EOF,buf,lastbf,MAXBUF) curln = 0 lastln = 0 return end
WATCOM Fortran77版は以下の通り。
c setbuf.f (in memory) -- initialize line storage buffer subroutine setbuf integer addset,junk include cbuf.fi include clines.fi call relink(1,1,1,1) ! LINE0(1) lastbf = 1 + 12 ! LINE0(1) TEXT(12) junk = addset(-2,buf,lastbf,20000000) ! EOS(-2) MAXBUF(20000000) curln = 0 lastln = 0 return end
clrbuf()のRATFOR版は、以下の通り。
# clrbuf.r4 -- initialize for new file subroutine clrbuf return # nothing to do end
WATCOM Fortran77版は以下の通り。
c clrbuf.for -- initialize for new file subroutine clrbuf return ! nothing to do end
getind()のRATFOR版は、以下の通り。getbufptr()は、バッファbufから、integerの 指標を取り出すためのルーチンである。
# getind.r4 -- locate line index in buffer integer function getind(line) integer line integer j,k,getbufptr include cbuf.fi k = LINE0 for (j = 0; j < line; j = j + 1) k = getbufptr(buf(k+NEXT)) getind = k return end
WATCOM Fortran77版は以下の通り。
c getind.f -- locate line index in buffer integer function getind(line) integer line integer j,k,getbufptr include cbuf.fi k = 1 ! LINE0(1) j = 0 while (j .lt. line) do k = getbufptr(buf(k+4)) ! NEXT(4) j = j + 1 end while getind = k return end
getbufptr()のRATFOR版は、以下の通り。
# getbufptr.r4 integer function getbufptr(buf) integer buf getbufptr = buf return end
WATCOM Fortran77版は以下の通り。
c getbufptr.for integer function getbufptr(buf) integer buf getbufptr = buf return end
inject()のRATFOR版は、以下の通り。
# inject.f -- put text from line after curln integer function inject(lin) character lin(MAXLINE) integer addset,junk integer getind,nextln integer i,k1,k2,k3 include cbuf.ri include clines.ri for (i = 1; lin(i) != EOS; ) { k3 = lastbf lastbf = lastbf + TEXT while (lin(i) != EOS) { junk = addset(lin(i),buf,lastbf,MAXBUF) i = i + 1 if (lin(i-1) == NEWLINE) break } if (addset(EOS,buf,lastbf,MAXBUF) == NO) { inject = ERR break } k1 = getind(curln) k2 = getind(nextln(curln)) call relink(k1,k3,k3,k2) call relink(k3,k2,k1,k3) curln = curln + 1 lastln = lastln + 1 inject = OK } return end
WATCOM Fortran77版は以下の通り。
c inject.f -- put text from line after curln integer function inject(lin) integer*1 lin(*) integer addset,junk integer getind,nextln integer i,k1,k2,k3 include cbuf.fi include clines.fi i = 1 while (lin(i) .ne. -2) do ! EOS(-2) k3 = lastbf lastbf = lastbf + 12 ! TEXT(12) while (lin(i) .ne. -2) do ! EOS(-2) junk = addset(lin(i),buf,lastbf,20000000) ! MAXBUF(20000000) i = i + 1 if (lin(i-1) .eq. 10) then ! NEWLINE(10) exit end if end while if (addset(-2,buf,lastbf,20000000) .eq. 0) then ! NO(0) MAXBUF(20000000) inject = -3 ! ERR(-3) exit end if k1 = getind(curln) k2 = getind(nextln(curln)) call relink(k1,k3,k3,k2) call relink(k3,k2,k1,k3) curln = curln + 1 lastln = lastln + 1 inject = -2 ! OK(-2) end while return end
relink()のRATFOR版は、以下の通り。
# relink.r4 -- rewrite two harf links subroutine relink(a,x,y,b) integer a,b,x,y include cbuf.ri call setbufptr(a,buf(x+PREV)) call setbufptr(b,buf(y+NEXT)) return end
WATCOM Fortran77版は以下の通り。
c relink.f -- rewrite two harf links subroutine relink(a,x,y,b) integer a,b,x,y include cbuf.fi call setbufptr(a,buf(x+0)) ! PREV(0) call setbufptr(b,buf(y+4)) ! NEXT(4) return end
ここで、setbufptr()は指標をセットするルーチン。
setbufptr()のRATFOR版は、以下の通り。
# setbufptr.r4 subroutine setbufptr(ptr,buf) integer ptr,buf buf = ptr return end
WATCOM Fortran77版は以下の通り。
c setbufptr.for subroutine setbufptr(ptr,buf) integer ptr,buf buf = ptr return end
最近のコメント