]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/langprintf.c
2 * "$Id: langprintf.c 6187 2007-01-10 16:20:42Z mike $"
4 * Localized printf/puts functions for the Common UNIX Printing
7 * Copyright 2002-2007 by Easy Software Products.
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
16 * Attn: CUPS Licensing Information
17 * Easy Software Products
18 * 44141 Airport View Drive, Suite 204
19 * Hollywood, Maryland 20636 USA
21 * Voice: (301) 373-9600
22 * EMail: cups-info@cups.org
23 * WWW: http://www.cups.org
25 * This file is subject to the Apple OS-Developed Software exception.
29 * _cupsLangPrintf() - Print a formatted message string to a file.
30 * _cupsLangPuts() - Print a static message string to a file.
31 * _cupsSetLocale() - Set the current locale and transcode the command-line.
35 * Include necessary headers...
43 * '_cupsLangPrintf()' - Print a formatted message string to a file.
46 int /* O - Number of bytes written */
47 _cupsLangPrintf(FILE *fp
, /* I - File to write to */
48 const char *message
, /* I - Message string to use */
49 ...) /* I - Additional arguments as needed */
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 */
55 _cups_globals_t
*cg
; /* Global data */
67 if (!cg
->lang_default
)
68 cg
->lang_default
= cupsLangDefault();
71 * Format the string...
74 va_start(ap
, message
);
75 bytes
= vsnprintf(buffer
, sizeof(buffer
),
76 _cupsLangString(cg
->lang_default
, message
), ap
);
80 * Transcode to the destination charset...
83 bytes
= cupsUTF8ToCharset(output
, (cups_utf8_t
*)buffer
, sizeof(output
),
84 cg
->lang_default
->encoding
);
87 * Write the string and return the number of bytes written...
91 return ((int)fwrite(output
, 1, bytes
, fp
));
98 * '_cupsLangPuts()' - Print a static message string to a file.
101 int /* O - Number of bytes written */
102 _cupsLangPuts(FILE *fp
, /* I - File to write to */
103 const char *message
) /* I - Message string to use */
105 int bytes
; /* Number of bytes formatted */
106 char output
[2048]; /* Message buffer */
107 _cups_globals_t
*cg
; /* Global data */
119 if (!cg
->lang_default
)
120 cg
->lang_default
= cupsLangDefault();
123 * Transcode to the destination charset...
126 bytes
= cupsUTF8ToCharset(output
,
127 (cups_utf8_t
*)_cupsLangString(cg
->lang_default
,
129 sizeof(output
), cg
->lang_default
->encoding
);
132 * Write the string and return the number of bytes written...
136 return ((int)fwrite(output
, 1, bytes
, fp
));
143 * '_cupsSetLocale()' - Set the current locale and transcode the command-line.
147 _cupsSetLocale(char *argv
[]) /* IO - Command-line arguments */
149 int i
; /* Looping var */
150 char buffer
[8192]; /* Command-line argument buffer */
151 _cups_globals_t
*cg
; /* Global data */
153 const char *lc_time
; /* Current LC_TIME value */
154 char new_lc_time
[255], /* New LC_TIME value */
155 *charset
; /* Pointer to character set */
160 * Set the locale so that times, etc. are displayed properly.
162 * Unfortunately, while we need the localized time value, we *don't*
163 * want to use the localized charset for the time value, so we need
164 * to set LC_TIME to the locale name with .UTF-8 on the end (if
165 * the locale includes a character set specifier...)
168 setlocale(LC_ALL
, "");
171 if ((lc_time
= setlocale(LC_TIME
, NULL
)) == NULL
)
172 lc_time
= setlocale(LC_ALL
, NULL
);
176 strlcpy(new_lc_time
, lc_time
, sizeof(new_lc_time
));
177 if ((charset
= strchr(new_lc_time
, '.')) == NULL
)
178 charset
= new_lc_time
+ strlen(new_lc_time
);
180 strlcpy(charset
, ".UTF-8", sizeof(new_lc_time
) - (charset
- new_lc_time
));
183 strcpy(new_lc_time
, "C");
185 setlocale(LC_TIME
, new_lc_time
);
189 * Initialize the default language info...
194 if (!cg
->lang_default
)
195 cg
->lang_default
= cupsLangDefault();
198 * Transcode the command-line arguments from the locale charset to
202 if (cg
->lang_default
->encoding
!= CUPS_US_ASCII
&&
203 cg
->lang_default
->encoding
!= CUPS_UTF8
)
205 for (i
= 1; argv
[i
]; i
++)
208 * Try converting from the locale charset to UTF-8...
211 if (cupsCharsetToUTF8((cups_utf8_t
*)buffer
, argv
[i
], sizeof(buffer
),
212 cg
->lang_default
->encoding
) < 0)
216 * Save the new string if it differs from the original...
219 if (strcmp(buffer
, argv
[i
]))
220 argv
[i
] = strdup(buffer
);
227 * End of "$Id: langprintf.c 6187 2007-01-10 16:20:42Z mike $".