! memo.bas - inserts text from MEMOPAD file into an ANDI formatted report
! June 2, 1983 ske
! last update 7/20/83 - Works, needs more testing, screen bells & whistles

! get the name of the report file (.DAT default extension)
! get the six letter name of the ANDI file that the data came from
! open files
! Loop through the .DAT file
! search for the word MEMOPAD followed by the memopad record number
! if no MEMOPAD, just send out the line
! if it sez MEMOPAD
!       fetch the text from the .PAD file
!       follow the links to the rest of the MEMOPAD text
!       format it nicely
!       send the line to the .LST file
! tidy up

MAP1 DAT'FILE,S,10
MAP1 MEMO'FILE,S,10
MAP1 ANDI'FILE,S,10
MAP1 LST'FILE,S,10

MAP1 REPORT'LINE,S,150                  ! Line of text from DAT'FILE
MAP1 UPPER'CASE'LINE,S,150              ! REPORT'LINE Converted to upper
                                       ! case for search purposes

MAP1 MEMO'RECORD                        ! DRAVAC's layout for MEMO'FILE
       MAP2 NEXT'RECORD,B,2            ! If NEXT'RECORD = 0 we're done
       MAP2 TEXT,S,62                  ! Not to mention Michael's packing
                                       ! scheme (see MOVE'TEXT'TO'STASH)

MAP1 STASH,S,510                        ! holds TEXT in memory & then get's
                                       ! chopped up into <79 chr lengths
MAP1 OUTPUT'LINE,S,82                   ! Sent directly to the .LST file

MAP1 MEMO'REC,F
MAP1 DUMMY,S,1                          ! for the PAUSE subroutine
MAP1 X,F                                ! Results of instr & lookups, etc.
                                       ! X is a general holding tank
                                       ! and is used in subroutines
                                       ! as a local variable


filebase 1                              ! would you believe this isn't
                                       ! documented by the ANDI manual?
!
!               SEE ALSO THE FILE MEMO.HLP FOR DOCUMENTATION
!               ABOUT OPERATING THIS PROGRAM, DEFAULTS, ETC.
!

INITIALIZE:
       ? tab(-1,0); tab(3,30); "Generic Memo Pad Text Fetcher"
       call ENTER'DAT'FILE
       call LOOKUP'DAT'FILE
       call ENTER'ANDI'FILE
       call LOOKUP'ANDI'FILE
       X = 0
       X = instr(1,DAT'FILE,".")
       LST'FILE = DAT'FILE[1,X-1] + ".LST"
       X = 0
       X = instr(1,ANDI'FILE,".")
       MEMO'FILE = ANDI'FILE[1,X-1] + ".PAD"
       open #1, DAT'FILE, input
       open #2, MEMO'FILE, random, 64, MEMO'REC
       open #3, LST'FILE, output
       call CLEAR'WINDOW
       ? "Processing";

MAIN'LOOP:
       input line #1, REPORT'LINE
       ? ".";
       if eof(1) = 1 &
           then &
               goto TIDY'UP
       UPPER'CASE'LINE = ucs(REPORT'LINE)
       X = 0
       X = instr(1,UPPER'CASE'LINE,"MEMOPAD")
       if X = 0 &
           then &
               ? #3, REPORT'LINE
       if X > 0 &
           then &
               MEMO'REC = REPORT'LINE[8,len(REPORT'LINE)] :&
               if MEMO'REC # 0 &
                   then &
                       STASH = "" :&
                       OUTPUT'LINE = "" :&
                       call LITTLE'LOOP
       goto MAIN'LOOP

TIDY'UP:
       ? tab(-1,0); tab(10,35); "Done";
       ? tab(17,10); "The final results are in "; LST'FILE
       close #1
       close #2
       close #3
       ? tab(23,1);
       end

! ==========================================================================

ENTER'DAT'FILE:
       call CLEAR'WINDOW
       input "Please enter the AMOS name for the report you created --> ", DAT'FILE
       DAT'FILE = ucs(DAT'FILE)
       X = 0
       X = instr(1,DAT'FILE,".")
       if X = 0 &
           then &
               DAT'FILE = DAT'FILE + ".DAT"
       return

LOOKUP'DAT'FILE:
       X = 0
       lookup  DAT'FILE, X
       if X <= 0 &
           then &
               call CLEAR'WINDOW :&
               ? "I'm terribly sorry, but I can't find that file. Please check" :&
               ? "The spelling, etc. and try again " :&
               call PAUSE :&
               goto ENTER'DAT'FILE
       return

ENTER'ANDI'FILE:
       call CLEAR'WINDOW
       input "Thanks, now what is the AMOS name of the ANDI file you used --> ", ANDI'FILE
       ANDI'FILE = ucs(ANDI'FILE)
       X = 0
       X = instr(1,ANDI'FILE,".")
       if X = 0 &
           then &
               ANDI'FILE = ANDI'FILE + ".DAT"
       return

LOOKUP'ANDI'FILE:
       X = 0
       lookup  ANDI'FILE, X
       if X => 0 &
           then &
               call CLEAR'WINDOW :&
               ? "I'm terribly sorry, but I can't find that file. Please check" :&
               ? "the spelling, etc. and try again " :&
               call PAUSE :&
               goto ENTER'ANDI'FILE
       return

LITTLE'LOOP:
       read #2, MEMO'RECORD
       call MOVE'TEXT'TO'STASH
       call CHECK'LINE'LENGTH
       if NEXT'RECORD # 0 &
           then &
               MEMO'REC = NEXT'RECORD :&
               goto LITTLE'LOOP
       if len(STASH) > 0 &
           then &
               call FLUSH'THE'STASH
       return

CLEAR'WINDOW:
       ? TAB(10,1); TAB(-1,10);
       RETURN

MOVE'TEXT'TO'STASH:
       ! Also included is a routine to expand the text and add spaces
       ! if the ASCII value of a character is > 128
       for N = 1 to len(TEXT)
           BLANKS = 0
           if asc(TEXT[N,N]) > 128 &
               then &
                   BLANKS = asc(TEXT[N,N]) - 128 :&
                   STASH = STASH + space(BLANKS) &
               else &
                   STASH = STASH + TEXT[N,N]
       next N
       return

++include PAUSE

CHECK'LINE'LENGTH:
       ! This program assumes a 79 character line length. Feel free
       ! to change this based on your terminal (maybe 80 is better)
       ! or the length of your MEMOPAD entry window
       if len(STASH) > 79 &
           then &
               SPOT = 79 :&
               call PRINT'A'LINE :&
               goto CHECK'LINE'LENGTH
       return

PRINT'A'LINE:
       ! we're searching backwards through the line from the SPOT
       ! for a space. Everything to the left of the space goes to
       ! the LST'FILE. STASH then becomes everything to the right
       ! of the space.
       ! I couldn't see any tabs (chr(9)) in the file, but what the hey?
       ! chr(32) is a space in case you forgot your ASCII table
       if asc(STASH[SPOT,SPOT]) = 32 &
       or asc(STASH[SPOT,SPOT]) = 9 &
          then &
               OUTPUT'LINE = STASH[1,SPOT-1] :&
               STASH = STASH[SPOT+1,len(STASH)] :&
               ? #3, OUTPUT'LINE :&
               OUTPUT'LINE = "" &
           else &
               SPOT = SPOT - 1 :&
               goto PRINT'A'LINE
       return

FLUSH'THE'STASH:
       ? #3, STASH
       OUTPUT'LINE = ""
       STASH = ""
       return