'===========================================================================
' Subject: PB TEXT COMPILER                   Date: 06-23-98 (09:06)       
'  Author: Dieter Folger                      Code: PB                     
'  Origin: folger@bamberg.baynet.de         Packet: TEXT.ABC
'===========================================================================
'---------------------------------------------------------------------------
' TEXE.BAS for Power Basic 3.2
' Text compiler for ASCII texts
' Freeware (c) 1997 by Dieter Folger
' NOTE: Do not compile in IDE - use PBC.EXE
'---------------------------------------------------------------------------
' If you want to convert an ASCII text file, e.g. a readme file, to an
' executable program, i.e. a self displaying EXE file, you need a text
' compiler. Then the user can read this file without the help of an
' ASCII viewer or an editor. TEXE is such a compiler. It converts any
' text file up to 10000 lines.

' How TEXE works: When TEXE is started the file length is compared with
' the predefined value of FileEnd&. If they match, then there is no
' text file included yet and the user is prompted for two file names:
' the name of the text file and the compiled file name.
' This EXE file is written via SHELL using the DOS COPY command.

' When the EXE file is started, it reads its own file name (ProgName$
' is a very useful function!), opens this file and reads the text into
' an array. Variable FileEnd& tells the program the position from where
' to start reading (= length of EXE without text). You have to adjust
' this value if you make any changes to the code. Otherwise the program
' would't work correctly. Then the text is shown and can be scrolled
' down, left, up and right. The Esc key gets the user back to the command
' line.
'---------------------------------------------------------------------------
DEFINT A-Z
IF BIT (PbvHost,5) THEN       ' Dont't start in IDE
   PRINT "Don't start in IDE"  ' ProgName$ would be "PB.EXE"
   END
END IF
Program$ = ProgName$         ' program reads its own name
OPEN Program$ FOR INPUT AS #1 ' open this EXE file
' You have to change FileEnd& if you make any changes in source:
FileEnd& = 43859              ' length of EXE file without text
'---------------------------------------------------------------------------
' This part writes the self displaying EXE file
'---------------------------------------------------------------------------
IF LOF(1) = FileEnd& THEN     ' EXE is without text
   INPUT "Textfile to compile: ", TextFile$
   IF DIR$(TextFile$) = "" THEN
      PRINT TextFile$; " not found" : END
   END IF
   INPUT "Name of EXE file: ", ExeFile$
   IF ExeFile$ = "" THEN END
   ' Force an *.EXE filename:
   IF INSTR(ExeFile$,".") = 0 THEN ExeFile$ = ExeFile$ + ".EXE"
   IF INSTR(ExeFile$,".EXE") = 0 THEN ExeFile$ = EXTRACT$(ExeFile$,".") + ".EXE"
   'This does it:
   SHELL "COPY /B " + Program$ + " + " + TextFile$ + " " + ExeFile$ + " > nul"
   PRINT UCASE$(ExeFile$); " written"
   END
END IF
'---------------------------------------------------------------------------
' This part displays the text
'---------------------------------------------------------------------------
SEEK #1,FileEnd&              ' go to start of text
DO                            ' get number of text lines
  IF EOF(1) THEN EXIT LOOP
  LINE INPUT #1, L$
  INCR Lin
LOOP UNTIL EOF(1)
IF Lin > 10000 THEN Lin = 10000 ' max lines
DIM L$(Lin)                   ' dim array for text lines
SEEK #1, FileEnd&             ' go to start of text again
FOR i=1 TO Lin
    LINE INPUT #1,L$(i)       ' read text lines in array
NEXT
'make screen and show text:
COLOR 14,1
LOCATE 1,1 : PRINT SPACE$(80);
LOCATE 1,2 : PRINT Program$;
P = POS(0)
LOCATE 25,1 : PRINT SPACE$(80);
LOCATE 25,2
PRINT "Keys ";CHR$(25);" "CHR$(24);" ";CHR$(26);" ";CHR$(27);" Esc";
Start=1 : Offset=1
DO
    LOCATE 1,P : COLOR 14,1
    Print " Line"; Start; "of"; Lin-1; "       ";
    COLOR 0,3
    FOR i = 2 TO 24
        LOCATE i,1
        PRINT MID$(L$(Start+i-2) + SPACE$(80),Offset,80);
    NEXT
    DO : K$ = INKEY$ : LOOP UNTIL LEN(K$)
    SELECT CASE RIGHT$(K$,1)
           CASE "P" 'Cursor down
                INCR Start
           CASE "M" 'Cursor right
                INCR Offset
           CASE "K" 'Cursor left
                 DECR Offset
           CASE "H" 'Cursor up
                DECR Start
           CASE "G" :'Home
                Start = 1
           CASE "O": 'End
                Start = Lin - 22
           CASE "Q": 'PgDn
                INCR Start,23
           CASE "I": 'PgUp
                DECR Start,23
           CASE CHR$(27) : END
     END SELECT
     IF Start > Lin - 22 THEN Start = Lin - 22
     If Start < 1 THEN Start = 1
     If Offset < 1 THEN Offset = 1
LOOP
END
'---------------------------------------------------------------------------
FUNCTION ProgName$ ' get path and name of running program
'---------------------------------------------------------------------------
 ! mov ax,&h6200
 ! int &h21
 ! mov es,bx
 ! mov ax, word ptr es:[&h2c]
 ! mov pbvDefSeg, ax
 FOR i = 0 TO 1024
     IF PEEK$(i,4) = CHR$(0,0,1,0) THEN EXIT FOR
 NEXT
 WHILE PEEK(i + 4) <> 0
   Tmp$ = Tmp$ + CHR$(PEEK(i+4))
   INCR i
 WEND
 DEF SEG
 ProgName$ = Tmp$
END FUNCTION
'==== eof ==================================================================
