]>
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 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...
34 * '_cupsLangPrintf()' - Print a formatted message string to a file.
37 int /* O - Number of bytes written */
38 _cupsLangPrintf(FILE *fp
, /* I - File to write to */
39 const char *message
, /* I - Message string to use */
40 ...) /* I - Additional arguments as needed */
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 */
46 _cups_globals_t
*cg
; /* Global data */
58 if (!cg
->lang_default
)
59 cg
->lang_default
= cupsLangDefault();
62 * Format the string...
65 va_start(ap
, message
);
66 bytes
= vsnprintf(buffer
, sizeof(buffer
),
67 _cupsLangString(cg
->lang_default
, message
), ap
);
71 * Transcode to the destination charset...
74 bytes
= cupsUTF8ToCharset(output
, (cups_utf8_t
*)buffer
, sizeof(output
),
75 cg
->lang_default
->encoding
);
78 * Write the string and return the number of bytes written...
82 return ((int)fwrite(output
, 1, bytes
, fp
));
89 * '_cupsLangPuts()' - Print a static message string to a file.
92 int /* O - Number of bytes written */
93 _cupsLangPuts(FILE *fp
, /* I - File to write to */
94 const char *message
) /* I - Message string to use */
96 int bytes
; /* Number of bytes formatted */
97 char output
[2048]; /* Message buffer */
98 _cups_globals_t
*cg
; /* Global data */
110 if (!cg
->lang_default
)
111 cg
->lang_default
= cupsLangDefault();
114 * Transcode to the destination charset...
117 bytes
= cupsUTF8ToCharset(output
,
118 (cups_utf8_t
*)_cupsLangString(cg
->lang_default
,
120 sizeof(output
), cg
->lang_default
->encoding
);
123 * Write the string and return the number of bytes written...
127 return ((int)fwrite(output
, 1, bytes
, fp
));
134 * '_cupsSetLocale()' - Set the current locale and transcode the command-line.
138 _cupsSetLocale(char *argv
[]) /* IO - Command-line arguments */
140 int i
; /* Looping var */
141 char buffer
[8192]; /* Command-line argument buffer */
142 _cups_globals_t
*cg
; /* Global data */
144 const char *lc_time
; /* Current LC_TIME value */
145 char new_lc_time
[255], /* New LC_TIME value */
146 *charset
; /* Pointer to character set */
151 * Set the locale so that times, etc. are displayed properly.
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...)
159 setlocale(LC_ALL
, "");
162 if ((lc_time
= setlocale(LC_TIME
, NULL
)) == NULL
)
163 lc_time
= setlocale(LC_ALL
, NULL
);
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
);
171 strlcpy(charset
, ".UTF-8", sizeof(new_lc_time
) - (charset
- new_lc_time
));
174 strcpy(new_lc_time
, "C");
176 setlocale(LC_TIME
, new_lc_time
);
180 * Initialize the default language info...
185 if (!cg
->lang_default
)
186 cg
->lang_default
= cupsLangDefault();
189 * Transcode the command-line arguments from the locale charset to
193 if (cg
->lang_default
->encoding
!= CUPS_US_ASCII
&&
194 cg
->lang_default
->encoding
!= CUPS_UTF8
)
196 for (i
= 1; argv
[i
]; i
++)
199 * Try converting from the locale charset to UTF-8...
202 if (cupsCharsetToUTF8((cups_utf8_t
*)buffer
, argv
[i
], sizeof(buffer
),
203 cg
->lang_default
->encoding
) < 0)
207 * Save the new string if it differs from the original...
210 if (strcmp(buffer
, argv
[i
]))
211 argv
[i
] = strdup(buffer
);
218 * End of "$Id: langprintf.c 6649 2007-07-11 21:46:42Z mike $".