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