home *** CD-ROM | disk | FTP | other *** search
/ PDA Software Library / pdasoftwarelib.iso / PSION / MISC / READER / ZVR.ZIP / ZVR.OPL < prev    next >
Encoding:
Text File  |  1995-04-10  |  12.7 KB  |  699 lines

  1. REM
  2. REM ZVR V1.1.0
  3. REM
  4. REM This version of Ewan Paton's VR3a code is capable of
  5. REM processing ZVR compressed vertical reader files.
  6. REM
  7. REM Changes from the original code:
  8. REM
  9. REM        reformatted so that I can follow it better
  10. REM        added decompression
  11. REM        file path changed to \txt\books (was \vr)
  12. REM        extension changed to zvr (was txt)
  13. REM        RC file moved to \opd\zvr.rc
  14. REM            (was \vr\vrinit.rc)
  15. REM        different font default (matter of taste)
  16. REM        default Psion+J jump line is now top line,
  17. REM            not current line (i.e., 2 pages later...)
  18. REM        fixed Psion+F and Psion+G so that they actually
  19. REM            worked (seems to have been a problem in Ewan's
  20. REM            original code stemming from the introduction
  21. REM            of the behind-the-scenes painting of the next
  22. REM            page)
  23. REM
  24. REM Known problems:
  25. REM        Moving to the next page does not correctly
  26. REM            update vrTOP%, which means that exiting and
  27. REM            restarting a documents sometimes skips a
  28. REM            page or so.
  29. REM
  30.  
  31. REM
  32. REM APP definition: note that the original program
  33. REM looked for its files in \VR rather than \TXT.
  34. REM
  35. APP ZVR
  36.     TYPE $9003
  37.     PATH "\txt\books"
  38.     EXT "zvr"
  39.     ICON "\opl\vr.pic"
  40. ENDA
  41.  
  42. PROC main:
  43.     REM
  44.     REM Global variables used by the compression system
  45.     REM
  46.     GLOBAL zActive%                            :REM <>0 if decompressing
  47.     GLOBAL zDict$(255,16)                :REM array of strings
  48.     GLOBAL zLine$(255)                    :REM line just read
  49.  
  50.     GLOBAL vrRC$(30)                        :REM name of .rc file
  51.  
  52.     GLOBAL vWID%(2)                            :REM window IDs
  53.                                                             :REM foreground/background
  54.  
  55.     GLOBAL vINFO%(3),vHDLR$(8),vMODE%,vN%,vMAP&(64),vBIT%,vTMP%
  56.     GLOBAL vrFND$(30),vrFILE$(128),vrSTWIN%,vrFID%,vrSTYLE%,vrHDL%
  57.  
  58.     GLOBAL vrLN%                                :REM next unread line #
  59.     GLOBAL vrTOP%                                :REM top visible line #
  60.  
  61.     LOCAL f$(128),ret%,off%(6)
  62.  
  63.     REM
  64.     REM Name of the file in which to hold bookmarks.
  65.     REM Ewan's original program put this file in
  66.     REM M:\VR, which is a bit unusual.  The name has
  67.     REM also changed.
  68.     REM
  69.     vrRC$ = "M:\OPD\ZVR.RC"
  70.  
  71.     REM
  72.     REM Load the vertical printing library from the
  73.     REM LIB subdirectory of wherever this program was
  74.     REM loaded from.
  75.     REM
  76.     f$=CMD$(1)
  77.     f$=PARSE$("",f$,off%())
  78.     f$=left$(f$,off%(4)-1)+"LIB\VPRINT.OPO"
  79.     TRAP LOADM f$
  80.     IF ERR <> 0
  81.         dINIT
  82.         dTEXT "",f$
  83.         dTEXT "",ERR$(ERR)
  84.         LOCK ON
  85.         DIALOG
  86.         LOCK OFF
  87.         STOP
  88.     ENDIF
  89.  
  90.     vHDLR$="handler"
  91.     vrFND$=""
  92.  
  93.     REM
  94.     REM Open the requested file
  95.     REM
  96.     system:(CMD$(3)+CMD$(2))
  97.  
  98.     gUPDATE OFF
  99.     ONERR catch
  100. catch::
  101.     ONERR OFF
  102.  
  103.     vPAGE:(0)
  104.  
  105.     DO
  106.         IF TESTEVENT
  107.             ONERR catch
  108.             IF handler%: <>0
  109.                 ToFront:
  110.             ENDIF
  111.             ONERR OFF
  112.         ENDIF
  113.     
  114.         REM
  115.         REM read in the next line of the file
  116.         REM
  117.         ret% = zRdLine:
  118.  
  119.         IF ret%>=0
  120.             vrLN%=vrLN%+1
  121.             vPRINT:(ADDR(zLine$))
  122.         ELSEIF ret%=-36
  123.             ONERR catch
  124.             if gRANK <>1
  125.                 vPAGE:(1)
  126.             endif
  127.             vPAGE:(2)
  128.         ELSE
  129.             dINIT
  130.             dTEXT "",vrFILE$
  131.             dTEXT "",ERR$(ERR)
  132.             LOCK ON
  133.             DIALOG
  134.             LOCK OFF
  135.             procx:(%x)
  136.         ENDIF
  137.     UNTIL ret%=-36
  138.     ret%=IOCLOSE(vrHDL%)
  139. ENDP
  140.  
  141. REM
  142. REM ToFront
  143. REM
  144. REM If the current drawable is not in the foreground,
  145. REM bring it to the foreground.
  146. REM
  147. PROC ToFront:
  148.     IF gRANK <> 1
  149.         vSWITCH:
  150.     ENDIF
  151. ENDP
  152.  
  153. PROC handler%:
  154.     LOCAL sw%,k%,a$(5),a%(6)
  155.  
  156.     GETEVENT a%()
  157.     sw%=0
  158.  
  159.     IF (a%(1) AND $400) <> 0
  160.         IF a%(1) = $404
  161.             system:(GETCMD$) 
  162.             RAISE -114
  163.         ENDIF
  164.  
  165.     REM
  166.     REM Help key pressed?
  167.     REM
  168.     ELSEIF a%(1)=291
  169.         help:
  170.  
  171.     REM
  172.     REM Control-Menu pressed?
  173.     REM
  174.     ELSEIF a%(1)=290 AND((a%(2) AND $0004)=4)
  175.         IF vrSTWIN% = 0
  176.             vrSTWIN%=1
  177.             STATUSWIN ON
  178.         ELSE
  179.             vrSTWIN%=0
  180.             STATUSWIN OFF
  181.         ENDIF
  182.         FONT -$3fff,0
  183.         vFONT:(vrFID%,vrSTYLE%)
  184.         jump:(vrTOP%)
  185.         vPAGE:(0)
  186.         RAISE -114
  187.  
  188.     REM
  189.     REM Menu pressed?
  190.     REM
  191.     ELSEIF a%(1)=290
  192.         mINIT
  193.         mCARD "File","Open",%o,"Review Bookmarks",%b,"About vr3a",%v
  194.         mCARD "Search","Count",%c,"Find text",%f,"Find again",%g,"Jump to Line",%j
  195.         mCARD "Special","Set preferences",%q,"Zoom in",%z,"Zoom out",%Z,"Exit",%x
  196.         k%=MENU
  197.         if k% AND (LOC("bocfgjqzxv",CHR$(k%))<>0)
  198.             a$="proc"+chr$(k%)
  199.             @(a$):(k%)
  200.         endif
  201.  
  202.     REM
  203.     REM PSION+key pressed?
  204.     REM
  205.     ELSEIF (a%(1) AND $200) <>0 
  206.         k%=a%(1) AND $FF
  207.         IF a%(2) AND 2
  208.             k%=k% AND $DF
  209.         ENDIF
  210.         IF k% AND (LOC("bocfgjqzxv",CHR$(k%))<>0)
  211.             a$="proc"+chr$(k%)
  212.             @(a$):(k%)
  213.         ENDIF
  214.  
  215.     REM
  216.     REM Space bar pressed?
  217.     REM
  218.     ELSEIF a%(1)=$20
  219.         sw%=1
  220.         vrTOP%=vrLN%
  221.     ENDIF
  222.  
  223.     return sw%
  224. ENDP
  225.  
  226. PROC procb:(k%)
  227.     LOCAL sw%
  228.     OPEN vrRC$,A,file$,top%,fid%,mode%,stwin%
  229.     ONERR abort
  230.     FIRST
  231.     DO
  232.         dINIT "Review Bookmarks"
  233.         dTEXT "",A.file$
  234.         dTEXT "","Line "+num$(A.top%,4)
  235.         dBUTTONS "Next",%n,"Prev",%p,"Delete",8
  236.         LOCK ON
  237.         sw%=DIALOG
  238.         LOCK OFF
  239.         if sw%=%n
  240.             NEXT
  241.             IF EOF
  242.                 LAST
  243.             endif
  244.         elseif sw%=%p
  245.             BACK
  246.         elseif sw%=8
  247.             ERASE
  248.         endif
  249.     UNTIL sw%=0
  250. abort::
  251.     CLOSE 
  252. ENDP
  253.  
  254. PROC procc:(k%)
  255.     dINIT
  256.     dTEXT "Lines",num$(count%:,5),1
  257.     LOCK ON
  258.     DIALOG
  259.     LOCK OFF
  260. ENDP
  261.  
  262. REM
  263. REM procf: find text
  264. REM
  265. PROC procf:(k%)
  266.     LOCAL sw%
  267.     dINIT "Find"
  268.     dEDIT vrFND$,"Find text"
  269.     dCHOICE sw%,"Direction","Forwards,From Start of File"
  270.     LOCK ON
  271.     IF DIALOG
  272.         LOCK OFF
  273.         IF sw%=2
  274.             jump:(1)
  275.         ENDIF
  276.         procg:(0)
  277.     ENDIF
  278.     LOCK OFF
  279. ENDP
  280.  
  281. REM
  282. REM procg: continue search
  283. REM
  284. PROC procg:(k%)
  285.     LOCAL ret%
  286.     GLOBAL zLine$(255)
  287.     BUSY "Searching",3
  288.     DO
  289.         REM
  290.         REM read in the next line of the file
  291.         REM
  292.         ret% = zRdLine:
  293.         IF ret%>=0
  294.             IF LOC(zLine$,vrFND$)<>0
  295.                 BUSY OFF
  296.                 vrTOP%=vrLN%
  297.                 vrLN%=vrLN%+1
  298.                 jump:(vrTOP%)
  299.                 ToFront:
  300.                 RAISE -114
  301.             ENDIF
  302.             vrLN%=vrLN%+1
  303.         ENDIF
  304.     UNTIL (ret%=-36)
  305.     BUSY OFF
  306.     GIPRINT "Not Found",3
  307.     jump:(vrTOP%) 
  308.     RAISE -114
  309. ENDP
  310.  
  311. REM
  312. REM procj
  313. REM
  314. REM Called on Psion+j: jump to line
  315. REM
  316. PROC procj:(k%)
  317.     LOCAL ln%,ln&
  318.     ln& = vrTOP%
  319.     dINIT "Jump to line"
  320.     dLONG ln&,"Line",1,32000
  321.     LOCK ON
  322.     IF DIALOG
  323.         LOCK OFF
  324.         ln%=ln&
  325.         jump:(ln%)
  326.         vrTOP%=vrLN%
  327.         ToFront:
  328.         RAISE -114
  329.     ENDIF
  330.     LOCK OFF
  331. ENDP
  332.  
  333. REM
  334. REM proco
  335. REM
  336. REM Called for PSION+O, and for the system's
  337. REM request to open a file.
  338. REM
  339. PROC proco:(k%)
  340.     LOCAL ret%,f$(128)
  341.     f$="\VR\*.txt"
  342.     if k%<>0
  343.         do
  344.             dInit "Select a file to read"
  345.             dFile f$,"Filename:",72
  346.             LOCK ON
  347.             if Dialog
  348.                 LOCK OFF 
  349.             else
  350.                 LOCK OFF
  351.                 return
  352.             endif
  353.         Until f$<>""
  354.         putinf:
  355.         vrFILE$=f$
  356.     endif
  357.  
  358.     REM
  359.     REM Close the open file, if there is one.
  360.     REM
  361.     IF vrHDL% <> 0
  362.         ret%=IOCLOSE(vrHDL%)
  363.     ENDIF
  364.  
  365.     REM
  366.     REM Open the selected file
  367.     REM
  368.     ret%=IOOpen(vrHDL%,vrFILE$,$0620)
  369.     zRdDict:(vrHDL%)
  370.  
  371.     vrLN%=1
  372.     getinf:
  373.     vFONT:(vrFID%,vrSTYLE%)
  374.     jump:(vrTOP%)
  375.     setname vrFILE$
  376.     if k% <> 0
  377.         RAISE -114
  378.     endif
  379. ENDP
  380.  
  381. PROC procq:(k%)
  382.     LOCAL md%,fid%,style%
  383.     md%=vMODE%
  384.     fid%=(vrFID%-4)
  385.     IF vrSTYLE% AND 16
  386.         style%=2
  387.     ELSE
  388.         style%=1
  389.     ENDIF
  390.     dINIT "Set preferences"
  391.     dCHOICE fid%,"Font:","Roman 8,Roman 11,Roman 13,Roman 16,Swiss 8,Swiss 11,Swiss 13,Swiss 16"
  392.     dCHOICE style%,"Character widths","Proportional,Monospaced"
  393.     dCHOICE md%,"Display mode:","Line truncate,Line wrap,Word wrap,Paragraph"
  394.     LOCK ON
  395.     IF DIALOG
  396.         LOCK OFF
  397.         IF fid%<>(vrFID%-4) or vrSTYLE%<>(16*(style%-1))
  398.             vrSTYLE%= 16*(style%-1)
  399.             vrFID%=fid%+4
  400.             vFONT:(vrFID%,vrSTYLE%)
  401.         ENDIF
  402.         vMODE%=md%
  403.         jump:(vrTOP%)
  404.         RAISE -114
  405.     ENDIF
  406.     LOCK OFF
  407. ENDP
  408.  
  409. PROC procv:(k%)
  410.     dINIT "About ZVR V1.1.0"
  411.     dTEXT "","by Ian Young <ian@rats.demon.co.uk>"
  412.     dTEXT "","   heavily based on..."
  413.     dTEXT "","Vertical Reader for Psion Series 3a"
  414.     dTEXT "","Version 1.1 - Feb '93"
  415.     dTEXT "","by Ewan Paton - paton@vmark.co.uk"
  416.     LOCK ON
  417.     DIALOG
  418.     LOCK OFF
  419. ENDP
  420.  
  421. REM
  422. REM procx
  423. REM
  424. REM Called for PSION+x: Exit program
  425. REM
  426. PROC procx:(k%)
  427.     putinf:
  428.     STOP
  429. ENDP
  430.  
  431. REM
  432. REM procz: zoom text
  433. REM
  434. PROC procz:(k%)
  435.     IF k%=%z
  436.         vrFID%=(((vrFID%-1) AND (NOT 3)) OR ((vrFID%) AND 3))+1
  437.     ELSE
  438.         vrFID%=(((vrFID%-1) AND (NOT 3)) OR ((vrFID%-2) AND 3))+1
  439.     ENDIF
  440.     vFONT:(vrFID%,vrSTYLE%)
  441.     vPAGE:(0)
  442.     jump:(vrTOP%)
  443.     RAISE -114
  444. ENDP
  445.  
  446. PROC count%:
  447.     LOCAL ret%,hdl%,line%,buf$(255)
  448.     line%=1
  449.     ret%=IOOpen(hdl%,vrFILE$,$0620)
  450.     BUSY "Counting",3
  451.     zRdDict:(hdl%)
  452.     WHILE ret%>=0
  453.         ret%=IOREAD(hdl%,ADDR(buf$)+1,255)
  454.         line%=line%+1
  455.     ENDWH
  456.     ret%=IOCLOSE(hdl%)
  457.     BUSY OFF
  458.     return line%
  459. ENDP
  460.  
  461. PROC getinf:
  462.     TRAP OPEN vrRC$,A,file$,top%,fid%,mode%,style%,stwin%
  463.     if ERR = 0
  464.         if FIND(vrFILE$)<>0
  465.             vrTOP%=A.top%
  466.             vrFID%=A.fid%
  467.             vMODE%=A.mode%
  468.             vrSTYLE%=A.style%
  469.             IF vrSTWIN%<>A.stwin%
  470.                 IF vrSTWIN%=0
  471.                     STATUSWIN ON
  472.                     FONT -$3fff,0
  473.                 ELSE
  474.                     STATUSWIN OFF
  475.                     FONT -$3fff,0
  476.                 ENDIF
  477.                 vrSTWIN%=A.stwin%
  478.             ENDIF
  479.         else
  480.             vrTOP%=1
  481.             vrFID%=10
  482.             vMODE%=4
  483.             vrSTYLE%=0
  484.         endif
  485.     ELSE
  486.         CREATE vrRC$,A,file$,top%,fid%,mode%,style%,stwin%
  487.         vrTOP%=1
  488.         vrFID%=10
  489.         vMODE%=4
  490.         vrSTYLE%=0
  491.     ENDIF
  492.     CLOSE
  493. ENDP
  494.  
  495. PROC help:
  496.     dINIT "Help:Vertical Reader"
  497.     dTEXT "","Vr3a displays ascii text files in landscape orientation in a choice"
  498.     dTEXT "","of 8 builtin proportional fonts. Files in any /VR directories with" 
  499.     dTEXT "","a .TXT suffix will appear beneath the application icon."
  500.     dTEXT "","The last position and display preferences for any file displayed"
  501.     dTEXT "","with Vr3a are saved between invocations in the vrinit.rc file."
  502.     dTEXT "","Menu commands should be self explanatory."
  503.     dTEXT "","The Enter key scrolls the display one line and the Space key"
  504.     dTEXT "","displays the next page."
  505.     LOCK ON
  506.     DIALOG
  507.     LOCK OFF
  508. ENDP
  509.  
  510. PROC jump:(ln%)
  511.     LOCAL off&,ret%,buf$(255)
  512.  
  513.     REM
  514.     REM If we have gone too far, we must seek back
  515.     REM to the start of the file and then skip
  516.     REM forward again.
  517.     REM
  518.     IF ln% < vrLN%
  519.         off&=0
  520.         ret%=IOSEEK(vrHDL%,6,off&)
  521.         zRdDict:(vrHDL%)
  522.         vrLN%=1
  523.     ENDIF
  524.     BUSY "Seeking to line "+num$(ln%,4),3
  525.     WHILE vrLN% < ln%
  526.         ret%=IOREAD(vrHDL%,ADDR(buf$)+1,255)
  527.         vrLN%=vrLN%+1
  528.     ENDWH
  529.     BUSY OFF
  530. ENDP
  531.  
  532. PROC putinf:
  533.     OPEN vrRC$,A,file$,top%,fid%,mode%,style%,stwin%
  534.     if FIND(vrFILE$)<>0
  535.         A.top%=vrTOP%
  536.         A.fid%=vrFID%
  537.         A.mode%=vMODE%
  538.         A.style%=vrSTYLE%
  539.         A.stwin%=vrSTWIN%
  540.         UPDATE
  541.     else
  542.         A.file$=vrFILE$
  543.         A.top%=vrTOP%
  544.         A.fid%=vrFID%
  545.         A.mode%=vMODE%
  546.         A.style%=vrSTYLE%
  547.         A.stwin%=vrSTWIN%
  548.         APPEND
  549.     endif
  550.     CLOSE
  551. ENDP
  552.  
  553. PROC system:(msg$)
  554.     IF LEFT$(msg$,1)="O"
  555.         vrFILE$=mid$(msg$,2,128)
  556.         proco:(0)
  557.     ELSEIF LEFT$(msg$,1)="C"
  558.         putinf:
  559.         vrFILE$=mid$(msg$,2,128)
  560.         proco:(0)
  561.     ELSEIF LEFT$(msg$,1)="X"
  562.         procx:
  563.     ENDIF
  564. ENDP
  565.  
  566. REM ##################################################
  567. REM ###                                            ###
  568. REM ###         D E C O M P R E S S I O N          ###
  569. REM ###                                            ###
  570. REM ##################################################
  571.  
  572. REM
  573. REM zRdDict
  574. REM
  575. REM The argument handle% is the file handle for
  576. REM a text file which may or may not contain
  577. REM compressed information.  The file is currently
  578. REM positioned at the beginning.  This function
  579. REM determines whether the file contains compressed
  580. REM data by looking at the first line, which will be
  581. REM a magic string for a compressed file.  The next
  582. REM 255 lines after that are the compression
  583. REM dictionary.
  584. REM
  585. PROC zRdDict:(handle%)
  586.     LOCAL ret%
  587.     LOCAL i%
  588.     LOCAL off&
  589.     LOCAL line$(255)
  590.  
  591.     REM
  592.     REM Assume uncompressed file by default
  593.     REM
  594.     zActive% = 0
  595.  
  596.     REM
  597.     REM Read first line.
  598.     REM
  599.     ret% = IORead(handle%, ADDR(line$)+1,255)
  600.     IF ret% < 0 :RETURN    :ENDIF
  601.     PokeB ADDR(line$),ret%
  602.     REM
  603.     REM If this is not a compressed file, seek back
  604.     REM to the beginning again.
  605.     REM
  606.     IF line$ <> "!!Compressed!!"
  607.         off& = 0
  608.         ret%=IOSEEK(vrHDL%,6,off&)
  609.         RETURN
  610.     ENDIF
  611.  
  612.     REM
  613.     REM Has magic tag; now read dictionary
  614.     REM
  615.     i% = 1
  616.     DO
  617.         REM
  618.         REM Read a dictionary entry
  619.         REM
  620.         ret% = IORead(handle%, ADDR(line$)+1, 255)
  621.  
  622.         REM
  623.         REM If there is a problem, seek back to the
  624.         REM start and assume that the file is not
  625.         REM compressed after all.
  626.         REM
  627.         IF ret% < 0
  628.             off& = 0
  629.             ret%=IOSEEK(vrHDL%,6,off&)
  630.         RETURN
  631.  
  632.         REM
  633.         REM An empty line means the symbol is defined
  634.         REM as itself.
  635.         REM
  636.         ELSEIF ret% = 0
  637.             line$ = CHR$(i%)
  638.  
  639.         REM
  640.         REM Otherwise, the line is the symbol's
  641.         REM definition.
  642.         REM
  643.         ELSE
  644.             PokeB ADDR(line$), ret%
  645.         ENDIF
  646.         zDict$(i%) = line$
  647.         i% = i%+1
  648.     UNTIL i% = 256
  649.  
  650.     REM
  651.     REM All has gone well: we can set decompression
  652.     REM active now.
  653.     REM
  654.     zActive% = 1
  655. ENDP
  656.  
  657. REM
  658. REM zRdLine$
  659. REM
  660. REM Reads the next line from the currently open
  661. REM file, and decompresses it (if required) into
  662. REM the global variable zLine$.
  663. REM
  664. PROC zRdLine:
  665.     LOCAL ret%
  666.     LOCAL line$(255)
  667.     LOCAL q%, i%
  668.  
  669.     IF zActive%
  670.         q% = ADDR(line$)
  671.     ELSE
  672.         q% = ADDR(zLine$)
  673.     ENDIF
  674.  
  675.     ret% = IORead(vrHDL%,q%+1,255)
  676.     IF ret% >= 0
  677.         PokeB q%, ret%
  678.     ELSE
  679.         RETURN ret%
  680.     ENDIF
  681.  
  682.     REM
  683.     REM We have read a line in.  If compression is
  684.     REM active, we have it in line$ and must decompress
  685.     REM into zLine$
  686.     REM
  687.     IF zActive%
  688.         zLine$ = ""
  689.         i% = ret%
  690.         WHILE i%
  691.             q% = q% + 1
  692.             zLine$ = zLine$ + zDict$(PeekB(q%))
  693.             i% = i% - 1
  694.         ENDWH
  695.     ENDIF
  696.     RETURN ret%
  697. ENDP
  698.  
  699.