]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/langprintf.c
Merge changes from CUPS 1.4svn-r7715.
[thirdparty/cups.git] / cups / langprintf.c
1 /*
2 * "$Id: langprintf.c 7193 2008-01-07 23:01:40Z mike $"
3 *
4 * Localized printf/puts functions for the Common UNIX Printing
5 * System (CUPS).
6 *
7 * Copyright 2007-2008 by Apple Inc.
8 * Copyright 2002-2007 by Easy Software Products.
9 *
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * file is missing or damaged, see the license at "http://www.cups.org/".
15 *
16 * This file is subject to the Apple OS-Developed Software exception.
17 *
18 * Contents:
19 *
20 * _cupsLangPrintf() - Print a formatted message string to a file.
21 * _cupsLangPuts() - Print a static message string to a file.
22 * _cupsSetLocale() - Set the current locale and transcode the command-line.
23 */
24
25 /*
26 * Include necessary headers...
27 */
28
29 #include <stdio.h>
30 #include "globals.h"
31 #include <errno.h>
32
33
34 /*
35 * '_cupsLangPrintError()' - Print a message followed by a standard error.
36 */
37
38 void
39 _cupsLangPrintError(const char *message)/* I - Message */
40 {
41 int bytes; /* Number of bytes formatted */
42 int last_errno; /* Last error */
43 char buffer[2048], /* Message buffer */
44 output[8192]; /* Output buffer */
45 _cups_globals_t *cg; /* Global data */
46
47
48 /*
49 * Range check...
50 */
51
52 if (!message)
53 return;
54
55 /*
56 * Save the errno value...
57 */
58
59 last_errno = errno;
60
61 /*
62 * Get the message catalog...
63 */
64
65 cg = _cupsGlobals();
66
67 if (!cg->lang_default)
68 cg->lang_default = cupsLangDefault();
69
70 /*
71 * Format the message...
72 */
73
74 snprintf(buffer, sizeof(buffer), "%s: %s",
75 _cupsLangString(cg->lang_default, message), strerror(last_errno));
76
77 /*
78 * Convert and write to stderr...
79 */
80
81 bytes = cupsUTF8ToCharset(output, (cups_utf8_t *)buffer, sizeof(output),
82 cg->lang_default->encoding);
83
84 if (bytes > 0)
85 fwrite(output, 1, bytes, stderr);
86 }
87
88
89 /*
90 * '_cupsLangPrintf()' - Print a formatted message string to a file.
91 */
92
93 int /* O - Number of bytes written */
94 _cupsLangPrintf(FILE *fp, /* I - File to write to */
95 const char *message, /* I - Message string to use */
96 ...) /* I - Additional arguments as needed */
97 {
98 int bytes; /* Number of bytes formatted */
99 char buffer[2048], /* Message buffer */
100 output[8192]; /* Output buffer */
101 va_list ap; /* Pointer to additional arguments */
102 _cups_globals_t *cg; /* Global data */
103
104
105 /*
106 * Range check...
107 */
108
109 if (!fp || !message)
110 return (-1);
111
112 cg = _cupsGlobals();
113
114 if (!cg->lang_default)
115 cg->lang_default = cupsLangDefault();
116
117 /*
118 * Format the string...
119 */
120
121 va_start(ap, message);
122 vsnprintf(buffer, sizeof(buffer),
123 _cupsLangString(cg->lang_default, message), ap);
124 va_end(ap);
125
126 /*
127 * Transcode to the destination charset...
128 */
129
130 bytes = cupsUTF8ToCharset(output, (cups_utf8_t *)buffer, sizeof(output),
131 cg->lang_default->encoding);
132
133 /*
134 * Write the string and return the number of bytes written...
135 */
136
137 if (bytes > 0)
138 return ((int)fwrite(output, 1, bytes, fp));
139 else
140 return (bytes);
141 }
142
143
144 /*
145 * '_cupsLangPuts()' - Print a static message string to a file.
146 */
147
148 int /* O - Number of bytes written */
149 _cupsLangPuts(FILE *fp, /* I - File to write to */
150 const char *message) /* I - Message string to use */
151 {
152 int bytes; /* Number of bytes formatted */
153 char output[2048]; /* Message buffer */
154 _cups_globals_t *cg; /* Global data */
155
156
157 /*
158 * Range check...
159 */
160
161 if (!fp || !message)
162 return (-1);
163
164 cg = _cupsGlobals();
165
166 if (!cg->lang_default)
167 cg->lang_default = cupsLangDefault();
168
169 /*
170 * Transcode to the destination charset...
171 */
172
173 bytes = cupsUTF8ToCharset(output,
174 (cups_utf8_t *)_cupsLangString(cg->lang_default,
175 message),
176 sizeof(output), cg->lang_default->encoding);
177
178 /*
179 * Write the string and return the number of bytes written...
180 */
181
182 if (bytes > 0)
183 return ((int)fwrite(output, 1, bytes, fp));
184 else
185 return (bytes);
186 }
187
188
189 /*
190 * '_cupsSetLocale()' - Set the current locale and transcode the command-line.
191 */
192
193 void
194 _cupsSetLocale(char *argv[]) /* IO - Command-line arguments */
195 {
196 int i; /* Looping var */
197 char buffer[8192]; /* Command-line argument buffer */
198 _cups_globals_t *cg; /* Global data */
199 #ifdef LC_TIME
200 const char *lc_time; /* Current LC_TIME value */
201 char new_lc_time[255], /* New LC_TIME value */
202 *charset; /* Pointer to character set */
203 #endif /* LC_TIME */
204
205
206 /*
207 * Set the locale so that times, etc. are displayed properly.
208 *
209 * Unfortunately, while we need the localized time value, we *don't*
210 * want to use the localized charset for the time value, so we need
211 * to set LC_TIME to the locale name with .UTF-8 on the end (if
212 * the locale includes a character set specifier...)
213 */
214
215 setlocale(LC_ALL, "");
216
217 #ifdef LC_TIME
218 if ((lc_time = setlocale(LC_TIME, NULL)) == NULL)
219 lc_time = setlocale(LC_ALL, NULL);
220
221 if (lc_time)
222 {
223 strlcpy(new_lc_time, lc_time, sizeof(new_lc_time));
224 if ((charset = strchr(new_lc_time, '.')) == NULL)
225 charset = new_lc_time + strlen(new_lc_time);
226
227 strlcpy(charset, ".UTF-8", sizeof(new_lc_time) - (charset - new_lc_time));
228 }
229 else
230 strcpy(new_lc_time, "C");
231
232 setlocale(LC_TIME, new_lc_time);
233 #endif /* LC_TIME */
234
235 /*
236 * Initialize the default language info...
237 */
238
239 cg = _cupsGlobals();
240
241 if (!cg->lang_default)
242 cg->lang_default = cupsLangDefault();
243
244 /*
245 * Transcode the command-line arguments from the locale charset to
246 * UTF-8...
247 */
248
249 if (cg->lang_default->encoding != CUPS_US_ASCII &&
250 cg->lang_default->encoding != CUPS_UTF8)
251 {
252 for (i = 1; argv[i]; i ++)
253 {
254 /*
255 * Try converting from the locale charset to UTF-8...
256 */
257
258 if (cupsCharsetToUTF8((cups_utf8_t *)buffer, argv[i], sizeof(buffer),
259 cg->lang_default->encoding) < 0)
260 continue;
261
262 /*
263 * Save the new string if it differs from the original...
264 */
265
266 if (strcmp(buffer, argv[i]))
267 argv[i] = strdup(buffer);
268 }
269 }
270 }
271
272
273 /*
274 * End of "$Id: langprintf.c 7193 2008-01-07 23:01:40Z mike $".
275 */