]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/langprintf.c
c6154f6fd52d10e625f132c45ba7ea6c17d6cb02
4 * Localized printf/puts functions for CUPS.
6 * Copyright 2007-2010 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 output
[8192]; /* Output buffer */
46 _cups_globals_t
*cg
; /* Global data */
57 * Save the errno value...
63 * Get the message catalog...
68 if (!cg
->lang_default
)
69 cg
->lang_default
= cupsLangDefault();
72 * Format the message...
75 snprintf(buffer
, sizeof(buffer
), "%s%s: %s\n", prefix
? prefix
: "",
76 _cupsLangString(cg
->lang_default
, message
), strerror(last_errno
));
79 * Convert and write to stderr...
82 bytes
= cupsUTF8ToCharset(output
, (cups_utf8_t
*)buffer
, sizeof(output
),
83 cg
->lang_default
->encoding
);
86 fwrite(output
, 1, bytes
, stderr
);
91 * '_cupsLangPrintFilter()' - Print a formatted filter message string to a file.
94 int /* O - Number of bytes written */
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 */
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 */
118 if (!cg
->lang_default
)
119 cg
->lang_default
= cupsLangDefault();
122 * Format the string...
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
);
132 * Transcode to the destination charset...
135 bytes
= cupsUTF8ToCharset(output
, (cups_utf8_t
*)buffer
, sizeof(output
),
136 cg
->lang_default
->encoding
);
139 * Write the string and return the number of bytes written...
143 return ((int)fwrite(output
, 1, bytes
, fp
));
150 * '_cupsLangPrintf()' - Print a formatted message string to a file.
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 */
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 */
174 if (!cg
->lang_default
)
175 cg
->lang_default
= cupsLangDefault();
178 * Format the string...
181 va_start(ap
, message
);
182 vsnprintf(buffer
, sizeof(buffer
),
183 _cupsLangString(cg
->lang_default
, message
), ap
);
187 * Transcode to the destination charset...
190 bytes
= cupsUTF8ToCharset(output
, (cups_utf8_t
*)buffer
, sizeof(output
),
191 cg
->lang_default
->encoding
);
194 * Write the string and return the number of bytes written...
198 return ((int)fwrite(output
, 1, bytes
, fp
));
205 * '_cupsLangPuts()' - Print a static message string to a file.
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 */
212 int bytes
; /* Number of bytes formatted */
213 char output
[8192]; /* Message buffer */
214 _cups_globals_t
*cg
; /* Global data */
226 if (!cg
->lang_default
)
227 cg
->lang_default
= cupsLangDefault();
230 * Transcode to the destination charset...
233 bytes
= cupsUTF8ToCharset(output
,
234 (cups_utf8_t
*)_cupsLangString(cg
->lang_default
,
236 sizeof(output
), cg
->lang_default
->encoding
);
239 * Write the string and return the number of bytes written...
243 return ((int)fwrite(output
, 1, bytes
, fp
));
250 * '_cupsSetLocale()' - Set the current locale and transcode the command-line.
254 _cupsSetLocale(char *argv
[]) /* IO - Command-line arguments */
256 int i
; /* Looping var */
257 char buffer
[8192]; /* Command-line argument buffer */
258 _cups_globals_t
*cg
; /* Global data */
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 */
267 * Set the locale so that times, etc. are displayed properly.
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...)
275 setlocale(LC_ALL
, "");
278 if ((lc_time
= setlocale(LC_TIME
, NULL
)) == NULL
)
279 lc_time
= setlocale(LC_ALL
, NULL
);
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
);
287 strlcpy(charset
, ".UTF-8", sizeof(new_lc_time
) - (charset
- new_lc_time
));
290 strcpy(new_lc_time
, "C");
292 setlocale(LC_TIME
, new_lc_time
);
296 * Initialize the default language info...
301 if (!cg
->lang_default
)
302 cg
->lang_default
= cupsLangDefault();
305 * Transcode the command-line arguments from the locale charset to
309 if (cg
->lang_default
->encoding
!= CUPS_US_ASCII
&&
310 cg
->lang_default
->encoding
!= CUPS_UTF8
)
312 for (i
= 1; argv
[i
]; i
++)
315 * Try converting from the locale charset to UTF-8...
318 if (cupsCharsetToUTF8((cups_utf8_t
*)buffer
, argv
[i
], sizeof(buffer
),
319 cg
->lang_default
->encoding
) < 0)
323 * Save the new string if it differs from the original...
326 if (strcmp(buffer
, argv
[i
]))
327 argv
[i
] = strdup(buffer
);