2 // Shared message catalog class for the CUPS PPD Compiler.
4 // Copyright 2007-2017 by Apple Inc.
5 // Copyright 2002-2006 by Easy Software Products.
7 // Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 // Include necessary headers...
14 #include "ppdc-private.h"
18 // Character encodings...
34 #if defined(__APPLE__) && defined(CUPS_BUNDLEDIR)
35 static void apple_add_message(CFStringRef key
, CFStringRef val
, ppdcCatalog
*c
);
36 #endif /* __APPLE__ && CUPS_BUNDLEDIR */
37 static int get_utf8(char *&ptr
);
38 static int get_utf16(cups_file_t
*fp
, ppdc_cs_t
&cs
);
39 static int put_utf8(int ch
, char *&ptr
, char *end
);
40 static int put_utf16(cups_file_t
*fp
, int ch
);
44 // 'ppdcCatalog::ppdcCatalog()' - Create a shared message catalog.
47 ppdcCatalog::ppdcCatalog(const char *l
, // I - Locale
48 const char *f
) // I - Message catalog file
53 locale
= new ppdcString(l
);
54 filename
= new ppdcString(f
);
55 messages
= new ppdcArray();
57 if (l
&& strcmp(l
, "en"))
59 // Try loading the base messages for this locale...
60 char pofile
[1024]; // Message catalog file
63 #if defined(__APPLE__) && defined(CUPS_BUNDLEDIR)
64 char applelang
[256]; // Apple language ID
65 CFURLRef url
; // URL to cups.strings file
66 CFReadStreamRef stream
= NULL
; // File stream
67 CFPropertyListRef plist
= NULL
; // Localization file
69 snprintf(pofile
, sizeof(pofile
), CUPS_BUNDLEDIR
"/Resources/%s.lproj/cups.strings", _cupsAppleLanguage(l
, applelang
, sizeof(applelang
)));
70 if (access(pofile
, 0))
72 // Try alternate lproj directory names...
73 const char *tl
= l
; // Temporary locale string
75 if (!strncmp(l
, "en", 2))
77 else if (!strncmp(l
, "nb", 2))
79 else if (!strncmp(l
, "nl", 2))
81 else if (!strncmp(l
, "fr", 2))
83 else if (!strncmp(l
, "de", 2))
85 else if (!strncmp(l
, "it", 2))
87 else if (!strncmp(l
, "ja", 2))
89 else if (!strncmp(l
, "es", 2))
92 snprintf(pofile
, sizeof(pofile
), CUPS_BUNDLEDIR
"/Resources/%s.lproj/cups.strings", tl
);
95 url
= CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault
, (UInt8
*)pofile
, (CFIndex
)strlen(pofile
), false);
98 stream
= CFReadStreamCreateWithFile(kCFAllocatorDefault
, url
);
103 * Read the property list containing the localization data.
106 CFReadStreamOpen(stream
);
108 plist
= CFPropertyListCreateWithStream(kCFAllocatorDefault
, stream
, 0, kCFPropertyListImmutable
, NULL
, NULL
);
110 if (plist
&& CFGetTypeID(plist
) == CFDictionaryGetTypeID())
111 CFDictionaryApplyFunction((CFDictionaryRef
)plist
, (CFDictionaryApplierFunction
)apple_add_message
, this);
123 _cups_globals_t
*cg
= _cupsGlobals();
124 // Global information
126 snprintf(pofile
, sizeof(pofile
), "%s/%s/cups_%s.po", cg
->localedir
, l
, l
);
128 if (load_messages(pofile
) && strchr(l
, '_'))
130 // Try the base locale...
131 char baseloc
[3]; // Base locale...
134 strlcpy(baseloc
, l
, sizeof(baseloc
));
135 snprintf(pofile
, sizeof(pofile
), "%s/%s/cups_%s.po", cg
->localedir
,
138 load_messages(pofile
);
140 #endif /* __APPLE__ && CUPS_BUNDLEDIR */
149 // 'ppdcCatalog::~ppdcCatalog()' - Destroy a shared message catalog.
152 ppdcCatalog::~ppdcCatalog()
163 // 'ppdcCatalog::add_message()' - Add a new message.
167 ppdcCatalog::add_message(
168 const char *id
, // I - Message ID to add
169 const char *string
) // I - Translation string
171 ppdcMessage
*m
; // Current message
172 char text
[1024]; // Text to translate
175 // Range check input...
179 // Verify that we don't already have the message ID...
180 for (m
= (ppdcMessage
*)messages
->first();
182 m
= (ppdcMessage
*)messages
->next())
183 if (!strcmp(m
->id
->value
, id
))
187 m
->string
->release();
188 m
->string
= new ppdcString(string
);
193 // Add the message...
196 snprintf(text
, sizeof(text
), "TRANSLATE %s", id
);
200 messages
->add(new ppdcMessage(id
, string
));
205 // 'ppdcCatalog::find_message()' - Find a message in a catalog...
208 const char * // O - Message text
209 ppdcCatalog::find_message(
210 const char *id
) // I - Message ID
212 ppdcMessage
*m
; // Current message
218 for (m
= (ppdcMessage
*)messages
->first();
220 m
= (ppdcMessage
*)messages
->next())
221 if (!strcmp(m
->id
->value
, id
))
222 return (m
->string
->value
);
229 // 'ppdcCatalog::load_messages()' - Load messages from a .po file.
232 int // O - 0 on success, -1 on failure
233 ppdcCatalog::load_messages(
234 const char *f
) // I - Message catalog file
236 cups_file_t
*fp
; // Message file
237 char line
[4096], // Line buffer
238 *ptr
, // Pointer into buffer
239 id
[4096], // Translation ID
240 str
[4096]; // Translation string
241 int linenum
; // Line number
244 // Open the message catalog file...
245 if ((fp
= cupsFileOpen(f
, "r")) == NULL
)
248 if ((ptr
= (char *)strrchr(f
, '.')) == NULL
)
249 goto unknown_load_format
;
250 else if (!strcmp(ptr
, ".strings"))
253 * Read messages in macOS ".strings" format, which are either UTF-8/UTF-16
254 * text files of the format:
258 * Strings files can also contain C-style comments.
261 ppdc_cs_t cs
= PPDC_CS_AUTO
; // Character set for file
262 int ch
; // Current character from file
263 char *end
; // End of buffer
271 while ((ch
= get_utf16(fp
, cs
)) != 0)
277 if ((ch
= get_utf16(fp
, cs
)) == 0)
292 put_utf8(ch
, ptr
, end
);
296 // Start of a comment?
297 if ((ch
= get_utf16(fp
, cs
)) == 0)
305 while ((ch
= get_utf16(fp
, cs
)) != 0)
307 if (ch
== '/' && lastch
== '*')
315 // Skip C++ comment...
316 while ((ch
= get_utf16(fp
, cs
)) != 0)
323 // Start quoted string...
327 end
= str
+ sizeof(str
) - 1;
332 end
= id
+ sizeof(id
) - 1;
338 add_message(id
, str
);
343 else if (!strcmp(ptr
, ".po") || !strcmp(ptr
, ".gz"))
346 * Read messages from the catalog file until EOF...
348 * The format is the GNU gettext .po format, which is fairly simple:
351 * msgstr "localized text"
353 * The ID and localized text can span multiple lines using the form:
358 * "localized text spanning "
362 int which
, // In msgid?
363 haveid
, // Did we get a msgid string?
364 havestr
; // Did we get a msgstr string?
373 while (cupsFileGets(fp
, line
, sizeof(line
)))
377 // Skip blank and comment lines...
378 if (line
[0] == '#' || !line
[0])
381 // Strip the trailing quote...
382 if ((ptr
= (char *)strrchr(line
, '\"')) == NULL
)
384 _cupsLangPrintf(stderr
,
385 _("ppdc: Expected quoted string on line %d of %s."),
393 // Find start of value...
394 if ((ptr
= strchr(line
, '\"')) == NULL
)
396 _cupsLangPrintf(stderr
,
397 _("ppdc: Expected quoted string on line %d of %s."),
405 // Unquote the text...
406 char *sptr
, *dptr
; // Source/destination pointers
408 for (sptr
= ptr
, dptr
= ptr
; *sptr
;)
417 while (isdigit(*sptr
))
419 *dptr
= *dptr
* 8 + *sptr
- '0';
429 else if (*sptr
== 'r')
431 else if (*sptr
== 't')
445 // Create or add to a message...
446 if (!strncmp(line
, "msgid", 5))
448 if (haveid
&& havestr
)
449 add_message(id
, str
);
451 strlcpy(id
, ptr
, sizeof(id
));
457 else if (!strncmp(line
, "msgstr", 6))
461 _cupsLangPrintf(stderr
,
462 _("ppdc: Need a msgid line before any "
463 "translation strings on line %d of %s."),
469 strlcpy(str
, ptr
, sizeof(str
));
473 else if (line
[0] == '\"' && which
== 2)
474 strlcat(str
, ptr
, sizeof(str
));
475 else if (line
[0] == '\"' && which
== 1)
476 strlcat(id
, ptr
, sizeof(id
));
479 _cupsLangPrintf(stderr
, _("ppdc: Unexpected text on line %d of %s."),
486 if (haveid
&& havestr
)
487 add_message(id
, str
);
490 goto unknown_load_format
;
493 * Close the file and return...
501 * Unknown format error...
506 _cupsLangPrintf(stderr
,
507 _("ppdc: Unknown message catalog format for \"%s\"."), f
);
514 // 'ppdcCatalog::save_messages()' - Save the messages to a .po file.
517 int // O - 0 on success, -1 on error
518 ppdcCatalog::save_messages(
519 const char *f
) // I - File to save to
521 cups_file_t
*fp
; // Message file
522 ppdcMessage
*m
; // Current message
523 char *ptr
; // Pointer into string
524 int utf16
; // Output UTF-16 .strings file?
525 int ch
; // Current character
529 if ((ptr
= (char *)strrchr(f
, '.')) == NULL
)
532 if (!strcmp(ptr
, ".gz"))
533 fp
= cupsFileOpen(f
, "w9");
535 fp
= cupsFileOpen(f
, "w");
540 // For .strings files, write a BOM for big-endian output...
541 utf16
= !strcmp(ptr
, ".strings");
544 put_utf16(fp
, 0xfeff);
546 // Loop through all of the messages...
547 for (m
= (ppdcMessage
*)messages
->first();
549 m
= (ppdcMessage
*)messages
->next())
556 while ((ch
= get_utf8(ptr
)) != 0)
582 ptr
= m
->string
->value
;
583 while ((ch
= get_utf8(ptr
)) != 0)
609 cupsFilePuts(fp
, "msgid \"");
610 for (ptr
= m
->id
->value
; *ptr
; ptr
++)
614 cupsFilePuts(fp
, "\\n");
617 cupsFilePuts(fp
, "\\\\");
620 cupsFilePuts(fp
, "\\\"");
623 cupsFilePutChar(fp
, *ptr
);
626 cupsFilePuts(fp
, "\"\n");
628 cupsFilePuts(fp
, "msgstr \"");
629 for (ptr
= m
->string
->value
; *ptr
; ptr
++)
633 cupsFilePuts(fp
, "\\n");
636 cupsFilePuts(fp
, "\\\\");
639 cupsFilePuts(fp
, "\\\"");
642 cupsFilePutChar(fp
, *ptr
);
645 cupsFilePuts(fp
, "\"\n");
647 cupsFilePutChar(fp
, '\n');
657 #if defined(__APPLE__) && defined(CUPS_BUNDLEDIR)
659 // 'apple_add_message()' - Add a message from a localization dictionary.
663 apple_add_message(CFStringRef key
, // I - Localization key
664 CFStringRef val
, // I - Localized value
665 ppdcCatalog
*c
) // I - Message catalog
667 char id
[1024], // Message id
668 str
[1024]; // Localized message
671 if (CFStringGetCString(key
, id
, sizeof(id
), kCFStringEncodingUTF8
) &&
672 CFStringGetCString(val
, str
, sizeof(str
), kCFStringEncodingUTF8
))
673 c
->add_message(id
, str
);
675 #endif /* __APPLE__ && CUPS_BUNDLEDIR */
679 // 'get_utf8()' - Get a UTF-8 character.
682 static int // O - Unicode character or 0 on EOF
683 get_utf8(char *&ptr
) // IO - Pointer to character
685 int ch
; // Current character
688 if ((ch
= *ptr
++ & 255) < 0xc0)
691 if ((ch
& 0xe0) == 0xc0)
694 if ((*ptr
& 0xc0) != 0x80)
697 ch
= ((ch
& 0x1f) << 6) | (*ptr
++ & 0x3f);
699 else if ((ch
& 0xf0) == 0xe0)
701 // Three-byte UTF-8...
702 if ((*ptr
& 0xc0) != 0x80)
705 ch
= ((ch
& 0x0f) << 6) | (*ptr
++ & 0x3f);
707 if ((*ptr
& 0xc0) != 0x80)
710 ch
= (ch
<< 6) | (*ptr
++ & 0x3f);
712 else if ((ch
& 0xf8) == 0xf0)
714 // Four-byte UTF-8...
715 if ((*ptr
& 0xc0) != 0x80)
718 ch
= ((ch
& 0x07) << 6) | (*ptr
++ & 0x3f);
720 if ((*ptr
& 0xc0) != 0x80)
723 ch
= (ch
<< 6) | (*ptr
++ & 0x3f);
725 if ((*ptr
& 0xc0) != 0x80)
728 ch
= (ch
<< 6) | (*ptr
++ & 0x3f);
736 // 'get_utf16()' - Get a UTF-16 character...
739 static int // O - Unicode character or 0 on EOF
740 get_utf16(cups_file_t
*fp
, // I - File to read from
741 ppdc_cs_t
&cs
) // IO - Character set of file
743 int ch
; // Current character
744 unsigned char buffer
[3]; // Bytes
747 if (cs
== PPDC_CS_AUTO
)
749 // Get byte-order-mark, if present...
750 if (cupsFileRead(fp
, (char *)buffer
, 2) != 2)
753 if (buffer
[0] == 0xfe && buffer
[1] == 0xff)
755 // Big-endian UTF-16...
756 cs
= PPDC_CS_UTF16BE
;
758 if (cupsFileRead(fp
, (char *)buffer
, 2) != 2)
761 else if (buffer
[0] == 0xff && buffer
[1] == 0xfe)
763 // Little-endian UTF-16...
764 cs
= PPDC_CS_UTF16LE
;
766 if (cupsFileRead(fp
, (char *)buffer
, 2) != 2)
769 else if (buffer
[0] == 0x00 && buffer
[1] != 0x00)
771 // No BOM, assume big-endian UTF-16...
772 cs
= PPDC_CS_UTF16BE
;
774 else if (buffer
[0] != 0x00 && buffer
[1] == 0x00)
776 // No BOM, assume little-endian UTF-16...
777 cs
= PPDC_CS_UTF16LE
;
781 // No BOM, assume UTF-8...
787 else if (cs
!= PPDC_CS_UTF8
)
789 if (cupsFileRead(fp
, (char *)buffer
, 2) != 2)
793 if (cs
== PPDC_CS_UTF8
)
795 // UTF-8 character...
796 if ((ch
= cupsFileGetChar(fp
)) < 0)
799 if ((ch
& 0xe0) == 0xc0)
802 if (cupsFileRead(fp
, (char *)buffer
, 1) != 1)
805 if ((buffer
[0] & 0xc0) != 0x80)
808 ch
= ((ch
& 0x1f) << 6) | (buffer
[0] & 0x3f);
810 else if ((ch
& 0xf0) == 0xe0)
812 // Three-byte UTF-8...
813 if (cupsFileRead(fp
, (char *)buffer
, 2) != 2)
816 if ((buffer
[0] & 0xc0) != 0x80 ||
817 (buffer
[1] & 0xc0) != 0x80)
820 ch
= ((((ch
& 0x0f) << 6) | (buffer
[0] & 0x3f)) << 6) |
823 else if ((ch
& 0xf8) == 0xf0)
825 // Four-byte UTF-8...
826 if (cupsFileRead(fp
, (char *)buffer
, 3) != 3)
829 if ((buffer
[0] & 0xc0) != 0x80 ||
830 (buffer
[1] & 0xc0) != 0x80 ||
831 (buffer
[2] & 0xc0) != 0x80)
834 ch
= ((((((ch
& 0x07) << 6) | (buffer
[0] & 0x3f)) << 6) |
835 (buffer
[1] & 0x3f)) << 6) | (buffer
[2] & 0x3f);
840 // UTF-16 character...
841 if (cs
== PPDC_CS_UTF16BE
)
842 ch
= (buffer
[0] << 8) | buffer
[1];
844 ch
= (buffer
[1] << 8) | buffer
[0];
846 if (ch
>= 0xd800 && ch
<= 0xdbff)
848 // Handle multi-word encoding...
851 if (cupsFileRead(fp
, (char *)buffer
, 2) != 2)
854 if (cs
== PPDC_CS_UTF16BE
)
855 lch
= (buffer
[0] << 8) | buffer
[1];
857 lch
= (buffer
[1] << 8) | buffer
[0];
859 if (lch
< 0xdc00 || lch
>= 0xdfff)
862 ch
= (((ch
& 0x3ff) << 10) | (lch
& 0x3ff)) + 0x10000;
871 // 'put_utf8()' - Add a UTF-8 character to a string.
874 static int // O - 0 on success, -1 on failure
875 put_utf8(int ch
, // I - Unicode character
876 char *&ptr
, // IO - String pointer
877 char *end
) // I - End of buffer
890 if ((ptr
+ 1) >= end
)
893 *ptr
++ = (char)(0xc0 | (ch
>> 6));
894 *ptr
++ = (char)(0x80 | (ch
& 0x3f));
896 else if (ch
< 0x10000)
898 // Three-byte UTF-8...
899 if ((ptr
+ 2) >= end
)
902 *ptr
++ = (char)(0xe0 | (ch
>> 12));
903 *ptr
++ = (char)(0x80 | ((ch
>> 6) & 0x3f));
904 *ptr
++ = (char)(0x80 | (ch
& 0x3f));
908 // Four-byte UTF-8...
909 if ((ptr
+ 3) >= end
)
912 *ptr
++ = (char)(0xf0 | (ch
>> 18));
913 *ptr
++ = (char)(0x80 | ((ch
>> 12) & 0x3f));
914 *ptr
++ = (char)(0x80 | ((ch
>> 6) & 0x3f));
915 *ptr
++ = (char)(0x80 | (ch
& 0x3f));
923 // 'put_utf16()' - Write a UTF-16 character to a file.
926 static int // O - 0 on success, -1 on failure
927 put_utf16(cups_file_t
*fp
, // I - File to write to
928 int ch
) // I - Unicode character
930 unsigned char buffer
[4]; // Output buffer
935 // One-word UTF-16 big-endian...
936 buffer
[0] = (unsigned char)(ch
>> 8);
937 buffer
[1] = (unsigned char)ch
;
939 if (cupsFileWrite(fp
, (char *)buffer
, 2) == 2)
944 // Two-word UTF-16 big-endian...
947 buffer
[0] = (unsigned char)(0xd8 | (ch
>> 18));
948 buffer
[1] = (unsigned char)(ch
>> 10);
949 buffer
[2] = (unsigned char)(0xdc | ((ch
>> 8) & 0x03));
950 buffer
[3] = (unsigned char)ch
;
952 if (cupsFileWrite(fp
, (char *)buffer
, 4) == 4)