]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
d09495fa | 2 | * "$Id: langprintf.c 5833 2006-08-16 20:05:58Z mike $" |
ef416fc2 | 3 | * |
4 | * Localized printf/puts functions for the Common UNIX Printing | |
5 | * System (CUPS). | |
6 | * | |
fa73b229 | 7 | * Copyright 2002-2006 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. | |
d09495fa | 31 | * _cupsSetLocale() - Set the current locale. |
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) | |
91 | return (fwrite(output, 1, bytes, fp)); | |
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) | |
136 | return (fwrite(output, 1, bytes, fp)); | |
137 | else | |
138 | return (bytes); | |
139 | } | |
140 | ||
141 | ||
142 | /* | |
d09495fa | 143 | * '_cupsSetLocale()' - Set the current locale. |
144 | */ | |
145 | ||
146 | void | |
147 | _cupsSetLocale(void) | |
148 | { | |
149 | #ifdef LC_TIME | |
150 | const char *lc_time; /* Current LC_TIME value */ | |
151 | char new_lc_time[255], /* New LC_TIME value */ | |
152 | *charset; /* Pointer to character set */ | |
153 | #endif /* LC_TIME */ | |
154 | ||
155 | ||
156 | /* | |
157 | * Set the locale so that times, etc. are displayed properly. | |
158 | * | |
159 | * Unfortunately, while we need the localized time value, we *don't* | |
160 | * want to use the localized charset for the time value, so we need | |
161 | * to set LC_TIME to the locale name with .UTF-8 on the end (if | |
162 | * the locale includes a character set specifier...) | |
163 | */ | |
164 | ||
165 | setlocale(LC_ALL, ""); | |
166 | ||
167 | #ifdef LC_TIME | |
168 | if ((lc_time = setlocale(LC_TIME, NULL)) == NULL) | |
169 | lc_time = setlocale(LC_ALL, NULL); | |
170 | ||
171 | if (lc_time) | |
172 | { | |
173 | strlcpy(new_lc_time, lc_time, sizeof(new_lc_time)); | |
174 | if ((charset = strchr(new_lc_time, '.')) == NULL) | |
175 | charset = new_lc_time + strlen(new_lc_time); | |
176 | ||
177 | strlcpy(charset, ".UTF-8", sizeof(new_lc_time) - (charset - new_lc_time)); | |
178 | } | |
179 | else | |
180 | strcpy(new_lc_time, "C"); | |
181 | ||
182 | setlocale(LC_TIME, new_lc_time); | |
183 | #endif /* LC_TIME */ | |
184 | } | |
185 | ||
186 | ||
187 | /* | |
188 | * End of "$Id: langprintf.c 5833 2006-08-16 20:05:58Z mike $". | |
ef416fc2 | 189 | */ |