]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/langprintf.c
2 * Localized printf/puts functions for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 2002-2007 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include "cups-private.h"
18 * '_cupsLangPrintError()' - Print a message followed by a standard error.
22 _cupsLangPrintError(const char *prefix
, /* I - Non-localized message prefix */
23 const char *message
)/* I - Message */
25 ssize_t bytes
; /* Number of bytes formatted */
26 int last_errno
; /* Last error */
27 char buffer
[2048], /* Message buffer */
28 *bufptr
, /* Pointer into buffer */
29 output
[8192]; /* Output buffer */
30 _cups_globals_t
*cg
; /* Global data */
41 * Save the errno value...
47 * Get the message catalog...
52 if (!cg
->lang_default
)
53 cg
->lang_default
= cupsLangDefault();
56 * Format the message...
61 snprintf(buffer
, sizeof(buffer
), "%s:", prefix
);
62 bufptr
= buffer
+ strlen(buffer
);
67 snprintf(bufptr
, sizeof(buffer
) - (size_t)(bufptr
- buffer
),
68 /* TRANSLATORS: Message is "subject: error" */
69 _cupsLangString(cg
->lang_default
, _("%s: %s")),
70 _cupsLangString(cg
->lang_default
, message
), strerror(last_errno
));
71 strlcat(buffer
, "\n", sizeof(buffer
));
74 * Convert and write to stderr...
77 bytes
= cupsUTF8ToCharset(output
, (cups_utf8_t
*)buffer
, sizeof(output
),
78 cg
->lang_default
->encoding
);
81 fwrite(output
, 1, (size_t)bytes
, stderr
);
86 * '_cupsLangPrintFilter()' - Print a formatted filter message string to a file.
89 int /* O - Number of bytes written */
91 FILE *fp
, /* I - File to write to */
92 const char *prefix
, /* I - Non-localized message prefix */
93 const char *message
, /* I - Message string to use */
94 ...) /* I - Additional arguments as needed */
96 ssize_t bytes
; /* Number of bytes formatted */
97 char temp
[2048], /* Temporary format buffer */
98 buffer
[2048], /* Message buffer */
99 output
[8192]; /* Output buffer */
100 va_list ap
; /* Pointer to additional arguments */
101 _cups_globals_t
*cg
; /* Global data */
113 if (!cg
->lang_default
)
114 cg
->lang_default
= cupsLangDefault();
117 * Format the string...
120 va_start(ap
, message
);
121 snprintf(temp
, sizeof(temp
), "%s: %s\n", prefix
,
122 _cupsLangString(cg
->lang_default
, message
));
123 vsnprintf(buffer
, sizeof(buffer
), temp
, ap
);
127 * Transcode to the destination charset...
130 bytes
= cupsUTF8ToCharset(output
, (cups_utf8_t
*)buffer
, sizeof(output
),
131 cg
->lang_default
->encoding
);
134 * Write the string and return the number of bytes written...
138 return ((int)fwrite(output
, 1, (size_t)bytes
, fp
));
145 * '_cupsLangPrintf()' - Print a formatted message string to a file.
148 int /* O - Number of bytes written */
149 _cupsLangPrintf(FILE *fp
, /* I - File to write to */
150 const char *message
, /* I - Message string to use */
151 ...) /* I - Additional arguments as needed */
153 ssize_t bytes
; /* Number of bytes formatted */
154 char buffer
[2048], /* Message buffer */
155 output
[8192]; /* Output buffer */
156 va_list ap
; /* Pointer to additional arguments */
157 _cups_globals_t
*cg
; /* Global data */
169 if (!cg
->lang_default
)
170 cg
->lang_default
= cupsLangDefault();
173 * Format the string...
176 va_start(ap
, message
);
177 vsnprintf(buffer
, sizeof(buffer
) - 1,
178 _cupsLangString(cg
->lang_default
, message
), ap
);
181 strlcat(buffer
, "\n", sizeof(buffer
));
184 * Transcode to the destination charset...
187 bytes
= cupsUTF8ToCharset(output
, (cups_utf8_t
*)buffer
, sizeof(output
),
188 cg
->lang_default
->encoding
);
191 * Write the string and return the number of bytes written...
195 return ((int)fwrite(output
, 1, (size_t)bytes
, fp
));
202 * '_cupsLangPuts()' - Print a static message string to a file.
205 int /* O - Number of bytes written */
206 _cupsLangPuts(FILE *fp
, /* I - File to write to */
207 const char *message
) /* I - Message string to use */
209 ssize_t bytes
; /* Number of bytes formatted */
210 char output
[8192]; /* Message buffer */
211 _cups_globals_t
*cg
; /* Global data */
223 if (!cg
->lang_default
)
224 cg
->lang_default
= cupsLangDefault();
227 * Transcode to the destination charset...
230 bytes
= cupsUTF8ToCharset(output
,
231 (cups_utf8_t
*)_cupsLangString(cg
->lang_default
,
233 sizeof(output
) - 4, cg
->lang_default
->encoding
);
234 bytes
+= cupsUTF8ToCharset(output
+ bytes
, (cups_utf8_t
*)"\n", (int)(sizeof(output
) - (size_t)bytes
), cg
->lang_default
->encoding
);
237 * Write the string and return the number of bytes written...
241 return ((int)fwrite(output
, 1, (size_t)bytes
, fp
));
248 * '_cupsSetLocale()' - Set the current locale and transcode the command-line.
252 _cupsSetLocale(char *argv
[]) /* IO - Command-line arguments */
254 int i
; /* Looping var */
255 char buffer
[8192]; /* Command-line argument buffer */
256 _cups_globals_t
*cg
; /* Global data */
258 const char *lc_time
; /* Current LC_TIME value */
259 char new_lc_time
[255], /* New LC_TIME value */
260 *charset
; /* Pointer to character set */
265 * Set the locale so that times, etc. are displayed properly.
267 * Unfortunately, while we need the localized time value, we *don't*
268 * want to use the localized charset for the time value, so we need
269 * to set LC_TIME to the locale name with .UTF-8 on the end (if
270 * the locale includes a character set specifier...)
273 setlocale(LC_ALL
, "");
276 if ((lc_time
= setlocale(LC_TIME
, NULL
)) == NULL
)
277 lc_time
= setlocale(LC_ALL
, NULL
);
281 strlcpy(new_lc_time
, lc_time
, sizeof(new_lc_time
));
282 if ((charset
= strchr(new_lc_time
, '.')) == NULL
)
283 charset
= new_lc_time
+ strlen(new_lc_time
);
285 strlcpy(charset
, ".UTF-8", sizeof(new_lc_time
) - (size_t)(charset
- new_lc_time
));
288 strlcpy(new_lc_time
, "C", sizeof(new_lc_time
));
290 setlocale(LC_TIME
, new_lc_time
);
294 * Initialize the default language info...
299 if (!cg
->lang_default
)
300 cg
->lang_default
= cupsLangDefault();
303 * Transcode the command-line arguments from the locale charset to
307 if (cg
->lang_default
->encoding
!= CUPS_US_ASCII
&&
308 cg
->lang_default
->encoding
!= CUPS_UTF8
)
310 for (i
= 1; argv
[i
]; i
++)
313 * Try converting from the locale charset to UTF-8...
316 if (cupsCharsetToUTF8((cups_utf8_t
*)buffer
, argv
[i
], sizeof(buffer
),
317 cg
->lang_default
->encoding
) < 0)
321 * Save the new string if it differs from the original...
324 if (strcmp(buffer
, argv
[i
]))
325 argv
[i
] = strdup(buffer
);