Subversion Repositories svn1

Rev

Rev 54 | 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
    {
54 - 74
        pname = p_filename( filebase, name[0] ? name : "", ext[0] ? ext : "lst" );
75
    }
1 root 76
 
54 - 77
    /*
78
    **  Now use basic function
79
    */
80
    return open_printer_name (pname, width, html, title );
81
}
82
 
83
/*----------------------------------------------------------------------------
84
 * FUNCTION           : open_printer_name
85
 *
86
 * DESCRIPTION        : Open a printer with a known name
87
 *
88
 *      This function is called to open the printer and prepare to print
89
 *
90
 *  Parameters:
91
 *      pname       Name of print file
92
 *      width       Width of the printed file
93
 *      html        Printing in HTML mode
94
 *
95
 *  Returns:
96
 *      TRUE if the printer can be attached
97
 *
98
 *
99
 *  Note: Use an "htm" file extension as DOS cannot handle 4 letter extensions
100
----------------------------------------------------------------------------*/
101
 
102
bool open_printer_name( char *pname, int width, bool html, char *title )
103
{
1 root 104
    print_width = width ? width : 80;
105
    print_col = 0;
106
    print_line = 0;
107
    print_underline_start = -1;
108
    print_underline_end = -1;
109
    print_current_colour = 0;
110
 
111
    /*
112
    **  Open the printer
113
    **  Ensure that it is opened in text mode
114
    */
115
    if( ( pfile = fopen( pname, "wt" ) ) != NULL )
116
    {
117
        time( &pri_time );                       /* Latch the time */
118
 
119
        /*
120
        **  Print out the HTML file header
121
        */
122
        if ( html )
123
        {
124
            print( "<HTML>\n" );
125
            print( "<HEAD>\n" );
126
            print( "<TITLE>%s - %s</TITLE>\n", config.event_name, title );
127
//            print( "<LINK rel=\"stylesheet\" href=\"brmr.css\" type=\"text/css\">\n");
128
            print( "</HEAD>\n" );
129
            print( "<BODY LANG=\"en-US\">\n" );
130
            print( "<PRE>" );
131
            print_html = TRUE;
132
        }
133
 
134
        /*
135
        **  Print out a common banner
136
        */
137
        printbanner(title);
138
    }
139
    else
140
    {
141
        beep();                                /* Printer not available */
142
        printf( "Printer not available" );
143
        flush_out();
144
        sleep( 5 );
145
    }
146
    return ( pfile != 0 );
147
}
148
 
149
/*========================================================================
150
 *
151
 *  Close the printer
152
 *
153
 *  Purpose:
154
 *      This function is called to close and release the printer
155
 *      All reports are terminated with a formfeed
156
 *
157
 *  Parameters:
158
 *      None
159
 *
160
 *  Returns:
161
 *      TRUE if the printer channel can be released
162
 *
163
 *========================================================================*/
164
 
165
bool close_printer(void)
166
{
167
    if ( print_html )
168
    {
169
        print_html = FALSE;
170
        print( "</PRE>\n" );
171
        print( "</BODY>\n" );
172
        print( "</HTML>\n" );
173
    }
174
    else
175
    {
176
        print( "\014" );
177
    }
178
    fclose( pfile );
179
    return ( TRUE );
180
}
181
 
182
/*========================================================================
183
 *
184
 *  Print to to the printer
185
 *
186
 *  Purpose:
187
 *      This function is called to do print data on the printer
188
 *      Track current character index so as to assist in tracking the
189
 *      use of underline.
190
 *
191
 *  Assume:
192
 *      New lines are at the end of a line printed. This does allow for
193
 *      a single new-line.
194
 *
195
 *
196
 *  Parameters:
197
 *      Standard printf type parameters
198
 *
199
 *  Returns:
200
 *      TRUE : Operation was succesful
201
 *
202
 *========================================================================*/
203
 
204
int print( char *format, ... )
205
{
206
    va_list     ap;
207
    char        pp[200];
208
    int         len;
209
    bool        eol = FALSE;
210
 
211
 
212
    /*
213
    **  If this is the start of a new line then we may need to colour it
214
    */
215
    if ( print_col == 0 && print_html && (print_line & 1) )
216
    {
217
        print_colour( HTML_COLOUR_GREEN );
218
    }
219
 
220
    /*
221
    **  If this is the start of a new line then we may need to perform
222
    **  a perf-skip
223
    */
224
    if ( print_col == 0 && print_line && ! print_html && config.lines_per_page && config.perf_skip )
225
    {
226
        if ( 0 == ((print_line + 2) % (config.lines_per_page - config.perf_skip)) )
227
        {
228
            int count = config.perf_skip;
229
            while ( count-- )
230
                fwrite( "\n", 1, 1, pfile );
231
        }
232
    }
233
 
234
    va_start( ap, format );
235
    len = vsprintf( pp, format, ap );
236
    va_end( ap );
237
 
238
    /*
239
    **  Detect the end of a line and flag for later processing
240
    */
241
    if ( len > 0 && pp[len - 1] == '\n' )
242
    {
243
        len--;
244
        eol = TRUE;
245
    }
246
 
247
    if ( len )
248
    {
249
        fwrite( pp, 1, len, pfile );
250
        print_col += len;
251
    }
252
 
253
    /*
254
    **  Perform End of Line operation before printing the final newline
255
    */
256
    if ( eol )
257
    {
258
        print_line++;
259
 
260
        /*
261
        **  Detect the end of a non-HTML underline
262
        */
263
        print_underline ( FALSE );
264
        if ( print_underline_start < print_underline_end )
265
        {
266
            int length;
267
 
268
            fwrite( "\n", 1, 1, pfile );
269
 
270
            length = print_underline_start;
271
            while( length-- )
272
                fwrite( " ", 1, 1, pfile );
273
 
274
            length = print_underline_end - print_underline_start;
275
            while ( length-- )
276
                fwrite( "=", 1, 1, pfile );
277
        }
278
 
279
        print_underline_start = -1;
280
        print_underline_end = -1;
281
        print_col = 0;
282
 
283
        /*
284
        **  Track the background colour
285
        */
286
        if ( print_html )
287
        {
288
            print_colour( 0 );
289
        }
290
 
291
        /*
292
        **  Now print the final newline
293
        */
294
        fwrite( "\n", 1, 1, pfile );
295
        len ++;
296
    }
297
 
298
    return ( len );
299
}
300
 
301
/*========================================================================
302
 *
51 - 303
 *  Print to to the printer
304
 *
305
 *  Purpose:
306
 *      This function is called to do print data on the printer
307
 *
308
 *  Assume:
309
 *      One CSV field per print
310
 *      New lines are at the end of a line printed. This does allow for
311
 *      a single new-line.
312
 *
313
 *
314
 *  Parameters:
315
 *      Standard printf type parameters
316
 *
317
 *  Returns:
318
 *      TRUE : Operation was succesful
319
 *
320
 *========================================================================*/
321
 
322
int csv_print( char *format, ... )
323
{
324
    va_list     ap;
325
    char        pp[200];
326
    int         len;
327
    bool        eol = FALSE;
328
 
329
 
330
    va_start( ap, format );
331
    len = vsprintf( pp, format, ap );
332
    va_end( ap );
333
 
334
    /*
335
    **  Detect the end of a line and flag for later processing
336
    */
337
    if ( len > 0 && pp[len - 1] == '\n' )
338
    {
339
        len--;
340
        eol = TRUE;
341
    }
342
 
343
    if ( ! eol )
344
    {
345
        if ( print_col )
346
            fwrite( ",", 1, 1, pfile );
347
        fwrite( "\"", 1, 1, pfile );
348
        fwrite( pp, 1, len, pfile );
349
        print_col += len;
350
        fwrite( "\"", 1, 1, pfile );
351
    }
352
 
353
    /*
354
    **  Perform End of Line operation before printing the final newline
355
    */
356
    if ( eol )
357
    {
358
        print_line++;
359
        print_col = 0;
360
 
361
        /*
362
        **  Now print the final newline
363
        */
364
        fwrite( "\n", 1, 1, pfile );
365
        len ++;
366
    }
367
 
368
    return ( len );
369
}
370
 
371
 
372
/*========================================================================
373
 *
1 root 374
 *  Print to to the printer without any frills
375
 *
376
 *  Purpose:
377
 *      This function is called to do print data on the printer
378
 *
379
 *  Parameters:
380
 *      Standard printf type parameters
381
 *
382
 *  Returns:
383
 *      TRUE : Operation was succesful
384
 *
385
 *========================================================================*/
386
 
387
int raw_print( char *format, ... )
388
{
389
    va_list     ap;
390
    char        pp[200];
391
    int         len;
392
 
393
 
394
    va_start( ap, format );
395
    len = vsprintf( pp, format, ap );
396
    va_end( ap );
397
 
398
    fwrite( pp, 1, len, pfile );
399
 
400
    return ( len );
401
}
402
 
403
 
404
/*========================================================================
405
 *
406
 *  Control bolding
407
 *
408
 *  Purpose:
409
 *      This function is called to turn bolding on and off
410
 *      This function will ONLY affect HTML printing
411
 *
412
 *  Parameters:
413
 *      on              - TRUE
414
 *
415
 *  Returns:
416
 *      Nothing
417
 *
418
 *========================================================================*/
419
 
420
void print_bold( bool on )
421
{
422
    if ( print_html )
423
    {
424
        if ( on )
425
            raw_print( "<B>" );
426
        else
427
            raw_print( "</B>" );
428
    }
429
}
430
 
431
/*========================================================================
432
 *
433
 *  Control underline
434
 *
435
 *  Purpose:
436
 *      This function is called to turn underline on and off
437
 *      This function will ONLY affect HTML printing and Non-HTML printing
438
 *      But in a different manner
439
 *
440
 *  Parameters:
441
 *      on              - TRUE
442
 *
443
 *  Returns:
444
 *      Nothing
445
 *
446
 *========================================================================*/
447
 
448
void print_underline( bool on )
449
{
450
    if ( print_html )
451
    {
452
        /*
453
        **  For HTML printing underline is simple
454
        */
455
        if ( on )
456
        {
457
            raw_print( "<U>" );
458
            print_underline_start = 1;
459
        }
460
        else if ( print_underline_start > 0 )
461
        {
462
            raw_print( "</U>" );
463
            print_underline_start = 0;
464
        }
465
    }
466
    else
467
    {
468
        /*
469
        **  Non-HTML printing
470
        **  Store underline start and stop column
471
        */
472
        if ( on )
473
            print_underline_start = print_col;
474
        else if ( print_underline_start >= 0 )
475
            print_underline_end = print_col;
476
    }
477
}
478
 
479
/*========================================================================
480
 *
481
 *  Control colour - HTML printing only
482
 *
483
 *  Purpose:
484
 *      This function is called to change the background colour within
485
 *      HTML text
486
 *
487
 *  Parameters:
488
 *      colour      - Colour control string
489
 *                    or NULL to trun colour OFF
490
 *
491
 *
492
 *  Returns:
493
 *      Nothing
494
 *
495
 *========================================================================*/
496
 
497
void print_colour( long colour )
498
{
499
    if ( print_html )
500
    {
501
        if ( print_current_colour )
502
        {
503
            raw_print( "</SPAN>" );
504
            print_current_colour = 0L;
505
        }
506
 
507
        if ( colour )
508
        {
509
            print_current_colour = colour;
510
            raw_print( "<SPAN STYLE=\"background: #%6.6lx\">", colour );
511
        }
512
    }
513
}
514
 
515
 
516
/*========================================================================
517
 *
518
 *  Print a page banner
519
 *
520
 *  Purpose:
521
 *      This function is called to print a page banner
522
 *
523
 *  Parameters:
524
 *      None
525
 *
526
 *  Returns:
527
 *      Nothing
528
 *
529
 *========================================================================*/
530
 
531
void printbanner( char *title )
532
{
533
    int         l, s;
534
 
51 - 535
    if ( !title )
536
        return;
537
 
1 root 538
    l = strlen( config.event_name );
539
    s = ( print_width - l ) / 2;
540
    print( "%*s", s, "" );
541
 
542
    if ( print_html )
543
        print( "<A HREF=\"%s\">", p_filename(filebase, "index" ,"html"));
544
    print_underline( TRUE);
545
    print_bold( TRUE );
546
 
547
    print( "%s", config.event_name );
548
 
549
    print_bold( FALSE );
550
    print_underline( FALSE );
551
    if ( print_html )
552
        print( "</A>" );
553
 
554
    print( "\n" );
555
 
556
    /*
557
    **  Print out one line with the report title
558
    **  on the left and the report time on the right
559
    */
560
    if ( title )
561
        print( "%s", title );
562
 
563
    s = print_width - print_col - 24 - 4;
564
    print( "%*s", s , "" );
565
 
566
    print( "%.24s", ctime( &pri_time ) );
567
    print( "\n" );
568
    print( "\n" );
569
}
570
 
571
 
572
/*========================================================================
573
 *
574
 *  Format a filename
575
 *
576
 *  Purpose:
577
 *      This function is called create a suitably formatted filename
578
 *      for use in an HTML tag or by the file system
579
 *
580
 *      Currently all output filenames are created in uppercase
581
 *      This is a limitation of DOS
582
 *      This function is used to ensure that all files are created with
583
 *      an uppercase name and that all HTML file references are also
584
 *      in uppercase.
585
 *
586
 *
587
 *  Parameters:
588
 *      filename        - Base filename
589
 *      Suffix          - Optional part of the basename
590
 *                        If present is used to create part of the basename
591
 *      ext             - Extension
592
 *
593
 *  Returns:
594
 *      Address of a static string that will reference the file
595
 *
596
 *========================================================================*/
597
 
598
char * p_filename( char *filename, char *suffix, char *ext )
599
{
600
    char    *name;
601
    char    *cptr;
602
 
603
#ifndef LONG_FILE_NAMES
604
    /*
605
    **  Limit the filename to 8.3 format
606
    **  This is a limitation of the underlying file access library
607
    **  and may be removed in the future
608
    */
609
    if ( suffix[0] )
610
        name = tprintf ( "%.4s_%.3s.%.3s", filename, suffix, ext );
611
    else
612
        name = tprintf ( "%.8s.%.3s", filename, ext );
613
 
614
 
615
    /*
616
    **  Uppercase the filename
617
    */
618
    cptr = name;
619
    while ( *cptr )
620
    {
621
        *cptr = toupper( *cptr );
622
        cptr++;
623
    }
624
#else
625
    /*
626
    **  This compiler and runtime library supports
627
    **  long filenames - created printed filenames in lower case
628
    */
629
    if ( suffix[0] )
630
        name = tprintf ( "%s_%s.%s", filename, suffix, ext );
631
    else
632
        name = tprintf ( "%s_%s.%s", filename, ext, "txt" );
633
 
634
    cptr = name;
635
    while ( *cptr )
636
    {
637
        *cptr = tolower( *cptr );
638
        cptr++;
639
    }
640
#endif
641
 
642
    return( name );
643
}
644
 
645
 
646
/*========================================================================
647
 *
648
 *  Print to a temp string buffer
649
 *
650
 *  Purpose:
651
 *      This function is similar to sprintf() except that the routine
652
 *      maintains a list of string buffers and will return one to the
653
 *      user.
654
 *
655
 *      This function allows a user to create small, short lived
656
 *      printed strings, without the memory managment overhead.
657
 *
658
 *      Down-side. Use the string quickly.
659
 *                 Strings have a limited length
660
 *
661
 *  Parameters:
662
 *      Standard printf type parameters
663
 *
664
 *  Returns:
665
 *      TRUE : Operation was succesful
666
 *
667
 *========================================================================*/
668
 
669
#define TBUF_COUNT  5               /* Number of buffers */
670
#define TBUF_LENGTH 100             /* Max length of a single print */
671
 
672
char *tprintf( char *format, ... )
673
{
674
    static char tbuf[TBUF_COUNT][TBUF_LENGTH];
675
    static int  index = 0;
676
 
677
    va_list     ap;
678
    char       *pp;
679
 
680
    /*
681
    **  Get the next entry from the small store
682
    */
683
    index++;
684
    if( index >= TBUF_COUNT )
685
        index = 0;
686
    pp = tbuf[index];
687
 
688
    va_start( ap, format );
689
    vsprintf( pp, format, ap );
690
    va_end( ap );
691
 
692
    return ( pp );
693
}
694
 
695
/********************************* EOF ***********************************/