Subversion Repositories svn1-original

Rev

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