]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/langprintf.c
afb189c573911b397b17ce0ddde3088392b68b83
[thirdparty/cups.git] / cups / langprintf.c
1 /*
2 * "$Id: langprintf.c 6649 2007-07-11 21:46:42Z mike $"
3 *
4 * Localized printf/puts functions for the Common UNIX Printing
5 * System (CUPS).
6 *
7 * Copyright 2007-2008 by Apple Inc.
8 * Copyright 2002-2007 by Easy Software Products.
9 *
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/".
15 *
16 * This file is subject to the Apple OS-Developed Software exception.
17 *
18 * Contents:
19 *
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.
23 */
24
25 /*
26 * Include necessary headers...
27 */
28
29 #include <stdio.h>
30 #include "globals.h"
31 #include <errno.h>
32
33
34 /*
35 * '_cupsLangPrintError()' - Print a message followed by a standard error.
36 */
37
38 void
39 _cupsLangPrintError(const char *message)/* I - Message */
40 {
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 */
46
47
48 /*
49 * Range check...
50 */
51
52 if (!message)
53 return;
54
55 /*
56 * Save the errno value...
57 */
58
59 last_errno = errno;
60
61 /*
62 * Get the message catalog...
63 */
64
65 cg = _cupsGlobals();
66
67 if (!cg->lang_default)
68 cg->lang_default = cupsLangDefault();
69
70 /*
71 * Format the message...
72 */
73
74 bytes = snprintf(buffer, sizeof(buffer), "%s: %s",
75 _cupsLangString(cg->lang_default, message),
76 strerror(last_errno));
77
78 /*
79 * Convert and write to stderr...
80 */
81
82 bytes = cupsUTF8ToCharset(output, (cups_utf8_t *)buffer, sizeof(output),
83 cg->lang_default->encoding);
84
85 if (bytes > 0)
86 fwrite(output, 1, bytes, stderr);
87 }
88
89
90 /*
91 * '_cupsLangPrintf()' - Print a formatted message string to a file.
92 */
93
94 int /* O - Number of bytes written */
95 _cupsLangPrintf(FILE *fp, /* I - File to write to */
96 const char *message, /* I - Message string to use */
97 ...) /* I - Additional arguments as needed */
98 {
99 int bytes; /* Number of bytes formatted */
100 char buffer[2048], /* Message buffer */
101 output[8192]; /* Output buffer */
102 va_list ap; /* Pointer to additional arguments */
103 _cups_globals_t *cg; /* Global data */
104
105
106 /*
107 * Range check...
108 */
109
110 if (!fp || !message)
111 return (-1);
112
113 cg = _cupsGlobals();
114
115 if (!cg->lang_default)
116 cg->lang_default = cupsLangDefault();
117
118 /*
119 * Format the string...
120 */
121
122 va_start(ap, message);
123 bytes = vsnprintf(buffer, sizeof(buffer),
124 _cupsLangString(cg->lang_default, message), ap);
125 va_end(ap);
126
127 /*
128 * Transcode to the destination charset...
129 */
130
131 bytes = cupsUTF8ToCharset(output, (cups_utf8_t *)buffer, sizeof(output),
132 cg->lang_default->encoding);
133
134 /*
135 * Write the string and return the number of bytes written...
136 */
137
138 if (bytes > 0)
139 return ((int)fwrite(output, 1, bytes, fp));
140 else
141 return (bytes);
142 }
143
144
145 /*
146 * '_cupsLangPuts()' - Print a static message string to a file.
147 */
148
149 int /* O - Number of bytes written */
150 _cupsLangPuts(FILE *fp, /* I - File to write to */
151 const char *message) /* I - Message string to use */
152 {
153 int bytes; /* Number of bytes formatted */
154 char output[2048]; /* Message buffer */
155 _cups_globals_t *cg; /* Global data */
156
157
158 /*
159 * Range check...
160 */
161
162 if (!fp || !message)
163 return (-1);
164
165 cg = _cupsGlobals();
166
167 if (!cg->lang_default)
168 cg->lang_default = cupsLangDefault();
169
170 /*
171 * Transcode to the destination charset...
172 */
173
174 bytes = cupsUTF8ToCharset(output,
175 (cups_utf8_t *)_cupsLangString(cg->lang_default,
176 message),
177 sizeof(output), cg->lang_default->encoding);
178
179 /*
180 * Write the string and return the number of bytes written...
181 */
182
183 if (bytes > 0)
184 return ((int)fwrite(output, 1, bytes, fp));
185 else
186 return (bytes);
187 }
188
189
190 /*
191 * '_cupsSetLocale()' - Set the current locale and transcode the command-line.
192 */
193
194 void
195 _cupsSetLocale(char *argv[]) /* IO - Command-line arguments */
196 {
197 int i; /* Looping var */
198 char buffer[8192]; /* Command-line argument buffer */
199 _cups_globals_t *cg; /* Global data */
200 #ifdef LC_TIME
201 const char *lc_time; /* Current LC_TIME value */
202 char new_lc_time[255], /* New LC_TIME value */
203 *charset; /* Pointer to character set */
204 #endif /* LC_TIME */
205
206
207 /*
208 * Set the locale so that times, etc. are displayed properly.
209 *
210 * Unfortunately, while we need the localized time value, we *don't*
211 * want to use the localized charset for the time value, so we need
212 * to set LC_TIME to the locale name with .UTF-8 on the end (if
213 * the locale includes a character set specifier...)
214 */
215
216 setlocale(LC_ALL, "");
217
218 #ifdef LC_TIME
219 if ((lc_time = setlocale(LC_TIME, NULL)) == NULL)
220 lc_time = setlocale(LC_ALL, NULL);
221
222 if (lc_time)
223 {
224 strlcpy(new_lc_time, lc_time, sizeof(new_lc_time));
225 if ((charset = strchr(new_lc_time, '.')) == NULL)
226 charset = new_lc_time + strlen(new_lc_time);
227
228 strlcpy(charset, ".UTF-8", sizeof(new_lc_time) - (charset - new_lc_time));
229 }
230 else
231 strcpy(new_lc_time, "C");
232
233 setlocale(LC_TIME, new_lc_time);
234 #endif /* LC_TIME */
235
236 /*
237 * Initialize the default language info...
238 */
239
240 cg = _cupsGlobals();
241
242 if (!cg->lang_default)
243 cg->lang_default = cupsLangDefault();
244
245 /*
246 * Transcode the command-line arguments from the locale charset to
247 * UTF-8...
248 */
249
250 if (cg->lang_default->encoding != CUPS_US_ASCII &&
251 cg->lang_default->encoding != CUPS_UTF8)
252 {
253 for (i = 1; argv[i]; i ++)
254 {
255 /*
256 * Try converting from the locale charset to UTF-8...
257 */
258
259 if (cupsCharsetToUTF8((cups_utf8_t *)buffer, argv[i], sizeof(buffer),
260 cg->lang_default->encoding) < 0)
261 continue;
262
263 /*
264 * Save the new string if it differs from the original...
265 */
266
267 if (strcmp(buffer, argv[i]))
268 argv[i] = strdup(buffer);
269 }
270 }
271 }
272
273
274 /*
275 * End of "$Id: langprintf.c 6649 2007-07-11 21:46:42Z mike $".
276 */