How to read from a file with both text and binary

B

Brian

I am processing print job files. They all contain a Start banner with plain
text then following the banner comes the data to be printed. This data can
either be plain text or PCL (binary). I need to be able to determine what
format the data is in because the processing is different. I tried using -T
and -B on the file, but Perl returns -T as TRUE, I guess becuase of the
plain text header. The PCL always starts with the string ^[E^[... so I
tried searching for that, but to no avail with the following code:

while ( <SRCFILE> ) {
if ( $_ =~ /\^\[E\^\[/ ) { # Does this line contain ^[E^[
$pclFlag = 1;
print " PCL File detected\n";
}
}

Here is an example of part of a print job file containing PCL data:

* PRINT TIME: 13:30:38
*
* PRINT DATE: 23 JUN 2006
*
* PRINT NAME: TA104002
*
* SYSTEM: MVSA
*
*
*
**START*****START*****START*****START*****START*****START*****START*****START**
^[E^[&u300D^[*v1O^[*v0N^[*c00001D^[)s64W@^B)$7^A^^¦^U^BÿªÃðÄðÇãñðãñåñððó÷^[*c0E^[(s17W^D^N^A^A^Ax^[*c1E^[(s17W^D^N^A^A^Ax^[*c2E^[(s17W^D
^N^A^A^Ax^[*c3E^[(s17W^D^N^A^A^Ax^[*c4E^[(s17W^D^N^A^A^Ax^[*c5E^[(s17W^D^N^A^A^Ax^[*c6E^[(s17W^D^N^A^A^Ax^[*c7E^[(s17W^D^N^A^A^Ax^[*c8E^[
(s17W^D^N^A^A^Ax^[*c9E^[(s17W^D^N^A^A^Ax^[*c10E^[
 
T

tuser

Brian said:
I am processing print job files. They all contain a Start banner with plain
text then following the banner comes the data to be printed. This data can
either be plain text or PCL (binary). I need to be able to determine what
format the data is in because the processing is different. I tried using-T
and -B on the file, but Perl returns -T as TRUE, I guess becuase of the
plain text header. The PCL always starts with the string ^[E^[... so I
tried searching for that, but to no avail with the following code:

while ( <SRCFILE> ) {
if ( $_ =~ /\^\[E\^\[/ ) { # Does this line contain ^[E^[
$pclFlag = 1;
print " PCL File detected\n";
}
}

Here is an example of part of a print job file containing PCL data:

* PRINT TIME: 13:30:38
*
* PRINT DATE: 23 JUN 2006
*
* PRINT NAME: TA104002
*
* SYSTEM: MVSA
*
*
*
**START*****START*****START*****START*****START*****START*****START*****START**
^[E^[&u300D^[*v1O^[*v0N^[*c00001D^[)s64W@^B)$7^A^^¦^U^BÿªÃðÄðÇãñðãñåñððó÷^[*c0E^[(s17W^D^N^A^A^Ax^[*c1E^[(s17W^D^N^A^A^Ax^[*c2E^[(s17W^D
^N^A^A^Ax^[*c3E^[(s17W^D^N^A^A^Ax^[*c4E^[(s17W^D^N^A^A^Ax^[*c5E^[(s17W^D^N^A^A^Ax^[*c6E^[(s17W^D^N^A^A^Ax^[*c7E^[(s17W^D^N^A^A^Ax^[*c8E^[
(s17W^D^N^A^A^Ax^[*c9E^[(s17W^D^N^A^A^Ax^[*c10E^[

I have taken your print job file literally, i.e. the sequence "^[E^["
literally means 5 characters:
character "^", followed by another character "[", followed by "E", then
followed by another "^", and a final "["

....and, no surprise, the match "...if ( $_ =~ /\^\[E\^\[/ )..." works
fine.

However, I suspect that the 2-character sequence "^[" is not to be
taken literally, but really stands for one single control character
(possibly an octal "\033").

You could try the following instead:
if ( $_ =~ /\033E\033/ )
 
A

A. Sinan Unur

Brian said:
I am processing print job files.
....
Here is an example of part of a print job file containing PCL data:

* PRINT TIME: 13:30:38
*
* PRINT DATE: 23 JUN 2006
*
* PRINT NAME: TA104002
*
* SYSTEM: MVSA
*
*
*
**START*****START*****START*****START*****START*****START*****START***
**START**
^[E^[&u300D^[*v1O^[*v0N^[*c00001D^[)s64W@^B)$7^A^^¦^U^BÿªÃðÄ
....

However, I suspect that the 2-character sequence "^[" is not to be
taken literally, but really stands for one single control character
(possibly an octal "\033").

You could try the following instead:
if ( $_ =~ /\033E\033/ )

You are most likely correct that ^[ stands for the escape character. In
that case, you can also use \e instead of the octal code.

Sinan
--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top