home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #3 / amigamamagazinepolishissue1998.iso / bazy / amigabase-stamps / arexx / printpages.rexx < prev   
OS/2 REXX Batch file  |  1995-03-11  |  5KB  |  185 lines

  1. /* AREXX-macro used to print text to a printer, with pagenumbers on each page
  2.  
  3. © 1995 Mads Lie Jensen.
  4. You may change this one as much as you like to!!!
  5.  
  6. Written with AmigaBase in mind. Ofcourse, it could also have been writen in
  7. AmigaBase, but then would other programs not be able to use it.
  8.  
  9. This is a prototype of the macro. There are still a few things needed to be
  10. done. But it can be used, if you just remember the following:
  11. There is no checking of linelengths, so if a line is longer than 'chars',
  12. it will print out on two lines, but the program will only think that it printed
  13. one line. This would then cause the page-numbers to be printed on the wrong
  14. pages.
  15.  
  16. Things to implement:
  17.  
  18.    Possible to select if the 'text' should be printed at the top or bottom
  19.    of a page.
  20.  
  21.    Selection of print pitch, like ELITE, CONDENSED and so on.
  22.  
  23.    Select a value to indent all the text by, so you could get a nice left margin
  24.  
  25.  
  26.    argument-table
  27.    --------------
  28.    (S)   File to print
  29.    (I)   Lines pr. page
  30.    (I)   Characters pr. line.
  31.    (S)   Tekst to print at the bottom line.
  32.          %p means insert the current pagenumber.
  33.          %m means insert the maximum number of pages.
  34.    (I)   Justification of the text.
  35.          -1 Left justified
  36.           0 Centered
  37.           1 Right justified
  38.    (S)   If you want a FormFeed sent after each page, this is YES. All other
  39.          values means no.
  40.  
  41.    The following line is used to run the program during development:
  42.  
  43.    rx REXX:MLJ-Products/Printpages.rexx REXX:MLJ-Products/Printpages.rexx 65 80 "Side %p/%m (%p)" 1 YES
  44. */
  45.  
  46. SIGNAL ON ERROR
  47. OPTIONS RESULTS
  48. /*TRACE R*/
  49.  
  50.  
  51. /* Reads a standard CLI-line with each argument separeted from
  52.    each other by spaces and then exchange all blanks between the arguments
  53.    with kommas (,). Blanks in strings included in "" is not exchanged.
  54.    Lastly, the arguments are trimmed for "-signs, and the number of arguments
  55.    and the arguments themselves, are returned
  56. */
  57.  
  58. PARSE ARG arguments
  59.  
  60. i = 0
  61. bracket = 0
  62. argnum = 1
  63. DO i = 1 FOR LENGTH(arguments) BY 1
  64.  
  65.    c = SUBSTR(arguments,i,1)
  66.    IF c = '"' THEN bracket = ~bracket
  67.  
  68.    IF (c = ' ' & ~bracket) THEN DO
  69.       arguments = DELSTR(arguments,i,1)
  70.       arguments = INSERT(',',arguments,i-1,1)
  71.       argnum = argnum +1
  72.       END
  73.    END
  74. IF argnum < 6 | argnum >7 THEN
  75.    CALL END('Wrong number of arguments!')
  76.  
  77. arguments = COMPRESS(arguments,'"')
  78.  
  79. PARSE VAR arguments file ',' lines ',' chars ',' text ',' just ',' ff .
  80.  
  81. /* Check if the parameters are legal */
  82. IF ~DATATYPE(lines,'W') | ~DATATYPE(chars,'W') | (just <-1 & just >1) THEN
  83.    CALL END('Wrong argument-type(s)!')
  84.  
  85. /* Set the ff value to something usefull */
  86. IF UPPER(ff) = 'YES' THEN
  87.    ff='0C'x
  88. ELSE
  89.    ff = ''
  90.  
  91. IF ~EXISTS(file) THEN
  92.    CALL END('File not found!')
  93.  
  94. IF OPEN('Fromfile',file,'R') THEN DO
  95.  
  96.    /* Get the number of lines in the file. If the final line in your file
  97.       has an ending RETURN-code, which normal files normally have, then
  98.       the number of lines will be one bigger than the actual number of lines
  99.    */
  100.    numlines = 0
  101.    DO numlines = numlines +1 UNTIL EOF('Fromfile')
  102.       CALL READLN('Fromfile')
  103.    END
  104.  
  105.    IF OPEN('printer','PRT:','W') THEN DO
  106.  
  107.       pos = SEEK('Fromfile',0,'BEGIN')
  108.       pages = TRUNC(numlines / (lines-1)) + 1
  109.       curpage = 0
  110.  
  111.       /* Exchange the %m-sign. This will always be the same value through
  112.          the entire document, so theres no need to do this loop more than
  113.          once, like the %s-sign which will get a new value for every page.
  114.          We can also operate directly on the text-string, since this is
  115.          only done once.
  116.       */
  117.       position = POS('%m',text)
  118.       DO WHILE position > 0
  119.          text = DELSTR(text,position,2)
  120.          text = INSERT(pages,text,position-1)
  121.          position = POS('%m',text)
  122.       END
  123.  
  124.       DO pages
  125.          /* Write the text */
  126.          DO lines - 1
  127.             CALL WRITELN('printer',READLN('Fromfile'))
  128.          END
  129.  
  130.          curpage = curpage + 1
  131.  
  132.          /* Exchange the %-codes in the text-string with the apropriate values.
  133.             We have to operate on a copy of the text-string, or else we won't
  134.             be able to find any %p-signs next time we want to.
  135.          */
  136.  
  137.          newtext = text
  138.          position = POS('%p',newtext)
  139.          DO WHILE position > 0
  140.             newtext = DELSTR(newtext,position,2)
  141.             newtext = INSERT(curpage,newtext,position-1)
  142.             position = POS('%p',newtext)
  143.          END
  144.  
  145.          /* Now we calculate the text to write at the bottom line.
  146.             If it is to be left justified (-1), we don't need to do anything.
  147.             If right (1) justified we must put spaces in front of the text,
  148.             until it is exactly the length indicated by 'chars'.
  149.             If centered (0), we only pad out with half the numbers of spaces,
  150.             as we did with right justification.
  151.          */
  152.  
  153.          SELECT
  154.             WHEN just = 1 THEN
  155.                newtext = COPIES(' ',(chars - LENGTH(newtext))) || newtext
  156.             WHEN just = 0 THEN
  157.                newtext = CENTER(newtext,chars)
  158.             OTHERWISE
  159.                newtext = newtext
  160.          END
  161.  
  162.          CALL WRITELN('printer',newtext)
  163.          CALL WRITECH('printer',ff)
  164.  
  165.  
  166.       END
  167.    END
  168.  
  169.    ELSE
  170.       CALL END('Could not open printer')
  171. END
  172.  
  173. ELSE
  174.    CALL END('Could not open file')
  175.  
  176.  
  177. EXIT
  178.  
  179. END:
  180. PARSE ARG error
  181. SAY error
  182. EXIT
  183.  
  184.  
  185.