2 // Shared message catalog class for the CUPS PPD Compiler.
4 // Copyright 2007-2014 by Apple Inc.
5 // Copyright 2002-2006 by Easy Software Products.
7 // These coded instructions, statements, and computer programs are the
8 // property of Apple Inc. and are protected by Federal copyright
9 // law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 // which should have been included with this file. If this file is
11 // file is missing or damaged, see the license at "http://www.cups.org/".
15 // Include necessary headers...
18 #include "ppdc-private.h"
22 // Character encodings...
38 #if defined(__APPLE__) && defined(CUPS_BUNDLEDIR)
39 static void apple_add_message(CFStringRef key
, CFStringRef val
, ppdcCatalog
*c
);
40 #endif /* __APPLE__ && CUPS_BUNDLEDIR */
41 static int get_utf8(char *&ptr
);
42 static int get_utf16(cups_file_t
*fp
, ppdc_cs_t
&cs
);
43 static int put_utf8(int ch
, char *&ptr
, char *end
);
44 static int put_utf16(cups_file_t
*fp
, int ch
);
48 // 'ppdcCatalog::ppdcCatalog()' - Create a shared message catalog.
51 ppdcCatalog::ppdcCatalog(const char *l
, // I - Locale
52 const char *f
) // I - Message catalog file
57 locale
= new ppdcString(l
);
58 filename
= new ppdcString(f
);
59 messages
= new ppdcArray();
63 // Try loading the base messages for this locale...
64 char pofile
[1024]; // Message catalog file
67 #if defined(__APPLE__) && defined(CUPS_BUNDLEDIR)
68 char applelang
[256]; // Apple language ID
69 CFURLRef url
; // URL to cups.strings file
70 CFReadStreamRef stream
= NULL
; // File stream
71 CFPropertyListRef plist
= NULL
; // Localization file
73 snprintf(pofile
, sizeof(pofile
), CUPS_BUNDLEDIR
"/Resources/%s.lproj/cups.strings", _cupsAppleLanguage(l
, applelang
, sizeof(applelang
)));
74 if (access(pofile
, 0))
76 // Try alternate lproj directory names...
77 const char *tl
= l
; // Temporary locale string
79 if (!strncmp(l
, "en", 2))
81 else if (!strncmp(l
, "nb", 2) || !strncmp(l
, "nl", 2))
83 else if (!strncmp(l
, "fr", 2))
85 else if (!strncmp(l
, "de", 2))
87 else if (!strncmp(l
, "it", 2))
89 else if (!strncmp(l
, "ja", 2))
91 else if (!strncmp(l
, "es", 2))
94 snprintf(pofile
, sizeof(pofile
), CUPS_BUNDLEDIR
"/Resources/%s.lproj/cups.strings", tl
);
97 url
= CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault
, (UInt8
*)pofile
, (CFIndex
)strlen(pofile
), false);
100 stream
= CFReadStreamCreateWithFile(kCFAllocatorDefault
, url
);
105 * Read the property list containing the localization data.
108 CFReadStreamOpen(stream
);
110 plist
= CFPropertyListCreateWithStream(kCFAllocatorDefault
, stream
, 0, kCFPropertyListImmutable
, NULL
, NULL
);
112 if (plist
&& CFGetTypeID(plist
) == CFDictionaryGetTypeID())
113 CFDictionaryApplyFunction((CFDictionaryRef
)plist
, (CFDictionaryApplierFunction
)apple_add_message
, this);
125 _cups_globals_t
*cg
= _cupsGlobals();
126 // Global information
128 snprintf(pofile
, sizeof(pofile
), "%s/%s/cups_%s.po", cg
->localedir
, l
, l
);
130 if (load_messages(pofile
) && strchr(l
, '_'))
132 // Try the base locale...
133 char baseloc
[3]; // Base locale...
136 strlcpy(baseloc
, l
, sizeof(baseloc
));
137 snprintf(pofile
, sizeof(pofile
), "%s/%s/cups_%s.po", cg
->localedir
,
140 load_messages(pofile
);
142 #endif /* __APPLE__ && CUPS_BUNDLEDIR */
151 // 'ppdcCatalog::~ppdcCatalog()' - Destroy a shared message catalog.
154 ppdcCatalog::~ppdcCatalog()
165 // 'ppdcCatalog::add_message()' - Add a new message.
169 ppdcCatalog::add_message(
170 const char *id
, // I - Message ID to add
171 const char *string
) // I - Translation string
173 ppdcMessage
*m
; // Current message
174 char text
[1024]; // Text to translate
177 // Range check input...
181 // Verify that we don't already have the message ID...
182 for (m
= (ppdcMessage
*)messages
->first();
184 m
= (ppdcMessage
*)messages
->next())
185 if (!strcmp(m
->id
->value
, id
))
189 m
->string
->release();
190 m
->string
= new ppdcString(string
);
195 // Add the message...
198 snprintf(text
, sizeof(text
), "TRANSLATE %s", id
);
202 messages
->add(new ppdcMessage(id
, string
));
207 // 'ppdcCatalog::find_message()' - Find a message in a catalog...
210 const char * // O - Message text
211 ppdcCatalog::find_message(
212 const char *id
) // I - Message ID
214 ppdcMessage
*m
; // Current message
220 for (m
= (ppdcMessage
*)messages
->first();
222 m
= (ppdcMessage
*)messages
->next())
223 if (!strcmp(m
->id
->value
, id
))
224 return (m
->string
->value
);
231 // 'ppdcCatalog::load_messages()' - Load messages from a .po file.
234 int // O - 0 on success, -1 on failure
235 ppdcCatalog::load_messages(
236 const char *f
) // I - Message catalog file
238 cups_file_t
*fp
; // Message file
239 char line
[4096], // Line buffer
240 *ptr
, // Pointer into buffer
241 id
[4096], // Translation ID
242 str
[4096]; // Translation string
243 int linenum
; // Line number
246 // Open the message catalog file...
247 if ((fp
= cupsFileOpen(f
, "r")) == NULL
)
250 if ((ptr
= (char *)strrchr(f
, '.')) == NULL
)
251 goto unknown_load_format
;
252 else if (!strcmp(ptr
, ".strings"))
255 * Read messages in OS X ".strings" format, which are either UTF-8/UTF-16
256 * text files of the format:
260 * Strings files can also contain C-style comments.
263 ppdc_cs_t cs
= PPDC_CS_AUTO
; // Character set for file
264 int ch
; // Current character from file
265 char *end
; // End of buffer
273 while ((ch
= get_utf16(fp
, cs
)) != 0)
279 if ((ch
= get_utf16(fp
, cs
)) == 0)
294 put_utf8(ch
, ptr
, end
);
298 // Start of a comment?
299 if ((ch
= get_utf16(fp
, cs
)) == 0)
307 while ((ch
= get_utf16(fp
, cs
)) != 0)
309 if (ch
== '/' && lastch
== '*')
317 // Skip C++ comment...
318 while ((ch
= get_utf16(fp
, cs
)) != 0)
325 // Start quoted string...
329 end
= str
+ sizeof(str
) - 1;
334 end
= id
+ sizeof(id
) - 1;
340 add_message(id
, str
);
345 else if (!strcmp(ptr
, ".po") || !strcmp(ptr
, ".gz"))
348 * Read messages from the catalog file until EOF...
350 * The format is the GNU gettext .po format, which is fairly simple:
353 * msgstr "localized text"
355 * The ID and localized text can span multiple lines using the form:
360 * "localized text spanning "
364 int which
, // In msgid?
365 haveid
, // Did we get a msgid string?
366 havestr
; // Did we get a msgstr string?
375 while (cupsFileGets(fp
, line
, sizeof(line
)))
379 // Skip blank and comment lines...
380 if (line
[0] == '#' || !line
[0])
383 // Strip the trailing quote...
384 if ((ptr
= (char *)strrchr(line
, '\"')) == NULL
)
386 _cupsLangPrintf(stderr
,
387 _("ppdc: Expected quoted string on line %d of %s."),
395 // Find start of value...
396 if ((ptr
= strchr(line
, '\"')) == NULL
)
398 _cupsLangPrintf(stderr
,
399 _("ppdc: Expected quoted string on line %d of %s."),
407 // Unquote the text...
408 char *sptr
, *dptr
; // Source/destination pointers
410 for (sptr
= ptr
, dptr
= ptr
; *sptr
;)
419 while (isdigit(*sptr
))
421 *dptr
= *dptr
* 8 + *sptr
- '0';
431 else if (*sptr
== 'r')
433 else if (*sptr
== 't')
447 // Create or add to a message...
448 if (!strncmp(line
, "msgid", 5))
450 if (haveid
&& havestr
)
451 add_message(id
, str
);
453 strlcpy(id
, ptr
, sizeof(id
));
459 else if (!strncmp(line
, "msgstr", 6))
463 _cupsLangPrintf(stderr
,
464 _("ppdc: Need a msgid line before any "
465 "translation strings on line %d of %s."),
471 strlcpy(str
, ptr
, sizeof(str
));
475 else if (line
[0] == '\"' && which
== 2)
476 strlcat(str
, ptr
, sizeof(str
));
477 else if (line
[0] == '\"' && which
== 1)
478 strlcat(id
, ptr
, sizeof(id
));
481 _cupsLangPrintf(stderr
, _("ppdc: Unexpected text on line %d of %s."),
488 if (haveid
&& havestr
)
489 add_message(id
, str
);
492 goto unknown_load_format
;
495 * Close the file and return...
503 * Unknown format error...
508 _cupsLangPrintf(stderr
,
509 _("ppdc: Unknown message catalog format for \"%s\"."), f
);
516 // 'ppdcCatalog::save_messages()' - Save the messages to a .po file.
519 int // O - 0 on success, -1 on error
520 ppdcCatalog::save_messages(
521 const char *f
) // I - File to save to
523 cups_file_t
*fp
; // Message file
524 ppdcMessage
*m
; // Current message
525 char *ptr
; // Pointer into string
526 int utf16
; // Output UTF-16 .strings file?
527 int ch
; // Current character
531 if ((ptr
= (char *)strrchr(f
, '.')) == NULL
)
534 if (!strcmp(ptr
, ".gz"))
535 fp
= cupsFileOpen(f
, "w9");
537 fp
= cupsFileOpen(f
, "w");
542 // For .strings files, write a BOM for big-endian output...
543 utf16
= !strcmp(ptr
, ".strings");
546 put_utf16(fp
, 0xfeff);
548 // Loop through all of the messages...
549 for (m
= (ppdcMessage
*)messages
->first();
551 m
= (ppdcMessage
*)messages
->next())
558 while ((ch
= get_utf8(ptr
)) != 0)
584 ptr
= m
->string
->value
;
585 while ((ch
= get_utf8(ptr
)) != 0)
611 cupsFilePuts(fp
, "msgid \"");
612 for (ptr
= m
->id
->value
; *ptr
; ptr
++)
616 cupsFilePuts(fp
, "\\n");
619 cupsFilePuts(fp
, "\\\\");
622 cupsFilePuts(fp
, "\\\"");
625 cupsFilePutChar(fp
, *ptr
);
628 cupsFilePuts(fp
, "\"\n");
630 cupsFilePuts(fp
, "msgstr \"");
631 for (ptr
= m
->string
->value
; *ptr
; ptr
++)
635 cupsFilePuts(fp
, "\\n");
638 cupsFilePuts(fp
, "\\\\");
641 cupsFilePuts(fp
, "\\\"");
644 cupsFilePutChar(fp
, *ptr
);
647 cupsFilePuts(fp
, "\"\n");
649 cupsFilePutChar(fp
, '\n');
659 #if defined(__APPLE__) && defined(CUPS_BUNDLEDIR)
661 // 'apple_add_message()' - Add a message from a localization dictionary.
665 apple_add_message(CFStringRef key
, // I - Localization key
666 CFStringRef val
, // I - Localized value
667 ppdcCatalog
*c
) // I - Message catalog
669 char id
[1024], // Message id
670 str
[1024]; // Localized message
673 if (CFStringGetCString(key
, id
, sizeof(id
), kCFStringEncodingUTF8
) &&
674 CFStringGetCString(val
, str
, sizeof(str
), kCFStringEncodingUTF8
))
675 c
->add_message(id
, str
);
677 #endif /* __APPLE__ && CUPS_BUNDLEDIR */
681 // 'get_utf8()' - Get a UTF-8 character.
684 static int // O - Unicode character or 0 on EOF
685 get_utf8(char *&ptr
) // IO - Pointer to character
687 int ch
; // Current character
690 if ((ch
= *ptr
++ & 255) < 0xc0)
693 if ((ch
& 0xe0) == 0xc0)
696 if ((*ptr
& 0xc0) != 0x80)
699 ch
= ((ch
& 0x1f) << 6) | (*ptr
++ & 0x3f);
701 else if ((ch
& 0xf0) == 0xe0)
703 // Three-byte UTF-8...
704 if ((*ptr
& 0xc0) != 0x80)
707 ch
= ((ch
& 0x0f) << 6) | (*ptr
++ & 0x3f);
709 if ((*ptr
& 0xc0) != 0x80)
712 ch
= (ch
<< 6) | (*ptr
++ & 0x3f);
714 else if ((ch
& 0xf8) == 0xf0)
716 // Four-byte UTF-8...
717 if ((*ptr
& 0xc0) != 0x80)
720 ch
= ((ch
& 0x07) << 6) | (*ptr
++ & 0x3f);
722 if ((*ptr
& 0xc0) != 0x80)
725 ch
= (ch
<< 6) | (*ptr
++ & 0x3f);
727 if ((*ptr
& 0xc0) != 0x80)
730 ch
= (ch
<< 6) | (*ptr
++ & 0x3f);
738 // 'get_utf16()' - Get a UTF-16 character...
741 static int // O - Unicode character or 0 on EOF
742 get_utf16(cups_file_t
*fp
, // I - File to read from
743 ppdc_cs_t
&cs
) // IO - Character set of file
745 int ch
; // Current character
746 unsigned char buffer
[3]; // Bytes
749 if (cs
== PPDC_CS_AUTO
)
751 // Get byte-order-mark, if present...
752 if (cupsFileRead(fp
, (char *)buffer
, 2) != 2)
755 if (buffer
[0] == 0xfe && buffer
[1] == 0xff)
757 // Big-endian UTF-16...
758 cs
= PPDC_CS_UTF16BE
;
760 if (cupsFileRead(fp
, (char *)buffer
, 2) != 2)
763 else if (buffer
[0] == 0xff && buffer
[1] == 0xfe)
765 // Little-endian UTF-16...
766 cs
= PPDC_CS_UTF16LE
;
768 if (cupsFileRead(fp
, (char *)buffer
, 2) != 2)
771 else if (buffer
[0] == 0x00 && buffer
[1] != 0x00)
773 // No BOM, assume big-endian UTF-16...
774 cs
= PPDC_CS_UTF16BE
;
776 else if (buffer
[0] != 0x00 && buffer
[1] == 0x00)
778 // No BOM, assume little-endian UTF-16...
779 cs
= PPDC_CS_UTF16LE
;
783 // No BOM, assume UTF-8...
789 else if (cs
!= PPDC_CS_UTF8
)
791 if (cupsFileRead(fp
, (char *)buffer
, 2) != 2)
795 if (cs
== PPDC_CS_UTF8
)
797 // UTF-8 character...
798 if ((ch
= cupsFileGetChar(fp
)) < 0)
801 if ((ch
& 0xe0) == 0xc0)
804 if (cupsFileRead(fp
, (char *)buffer
, 1) != 1)
807 if ((buffer
[0] & 0xc0) != 0x80)
810 ch
= ((ch
& 0x1f) << 6) | (buffer
[0] & 0x3f);
812 else if ((ch
& 0xf0) == 0xe0)
814 // Three-byte UTF-8...
815 if (cupsFileRead(fp
, (char *)buffer
, 2) != 2)
818 if ((buffer
[0] & 0xc0) != 0x80 ||
819 (buffer
[1] & 0xc0) != 0x80)
822 ch
= ((((ch
& 0x0f) << 6) | (buffer
[0] & 0x3f)) << 6) |
825 else if ((ch
& 0xf8) == 0xf0)
827 // Four-byte UTF-8...
828 if (cupsFileRead(fp
, (char *)buffer
, 3) != 3)
831 if ((buffer
[0] & 0xc0) != 0x80 ||
832 (buffer
[1] & 0xc0) != 0x80 ||
833 (buffer
[2] & 0xc0) != 0x80)
836 ch
= ((((((ch
& 0x07) << 6) | (buffer
[0] & 0x3f)) << 6) |
837 (buffer
[1] & 0x3f)) << 6) | (buffer
[2] & 0x3f);
842 // UTF-16 character...
843 if (cs
== PPDC_CS_UTF16BE
)
844 ch
= (buffer
[0] << 8) | buffer
[1];
846 ch
= (buffer
[1] << 8) | buffer
[0];
848 if (ch
>= 0xd800 && ch
<= 0xdbff)
850 // Handle multi-word encoding...
853 if (cupsFileRead(fp
, (char *)buffer
, 2) != 2)
856 if (cs
== PPDC_CS_UTF16BE
)
857 lch
= (buffer
[0] << 8) | buffer
[1];
859 lch
= (buffer
[1] << 8) | buffer
[0];
861 if (lch
< 0xdc00 || lch
>= 0xdfff)
864 ch
= (((ch
& 0x3ff) << 10) | (lch
& 0x3ff)) + 0x10000;
873 // 'put_utf8()' - Add a UTF-8 character to a string.
876 static int // O - 0 on success, -1 on failure
877 put_utf8(int ch
, // I - Unicode character
878 char *&ptr
, // IO - String pointer
879 char *end
) // I - End of buffer
892 if ((ptr
+ 1) >= end
)
895 *ptr
++ = (char)(0xc0 | (ch
>> 6));
896 *ptr
++ = (char)(0x80 | (ch
& 0x3f));
898 else if (ch
< 0x10000)
900 // Three-byte UTF-8...
901 if ((ptr
+ 2) >= end
)
904 *ptr
++ = (char)(0xe0 | (ch
>> 12));
905 *ptr
++ = (char)(0x80 | ((ch
>> 6) & 0x3f));
906 *ptr
++ = (char)(0x80 | (ch
& 0x3f));
910 // Four-byte UTF-8...
911 if ((ptr
+ 3) >= end
)
914 *ptr
++ = (char)(0xf0 | (ch
>> 18));
915 *ptr
++ = (char)(0x80 | ((ch
>> 12) & 0x3f));
916 *ptr
++ = (char)(0x80 | ((ch
>> 6) & 0x3f));
917 *ptr
++ = (char)(0x80 | (ch
& 0x3f));
925 // 'put_utf16()' - Write a UTF-16 character to a file.
928 static int // O - 0 on success, -1 on failure
929 put_utf16(cups_file_t
*fp
, // I - File to write to
930 int ch
) // I - Unicode character
932 unsigned char buffer
[4]; // Output buffer
937 // One-word UTF-16 big-endian...
938 buffer
[0] = (unsigned char)(ch
>> 8);
939 buffer
[1] = (unsigned char)ch
;
941 if (cupsFileWrite(fp
, (char *)buffer
, 2) == 2)
946 // Two-word UTF-16 big-endian...
949 buffer
[0] = (unsigned char)(0xd8 | (ch
>> 18));
950 buffer
[1] = (unsigned char)(ch
>> 10);
951 buffer
[2] = (unsigned char)(0xdc | ((ch
>> 8) & 0x03));
952 buffer
[3] = (unsigned char)ch
;
954 if (cupsFileWrite(fp
, (char *)buffer
, 4) == 4)