]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/langprintf.c
Load cups into easysw/current.
[thirdparty/cups.git] / cups / langprintf.c
CommitLineData
ef416fc2 1/*
bc44d920 2 * "$Id: langprintf.c 6649 2007-07-11 21:46:42Z mike $"
ef416fc2 3 *
4 * Localized printf/puts functions for the Common UNIX Printing
5 * System (CUPS).
6 *
bc44d920 7 * Copyright 2007 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 *
20 * _cupsLangPrintf() - Print a formatted message string to a file.
21 * _cupsLangPuts() - Print a static message string to a file.
07725fee 22 * _cupsSetLocale() - Set the current locale and transcode the command-line.
ef416fc2 23 */
24
25/*
26 * Include necessary headers...
27 */
28
29#include <stdio.h>
fa73b229 30#include "globals.h"
ef416fc2 31
32
33/*
34 * '_cupsLangPrintf()' - Print a formatted message string to a file.
35 */
36
37int /* O - Number of bytes written */
38_cupsLangPrintf(FILE *fp, /* I - File to write to */
ef416fc2 39 const char *message, /* I - Message string to use */
40 ...) /* I - Additional arguments as needed */
41{
42 int bytes; /* Number of bytes formatted */
43 char buffer[2048], /* Message buffer */
44 output[8192]; /* Output buffer */
45 va_list ap; /* Pointer to additional arguments */
fa73b229 46 _cups_globals_t *cg; /* Global data */
ef416fc2 47
48
49 /*
50 * Range check...
51 */
52
53 if (!fp || !message)
54 return (-1);
55
fa73b229 56 cg = _cupsGlobals();
57
58 if (!cg->lang_default)
59 cg->lang_default = cupsLangDefault();
ef416fc2 60
61 /*
62 * Format the string...
63 */
64
65 va_start(ap, message);
66 bytes = vsnprintf(buffer, sizeof(buffer),
fa73b229 67 _cupsLangString(cg->lang_default, message), ap);
ef416fc2 68 va_end(ap);
69
70 /*
71 * Transcode to the destination charset...
72 */
73
74 bytes = cupsUTF8ToCharset(output, (cups_utf8_t *)buffer, sizeof(output),
fa73b229 75 cg->lang_default->encoding);
ef416fc2 76
77 /*
78 * Write the string and return the number of bytes written...
79 */
80
81 if (bytes > 0)
b86bc4cf 82 return ((int)fwrite(output, 1, bytes, fp));
ef416fc2 83 else
84 return (bytes);
85}
86
87
88/*
89 * '_cupsLangPuts()' - Print a static message string to a file.
90 */
91
92int /* O - Number of bytes written */
93_cupsLangPuts(FILE *fp, /* I - File to write to */
ef416fc2 94 const char *message) /* I - Message string to use */
95{
96 int bytes; /* Number of bytes formatted */
97 char output[2048]; /* Message buffer */
fa73b229 98 _cups_globals_t *cg; /* Global data */
ef416fc2 99
100
101 /*
102 * Range check...
103 */
104
105 if (!fp || !message)
106 return (-1);
107
fa73b229 108 cg = _cupsGlobals();
109
110 if (!cg->lang_default)
111 cg->lang_default = cupsLangDefault();
ef416fc2 112
113 /*
114 * Transcode to the destination charset...
115 */
116
117 bytes = cupsUTF8ToCharset(output,
fa73b229 118 (cups_utf8_t *)_cupsLangString(cg->lang_default,
119 message),
120 sizeof(output), cg->lang_default->encoding);
ef416fc2 121
122 /*
123 * Write the string and return the number of bytes written...
124 */
125
126 if (bytes > 0)
b86bc4cf 127 return ((int)fwrite(output, 1, bytes, fp));
ef416fc2 128 else
129 return (bytes);
130}
131
132
133/*
07725fee 134 * '_cupsSetLocale()' - Set the current locale and transcode the command-line.
d09495fa 135 */
136
137void
07725fee 138_cupsSetLocale(char *argv[]) /* IO - Command-line arguments */
d09495fa 139{
07725fee 140 int i; /* Looping var */
141 char buffer[8192]; /* Command-line argument buffer */
142 _cups_globals_t *cg; /* Global data */
d09495fa 143#ifdef LC_TIME
07725fee 144 const char *lc_time; /* Current LC_TIME value */
145 char new_lc_time[255], /* New LC_TIME value */
d09495fa 146 *charset; /* Pointer to character set */
147#endif /* LC_TIME */
148
149
150 /*
151 * Set the locale so that times, etc. are displayed properly.
152 *
153 * Unfortunately, while we need the localized time value, we *don't*
154 * want to use the localized charset for the time value, so we need
155 * to set LC_TIME to the locale name with .UTF-8 on the end (if
156 * the locale includes a character set specifier...)
157 */
158
159 setlocale(LC_ALL, "");
160
161#ifdef LC_TIME
162 if ((lc_time = setlocale(LC_TIME, NULL)) == NULL)
163 lc_time = setlocale(LC_ALL, NULL);
164
165 if (lc_time)
166 {
167 strlcpy(new_lc_time, lc_time, sizeof(new_lc_time));
168 if ((charset = strchr(new_lc_time, '.')) == NULL)
169 charset = new_lc_time + strlen(new_lc_time);
170
171 strlcpy(charset, ".UTF-8", sizeof(new_lc_time) - (charset - new_lc_time));
172 }
173 else
174 strcpy(new_lc_time, "C");
175
176 setlocale(LC_TIME, new_lc_time);
177#endif /* LC_TIME */
07725fee 178
179 /*
180 * Initialize the default language info...
181 */
182
183 cg = _cupsGlobals();
184
185 if (!cg->lang_default)
186 cg->lang_default = cupsLangDefault();
187
188 /*
189 * Transcode the command-line arguments from the locale charset to
190 * UTF-8...
191 */
192
193 if (cg->lang_default->encoding != CUPS_US_ASCII &&
194 cg->lang_default->encoding != CUPS_UTF8)
195 {
196 for (i = 1; argv[i]; i ++)
197 {
198 /*
199 * Try converting from the locale charset to UTF-8...
200 */
201
202 if (cupsCharsetToUTF8((cups_utf8_t *)buffer, argv[i], sizeof(buffer),
203 cg->lang_default->encoding) < 0)
204 continue;
205
206 /*
207 * Save the new string if it differs from the original...
208 */
209
210 if (strcmp(buffer, argv[i]))
211 argv[i] = strdup(buffer);
212 }
213 }
d09495fa 214}
215
216
217/*
bc44d920 218 * End of "$Id: langprintf.c 6649 2007-07-11 21:46:42Z mike $".
ef416fc2 219 */