Subversion Repositories svn1

Rev

Rev 1 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 root 1
/*************************************************************************
2
 *          Copyright (C) 1995 Embedded Solutions
3
 *                      All rights reserved
4
 *
5
 * file:        src\print.c
6
 *
7
 * purpose: Module to interface to the printer channel (file)
8
 *
9
 * functions
10
 *      open_printer            - Open the printer
11
 *      close_printer           - Close the printer
12
 *      print                   - Print to to the printer
13
 *      printbanner             - Print a page banner
14
 *
15
 * programmer: David Purdie
16
 *
17
 * revision date        by      reason
18
 *  00.0    27/01/95    DDP     Tidies up the program and formatted the file
19
 *
20
 **************************************************************************/
21
 
22
#include    <stdio.h>
23
#include    <time.h>
24
#include    <stdarg.h>
25
 
26
#include    "consts.h"
27
#include    "structs.h"
28
#include    "proto.h"
29
 
30
FILE       *pfile;                               /* Fd of the printer channel */
31
long        pri_time;                            /* Time printer is opened - for banners */
32
int         print_width;                         /* Width of the printout */
33
int         print_html = 0;                      /* Printing with HTML */
34
int         print_col = 0;                       /* Current print column */
35
int         print_line = 0;                      /* Current print line */
36
int         print_underline_start;               /* Start of underline */
37
int         print_underline_end;                 /* End of underline */
38
long        print_current_colour;                /* Current printing colour */
39
 
40
/*========================================================================
41
 *
42
 *  Open the printer
43
 *
44
 *  Purpose:
45
 *      This function is called to open the printer and prepare to print
46
 *
47
 *  Parameters:
48
 *      name        Name of print file
49
 *      ext         Ext of the print file
50
 *      width       Width of the printed file
51
 *      html        Printing in HTML mode
52
 *
53
 *  Returns:
54
 *      TRUE if the printer can be attached
55
 *
56
 *
57
 *  Note: Use an "htm" file extension as DOS cannot handle 4 letter extensions
58
 *========================================================================*/
59
 
60
bool open_printer( char *name, char *ext, int width, bool html, char *title )
61
{
62
    char    *pname;
63
 
64
    /*
65
    **  Determine the name of the file
66
    **  May need to modify the extension if it is an HTML file
67
    */
68
    if ( html )
69
    {
70
        pname = p_filename( name[0] ? name : filebase, ext ,"html" );
71
    }
72
    else
73
    {
74
        pname = p_filename( name[0] ? name : filebase, "", ext[0] ? ext : "lst" );
75
    }
76
 
77
    print_width = width ? width : 80;
78
    print_col = 0;
79
    print_line = 0;
80
    print_underline_start = -1;
81
    print_underline_end = -1;
82
    print_current_colour = 0;
83
 
84
    /*
85
    **  Open the printer
86
    **  Ensure that it is opened in text mode
87
    */
88
    if( ( pfile = fopen( pname, "wt" ) ) != NULL )
89
    {
90
        time( &pri_time );                       /* Latch the time */
91
 
92
        /*
93
        **  Print out the HTML file header
94
        */
95
        if ( html )
96
        {
97
            print( "<HTML>\n" );
98
            print( "<HEAD>\n" );
99
            print( "<TITLE>%s - %s</TITLE>\n", config.event_name, title );
100
//            print( "<LINK rel=\"stylesheet\" href=\"brmr.css\" type=\"text/css\">\n");
101
            print( "</HEAD>\n" );
102
            print( "<BODY LANG=\"en-US\">\n" );
103
            print( "<PRE>" );
104
            print_html = TRUE;
105
        }
106
 
107
        /*
108
        **  Print out a common banner
109
        */
110
        printbanner(title);
111
    }
112
    else
113
    {
114
        beep();                                /* Printer not available */
115
        printf( "Printer not available" );
116
        flush_out();
117
        sleep( 5 );
118
    }
119
    return ( pfile != 0 );
120
}
121
 
122
/*========================================================================
123
 *
124
 *  Close the printer
125
 *
126
 *  Purpose:
127
 *      This function is called to close and release the printer
128
 *      All reports are terminated with a formfeed
129
 *
130
 *  Parameters:
131
 *      None
132
 *
133
 *  Returns:
134
 *      TRUE if the printer channel can be released
135
 *
136
 *========================================================================*/
137
 
138
bool close_printer(void)
139
{
140
    if ( print_html )
141
    {
142
        print_html = FALSE;
143
        print( "</PRE>\n" );
144
        print( "</BODY>\n" );
145
        print( "</HTML>\n" );
146
    }
147
    else
148
    {
149
        print( "\014" );
150
    }
151
    fclose( pfile );
152
    return ( TRUE );
153
}
154
 
155
/*========================================================================
156
 *
157
 *  Print to to the printer
158
 *
159
 *  Purpose:
160
 *      This function is called to do print data on the printer
161
 *      Track current character index so as to assist in tracking the
162
 *      use of underline.
163
 *
164
 *  Assume:
165
 *      New lines are at the end of a line printed. This does allow for
166
 *      a single new-line.
167
 *
168
 *
169
 *  Parameters:
170
 *      Standard printf type parameters
171
 *
172
 *  Returns:
173
 *      TRUE : Operation was succesful
174
 *
175
 *========================================================================*/
176
 
177
int print( char *format, ... )
178
{
179
    va_list     ap;
180
    char        pp[200];
181
    int         len;
182
    bool        eol = FALSE;
183
 
184
 
185
    /*
186
    **  If this is the start of a new line then we may need to colour it
187
    */
188
    if ( print_col == 0 && print_html && (print_line & 1) )
189
    {
190
        print_colour( HTML_COLOUR_GREEN );
191
    }
192
 
193
    /*
194
    **  If this is the start of a new line then we may need to perform
195
    **  a perf-skip
196
    */
197
    if ( print_col == 0 && print_line && ! print_html && config.lines_per_page && config.perf_skip )
198
    {
199
        if ( 0 == ((print_line + 2) % (config.lines_per_page - config.perf_skip)) )
200
        {
201
            int count = config.perf_skip;
202
            while ( count-- )
203
                fwrite( "\n", 1, 1, pfile );
204
        }
205
    }
206
 
207
    va_start( ap, format );
208
    len = vsprintf( pp, format, ap );
209
    va_end( ap );
210
 
211
    /*
212
    **  Detect the end of a line and flag for later processing
213
    */
214
    if ( len > 0 && pp[len - 1] == '\n' )
215
    {
216
        len--;
217
        eol = TRUE;
218
    }
219
 
220
    if ( len )
221
    {
222
        fwrite( pp, 1, len, pfile );
223
        print_col += len;
224
    }
225
 
226
    /*
227
    **  Perform End of Line operation before printing the final newline
228
    */
229
    if ( eol )
230
    {
231
        print_line++;
232
 
233
        /*
234
        **  Detect the end of a non-HTML underline
235
        */
236
        print_underline ( FALSE );
237
        if ( print_underline_start < print_underline_end )
238
        {
239
            int length;
240
 
241
            fwrite( "\n", 1, 1, pfile );
242
 
243
            length = print_underline_start;
244
            while( length-- )
245
                fwrite( " ", 1, 1, pfile );
246
 
247
            length = print_underline_end - print_underline_start;
248
            while ( length-- )
249
                fwrite( "=", 1, 1, pfile );
250
        }
251
 
252
        print_underline_start = -1;
253
        print_underline_end = -1;
254
        print_col = 0;
255
 
256
        /*
257
        **  Track the background colour
258
        */
259
        if ( print_html )
260
        {
261
            print_colour( 0 );
262
        }
263
 
264
        /*
265
        **  Now print the final newline
266
        */
267
        fwrite( "\n", 1, 1, pfile );
268
        len ++;
269
    }
270
 
271
    return ( len );
272
}
273
 
274
/*========================================================================
275
 *
276
 *  Print to to the printer without any frills
277
 *
278
 *  Purpose:
279
 *      This function is called to do print data on the printer
280
 *
281
 *  Parameters:
282
 *      Standard printf type parameters
283
 *
284
 *  Returns:
285
 *      TRUE : Operation was succesful
286
 *
287
 *========================================================================*/
288
 
289
int raw_print( char *format, ... )
290
{
291
    va_list     ap;
292
    char        pp[200];
293
    int         len;
294
 
295
 
296
    va_start( ap, format );
297
    len = vsprintf( pp, format, ap );
298
    va_end( ap );
299
 
300
    fwrite( pp, 1, len, pfile );
301
 
302
    return ( len );
303
}
304
 
305
 
306
/*========================================================================
307
 *
308
 *  Control bolding
309
 *
310
 *  Purpose:
311
 *      This function is called to turn bolding on and off
312
 *      This function will ONLY affect HTML printing
313
 *
314
 *  Parameters:
315
 *      on              - TRUE
316
 *
317
 *  Returns:
318
 *      Nothing
319
 *
320
 *========================================================================*/
321
 
322
void print_bold( bool on )
323
{
324
    if ( print_html )
325
    {
326
        if ( on )
327
            raw_print( "<B>" );
328
        else
329
            raw_print( "</B>" );
330
    }
331
}
332
 
333
/*========================================================================
334
 *
335
 *  Control underline
336
 *
337
 *  Purpose:
338
 *      This function is called to turn underline on and off
339
 *      This function will ONLY affect HTML printing and Non-HTML printing
340
 *      But in a different manner
341
 *
342
 *  Parameters:
343
 *      on              - TRUE
344
 *
345
 *  Returns:
346
 *      Nothing
347
 *
348
 *========================================================================*/
349
 
350
void print_underline( bool on )
351
{
352
    if ( print_html )
353
    {
354
        /*
355
        **  For HTML printing underline is simple
356
        */
357
        if ( on )
358
        {
359
            raw_print( "<U>" );
360
            print_underline_start = 1;
361
        }
362
        else if ( print_underline_start > 0 )
363
        {
364
            raw_print( "</U>" );
365
            print_underline_start = 0;
366
        }
367
    }
368
    else
369
    {
370
        /*
371
        **  Non-HTML printing
372
        **  Store underline start and stop column
373
        */
374
        if ( on )
375
            print_underline_start = print_col;
376
        else if ( print_underline_start >= 0 )
377
            print_underline_end = print_col;
378
    }
379
}
380
 
381
/*========================================================================
382
 *
383
 *  Control colour - HTML printing only
384
 *
385
 *  Purpose:
386
 *      This function is called to change the background colour within
387
 *      HTML text
388
 *
389
 *  Parameters:
390
 *      colour      - Colour control string
391
 *                    or NULL to trun colour OFF
392
 *
393
 *
394
 *  Returns:
395
 *      Nothing
396
 *
397
 *========================================================================*/
398
 
399
void print_colour( long colour )
400
{
401
    if ( print_html )
402
    {
403
        if ( print_current_colour )
404
        {
405
            raw_print( "</SPAN>" );
406
            print_current_colour = 0L;
407
        }
408
 
409
        if ( colour )
410
        {
411
            print_current_colour = colour;
412
            raw_print( "<SPAN STYLE=\"background: #%6.6lx\">", colour );
413
        }
414
    }
415
}
416
 
417
 
418
/*========================================================================
419
 *
420
 *  Print a page banner
421
 *
422
 *  Purpose:
423
 *      This function is called to print a page banner
424
 *
425
 *  Parameters:
426
 *      None
427
 *
428
 *  Returns:
429
 *      Nothing
430
 *
431
 *========================================================================*/
432
 
433
void printbanner( char *title )
434
{
435
    int         l, s;
436
 
437
    l = strlen( config.event_name );
438
    s = ( print_width - l ) / 2;
439
    print( "%*s", s, "" );
440
 
441
    if ( print_html )
442
        print( "<A HREF=\"%s\">", p_filename(filebase, "index" ,"html"));
443
    print_underline( TRUE);
444
    print_bold( TRUE );
445
 
446
    print( "%s", config.event_name );
447
 
448
    print_bold( FALSE );
449
    print_underline( FALSE );
450
    if ( print_html )
451
        print( "</A>" );
452
 
453
    print( "\n" );
454
 
455
    /*
456
    **  Print out one line with the report title
457
    **  on the left and the report time on the right
458
    */
459
    if ( title )
460
        print( "%s", title );
461
 
462
    s = print_width - print_col - 24 - 4;
463
    print( "%*s", s , "" );
464
 
465
    print( "%.24s", ctime( &pri_time ) );
466
    print( "\n" );
467
    print( "\n" );
468
}
469
 
470
 
471
/*========================================================================
472
 *
473
 *  Format a filename
474
 *
475
 *  Purpose:
476
 *      This function is called create a suitably formatted filename
477
 *      for use in an HTML tag or by the file system
478
 *
479
 *      Currently all output filenames are created in uppercase
480
 *      This is a limitation of DOS
481
 *      This function is used to ensure that all files are created with
482
 *      an uppercase name and that all HTML file references are also
483
 *      in uppercase.
484
 *
485
 *
486
 *  Parameters:
487
 *      filename        - Base filename
488
 *      Suffix          - Optional part of the basename
489
 *                        If present is used to create part of the basename
490
 *      ext             - Extension
491
 *
492
 *  Returns:
493
 *      Address of a static string that will reference the file
494
 *
495
 *========================================================================*/
496
 
497
char * p_filename( char *filename, char *suffix, char *ext )
498
{
499
    char    *name;
500
    char    *cptr;
501
 
502
#ifndef LONG_FILE_NAMES
503
    /*
504
    **  Limit the filename to 8.3 format
505
    **  This is a limitation of the underlying file access library
506
    **  and may be removed in the future
507
    */
508
    if ( suffix[0] )
509
        name = tprintf ( "%.4s_%.3s.%.3s", filename, suffix, ext );
510
    else
511
        name = tprintf ( "%.8s.%.3s", filename, ext );
512
 
513
 
514
    /*
515
    **  Uppercase the filename
516
    */
517
    cptr = name;
518
    while ( *cptr )
519
    {
520
        *cptr = toupper( *cptr );
521
        cptr++;
522
    }
523
#else
524
    /*
525
    **  This compiler and runtime library supports
526
    **  long filenames - created printed filenames in lower case
527
    */
528
    if ( suffix[0] )
529
        name = tprintf ( "%s_%s.%s", filename, suffix, ext );
530
    else
531
        name = tprintf ( "%s_%s.%s", filename, ext, "txt" );
532
 
533
    cptr = name;
534
    while ( *cptr )
535
    {
536
        *cptr = tolower( *cptr );
537
        cptr++;
538
    }
539
#endif
540
 
541
    return( name );
542
}
543
 
544
 
545
/*========================================================================
546
 *
547
 *  Print to a temp string buffer
548
 *
549
 *  Purpose:
550
 *      This function is similar to sprintf() except that the routine
551
 *      maintains a list of string buffers and will return one to the
552
 *      user.
553
 *
554
 *      This function allows a user to create small, short lived
555
 *      printed strings, without the memory managment overhead.
556
 *
557
 *      Down-side. Use the string quickly.
558
 *                 Strings have a limited length
559
 *
560
 *  Parameters:
561
 *      Standard printf type parameters
562
 *
563
 *  Returns:
564
 *      TRUE : Operation was succesful
565
 *
566
 *========================================================================*/
567
 
568
#define TBUF_COUNT  5               /* Number of buffers */
569
#define TBUF_LENGTH 100             /* Max length of a single print */
570
 
571
char *tprintf( char *format, ... )
572
{
573
    static char tbuf[TBUF_COUNT][TBUF_LENGTH];
574
    static int  index = 0;
575
 
576
    va_list     ap;
577
    char       *pp;
578
 
579
    /*
580
    **  Get the next entry from the small store
581
    */
582
    index++;
583
    if( index >= TBUF_COUNT )
584
        index = 0;
585
    pp = tbuf[index];
586
 
587
    va_start( ap, format );
588
    vsprintf( pp, format, ap );
589
    va_end( ap );
590
 
591
    return ( pp );
592
}
593
 
594
/********************************* EOF ***********************************/