]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/langprintf.c
2 * "$Id: langprintf.c 7193 2008-01-07 23:01:40Z 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 * _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 command-line.
26 * Include necessary headers...
35 * '_cupsLangPrintError()' - Print a message followed by a standard error.
39 _cupsLangPrintError(const char *message
)/* I - Message */
41 int bytes
; /* Number of bytes formatted */
42 int last_errno
; /* Last error */
43 char buffer
[2048], /* Message buffer */
44 output
[8192]; /* Output buffer */
45 _cups_globals_t
*cg
; /* Global data */
56 * Save the errno value...
62 * Get the message catalog...
67 if (!cg
->lang_default
)
68 cg
->lang_default
= cupsLangDefault();
71 * Format the message...
74 snprintf(buffer
, sizeof(buffer
), "%s: %s",
75 _cupsLangString(cg
->lang_default
, message
), strerror(last_errno
));
78 * Convert and write to stderr...
81 bytes
= cupsUTF8ToCharset(output
, (cups_utf8_t
*)buffer
, sizeof(output
),
82 cg
->lang_default
->encoding
);
85 fwrite(output
, 1, bytes
, stderr
);
90 * '_cupsLangPrintf()' - Print a formatted message string to a file.
93 int /* O - Number of bytes written */
94 _cupsLangPrintf(FILE *fp
, /* I - File to write to */
95 const char *message
, /* I - Message string to use */
96 ...) /* I - Additional arguments as needed */
98 int bytes
; /* Number of bytes formatted */
99 char buffer
[2048], /* Message buffer */
100 output
[8192]; /* Output buffer */
101 va_list ap
; /* Pointer to additional arguments */
102 _cups_globals_t
*cg
; /* Global data */
114 if (!cg
->lang_default
)
115 cg
->lang_default
= cupsLangDefault();
118 * Format the string...
121 va_start(ap
, message
);
122 vsnprintf(buffer
, sizeof(buffer
),
123 _cupsLangString(cg
->lang_default
, message
), ap
);
127 * Transcode to the destination charset...
130 bytes
= cupsUTF8ToCharset(output
, (cups_utf8_t
*)buffer
, sizeof(output
),
131 cg
->lang_default
->encoding
);
134 * Write the string and return the number of bytes written...
138 return ((int)fwrite(output
, 1, bytes
, fp
));
145 * '_cupsLangPuts()' - Print a static message string to a file.
148 int /* O - Number of bytes written */
149 _cupsLangPuts(FILE *fp
, /* I - File to write to */
150 const char *message
) /* I - Message string to use */
152 int bytes
; /* Number of bytes formatted */
153 char output
[2048]; /* Message buffer */
154 _cups_globals_t
*cg
; /* Global data */
166 if (!cg
->lang_default
)
167 cg
->lang_default
= cupsLangDefault();
170 * Transcode to the destination charset...
173 bytes
= cupsUTF8ToCharset(output
,
174 (cups_utf8_t
*)_cupsLangString(cg
->lang_default
,
176 sizeof(output
), cg
->lang_default
->encoding
);
179 * Write the string and return the number of bytes written...
183 return ((int)fwrite(output
, 1, bytes
, fp
));
190 * '_cupsSetLocale()' - Set the current locale and transcode the command-line.
194 _cupsSetLocale(char *argv
[]) /* IO - Command-line arguments */
196 int i
; /* Looping var */
197 char buffer
[8192]; /* Command-line argument buffer */
198 _cups_globals_t
*cg
; /* Global data */
200 const char *lc_time
; /* Current LC_TIME value */
201 char new_lc_time
[255], /* New LC_TIME value */
202 *charset
; /* Pointer to character set */
207 * Set the locale so that times, etc. are displayed properly.
209 * Unfortunately, while we need the localized time value, we *don't*
210 * want to use the localized charset for the time value, so we need
211 * to set LC_TIME to the locale name with .UTF-8 on the end (if
212 * the locale includes a character set specifier...)
215 setlocale(LC_ALL
, "");
218 if ((lc_time
= setlocale(LC_TIME
, NULL
)) == NULL
)
219 lc_time
= setlocale(LC_ALL
, NULL
);
223 strlcpy(new_lc_time
, lc_time
, sizeof(new_lc_time
));
224 if ((charset
= strchr(new_lc_time
, '.')) == NULL
)
225 charset
= new_lc_time
+ strlen(new_lc_time
);
227 strlcpy(charset
, ".UTF-8", sizeof(new_lc_time
) - (charset
- new_lc_time
));
230 strcpy(new_lc_time
, "C");
232 setlocale(LC_TIME
, new_lc_time
);
236 * Initialize the default language info...
241 if (!cg
->lang_default
)
242 cg
->lang_default
= cupsLangDefault();
245 * Transcode the command-line arguments from the locale charset to
249 if (cg
->lang_default
->encoding
!= CUPS_US_ASCII
&&
250 cg
->lang_default
->encoding
!= CUPS_UTF8
)
252 for (i
= 1; argv
[i
]; i
++)
255 * Try converting from the locale charset to UTF-8...
258 if (cupsCharsetToUTF8((cups_utf8_t
*)buffer
, argv
[i
], sizeof(buffer
),
259 cg
->lang_default
->encoding
) < 0)
263 * Save the new string if it differs from the original...
266 if (strcmp(buffer
, argv
[i
]))
267 argv
[i
] = strdup(buffer
);
274 * End of "$Id: langprintf.c 7193 2008-01-07 23:01:40Z mike $".