[comp.os.vms] TPU procedure for rot13. My examples.

brodie@fps.mcw.edu (12/22/89)

Well, here it is, my first attempt at TPU programming.  I have to
thanks Geoff for forcing me to do this (well, not forcing, but I 
needed a good excuse to learn how to do junk inside of TPU for a
change....   up to this point, I'd never used my own SECTION file.
Damned neat, once you take the time to learn it......)


First off, I would like to show you my "first" attempt at a rot-13
procedure.   works pretty damned good, for a first try....
(no flames, please.  I'm a good programmer, but TPU is totally new to
me.  It doesn;t help when you don't know all of the neat stuff available..)

!
! TPU procedure to ROT13 a text file.
! Copyright (C) 1989 Kent C. Brodie / Medical College of Wisconsin
! This software is available to all VAX users, and can be distributed
! freely as long as the above copyright is included.   So there.
!
procedure eve_rot13
message ("Beginning ROT13 procedure...");
letterpat := ANY("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");

  position(beginning_of(current_buffer));			! Go to the start of the current buffer
 
loop;						! For each character in buffer
 exitif (mark(none) = end_of(current_buffer));	! exit if EOF found
 match_pos := search_quietly (letterpat, FORWARD);	! find the next (A-Z|a-z)
 exitif (match_pos = 0);
 position (match_pos);			! go there
 letterval := ASCII(current_character);
 newletter := "";
 if (letterval >= 65) and (letterval <= 90) 
  then
    if (letterval > 77)
      then
         letterval := letterval - 13;
      else
         letterval := letterval + 13;
    endif;
    newletter := ASCII(letterval);
  else
    if (letterval >= 97) and (letterval <= 122)
       then
          if (letterval > 109)
            then
               letterval := letterval - 13;
            else
               letterval := letterval + 13;
          endif;
       newletter := ASCII(letterval);
       endif;
  endif;
if (newletter <> "") 
  then
     deleted_char := erase_character(1);
     copy_text(newletter);
  endif;
endloop; 
endprocedure;



that was my first try.   Then I realized that was pretty gross, so I 
bettered it.  I removed all of those multiple if statements, and
used a long alphabet string.   Locate the letter, move 13 right,
grab the new letter, done.    My first example used too many of the
old "fortran" or "pascal" methods of doing it.    The second pass
made use of the TPU string handling junk.   much nicer.

!
! TPU procedure to ROT13 a text file.
! Copyright (C) 1989 Kent C. Brodie / Medical College of Wisconsin
! This software is available to all VAX users, and can be distributed
! freely as long as the above copyright is included.   So there.
!
! This procedure works by utilizing the string handling capabilities
! of TPU.  The string "letters" is used for the rot-ting process, which
! is why there are TWO sets of upper and lower case.  In this fashion,
! the "rot" factor (see variable rotfactor) can be changed to create a
! new rot program (such as rot17, etc.).
!
procedure eve_rot13

 !declare program constants to be used.   Change "rotfactor" to do the
 !rot-ting of your choice.  Due to popularity, the default is 13.
 !
 rotfactor := 13;
 letterpat := ANY("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
 letters := "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";

 message ("Beginning ROT13 procedure...");
 position(beginning_of(current_buffer));	! Go to the top...
 
loop;						! For each character in buffer
 exitif (mark(none) = end_of(current_buffer));	! exit if EOF found
 match_pos := search_quietly (letterpat, FORWARD);	! find the next (A-Z|a-z)
 exitif (match_pos = 0);			! exit if no more letters found
 position (match_pos);				! go there
 letterpos := index (letters,current_character); ! Look in "letters" for index
 letterpos := letterpos + rotfactor;		! ad the "rot factor" to the index
 newletter := substr (letters,letterpos,1);	! create the new letter
 deleted_char := erase_character(1);		! delete the old character
 copy_text(newletter);				! replace it with the new one.
endloop; 
endprocedure;


Now I thought I was done.  Nice, clean, compact code.   Then I saw it.
(gee, you mean it helps to RTFM?)   heh.    God bless it, it was right
in the (^!%^(&! book.   Seems there's this neat little TPU built-in
called TRANSLATE that does all the work for you, and is quite flexible.

I basically ended up stealing this from the book, but hey, that's what
us computer folk do best, make use of someone ELSE's work..

!
! TPU procedure to ROT13 a text file.
! 12/20/1989 Kent C. Brodie / Medical College of Wisconsin
!
! This procedure example was taken from page 4-352 of the VAXTPU reference
! manual.   Seems that TPU has a REAL easy way to do this, using the TRANSLATE
! built-in. 
!
procedure eve_rot13

  translate (current_buffer,
  "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm",
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
  message ("ROT13 complete.")
endprocedure;


cute, eh?

(and about 10 times faster than my code.  *sigh*...)
-------------------------------------------------------------------------------
Kent C. Brodie - Systems Manager		brodie@mcw.edu
Medical College of Wisconsin			+1 414 778 4500

"Gee, I hope these are the right coordinates..."  -Chief O'Brian; STTNG