Subversion Repositories svn1-original

Rev

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