]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/langprintf.c
c6154f6fd52d10e625f132c45ba7ea6c17d6cb02
[thirdparty/cups.git] / cups / langprintf.c
1 /*
2 * "$Id$"
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\n", prefix ? prefix : "",
76 _cupsLangString(cg->lang_default, message), strerror(last_errno));
77
78 /*
79 * Convert and write to stderr...
80 */
81
82 bytes = cupsUTF8ToCharset(output, (cups_utf8_t *)buffer, sizeof(output),
83 cg->lang_default->encoding);
84
85 if (bytes > 0)
86 fwrite(output, 1, bytes, stderr);
87 }
88
89
90 /*
91 * '_cupsLangPrintFilter()' - Print a formatted filter message string to a file.
92 */
93
94 int /* O - Number of bytes written */
95 _cupsLangPrintFilter(
96 FILE *fp, /* I - File to write to */
97 const char *prefix, /* I - Non-localized message prefix */
98 const char *message, /* I - Message string to use */
99 ...) /* I - Additional arguments as needed */
100 {
101 int bytes; /* Number of bytes formatted */
102 char temp[2048], /* Temporary format buffer */
103 buffer[2048], /* Message buffer */
104 output[8192]; /* Output buffer */
105 va_list ap; /* Pointer to additional arguments */
106 _cups_globals_t *cg; /* Global data */
107
108
109 /*
110 * Range check...
111 */
112
113 if (!fp || !message)
114 return (-1);
115
116 cg = _cupsGlobals();
117
118 if (!cg->lang_default)
119 cg->lang_default = cupsLangDefault();
120
121 /*
122 * Format the string...
123 */
124
125 va_start(ap, message);
126 snprintf(temp, sizeof(temp), "%s: %s\n", prefix,
127 _cupsLangString(cg->lang_default, message));
128 vsnprintf(buffer, sizeof(buffer), temp, ap);
129 va_end(ap);
130
131 /*
132 * Transcode to the destination charset...
133 */
134
135 bytes = cupsUTF8ToCharset(output, (cups_utf8_t *)buffer, sizeof(output),
136 cg->lang_default->encoding);
137
138 /*
139 * Write the string and return the number of bytes written...
140 */
141
142 if (bytes > 0)
143 return ((int)fwrite(output, 1, bytes, fp));
144 else
145 return (bytes);
146 }
147
148
149 /*
150 * '_cupsLangPrintf()' - Print a formatted message string to a file.
151 */
152
153 int /* O - Number of bytes written */
154 _cupsLangPrintf(FILE *fp, /* I - File to write to */
155 const char *message, /* I - Message string to use */
156 ...) /* I - Additional arguments as needed */
157 {
158 int bytes; /* Number of bytes formatted */
159 char buffer[2048], /* Message buffer */
160 output[8192]; /* Output buffer */
161 va_list ap; /* Pointer to additional arguments */
162 _cups_globals_t *cg; /* Global data */
163
164
165 /*
166 * Range check...
167 */
168
169 if (!fp || !message)
170 return (-1);
171
172 cg = _cupsGlobals();
173
174 if (!cg->lang_default)
175 cg->lang_default = cupsLangDefault();
176
177 /*
178 * Format the string...
179 */
180
181 va_start(ap, message);
182 vsnprintf(buffer, sizeof(buffer),
183 _cupsLangString(cg->lang_default, message), ap);
184 va_end(ap);
185
186 /*
187 * Transcode to the destination charset...
188 */
189
190 bytes = cupsUTF8ToCharset(output, (cups_utf8_t *)buffer, sizeof(output),
191 cg->lang_default->encoding);
192
193 /*
194 * Write the string and return the number of bytes written...
195 */
196
197 if (bytes > 0)
198 return ((int)fwrite(output, 1, bytes, fp));
199 else
200 return (bytes);
201 }
202
203
204 /*
205 * '_cupsLangPuts()' - Print a static message string to a file.
206 */
207
208 int /* O - Number of bytes written */
209 _cupsLangPuts(FILE *fp, /* I - File to write to */
210 const char *message) /* I - Message string to use */
211 {
212 int bytes; /* Number of bytes formatted */
213 char output[8192]; /* Message buffer */
214 _cups_globals_t *cg; /* Global data */
215
216
217 /*
218 * Range check...
219 */
220
221 if (!fp || !message)
222 return (-1);
223
224 cg = _cupsGlobals();
225
226 if (!cg->lang_default)
227 cg->lang_default = cupsLangDefault();
228
229 /*
230 * Transcode to the destination charset...
231 */
232
233 bytes = cupsUTF8ToCharset(output,
234 (cups_utf8_t *)_cupsLangString(cg->lang_default,
235 message),
236 sizeof(output), cg->lang_default->encoding);
237
238 /*
239 * Write the string and return the number of bytes written...
240 */
241
242 if (bytes > 0)
243 return ((int)fwrite(output, 1, bytes, fp));
244 else
245 return (bytes);
246 }
247
248
249 /*
250 * '_cupsSetLocale()' - Set the current locale and transcode the command-line.
251 */
252
253 void
254 _cupsSetLocale(char *argv[]) /* IO - Command-line arguments */
255 {
256 int i; /* Looping var */
257 char buffer[8192]; /* Command-line argument buffer */
258 _cups_globals_t *cg; /* Global data */
259 #ifdef LC_TIME
260 const char *lc_time; /* Current LC_TIME value */
261 char new_lc_time[255], /* New LC_TIME value */
262 *charset; /* Pointer to character set */
263 #endif /* LC_TIME */
264
265
266 /*
267 * Set the locale so that times, etc. are displayed properly.
268 *
269 * Unfortunately, while we need the localized time value, we *don't*
270 * want to use the localized charset for the time value, so we need
271 * to set LC_TIME to the locale name with .UTF-8 on the end (if
272 * the locale includes a character set specifier...)
273 */
274
275 setlocale(LC_ALL, "");
276
277 #ifdef LC_TIME
278 if ((lc_time = setlocale(LC_TIME, NULL)) == NULL)
279 lc_time = setlocale(LC_ALL, NULL);
280
281 if (lc_time)
282 {
283 strlcpy(new_lc_time, lc_time, sizeof(new_lc_time));
284 if ((charset = strchr(new_lc_time, '.')) == NULL)
285 charset = new_lc_time + strlen(new_lc_time);
286
287 strlcpy(charset, ".UTF-8", sizeof(new_lc_time) - (charset - new_lc_time));
288 }
289 else
290 strcpy(new_lc_time, "C");
291
292 setlocale(LC_TIME, new_lc_time);
293 #endif /* LC_TIME */
294
295 /*
296 * Initialize the default language info...
297 */
298
299 cg = _cupsGlobals();
300
301 if (!cg->lang_default)
302 cg->lang_default = cupsLangDefault();
303
304 /*
305 * Transcode the command-line arguments from the locale charset to
306 * UTF-8...
307 */
308
309 if (cg->lang_default->encoding != CUPS_US_ASCII &&
310 cg->lang_default->encoding != CUPS_UTF8)
311 {
312 for (i = 1; argv[i]; i ++)
313 {
314 /*
315 * Try converting from the locale charset to UTF-8...
316 */
317
318 if (cupsCharsetToUTF8((cups_utf8_t *)buffer, argv[i], sizeof(buffer),
319 cg->lang_default->encoding) < 0)
320 continue;
321
322 /*
323 * Save the new string if it differs from the original...
324 */
325
326 if (strcmp(buffer, argv[i]))
327 argv[i] = strdup(buffer);
328 }
329 }
330 }
331
332
333 /*
334 * End of "$Id$".
335 */