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