]>
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 the Common UNIX Printing
7 * Copyright 2007-2008 by Apple Inc.
8 * Copyright 2002-2007 by Easy Software Products.
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * file is missing or damaged, see the license at "http://www.cups.org/".
16 * This file is subject to the Apple OS-Developed Software exception.
20 * _cupsLangPrintError() - Print a message followed by a standard error.
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...
37 * '_cupsLangPrintError()' - Print a message followed by a standard error.
41 _cupsLangPrintError(const char *message
)/* I - Message */
43 int bytes
; /* Number of bytes formatted */
44 int last_errno
; /* Last error */
45 char buffer
[2048], /* Message 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...
76 snprintf(buffer
, sizeof(buffer
), "%s: %s",
77 _cupsLangString(cg
->lang_default
, message
), strerror(last_errno
));
80 * Convert and write to stderr...
83 bytes
= cupsUTF8ToCharset(output
, (cups_utf8_t
*)buffer
, sizeof(output
),
84 cg
->lang_default
->encoding
);
87 fwrite(output
, 1, bytes
, stderr
);
92 * '_cupsLangPrintf()' - Print a formatted message string to a file.
95 int /* O - Number of bytes written */
96 _cupsLangPrintf(FILE *fp
, /* I - File to write to */
97 const char *message
, /* I - Message string to use */
98 ...) /* I - Additional arguments as needed */
100 int bytes
; /* Number of bytes formatted */
101 char buffer
[2048], /* Message buffer */
102 output
[8192]; /* Output buffer */
103 va_list ap
; /* Pointer to additional arguments */
104 _cups_globals_t
*cg
; /* Global data */
116 if (!cg
->lang_default
)
117 cg
->lang_default
= cupsLangDefault();
120 * Format the string...
123 va_start(ap
, message
);
124 vsnprintf(buffer
, sizeof(buffer
),
125 _cupsLangString(cg
->lang_default
, message
), ap
);
129 * Transcode to the destination charset...
132 bytes
= cupsUTF8ToCharset(output
, (cups_utf8_t
*)buffer
, sizeof(output
),
133 cg
->lang_default
->encoding
);
136 * Write the string and return the number of bytes written...
140 return ((int)fwrite(output
, 1, bytes
, fp
));
147 * '_cupsLangPuts()' - Print a static message string to a file.
150 int /* O - Number of bytes written */
151 _cupsLangPuts(FILE *fp
, /* I - File to write to */
152 const char *message
) /* I - Message string to use */
154 int bytes
; /* Number of bytes formatted */
155 char output
[2048]; /* Message buffer */
156 _cups_globals_t
*cg
; /* Global data */
168 if (!cg
->lang_default
)
169 cg
->lang_default
= cupsLangDefault();
172 * Transcode to the destination charset...
175 bytes
= cupsUTF8ToCharset(output
,
176 (cups_utf8_t
*)_cupsLangString(cg
->lang_default
,
178 sizeof(output
), cg
->lang_default
->encoding
);
181 * Write the string and return the number of bytes written...
185 return ((int)fwrite(output
, 1, bytes
, fp
));
192 * '_cupsSetLocale()' - Set the current locale and transcode the command-line.
196 _cupsSetLocale(char *argv
[]) /* IO - Command-line arguments */
198 int i
; /* Looping var */
199 char buffer
[8192]; /* Command-line argument buffer */
200 _cups_globals_t
*cg
; /* Global data */
202 const char *lc_time
; /* Current LC_TIME value */
203 char new_lc_time
[255], /* New LC_TIME value */
204 *charset
; /* Pointer to character set */
209 * Set the locale so that times, etc. are displayed properly.
211 * Unfortunately, while we need the localized time value, we *don't*
212 * want to use the localized charset for the time value, so we need
213 * to set LC_TIME to the locale name with .UTF-8 on the end (if
214 * the locale includes a character set specifier...)
217 setlocale(LC_ALL
, "");
220 if ((lc_time
= setlocale(LC_TIME
, NULL
)) == NULL
)
221 lc_time
= setlocale(LC_ALL
, NULL
);
225 strlcpy(new_lc_time
, lc_time
, sizeof(new_lc_time
));
226 if ((charset
= strchr(new_lc_time
, '.')) == NULL
)
227 charset
= new_lc_time
+ strlen(new_lc_time
);
229 strlcpy(charset
, ".UTF-8", sizeof(new_lc_time
) - (charset
- new_lc_time
));
232 strcpy(new_lc_time
, "C");
234 setlocale(LC_TIME
, new_lc_time
);
238 * Initialize the default language info...
243 if (!cg
->lang_default
)
244 cg
->lang_default
= cupsLangDefault();
247 * Transcode the command-line arguments from the locale charset to
251 if (cg
->lang_default
->encoding
!= CUPS_US_ASCII
&&
252 cg
->lang_default
->encoding
!= CUPS_UTF8
)
254 for (i
= 1; argv
[i
]; i
++)
257 * Try converting from the locale charset to UTF-8...
260 if (cupsCharsetToUTF8((cups_utf8_t
*)buffer
, argv
[i
], sizeof(buffer
),
261 cg
->lang_default
->encoding
) < 0)
265 * Save the new string if it differs from the original...
268 if (strcmp(buffer
, argv
[i
]))
269 argv
[i
] = strdup(buffer
);
276 * End of "$Id: langprintf.c 7802 2008-07-28 18:50:45Z mike $".