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