home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
World of A1200
/
World_Of_A1200.iso
/
datafiles
/
text
/
c_manual
/
devices
/
printerdevice
/
example1.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-02-27
|
10KB
|
316 lines
/***********************************************************/
/* */
/* Amiga C Encyclopedia (ACE) V3.0 Amiga C Club (ACC) */
/* ------------------------------- ------------------ */
/* */
/* Book: ACM Devices Amiga C Club */
/* Chapter: Printer Device Tulevagen 22 */
/* File: Example1.c 181 41 LIDINGO */
/* Author: Anders Bjerin SWEDEN */
/* Date: 92-04-27 */
/* Version: 1.00 */
/* */
/* Copyright 1992, Anders Bjerin - Amiga C Club (ACC) */
/* */
/* Registered members may use this program freely in their */
/* own commercial/noncommercial programs/articles. */
/* */
/***********************************************************/
/* This program demonstrates how you can use the Printer */
/* Device to send (raw as well as translated) text to a */
/* printer. */
#include <exec/types.h> /* Data types. */
#include <exec/errors.h> /* Exec error messages. */
#include <devices/printer.h> /* Printer Device. */
#include <exec/io.h> /* Standard request block. */
/* Declare how the printer request block look like: */
union printerIO
{
struct IOStdReq ios;
struct IODRPReq iodrp;
struct IOPrtCmdReq iopc;
};
/* Declare a pointer to our reply port: */
struct MsgPort *replymp = NULL;
/* Declare a pointer our printer request block: */
union printerIO *printer_req = NULL;
/* Store the printer device error here: */
UWORD printer_dever = TRUE;
/* Declare our data buffer: (25 characters) */
BYTE buffer[] = "Anders Bjerin was here...";
/* Declare our functions: */
/* Our main function: */
void main();
/* Clears and removes everything nice and neatly: */
void clean_up( BYTE error, STRPTR text );
/* Prints some information about the error: */
void PrtError( BYTE error );
/* Sends characters (which are translated) to the printer: */
BYTE PrintText(
union printerIO *ioreq,
BYTE *data,
ULONG length
);
/* Sends raw (untranslated) characters to the printer: */
BYTE PrintRaw(
union printerIO *ioreq,
BYTE *data,
ULONG length
);
void main()
{
/* Error number: */
BYTE error;
/* Get a reply port: (No name, priority 0) */
replymp = (struct MsgPort *)
CreatePort( NULL, 0 );
if( !replymp )
clean_up( 0, "Could not create the reply port!" );
/* Create the printer request block: */
printer_req = (union printerIO *)
CreateExtIO( replymp, sizeof(union printerIO) );
if( !printer_req )
clean_up( 0, "Not enough memory for the printer request block!" );
/* Open the Printer Device: */
printer_dever = OpenDevice( "printer.device", 0, printer_req, 0 );
if( printer_dever )
clean_up( 0, "Could not open the Printer Device!" );
/* Send some text to the printer: (Will be translated) */
error = PrintText( printer_req, buffer, 25 );
if( error )
PrtError( error );
else
printf( "Printing normal text...\n" );
/* Send some raw (untranslated) text to the printer: */
error = PrintRaw( printer_req, buffer, 25 );
if( error )
PrtError( error );
else
printf( "Printing raw (untranslated) text...\n" );
/* Clean up and quit: */
clean_up( 0, "The End!" );
}
/* Close and return everything that has been */
/* opened and allocated before we quit: */
void clean_up( BYTE error, STRPTR text )
{
/* Print some information about the problem: */
if( error )
PrtError( error );
/* Close the Printer Device: */
if( !printer_dever )
CloseDevice( printer_req );
/* Deallocate the printer request block: */
if( printer_req )
DeleteExtIO( printer_req, sizeof(union printerIO) );
/* Remove the replyport: */
if( replymp )
DeletePort( replymp);
/* Print the message: */
printf( "\n%s\n", text );
/* Quit: */
exit( 0 );
}
/* PrtError() tells the user what went wrong. You give it the error code */
/* you received, and PrtError() will print a short description of the */
/* problem. Useful when debugging. (Printer errors) */
/* */
/* Synopsis: PrtError( error ); */
/* */
/* error: (BYTE) The error value you want to have explained. */
void PrtError( BYTE error )
{
switch( error )
{
/* EXEC error messages: (defined in "exec/errors.h") */
case IOERR_OPENFAIL:
printf( "Could not open the device!\n" );
break;
case IOERR_ABORTED:
printf( "The request was aborted!\n" );
break;
case IOERR_NOCMD:
printf( "Unknown Command!\n" );
break;
case IOERR_BADLENGTH:
printf( "Bad length of the command - data!\n" );
/* Printer Device errors: (defined in "devices/printer.h") */
case PDERR_CANCEL:
printf( "User cancelled the request!\n" );
break;
case PDERR_NOTGRAPHICS:
printf( "The printer does not support graphics!\n" );
break;
case PDERR_BADDIMENSION:
printf( "The printer dimension is not valid!\n" );
break;
case PDERR_INTERNALMEMORY:
printf( "Not enough memory for the internal printer functions!\n" );
break;
case PDERR_BUFFERMEMORY:
printf( "Not enough memory for the print buffer!\n" );
break;
default:
printf( "An unknown error was reported! Error nr: %d\n", error );
}
}
/* PrintText() sends characters (which will be translated) to the */
/* printer. Since the printer device will use the Preference's */
/* settings, it will know to which port (parallel or serial) the */
/* printer is connected to, what type of printer it is, and what */
/* special settings (margins, density, quality mode etc) the user */
/* have defined. */
/* */
/* Note! All characters which are sent with this function may be */
/* translated by Preferences before it is passed on to the */
/* printer. */
/* */
/* Synopsis: error = PrintText( io, data, length ); */
/* */
/* error: (BYTE) PrintWrite() returns 0 if everything was OK, */
/* else an error number is returned. */
/* */
/* io: (union printerIO *) Pointer to a printer request */
/* block. */
/* */
/* data: (BYTE *) Pointer to the first character that should */
/* be printed. */
/* */
/* length (ULONG) How many characters (bytes) you want to send */
/* to the printer. */
BYTE PrintText(
union printerIO *ioreq, /* Pointer to the printer request block. */
BYTE *data, /* Pointer to the data which should be printed. */
ULONG length /* How many characters (bytes) should be printed. */
)
{
/* We want to print some text: (send data to PRT:) */
ioreq->ios.io_Command = CMD_WRITE;
/* Give the start address of our data: */
ioreq->ios.io_Data = (APTR) data;
/* Set number of chracters that should be printed: */
ioreq->ios.io_Length = length;
/* Do our request, and return 0 if everything is OK, else */
/* return an error number: (This is a task sleep.) */
return( (BYTE) DoIO( ioreq ) );
}
/* PrintRaw() sends untranslated characters to the printer. Note */
/* that this is usually not a very good idea. Since the characters */
/* will not be translated by Preferences, you can not be sure that */
/* the characters you send will be the same when printed. */
/* */
/* Synopsis: error = PrintRaw( io, data, length ); */
/* */
/* error: (BYTE) PrintRaw() returns 0 if everything was OK, */
/* else an error number is returned. */
/* */
/* io: (union printerIO *) Pointer to a printer request */
/* block. */
/* */
/* data: (BYTE *) Pointer to the first character that should */
/* be printed. */
/* */
/* length (ULONG) How many characters (bytes) you want to send */
/* to the printer. */
BYTE PrintRaw(
union printerIO *ioreq, /* Pointer to the printer request block. */
BYTE *data, /* Pointer to the data which should be printed. */
ULONG length /* How many characters (bytes) should be printed. */
)
{
/* We want to print some raw (untranslated) text: */
ioreq->ios.io_Command = PRD_RAWWRITE;
/* Give the start address of our data: */
ioreq->ios.io_Data = (APTR) data;
/* Set number of chracters that should be printed: */
ioreq->ios.io_Length = length;
/* Do our request, and return 0 if everything is OK, else */
/* return an error number: (This is a task sleep.) */
return( (BYTE) DoIO( ioreq ) );
}