r26316: Use contexts for conversion functions.
[ira/wip.git] / source4 / lib / util / dprintf.c
1 /* 
2    Unix SMB/CIFS implementation.
3    display print functions
4    Copyright (C) Andrew Tridgell 2001
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20
21 /*
22   this module provides functions for printing internal strings in the 
23   "display charset".
24   
25   This charset may be quite different from the chosen unix charset.
26
27   Eventually these functions will need to take care of column count constraints
28
29   The d_ prefix on print functions in Samba refers to the display character set
30   conversion
31 */
32
33 #include "includes.h"
34 #include "system/locale.h"
35
36 _PUBLIC_ int d_vfprintf(FILE *f, const char *format, va_list ap) _PRINTF_ATTRIBUTE(2,0)
37 {
38         char *p, *p2;
39         int ret, maxlen, clen;
40         va_list ap2;
41
42         /* do any message translations */
43         va_copy(ap2, ap);
44         ret = vasprintf(&p, format, ap2);
45         va_end(ap2);
46
47         if (ret <= 0) return ret;
48
49         /* now we have the string in unix format, convert it to the display
50            charset, but beware of it growing */
51         maxlen = ret*2;
52 again:
53         p2 = (char *)malloc(maxlen);
54         if (!p2) {
55                 SAFE_FREE(p);
56                 return -1;
57         }
58         clen = convert_string(global_smb_iconv_convenience, CH_UNIX, CH_DISPLAY, p, ret, p2, maxlen);
59         if (clen == -1) {
60                 /* the string can't be converted - do the best we can,
61                    filling in non-printing chars with '?' */
62                 int i;
63                 for (i=0;i<ret;i++) {
64                         if (isprint(p[i]) || isspace(p[i])) {
65                                 fwrite(p+i, 1, 1, f);
66                         } else {
67                                 fwrite("?", 1, 1, f);
68                         }
69                 }
70                 SAFE_FREE(p);
71                 SAFE_FREE(p2);
72                 return ret;
73         }
74
75
76         if (clen >= maxlen) {
77                 /* it didn't fit - try a larger buffer */
78                 maxlen *= 2;
79                 SAFE_FREE(p2);
80                 goto again;
81         }
82
83         /* good, its converted OK */
84         SAFE_FREE(p);
85         ret = fwrite(p2, 1, clen, f);
86         SAFE_FREE(p2);
87
88         return ret;
89 }
90
91
92 _PUBLIC_ int d_fprintf(FILE *f, const char *format, ...) _PRINTF_ATTRIBUTE(2,3)
93 {
94         int ret;
95         va_list ap;
96
97         va_start(ap, format);
98         ret = d_vfprintf(f, format, ap);
99         va_end(ap);
100
101         return ret;
102 }
103
104 _PUBLIC_ int d_printf(const char *format, ...) _PRINTF_ATTRIBUTE(1,2)
105 {
106         int ret;
107         va_list ap;
108
109         va_start(ap, format);
110         ret = d_vfprintf(stdout, format, ap);
111         va_end(ap);
112
113         return ret;
114 }