]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/langprintf.c
2 * "$Id: langprintf.c 6649 2007-07-11 21:46:42Z mike $"
4 * Localized printf/puts functions for the Common UNIX Printing
7 * Copyright 2007-2008 by Apple Inc.
8 * Copyright 2002-2007 by Easy Software Products.
10 * These coded instructions, statements, and computer programs are the
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/".
16 * This file is subject to the Apple OS-Developed Software exception.
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 command-line.
26 * Include necessary headers...
35 * '_cupsLangPrintError()' - Print a message followed by a standard error.
39 _cupsLangPrintError(const char *message
)/* I - Message */
41 int bytes
; /* Number of bytes formatted */
42 int last_errno
; /* Last error */
43 char buffer
[2048], /* Message buffer */
44 output
[8192]; /* Output buffer */
45 _cups_globals_t
*cg
; /* Global data */
56 * Save the errno value...
62 * Get the message catalog...
67 if (!cg
->lang_default
)
68 cg
->lang_default
= cupsLangDefault();
71 * Format the message...
74 bytes
= snprintf(buffer
, sizeof(buffer
), "%s: %s",
75 _cupsLangString(cg
->lang_default
, message
),
76 strerror(last_errno
));
79 * Convert and write to stderr...
82 bytes
= cupsUTF8ToCharset(output
, (cups_utf8_t
*)buffer
, sizeof(output
),
83 cg
->lang_default
->encoding
);
86 fwrite(output
, 1, bytes
, stderr
);
91 * '_cupsLangPrintf()' - Print a formatted message string to a file.
94 int /* O - Number of bytes written */
95 _cupsLangPrintf(FILE *fp
, /* I - File to write to */
96 const char *message
, /* I - Message string to use */
97 ...) /* I - Additional arguments as needed */
99 int bytes
; /* Number of bytes formatted */
100 char buffer
[2048], /* Message buffer */
101 output
[8192]; /* Output buffer */
102 va_list ap
; /* Pointer to additional arguments */
103 _cups_globals_t
*cg
; /* Global data */
115 if (!cg
->lang_default
)
116 cg
->lang_default
= cupsLangDefault();
119 * Format the string...
122 va_start(ap
, message
);
123 bytes
= vsnprintf(buffer
, sizeof(buffer
),
124 _cupsLangString(cg
->lang_default
, message
), ap
);
128 * Transcode to the destination charset...
131 bytes
= cupsUTF8ToCharset(output
, (cups_utf8_t
*)buffer
, sizeof(output
),
132 cg
->lang_default
->encoding
);
135 * Write the string and return the number of bytes written...
139 return ((int)fwrite(output
, 1, bytes
, fp
));
146 * '_cupsLangPuts()' - Print a static message string to a file.
149 int /* O - Number of bytes written */
150 _cupsLangPuts(FILE *fp
, /* I - File to write to */
151 const char *message
) /* I - Message string to use */
153 int bytes
; /* Number of bytes formatted */
154 char output
[2048]; /* Message buffer */
155 _cups_globals_t
*cg
; /* Global data */
167 if (!cg
->lang_default
)
168 cg
->lang_default
= cupsLangDefault();
171 * Transcode to the destination charset...
174 bytes
= cupsUTF8ToCharset(output
,
175 (cups_utf8_t
*)_cupsLangString(cg
->lang_default
,
177 sizeof(output
), cg
->lang_default
->encoding
);
180 * Write the string and return the number of bytes written...
184 return ((int)fwrite(output
, 1, bytes
, fp
));
191 * '_cupsSetLocale()' - Set the current locale and transcode the command-line.
195 _cupsSetLocale(char *argv
[]) /* IO - Command-line arguments */
197 int i
; /* Looping var */
198 char buffer
[8192]; /* Command-line argument buffer */
199 _cups_globals_t
*cg
; /* Global data */
201 const char *lc_time
; /* Current LC_TIME value */
202 char new_lc_time
[255], /* New LC_TIME value */
203 *charset
; /* Pointer to character set */
208 * Set the locale so that times, etc. are displayed properly.
210 * Unfortunately, while we need the localized time value, we *don't*
211 * want to use the localized charset for the time value, so we need
212 * to set LC_TIME to the locale name with .UTF-8 on the end (if
213 * the locale includes a character set specifier...)
216 setlocale(LC_ALL
, "");
219 if ((lc_time
= setlocale(LC_TIME
, NULL
)) == NULL
)
220 lc_time
= setlocale(LC_ALL
, NULL
);
224 strlcpy(new_lc_time
, lc_time
, sizeof(new_lc_time
));
225 if ((charset
= strchr(new_lc_time
, '.')) == NULL
)
226 charset
= new_lc_time
+ strlen(new_lc_time
);
228 strlcpy(charset
, ".UTF-8", sizeof(new_lc_time
) - (charset
- new_lc_time
));
231 strcpy(new_lc_time
, "C");
233 setlocale(LC_TIME
, new_lc_time
);
237 * Initialize the default language info...
242 if (!cg
->lang_default
)
243 cg
->lang_default
= cupsLangDefault();
246 * Transcode the command-line arguments from the locale charset to
250 if (cg
->lang_default
->encoding
!= CUPS_US_ASCII
&&
251 cg
->lang_default
->encoding
!= CUPS_UTF8
)
253 for (i
= 1; argv
[i
]; i
++)
256 * Try converting from the locale charset to UTF-8...
259 if (cupsCharsetToUTF8((cups_utf8_t
*)buffer
, argv
[i
], sizeof(buffer
),
260 cg
->lang_default
->encoding
) < 0)
264 * Save the new string if it differs from the original...
267 if (strcmp(buffer
, argv
[i
]))
268 argv
[i
] = strdup(buffer
);
275 * End of "$Id: langprintf.c 6649 2007-07-11 21:46:42Z mike $".