]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f7deaa1a | 2 | * "$Id: langprintf.c 6187 2007-01-10 16:20:42Z mike $" |
ef416fc2 | 3 | * |
4 | * Localized printf/puts functions for the Common UNIX Printing | |
5 | * System (CUPS). | |
6 | * | |
b86bc4cf | 7 | * Copyright 2002-2007 by Easy Software Products. |
ef416fc2 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
10 | * property of Easy Software Products and are protected by Federal | |
11 | * copyright law. Distribution and use rights are outlined in the file | |
12 | * "LICENSE.txt" which should have been included with this file. If this | |
13 | * file is missing or damaged please contact Easy Software Products | |
14 | * at: | |
15 | * | |
16 | * Attn: CUPS Licensing Information | |
17 | * Easy Software Products | |
18 | * 44141 Airport View Drive, Suite 204 | |
19 | * Hollywood, Maryland 20636 USA | |
20 | * | |
21 | * Voice: (301) 373-9600 | |
22 | * EMail: cups-info@cups.org | |
23 | * WWW: http://www.cups.org | |
24 | * | |
25 | * This file is subject to the Apple OS-Developed Software exception. | |
26 | * | |
27 | * Contents: | |
28 | * | |
29 | * _cupsLangPrintf() - Print a formatted message string to a file. | |
30 | * _cupsLangPuts() - Print a static message string to a file. | |
07725fee | 31 | * _cupsSetLocale() - Set the current locale and transcode the command-line. |
ef416fc2 | 32 | */ |
33 | ||
34 | /* | |
35 | * Include necessary headers... | |
36 | */ | |
37 | ||
38 | #include <stdio.h> | |
fa73b229 | 39 | #include "globals.h" |
ef416fc2 | 40 | |
41 | ||
42 | /* | |
43 | * '_cupsLangPrintf()' - Print a formatted message string to a file. | |
44 | */ | |
45 | ||
46 | int /* O - Number of bytes written */ | |
47 | _cupsLangPrintf(FILE *fp, /* I - File to write to */ | |
ef416fc2 | 48 | const char *message, /* I - Message string to use */ |
49 | ...) /* I - Additional arguments as needed */ | |
50 | { | |
51 | int bytes; /* Number of bytes formatted */ | |
52 | char buffer[2048], /* Message buffer */ | |
53 | output[8192]; /* Output buffer */ | |
54 | va_list ap; /* Pointer to additional arguments */ | |
fa73b229 | 55 | _cups_globals_t *cg; /* Global data */ |
ef416fc2 | 56 | |
57 | ||
58 | /* | |
59 | * Range check... | |
60 | */ | |
61 | ||
62 | if (!fp || !message) | |
63 | return (-1); | |
64 | ||
fa73b229 | 65 | cg = _cupsGlobals(); |
66 | ||
67 | if (!cg->lang_default) | |
68 | cg->lang_default = cupsLangDefault(); | |
ef416fc2 | 69 | |
70 | /* | |
71 | * Format the string... | |
72 | */ | |
73 | ||
74 | va_start(ap, message); | |
75 | bytes = vsnprintf(buffer, sizeof(buffer), | |
fa73b229 | 76 | _cupsLangString(cg->lang_default, message), ap); |
ef416fc2 | 77 | va_end(ap); |
78 | ||
79 | /* | |
80 | * Transcode to the destination charset... | |
81 | */ | |
82 | ||
83 | bytes = cupsUTF8ToCharset(output, (cups_utf8_t *)buffer, sizeof(output), | |
fa73b229 | 84 | cg->lang_default->encoding); |
ef416fc2 | 85 | |
86 | /* | |
87 | * Write the string and return the number of bytes written... | |
88 | */ | |
89 | ||
90 | if (bytes > 0) | |
b86bc4cf | 91 | return ((int)fwrite(output, 1, bytes, fp)); |
ef416fc2 | 92 | else |
93 | return (bytes); | |
94 | } | |
95 | ||
96 | ||
97 | /* | |
98 | * '_cupsLangPuts()' - Print a static message string to a file. | |
99 | */ | |
100 | ||
101 | int /* O - Number of bytes written */ | |
102 | _cupsLangPuts(FILE *fp, /* I - File to write to */ | |
ef416fc2 | 103 | const char *message) /* I - Message string to use */ |
104 | { | |
105 | int bytes; /* Number of bytes formatted */ | |
106 | char output[2048]; /* Message buffer */ | |
fa73b229 | 107 | _cups_globals_t *cg; /* Global data */ |
ef416fc2 | 108 | |
109 | ||
110 | /* | |
111 | * Range check... | |
112 | */ | |
113 | ||
114 | if (!fp || !message) | |
115 | return (-1); | |
116 | ||
fa73b229 | 117 | cg = _cupsGlobals(); |
118 | ||
119 | if (!cg->lang_default) | |
120 | cg->lang_default = cupsLangDefault(); | |
ef416fc2 | 121 | |
122 | /* | |
123 | * Transcode to the destination charset... | |
124 | */ | |
125 | ||
126 | bytes = cupsUTF8ToCharset(output, | |
fa73b229 | 127 | (cups_utf8_t *)_cupsLangString(cg->lang_default, |
128 | message), | |
129 | sizeof(output), cg->lang_default->encoding); | |
ef416fc2 | 130 | |
131 | /* | |
132 | * Write the string and return the number of bytes written... | |
133 | */ | |
134 | ||
135 | if (bytes > 0) | |
b86bc4cf | 136 | return ((int)fwrite(output, 1, bytes, fp)); |
ef416fc2 | 137 | else |
138 | return (bytes); | |
139 | } | |
140 | ||
141 | ||
142 | /* | |
07725fee | 143 | * '_cupsSetLocale()' - Set the current locale and transcode the command-line. |
d09495fa | 144 | */ |
145 | ||
146 | void | |
07725fee | 147 | _cupsSetLocale(char *argv[]) /* IO - Command-line arguments */ |
d09495fa | 148 | { |
07725fee | 149 | int i; /* Looping var */ |
150 | char buffer[8192]; /* Command-line argument buffer */ | |
151 | _cups_globals_t *cg; /* Global data */ | |
d09495fa | 152 | #ifdef LC_TIME |
07725fee | 153 | const char *lc_time; /* Current LC_TIME value */ |
154 | char new_lc_time[255], /* New LC_TIME value */ | |
d09495fa | 155 | *charset; /* Pointer to character set */ |
156 | #endif /* LC_TIME */ | |
157 | ||
158 | ||
159 | /* | |
160 | * Set the locale so that times, etc. are displayed properly. | |
161 | * | |
162 | * Unfortunately, while we need the localized time value, we *don't* | |
163 | * want to use the localized charset for the time value, so we need | |
164 | * to set LC_TIME to the locale name with .UTF-8 on the end (if | |
165 | * the locale includes a character set specifier...) | |
166 | */ | |
167 | ||
168 | setlocale(LC_ALL, ""); | |
169 | ||
170 | #ifdef LC_TIME | |
171 | if ((lc_time = setlocale(LC_TIME, NULL)) == NULL) | |
172 | lc_time = setlocale(LC_ALL, NULL); | |
173 | ||
174 | if (lc_time) | |
175 | { | |
176 | strlcpy(new_lc_time, lc_time, sizeof(new_lc_time)); | |
177 | if ((charset = strchr(new_lc_time, '.')) == NULL) | |
178 | charset = new_lc_time + strlen(new_lc_time); | |
179 | ||
180 | strlcpy(charset, ".UTF-8", sizeof(new_lc_time) - (charset - new_lc_time)); | |
181 | } | |
182 | else | |
183 | strcpy(new_lc_time, "C"); | |
184 | ||
185 | setlocale(LC_TIME, new_lc_time); | |
186 | #endif /* LC_TIME */ | |
07725fee | 187 | |
188 | /* | |
189 | * Initialize the default language info... | |
190 | */ | |
191 | ||
192 | cg = _cupsGlobals(); | |
193 | ||
194 | if (!cg->lang_default) | |
195 | cg->lang_default = cupsLangDefault(); | |
196 | ||
197 | /* | |
198 | * Transcode the command-line arguments from the locale charset to | |
199 | * UTF-8... | |
200 | */ | |
201 | ||
202 | if (cg->lang_default->encoding != CUPS_US_ASCII && | |
203 | cg->lang_default->encoding != CUPS_UTF8) | |
204 | { | |
205 | for (i = 1; argv[i]; i ++) | |
206 | { | |
207 | /* | |
208 | * Try converting from the locale charset to UTF-8... | |
209 | */ | |
210 | ||
211 | if (cupsCharsetToUTF8((cups_utf8_t *)buffer, argv[i], sizeof(buffer), | |
212 | cg->lang_default->encoding) < 0) | |
213 | continue; | |
214 | ||
215 | /* | |
216 | * Save the new string if it differs from the original... | |
217 | */ | |
218 | ||
219 | if (strcmp(buffer, argv[i])) | |
220 | argv[i] = strdup(buffer); | |
221 | } | |
222 | } | |
d09495fa | 223 | } |
224 | ||
225 | ||
226 | /* | |
f7deaa1a | 227 | * End of "$Id: langprintf.c 6187 2007-01-10 16:20:42Z mike $". |
ef416fc2 | 228 | */ |