Announcement

Collapse
No announcement yet.

Scrub basic PCL control command codes from a file

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Scrub basic PCL control command codes from a file

    the code says it all
    Code:
    'pcl2txt
    'compiled with pbcc 6.04
    'but should compile with most PB compilers.
    
    'code to remove  HP LaserJet Printer Commands from a file, usually a printable file.
    'PJL files are not processed and skipped over
    'All properly formated PJL files have the 13 characters of "esc" + "%-12345X@PJL"
    ' at the begginning and end of a file are PJL files
    
    
    #COMPILE EXE
    %UNICODE = 0
    #DIM ALL
    #BREAK ON
    
    
    FUNCTION scrubpcl(BYREF sfilecontents AS STRING) AS LONG
        ' there same single string is manipulated to provide text that is non pcl code at the beginning
        ' and then new string is made by chopping the string of its excess characters at the
        ' end of the function
    
        LOCAL icounter,ioutputcounter AS DWORD
        LOCAL ipointer,ioutputpointer  AS BYTE PTR
        LOCAL ipcl AS LONG 'flag used for characters as part of pcl code
        LOCAL ipclcodefound AS LONG 'flag used for found pcl code
    
        ipointer=STRPTR(sfilecontents)
        ioutputpointer=ipointer
        ipcl=0&
        FOR icounter=1 TO LEN(sfilecontents)
            IF @ipointer=27? THEN
                ipcl=1&
                ipclcodefound=1&
                INCR ipointer
                ITERATE
            END IF
            IF ipcl THEN
                IF @ipointer>65? AND @ipointer<91? THEN  ipcl=0&
                INCR ipointer
                ITERATE
            END IF
            @ioutputpointer=@ipointer
            INCR ipointer
            INCR ioutputpointer
            INCR ioutputcounter
        NEXT icounter
        IF ioutputcounter=0& THEN
           sfilecontents=""
           ELSE
           sfilecontents=LEFT$(sfilecontents,ioutputcounter)
        END IF
    
    END FUNCTION
    
    FUNCTION helpmsg() AS LONG
           STDOUT "This program converts files with HP PCL printer commands to text files."
           STDOUT "It will not process PJL formatted files."
           STDOUT "This program will not convert graphic style printout."
           STDOUT "The format of the input file should be simple in nature with line feeds"
           STDOUT "  and control codes for changing the print style and not control codes"
           STDOUT "  that control where characters are placed on a page."
           STDOUT "Examples would be where text is being compressed, change in line spacing,"
           STDOUT "  bold print, and other similiar control codes that control the print of"
           STDOUT "  characters in a top to bottom fashion."
           STDOUT "There are many other programs that will handle more complex formatting."
           STDOUT "This program does not attempt to duplicate those other programs works."
           STDOUT "program usage:   program.exe inputfile outputfile"
           STDOUT "The output file will be over written."
           STDOUT "If the output file name is not given then .txt will be added to the"
           STDOUT "  filename given,  so a single filename placed on the command line"
           STDOUT "  like document.pcl will produce document.pcl.txt"
           STDOUT "Basically, this program wipe any group of characters that start with"
           STDOUT "  an escape character and end with a upper case A-Z character."
           STDOUT "An exit code of 0 means success else there was a failure."
           STDOUT "Exit codes being only 0 will produce an output file or this message."
    
    
    END FUNCTION
    
    
    
    FUNCTION PBMAIN () AS LONG
        LOCAL sinputfilename AS STRING
        LOCAL soutputfilename AS STRING
        LOCAL sfilecontents AS STRING
    
        'and exit code of 1 means program did not conversions
    
        sinputfilename=TRIM$(COMMAND$(1))
        IF LEN(sinputfilename)=0& THEN
               helpmsg
               EXIT FUNCTION
        END IF
    
        soutputfilename=TRIM$(COMMAND$(2))
        'if file input does not exist then quit the program
        IF NOT ISFILE(sinputfilename) THEN  FUNCTION=1:EXIT FUNCTION
        IF LEN(soutputfilename)=0& THEN soutputfilename=sinputfilename+".txt"
        'read the input file into string variable sfilecontents
        OPEN sinputfilename FOR BINARY ACCESS READ WRITE LOCK SHARED AS 1 LEN=32767
        GET$ 1, LOF(1), sfilecontents
        CLOSE 1
        'if file contents are zero in length the quit the program
        IF LEN(sfilecontents)<1 THEN FUNCTION=1:EXIT FUNCTION
    
        'refuse to process PJL files.
        'All properly formated PJL files have the 9 characters of "esc" + "%-12345X"
        ' at the begginning and end of a file are PJL files
        IF LEN(sfilecontents) > 12& THEN
            IF LEFT$(sfilecontents,13&)=CHR$(27)+"%-12345X@PJL" THEN
                STDOUT "file "+sinputfilename+" has a PJL format and not processed"
                FUNCTION=1
                EXIT FUNCTION
            END IF
        END IF
    
        scrubpcl(sfilecontents)
        'write the new file out
        IF LEN(sfilecontents) THEN
            IF ISFILE(soutputfilename) THEN KILL soutputfilename
            OPEN soutputfilename FOR BINARY ACCESS READ WRITE LOCK SHARED AS 1 LEN=32767
            PUT$ 1, sfilecontents
            CLOSE 1
        END IF
    
    END FUNCTION
    Attached Files
    p purvis
Working...
X