• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2017 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 *
6 *   Copyright (C) 2003, International Business Machines
7 *   Corporation and others.  All Rights Reserved.
8 *
9 *******************************************************************************
10 *
11 * File uprinter.cpp
12 *
13 * Modification History:
14 *
15 *   Date        Name        Description
16 *   03/18/2003  weiv        Creation.
17 *******************************************************************************
18 */
19 
20 #include "uprinter.h"
21 
UPrinter(FILE * file,const char * locale,const char * encoding,UBool transliterateNonPrintable)22 UPrinter::UPrinter(FILE *file, const char *locale, const char *encoding, UBool transliterateNonPrintable) {
23   _on = true;
24   out = u_finit(file, locale, encoding);
25   strcpy(_locale, locale);
26   if(transliterateNonPrintable) {
27     UErrorCode status = U_ZERO_ERROR;
28     UTransliterator *anyHex = utrans_open("[^\\u000d\\u000a\\u0009\\u0020-\\u007f] Any-Hex/Java", UTRANS_FORWARD, NULL, 0, NULL, &status);
29     u_fsettransliterator(out, U_WRITE, anyHex, &status);
30   }
31 };
32 
UPrinter(const char * name,const char * locale,const char * encoding,UTransliterator * trans,UBool transliterateNonPrintable)33 UPrinter::UPrinter(const char *name, const char *locale, const char *encoding, UTransliterator *trans, UBool transliterateNonPrintable) {
34   _on = true;
35   out = u_fopen(name, "wb", locale, encoding);
36   u_fputc(0xFEFF, out); // emit a BOM
37   strcpy(_locale, locale);
38   if(transliterateNonPrintable) {
39     UErrorCode status = U_ZERO_ERROR;
40     if(trans == NULL) {
41       UTransliterator *anyHex = utrans_open("[^\\u000d\\u000a\\u0009\\u0020-\\u007f] Any-Hex/Java", UTRANS_FORWARD, NULL, 0, NULL, &status);
42       u_fsettransliterator(out, U_WRITE, anyHex, &status);
43     } else {
44       u_fsettransliterator(out, U_WRITE, trans, &status);
45     }
46   }
47 };
48 
~UPrinter()49 UPrinter::~UPrinter() {
50   u_fclose(out);
51 }
52 
53 void
log(const UnicodeString & string,UBool nl)54 UPrinter::log(const UnicodeString &string, UBool nl) {
55   if(_on) {
56     log(((UnicodeString)string).getTerminatedBuffer(), nl);
57   }
58 }
59 
60 void
log(const UChar * string,UBool nl)61 UPrinter::log(const UChar *string, UBool nl) {
62   if(_on) {
63     u_fprintf(out, "%S", string);
64     if(nl) {
65       u_fprintf(out, "\n");
66     }
67     u_fflush(out);
68   }
69 }
70 /*
71 void
72 UPrinter::log(const char *string, UBool nl) {
73   if(_on) {
74     u_fprintf(out, "%s", string);
75     if(nl) {
76       u_fprintf(out, "\n");
77     }
78   }
79 }
80 */
81 void
log(const Line * line,UBool nl)82 UPrinter::log(const Line *line, UBool nl) {
83   if(_on) {
84     log(line->name);
85     if(line->expLen) {
86       log("/");
87       log(line->expansionString);
88     }
89     if(nl) {
90       u_fprintf(out, "\n");
91       u_fflush(out);
92     }
93   }
94 }
95 
log(const char * fmt,...)96 void UPrinter::log(const char *fmt, ...)
97 {
98     UChar buffer[4000];
99     va_list ap;
100 
101     va_start(ap, fmt);
102     /* sprintf it just to make sure that the information is valid */
103     u_vsprintf(buffer, _locale, fmt, ap);
104     va_end(ap);
105     if( _on ) {
106         log(buffer);
107     }
108 }
109 
110 void
on(void)111 UPrinter::on(void) {
112   _on = true;
113 }
114 
115 void
off(void)116 UPrinter::off(void) {
117   _on = false;
118 }
119