translitの下請けルーチン2015年01月03日 18:22

まだ紹介していないtranslitの下請けルーチンを紹介します。

まずは、length()。これは、文字列の長さを返します。文字列の最後を示すEOSは含みません。

# length.r4 -- compute length of string
      integer function length(str)
      character str(*)

      for (length = 0; str(length + 1) != EOS; length = length + 1)
          ;
      return
      end

Watcom Fortran77版は、下記の通り。

c length.for -- compute length of string
      integer function length(str)
      integer*1 str(*)
      
      length = 1
      while (str(length) .ne. -2) do      ! EOS(-2)
          length = length + 1
      end while
      length = length - 1
      return
      end

xindex()は次の通りです。フラグallbutに従いiindex()とは裏腹の結果を返します。

# xindex -- invert condition return by iindex
      integer function xindex(array,c,allbut,lastto)
      character array(ARB), c
      integer iindex
      integer lastto, allbut
      
      if (c == EOF) 
          xindex = 0
      else if (allbut == NO)
          xindex = iindex(array,c)
      else if (iindex(array,c) > 0)
          xindex = 0
      else
          xindex = lastto + 1
      return
      end

Watcom Fortran77版は、下記の通り。

c xindex -- invert condition return by iindex
      integer function xindex(array,c,allbut,lastto)
      integer*1 array(*),c
      integer allbut
      integer lastto,iindex
      
      if (c .eq. -1) then               ! EOF(-1)
          xindex = 0
      else if (allbut .eq. 0) then      ! NO(0)
          xindex = iindex(array,c)
      else if (iindex(array,c) .gt. 0) then
          xindex = 0
      else
          xindex = lastto + 1
      endif
      return
      end

最後に、error()です。これは、メッセージを表示しプログラムを停止させます。 メッセージは、固定文字列として引き渡す仕様になっているため、一工夫が必要です。

Watcom Fortran77版は、メッセージを表示するremark()を使って作りました。

c error.for -- print message s and stop
      subroutine error(s)
      character s(*)

      call remark(s)
      stop
      end
c remark.for -- print error message
      subroutine remark(s)
      character s(*)                    ! ARB(*)
      integer i
      
      i = 1
      while (s(i) .ne. '.') do
          call putc(ichar(s(i)))
          i = i + 1
      end while
      call putc(46)                     ! PERIOD(46)
      call putc(10)                     ! NEWLINE(10)
      return
      end

ここまでで、必要なパーツがそろいました。モジュールをビルドしライブラリーに登録し、translitを作成してください。 できましたら、早速テストしてみましょう。

C:\Users\Hiroya\Documents\ratfor\fortran\bat>..\exe\translit abc XYZ
abcdefg XYZ
XYZdefg XYZ
a b c X Y Z
X Y Z X Y Z
^Z

C:\Users\Hiroya\Documents\ratfor\fortran\bat>..\exe\translit a-z A
ABCDEFabcdefg
ABCDEFA
ABCDEFabcdefgXYZ
ABCDEFAXYZ
^Z

C:\Users\Hiroya\Documents\ratfor\fortran\bat>..\exe\translit @n #
abcd efg
hijk
^Z
abcd efg#hijk#

C:\Users\Hiroya\Documents\ratfor\fortran\bat>

いかがでしょうか。

さて、translitはフィルターです。ほかのプログラムとつなぎ合わせて使うことができます。

次回は、ファイルの処理をするための基礎ルーチンを紹介します。