]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/langprintf.c
2 * "$Id: langprintf.c 7802 2008-07-28 18:50:45Z mike $"
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 * _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
27 * Include necessary headers...
30 #include "cups-private.h"
34 * '_cupsLangPrintError()' - Print a message followed by a standard error.
38 _cupsLangPrintError(const char *message
)/* I - Message */
40 int bytes
; /* Number of bytes formatted */
41 int last_errno
; /* Last error */
42 char buffer
[2048], /* Message buffer */
43 output
[8192]; /* Output buffer */
44 _cups_globals_t
*cg
; /* Global data */
55 * Save the errno value...
61 * Get the message catalog...
66 if (!cg
->lang_default
)
67 cg
->lang_default
= cupsLangDefault();
70 * Format the message...
73 snprintf(buffer
, sizeof(buffer
), "%s: %s\n",
74 _cupsLangString(cg
->lang_default
, message
), strerror(last_errno
));
77 * Convert and write to stderr...
80 bytes
= cupsUTF8ToCharset(output
, (cups_utf8_t
*)buffer
, sizeof(output
),
81 cg
->lang_default
->encoding
);
84 fwrite(output
, 1, bytes
, stderr
);
89 * '_cupsLangPrintf()' - Print a formatted message string to a file.
92 int /* O - Number of bytes written */
93 _cupsLangPrintf(FILE *fp
, /* I - File to write to */
94 const char *message
, /* I - Message string to use */
95 ...) /* I - Additional arguments as needed */
97 int bytes
; /* Number of bytes formatted */
98 char buffer
[2048], /* Message buffer */
99 output
[8192]; /* Output buffer */
100 va_list ap
; /* Pointer to additional arguments */
101 _cups_globals_t
*cg
; /* Global data */
113 if (!cg
->lang_default
)
114 cg
->lang_default
= cupsLangDefault();
117 * Format the string...
120 va_start(ap
, message
);
121 vsnprintf(buffer
, sizeof(buffer
),
122 _cupsLangString(cg
->lang_default
, message
), ap
);
126 * Transcode to the destination charset...
129 bytes
= cupsUTF8ToCharset(output
, (cups_utf8_t
*)buffer
, sizeof(output
),
130 cg
->lang_default
->encoding
);
133 * Write the string and return the number of bytes written...
137 return ((int)fwrite(output
, 1, bytes
, fp
));
144 * '_cupsLangPuts()' - Print a static message string to a file.
147 int /* O - Number of bytes written */
148 _cupsLangPuts(FILE *fp
, /* I - File to write to */
149 const char *message
) /* I - Message string to use */
151 int bytes
; /* Number of bytes formatted */
152 char output
[2048]; /* Message buffer */
153 _cups_globals_t
*cg
; /* Global data */
165 if (!cg
->lang_default
)
166 cg
->lang_default
= cupsLangDefault();
169 * Transcode to the destination charset...
172 bytes
= cupsUTF8ToCharset(output
,
173 (cups_utf8_t
*)_cupsLangString(cg
->lang_default
,
175 sizeof(output
), cg
->lang_default
->encoding
);
178 * Write the string and return the number of bytes written...
182 return ((int)fwrite(output
, 1, bytes
, fp
));
189 * '_cupsSetLocale()' - Set the current locale and transcode the command-line.
193 _cupsSetLocale(char *argv
[]) /* IO - Command-line arguments */
195 int i
; /* Looping var */
196 char buffer
[8192]; /* Command-line argument buffer */
197 _cups_globals_t
*cg
; /* Global data */
199 const char *lc_time
; /* Current LC_TIME value */
200 char new_lc_time
[255], /* New LC_TIME value */
201 *charset
; /* Pointer to character set */
206 * Set the locale so that times, etc. are displayed properly.
208 * Unfortunately, while we need the localized time value, we *don't*
209 * want to use the localized charset for the time value, so we need
210 * to set LC_TIME to the locale name with .UTF-8 on the end (if
211 * the locale includes a character set specifier...)
214 setlocale(LC_ALL
, "");
217 if ((lc_time
= setlocale(LC_TIME
, NULL
)) == NULL
)
218 lc_time
= setlocale(LC_ALL
, NULL
);
222 strlcpy(new_lc_time
, lc_time
, sizeof(new_lc_time
));
223 if ((charset
= strchr(new_lc_time
, '.')) == NULL
)
224 charset
= new_lc_time
+ strlen(new_lc_time
);
226 strlcpy(charset
, ".UTF-8", sizeof(new_lc_time
) - (charset
- new_lc_time
));
229 strcpy(new_lc_time
, "C");
231 setlocale(LC_TIME
, new_lc_time
);
235 * Initialize the default language info...
240 if (!cg
->lang_default
)
241 cg
->lang_default
= cupsLangDefault();
244 * Transcode the command-line arguments from the locale charset to
248 if (cg
->lang_default
->encoding
!= CUPS_US_ASCII
&&
249 cg
->lang_default
->encoding
!= CUPS_UTF8
)
251 for (i
= 1; argv
[i
]; i
++)
254 * Try converting from the locale charset to UTF-8...
257 if (cupsCharsetToUTF8((cups_utf8_t
*)buffer
, argv
[i
], sizeof(buffer
),
258 cg
->lang_default
->encoding
) < 0)
262 * Save the new string if it differs from the original...
265 if (strcmp(buffer
, argv
[i
]))
266 argv
[i
] = strdup(buffer
);
273 * End of "$Id: langprintf.c 7802 2008-07-28 18:50:45Z mike $".