2 * "$Id: langprintf.c 9637 2011-03-21 23:03:22Z mike $"
4 * Localized printf/puts functions for CUPS.
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 2002-2007 by Easy Software Products.
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
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
28 * Include necessary headers...
31 #include "cups-private.h"
35 * '_cupsLangPrintError()' - Print a message followed by a standard error.
39 _cupsLangPrintError(const char *prefix, /* I - Non-localized message prefix */
40 const char *message)/* I - Message */
42 int bytes; /* Number of bytes formatted */
43 int last_errno; /* Last error */
44 char buffer[2048], /* Message buffer */
45 *bufptr, /* Pointer into buffer */
46 output[8192]; /* Output buffer */
47 _cups_globals_t *cg; /* Global data */
58 * Save the errno value...
64 * Get the message catalog...
69 if (!cg->lang_default)
70 cg->lang_default = cupsLangDefault();
73 * Format the message...
78 snprintf(buffer, sizeof(buffer), "%s:", prefix);
79 bufptr = buffer + strlen(buffer);
84 snprintf(bufptr, sizeof(buffer) - (bufptr - buffer),
85 /* TRANSLATORS: Message is "subject: error" */
86 _cupsLangString(cg->lang_default, _("%s: %s")),
87 _cupsLangString(cg->lang_default, message), strerror(last_errno));
88 strlcat(buffer, "\n", sizeof(buffer));
91 * Convert and write to stderr...
94 bytes = cupsUTF8ToCharset(output, (cups_utf8_t *)buffer, sizeof(output),
95 cg->lang_default->encoding);
98 fwrite(output, 1, bytes, stderr);
103 * '_cupsLangPrintFilter()' - Print a formatted filter message string to a file.
106 int /* O - Number of bytes written */
107 _cupsLangPrintFilter(
108 FILE *fp, /* I - File to write to */
109 const char *prefix, /* I - Non-localized message prefix */
110 const char *message, /* I - Message string to use */
111 ...) /* I - Additional arguments as needed */
113 int bytes; /* Number of bytes formatted */
114 char temp[2048], /* Temporary format buffer */
115 buffer[2048], /* Message buffer */
116 output[8192]; /* Output buffer */
117 va_list ap; /* Pointer to additional arguments */
118 _cups_globals_t *cg; /* Global data */
130 if (!cg->lang_default)
131 cg->lang_default = cupsLangDefault();
134 * Format the string...
137 va_start(ap, message);
138 snprintf(temp, sizeof(temp), "%s: %s\n", prefix,
139 _cupsLangString(cg->lang_default, message));
140 vsnprintf(buffer, sizeof(buffer), temp, ap);
144 * Transcode to the destination charset...
147 bytes = cupsUTF8ToCharset(output, (cups_utf8_t *)buffer, sizeof(output),
148 cg->lang_default->encoding);
151 * Write the string and return the number of bytes written...
155 return ((int)fwrite(output, 1, bytes, fp));
162 * '_cupsLangPrintf()' - Print a formatted message string to a file.
165 int /* O - Number of bytes written */
166 _cupsLangPrintf(FILE *fp, /* I - File to write to */
167 const char *message, /* I - Message string to use */
168 ...) /* I - Additional arguments as needed */
170 int bytes; /* Number of bytes formatted */
171 char buffer[2048], /* Message buffer */
172 output[8192]; /* Output buffer */
173 va_list ap; /* Pointer to additional arguments */
174 _cups_globals_t *cg; /* Global data */
186 if (!cg->lang_default)
187 cg->lang_default = cupsLangDefault();
190 * Format the string...
193 va_start(ap, message);
194 vsnprintf(buffer, sizeof(buffer) - 1,
195 _cupsLangString(cg->lang_default, message), ap);
198 strlcat(buffer, "\n", sizeof(buffer));
201 * Transcode to the destination charset...
204 bytes = cupsUTF8ToCharset(output, (cups_utf8_t *)buffer, sizeof(output),
205 cg->lang_default->encoding);
208 * Write the string and return the number of bytes written...
212 return ((int)fwrite(output, 1, bytes, fp));
219 * '_cupsLangPuts()' - Print a static message string to a file.
222 int /* O - Number of bytes written */
223 _cupsLangPuts(FILE *fp, /* I - File to write to */
224 const char *message) /* I - Message string to use */
226 int bytes; /* Number of bytes formatted */
227 char output[8192]; /* Message buffer */
228 _cups_globals_t *cg; /* Global data */
240 if (!cg->lang_default)
241 cg->lang_default = cupsLangDefault();
244 * Transcode to the destination charset...
247 bytes = cupsUTF8ToCharset(output,
248 (cups_utf8_t *)_cupsLangString(cg->lang_default,
250 sizeof(output) - 4, cg->lang_default->encoding);
251 bytes += cupsUTF8ToCharset(output + bytes, (cups_utf8_t *)"\n",
252 sizeof(output) - bytes,
253 cg->lang_default->encoding);
256 * Write the string and return the number of bytes written...
260 return ((int)fwrite(output, 1, bytes, fp));
267 * '_cupsSetLocale()' - Set the current locale and transcode the command-line.
271 _cupsSetLocale(char *argv[]) /* IO - Command-line arguments */
273 int i; /* Looping var */
274 char buffer[8192]; /* Command-line argument buffer */
275 _cups_globals_t *cg; /* Global data */
277 const char *lc_time; /* Current LC_TIME value */
278 char new_lc_time[255], /* New LC_TIME value */
279 *charset; /* Pointer to character set */
284 * Set the locale so that times, etc. are displayed properly.
286 * Unfortunately, while we need the localized time value, we *don't*
287 * want to use the localized charset for the time value, so we need
288 * to set LC_TIME to the locale name with .UTF-8 on the end (if
289 * the locale includes a character set specifier...)
292 setlocale(LC_ALL, "");
295 if ((lc_time = setlocale(LC_TIME, NULL)) == NULL)
296 lc_time = setlocale(LC_ALL, NULL);
300 strlcpy(new_lc_time, lc_time, sizeof(new_lc_time));
301 if ((charset = strchr(new_lc_time, '.')) == NULL)
302 charset = new_lc_time + strlen(new_lc_time);
304 strlcpy(charset, ".UTF-8", sizeof(new_lc_time) - (charset - new_lc_time));
307 strcpy(new_lc_time, "C");
309 setlocale(LC_TIME, new_lc_time);
313 * Initialize the default language info...
318 if (!cg->lang_default)
319 cg->lang_default = cupsLangDefault();
322 * Transcode the command-line arguments from the locale charset to
326 if (cg->lang_default->encoding != CUPS_US_ASCII &&
327 cg->lang_default->encoding != CUPS_UTF8)
329 for (i = 1; argv[i]; i ++)
332 * Try converting from the locale charset to UTF-8...
335 if (cupsCharsetToUTF8((cups_utf8_t *)buffer, argv[i], sizeof(buffer),
336 cg->lang_default->encoding) < 0)
340 * Save the new string if it differs from the original...
343 if (strcmp(buffer, argv[i]))
344 argv[i] = strdup(buffer);
351 * End of "$Id: langprintf.c 9637 2011-03-21 23:03:22Z mike $".