Subversion Repositories svn1-original

Rev

Rev 46 | Rev 54 | Go to most recent revision | 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
 *
51 - 276
 *  Print to to the printer
277
 *
278
 *  Purpose:
279
 *      This function is called to do print data on the printer
280
 *
281
 *  Assume:
282
 *      One CSV field per print
283
 *      New lines are at the end of a line printed. This does allow for
284
 *      a single new-line.
285
 *
286
 *
287
 *  Parameters:
288
 *      Standard printf type parameters
289
 *
290
 *  Returns:
291
 *      TRUE : Operation was succesful
292
 *
293
 *========================================================================*/
294
 
295
int csv_print( char *format, ... )
296
{
297
    va_list     ap;
298
    char        pp[200];
299
    int         len;
300
    bool        eol = FALSE;
301
 
302
 
303
    va_start( ap, format );
304
    len = vsprintf( pp, format, ap );
305
    va_end( ap );
306
 
307
    /*
308
    **  Detect the end of a line and flag for later processing
309
    */
310
    if ( len > 0 && pp[len - 1] == '\n' )
311
    {
312
        len--;
313
        eol = TRUE;
314
    }
315
 
316
    if ( ! eol )
317
    {
318
        if ( print_col )
319
            fwrite( ",", 1, 1, pfile );
320
        fwrite( "\"", 1, 1, pfile );
321
        fwrite( pp, 1, len, pfile );
322
        print_col += len;
323
        fwrite( "\"", 1, 1, pfile );
324
    }
325
 
326
    /*
327
    **  Perform End of Line operation before printing the final newline
328
    */
329
    if ( eol )
330
    {
331
        print_line++;
332
        print_col = 0;
333
 
334
        /*
335
        **  Now print the final newline
336
        */
337
        fwrite( "\n", 1, 1, pfile );
338
        len ++;
339
    }
340
 
341
    return ( len );
342
}
343
 
344
 
345
/*========================================================================
346
 *
1 root 347
 *  Print to to the printer without any frills
348
 *
349
 *  Purpose:
350
 *      This function is called to do print data on the printer
351
 *
352
 *  Parameters:
353
 *      Standard printf type parameters
354
 *
355
 *  Returns:
356
 *      TRUE : Operation was succesful
357
 *
358
 *========================================================================*/
359
 
360
int raw_print( char *format, ... )
361
{
362
    va_list     ap;
363
    char        pp[200];
364
    int         len;
365
 
366
 
367
    va_start( ap, format );
368
    len = vsprintf( pp, format, ap );
369
    va_end( ap );
370
 
371
    fwrite( pp, 1, len, pfile );
372
 
373
    return ( len );
374
}
375
 
376
 
377
/*========================================================================
378
 *
379
 *  Control bolding
380
 *
381
 *  Purpose:
382
 *      This function is called to turn bolding on and off
383
 *      This function will ONLY affect HTML printing
384
 *
385
 *  Parameters:
386
 *      on              - TRUE
387
 *
388
 *  Returns:
389
 *      Nothing
390
 *
391
 *========================================================================*/
392
 
393
void print_bold( bool on )
394
{
395
    if ( print_html )
396
    {
397
        if ( on )
398
            raw_print( "<B>" );
399
        else
400
            raw_print( "</B>" );
401
    }
402
}
403
 
404
/*========================================================================
405
 *
406
 *  Control underline
407
 *
408
 *  Purpose:
409
 *      This function is called to turn underline on and off
410
 *      This function will ONLY affect HTML printing and Non-HTML printing
411
 *      But in a different manner
412
 *
413
 *  Parameters:
414
 *      on              - TRUE
415
 *
416
 *  Returns:
417
 *      Nothing
418
 *
419
 *========================================================================*/
420
 
421
void print_underline( bool on )
422
{
423
    if ( print_html )
424
    {
425
        /*
426
        **  For HTML printing underline is simple
427
        */
428
        if ( on )
429
        {
430
            raw_print( "<U>" );
431
            print_underline_start = 1;
432
        }
433
        else if ( print_underline_start > 0 )
434
        {
435
            raw_print( "</U>" );
436
            print_underline_start = 0;
437
        }
438
    }
439
    else
440
    {
441
        /*
442
        **  Non-HTML printing
443
        **  Store underline start and stop column
444
        */
445
        if ( on )
446
            print_underline_start = print_col;
447
        else if ( print_underline_start >= 0 )
448
            print_underline_end = print_col;
449
    }
450
}
451
 
452
/*========================================================================
453
 *
454
 *  Control colour - HTML printing only
455
 *
456
 *  Purpose:
457
 *      This function is called to change the background colour within
458
 *      HTML text
459
 *
460
 *  Parameters:
461
 *      colour      - Colour control string
462
 *                    or NULL to trun colour OFF
463
 *
464
 *
465
 *  Returns:
466
 *      Nothing
467
 *
468
 *========================================================================*/
469
 
470
void print_colour( long colour )
471
{
472
    if ( print_html )
473
    {
474
        if ( print_current_colour )
475
        {
476
            raw_print( "</SPAN>" );
477
            print_current_colour = 0L;
478
        }
479
 
480
        if ( colour )
481
        {
482
            print_current_colour = colour;
483
            raw_print( "<SPAN STYLE=\"background: #%6.6lx\">", colour );
484
        }
485
    }
486
}
487
 
488
 
489
/*========================================================================
490
 *
491
 *  Print a page banner
492
 *
493
 *  Purpose:
494
 *      This function is called to print a page banner
495
 *
496
 *  Parameters:
497
 *      None
498
 *
499
 *  Returns:
500
 *      Nothing
501
 *
502
 *========================================================================*/
503
 
504
void printbanner( char *title )
505
{
506
    int         l, s;
507
 
51 - 508
    if ( !title )
509
        return;
510
 
1 root 511
    l = strlen( config.event_name );
512
    s = ( print_width - l ) / 2;
513
    print( "%*s", s, "" );
514
 
515
    if ( print_html )
516
        print( "<A HREF=\"%s\">", p_filename(filebase, "index" ,"html"));
517
    print_underline( TRUE);
518
    print_bold( TRUE );
519
 
520
    print( "%s", config.event_name );
521
 
522
    print_bold( FALSE );
523
    print_underline( FALSE );
524
    if ( print_html )
525
        print( "</A>" );
526
 
527
    print( "\n" );
528
 
529
    /*
530
    **  Print out one line with the report title
531
    **  on the left and the report time on the right
532
    */
533
    if ( title )
534
        print( "%s", title );
535
 
536
    s = print_width - print_col - 24 - 4;
537
    print( "%*s", s , "" );
538
 
539
    print( "%.24s", ctime( &pri_time ) );
540
    print( "\n" );
541
    print( "\n" );
542
}
543
 
544
 
545
/*========================================================================
546
 *
547
 *  Format a filename
548
 *
549
 *  Purpose:
550
 *      This function is called create a suitably formatted filename
551
 *      for use in an HTML tag or by the file system
552
 *
553
 *      Currently all output filenames are created in uppercase
554
 *      This is a limitation of DOS
555
 *      This function is used to ensure that all files are created with
556
 *      an uppercase name and that all HTML file references are also
557
 *      in uppercase.
558
 *
559
 *
560
 *  Parameters:
561
 *      filename        - Base filename
562
 *      Suffix          - Optional part of the basename
563
 *                        If present is used to create part of the basename
564
 *      ext             - Extension
565
 *
566
 *  Returns:
567
 *      Address of a static string that will reference the file
568
 *
569
 *========================================================================*/
570
 
571
char * p_filename( char *filename, char *suffix, char *ext )
572
{
573
    char    *name;
574
    char    *cptr;
575
 
576
#ifndef LONG_FILE_NAMES
577
    /*
578
    **  Limit the filename to 8.3 format
579
    **  This is a limitation of the underlying file access library
580
    **  and may be removed in the future
581
    */
582
    if ( suffix[0] )
583
        name = tprintf ( "%.4s_%.3s.%.3s", filename, suffix, ext );
584
    else
585
        name = tprintf ( "%.8s.%.3s", filename, ext );
586
 
587
 
588
    /*
589
    **  Uppercase the filename
590
    */
591
    cptr = name;
592
    while ( *cptr )
593
    {
594
        *cptr = toupper( *cptr );
595
        cptr++;
596
    }
597
#else
598
    /*
599
    **  This compiler and runtime library supports
600
    **  long filenames - created printed filenames in lower case
601
    */
602
    if ( suffix[0] )
603
        name = tprintf ( "%s_%s.%s", filename, suffix, ext );
604
    else
605
        name = tprintf ( "%s_%s.%s", filename, ext, "txt" );
606
 
607
    cptr = name;
608
    while ( *cptr )
609
    {
610
        *cptr = tolower( *cptr );
611
        cptr++;
612
    }
613
#endif
614
 
615
    return( name );
616
}
617
 
618
 
619
/*========================================================================
620
 *
621
 *  Print to a temp string buffer
622
 *
623
 *  Purpose:
624
 *      This function is similar to sprintf() except that the routine
625
 *      maintains a list of string buffers and will return one to the
626
 *      user.
627
 *
628
 *      This function allows a user to create small, short lived
629
 *      printed strings, without the memory managment overhead.
630
 *
631
 *      Down-side. Use the string quickly.
632
 *                 Strings have a limited length
633
 *
634
 *  Parameters:
635
 *      Standard printf type parameters
636
 *
637
 *  Returns:
638
 *      TRUE : Operation was succesful
639
 *
640
 *========================================================================*/
641
 
642
#define TBUF_COUNT  5               /* Number of buffers */
643
#define TBUF_LENGTH 100             /* Max length of a single print */
644
 
645
char *tprintf( char *format, ... )
646
{
647
    static char tbuf[TBUF_COUNT][TBUF_LENGTH];
648
    static int  index = 0;
649
 
650
    va_list     ap;
651
    char       *pp;
652
 
653
    /*
654
    **  Get the next entry from the small store
655
    */
656
    index++;
657
    if( index >= TBUF_COUNT )
658
        index = 0;
659
    pp = tbuf[index];
660
 
661
    va_start( ap, format );
662
    vsprintf( pp, format, ap );
663
    va_end( ap );
664
 
665
    return ( pp );
666
}
667
 
668
/********************************* EOF ***********************************/