]>
git.ipfire.org Git - thirdparty/cups.git/blob - ppdc/ppdc-catalog.cxx
4 // Shared message catalog class for the CUPS PPD Compiler.
6 // Copyright 2007-2008 by Apple Inc.
7 // Copyright 2002-2006 by Easy Software Products.
9 // These coded instructions, statements, and computer programs are the
10 // property of Apple Inc. and are protected by Federal copyright
11 // law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 // which should have been included with this file. If this file is
13 // file is missing or damaged, see the license at "http://www.cups.org/".
20 // Include necessary headers...
24 #include <cups/globals.h>
28 // Character encodings...
44 static int get_utf8(char *&ptr
);
45 static int get_utf16(cups_file_t
*fp
, ppdc_cs_t
&cs
);
46 static int put_utf8(int ch
, char *&ptr
, char *end
);
47 static int put_utf16(cups_file_t
*fp
, int ch
);
51 // 'ppdcCatalog::ppdcCatalog()' - Create a shared message catalog.
54 ppdcCatalog::ppdcCatalog(const char *l
, // I - Locale
55 const char *f
) // I - Message catalog file
58 _cups_globals_t
*cg
= _cupsGlobals();
62 locale
= new ppdcString(l
);
63 filename
= new ppdcString(f
);
64 messages
= new ppdcArray();
68 // Try loading the base messages for this locale...
69 char pofile
[1024]; // Message catalog file
72 snprintf(pofile
, sizeof(pofile
), "%s/%s/cups_%s.po", cg
->localedir
, l
, l
);
74 if (load_messages(pofile
) && strchr(l
, '_'))
76 // Try the base locale...
77 char baseloc
[3]; // Base locale...
80 strlcpy(baseloc
, l
, sizeof(baseloc
));
81 snprintf(pofile
, sizeof(pofile
), "%s/%s/cups_%s.po", cg
->localedir
,
84 load_messages(pofile
);
94 // 'ppdcCatalog::~ppdcCatalog()' - Destroy a shared message catalog.
97 ppdcCatalog::~ppdcCatalog()
106 // 'ppdcCatalog::add_message()' - Add a new message.
110 ppdcCatalog::add_message(
111 const char *id
, // I - Message ID to add
112 const char *string
) // I - Translation string
114 ppdcMessage
*m
; // Current message
115 char text
[1024]; // Text to translate
118 // Range check input...
122 // Verify that we don't already have the message ID...
123 for (m
= (ppdcMessage
*)messages
->first();
125 m
= (ppdcMessage
*)messages
->next())
126 if (!strcmp(m
->id
->value
, id
))
130 m
->string
->release();
131 m
->string
= new ppdcString(string
);
136 // Add the message...
139 snprintf(text
, sizeof(text
), "TRANSLATE %s", id
);
143 messages
->add(new ppdcMessage(id
, text
));
148 // 'ppdcCatalog::find_message()' - Find a message in a catalog...
151 const char * // O - Message text
152 ppdcCatalog::find_message(
153 const char *id
) // I - Message ID
155 ppdcMessage
*m
; // Current message
158 for (m
= (ppdcMessage
*)messages
->first();
160 m
= (ppdcMessage
*)messages
->next())
161 if (!strcmp(m
->id
->value
, id
))
162 return (m
->string
->value
);
169 // 'ppdcCatalog::load_messages()' - Load messages from a .po file.
172 int // O - 0 on success, -1 on failure
173 ppdcCatalog::load_messages(
174 const char *f
) // I - Message catalog file
176 cups_file_t
*fp
; // Message file
177 char line
[4096], // Line buffer
178 *ptr
, // Pointer into buffer
179 id
[4096], // Translation ID
180 str
[4096]; // Translation string
181 int linenum
; // Line number
184 // Open the message catalog file...
185 if ((fp
= cupsFileOpen(f
, "r")) == NULL
)
188 if ((ptr
= (char *)strrchr(f
, '.')) == NULL
)
189 goto unknown_load_format
;
190 else if (!strcmp(ptr
, ".strings"))
193 * Read messages in Mac OS X ".strings" format, which are UTF-16 text
194 * files of the format:
198 * Strings files can also contain C-style comments.
201 ppdc_cs_t cs
= PPDC_CS_AUTO
; // Character set for file
202 int ch
; // Current character from file
203 char *end
; // End of buffer
211 while ((ch
= get_utf16(fp
, cs
)) != 0)
217 if ((ch
= get_utf16(fp
, cs
)) == 0)
232 put_utf8(ch
, ptr
, end
);
236 // Start of a comment?
237 if ((ch
= get_utf16(fp
, cs
)) == 0)
245 while ((ch
= get_utf16(fp
, cs
)) != 0)
247 if (ch
== '/' && lastch
== '*')
255 // Skip C++ comment...
256 while ((ch
= get_utf16(fp
, cs
)) != 0)
263 // Start quoted string...
267 end
= str
+ sizeof(str
) - 1;
272 end
= id
+ sizeof(id
) - 1;
278 add_message(id
, str
);
282 else if (!strcmp(ptr
, ".po") || !strcmp(ptr
, ".gz"))
285 * Read messages from the catalog file until EOF...
287 * The format is the GNU gettext .po format, which is fairly simple:
290 * msgstr "localized text"
292 * The ID and localized text can span multiple lines using the form:
297 * "localized text spanning "
301 int which
, // In msgid?
302 haveid
, // Did we get a msgid string?
303 havestr
; // Did we get a msgstr string?
312 while (cupsFileGets(fp
, line
, sizeof(line
)))
316 // Skip blank and comment lines...
317 if (line
[0] == '#' || !line
[0])
320 // Strip the trailing quote...
321 if ((ptr
= (char *)strrchr(line
, '\"')) == NULL
)
323 _cupsLangPrintf(stderr
,
324 _("ERROR: Expected quoted string on line %d of %s!\n"),
332 // Find start of value...
333 if ((ptr
= strchr(line
, '\"')) == NULL
)
335 _cupsLangPrintf(stderr
,
336 _("ERROR: Expected quoted string on line %d of %s!\n"),
344 // Unquote the text...
345 char *sptr
, *dptr
; // Source/destination pointers
347 for (sptr
= ptr
, dptr
= ptr
; *sptr
;)
356 while (isdigit(*sptr
))
358 *dptr
= *dptr
* 8 + *sptr
- '0';
368 else if (*sptr
== 'r')
370 else if (*sptr
== 't')
384 // Create or add to a message...
385 if (!strncmp(line
, "msgid", 5))
387 if (haveid
&& havestr
)
388 add_message(id
, str
);
390 strlcpy(id
, ptr
, sizeof(id
));
396 else if (!strncmp(line
, "msgstr", 6))
400 _cupsLangPrintf(stderr
,
401 _("ERROR: Need a msgid line before any "
402 "translation strings on line %d of %s!\n"),
408 strlcpy(str
, ptr
, sizeof(str
));
412 else if (line
[0] == '\"' && which
== 2)
413 strlcat(str
, ptr
, sizeof(str
));
414 else if (line
[0] == '\"' && which
== 1)
415 strlcat(id
, ptr
, sizeof(id
));
418 _cupsLangPrintf(stderr
, _("ERROR: Unexpected text on line %d of %s!\n"),
425 if (haveid
&& havestr
)
426 add_message(id
, str
);
429 goto unknown_load_format
;
432 * Close the file and return...
440 * Unknown format error...
445 _cupsLangPrintf(stderr
,
446 _("ERROR: Unknown message catalog format for \"%s\"!\n"), f
);
453 // 'ppdcCatalog::save_messages()' - Save the messages to a .po file.
456 int // O - 0 on success, -1 on error
457 ppdcCatalog::save_messages(
458 const char *f
) // I - File to save to
460 cups_file_t
*fp
; // Message file
461 ppdcMessage
*m
; // Current message
462 char *ptr
; // Pointer into string
463 int utf16
; // Output UTF-16 .strings file?
464 int ch
; // Current character
468 if ((ptr
= (char *)strrchr(f
, '.')) == NULL
)
471 if (!strcmp(ptr
, ".gz"))
472 fp
= cupsFileOpen(f
, "w9");
474 fp
= cupsFileOpen(f
, "w");
479 // For .strings files, write a BOM for big-endian output...
480 utf16
= !strcmp(ptr
, ".strings");
483 put_utf16(fp
, 0xfeff);
485 // Loop through all of the messages...
486 for (m
= (ppdcMessage
*)messages
->first();
488 m
= (ppdcMessage
*)messages
->next())
495 while ((ch
= get_utf8(ptr
)) != 0)
521 ptr
= m
->string
->value
;
522 while ((ch
= get_utf8(ptr
)) != 0)
548 cupsFilePuts(fp
, "msgid \"");
549 for (ptr
= m
->id
->value
; *ptr
; ptr
++)
553 cupsFilePuts(fp
, "\\n");
556 cupsFilePuts(fp
, "\\\\");
559 cupsFilePuts(fp
, "\\\"");
562 cupsFilePutChar(fp
, *ptr
);
565 cupsFilePuts(fp
, "\"\n");
567 cupsFilePuts(fp
, "msgstr \"");
568 for (ptr
= m
->string
->value
; *ptr
; ptr
++)
572 cupsFilePuts(fp
, "\\n");
575 cupsFilePuts(fp
, "\\\\");
578 cupsFilePuts(fp
, "\\\"");
581 cupsFilePutChar(fp
, *ptr
);
584 cupsFilePuts(fp
, "\"\n");
586 cupsFilePutChar(fp
, '\n');
597 // 'get_utf8()' - Get a UTF-8 character.
600 static int // O - Unicode character or 0 on EOF
601 get_utf8(char *&ptr
) // IO - Pointer to character
603 int ch
; // Current character
606 if ((ch
= *ptr
++ & 255) < 0xc0)
609 if ((ch
& 0xe0) == 0xc0)
612 if ((*ptr
& 0xc0) != 0x80)
615 ch
= ((ch
& 0x1f) << 6) | (*ptr
++ & 0x3f);
617 else if ((ch
& 0xf0) == 0xe0)
619 // Three-byte UTF-8...
620 if ((*ptr
& 0xc0) != 0x80)
623 ch
= ((ch
& 0x0f) << 6) | (*ptr
++ & 0x3f);
625 if ((*ptr
& 0xc0) != 0x80)
628 ch
= (ch
<< 6) | (*ptr
++ & 0x3f);
630 else if ((ch
& 0xf8) == 0xf0)
632 // Four-byte UTF-8...
633 if ((*ptr
& 0xc0) != 0x80)
636 ch
= ((ch
& 0x07) << 6) | (*ptr
++ & 0x3f);
638 if ((*ptr
& 0xc0) != 0x80)
641 ch
= (ch
<< 6) | (*ptr
++ & 0x3f);
643 if ((*ptr
& 0xc0) != 0x80)
646 ch
= (ch
<< 6) | (*ptr
++ & 0x3f);
654 // 'get_utf16()' - Get a UTF-16 character...
657 static int // O - Unicode character or 0 on EOF
658 get_utf16(cups_file_t
*fp
, // I - File to read from
659 ppdc_cs_t
&cs
) // IO - Character set of file
661 int ch
; // Current character
662 unsigned char buffer
[3]; // Bytes
665 if (cs
== PPDC_CS_AUTO
)
667 // Get byte-order-mark, if present...
668 if (cupsFileRead(fp
, (char *)buffer
, 2) != 2)
671 if (buffer
[0] == 0xfe && buffer
[1] == 0xff)
673 // Big-endian UTF-16...
674 cs
= PPDC_CS_UTF16BE
;
676 if (cupsFileRead(fp
, (char *)buffer
, 2) != 2)
679 else if (buffer
[0] == 0xff && buffer
[1] == 0xfe)
681 // Little-endian UTF-16...
682 cs
= PPDC_CS_UTF16LE
;
684 if (cupsFileRead(fp
, (char *)buffer
, 2) != 2)
687 else if (buffer
[0] == 0x00 && buffer
[1] != 0x00)
689 // No BOM, assume big-endian UTF-16...
690 cs
= PPDC_CS_UTF16BE
;
692 else if (buffer
[0] != 0x00 && buffer
[1] == 0x00)
694 // No BOM, assume little-endian UTF-16...
695 cs
= PPDC_CS_UTF16LE
;
699 // No BOM, assume UTF-8...
705 else if (cs
!= PPDC_CS_UTF8
)
707 if (cupsFileRead(fp
, (char *)buffer
, 2) != 2)
711 if (cs
== PPDC_CS_UTF8
)
713 // UTF-8 character...
714 if ((ch
= cupsFileGetChar(fp
)) < 0)
717 if ((ch
& 0xe0) == 0xc0)
720 if (cupsFileRead(fp
, (char *)buffer
, 1) != 1)
723 if ((buffer
[0] & 0xc0) != 0x80)
726 ch
= ((ch
& 0x1f) << 6) | (buffer
[0] & 0x3f);
728 else if ((ch
& 0xf0) == 0xe0)
730 // Three-byte UTF-8...
731 if (cupsFileRead(fp
, (char *)buffer
, 2) != 2)
734 if ((buffer
[0] & 0xc0) != 0x80 ||
735 (buffer
[1] & 0xc0) != 0x80)
738 ch
= ((((ch
& 0x0f) << 6) | (buffer
[0] & 0x3f)) << 6) |
741 else if ((ch
& 0xf8) == 0xf0)
743 // Four-byte UTF-8...
744 if (cupsFileRead(fp
, (char *)buffer
, 3) != 3)
747 if ((buffer
[0] & 0xc0) != 0x80 ||
748 (buffer
[1] & 0xc0) != 0x80 ||
749 (buffer
[2] & 0xc0) != 0x80)
752 ch
= ((((((ch
& 0x07) << 6) | (buffer
[0] & 0x3f)) << 6) |
753 (buffer
[1] & 0x3f)) << 6) | (buffer
[2] & 0x3f);
758 // UTF-16 character...
759 if (cs
== PPDC_CS_UTF16BE
)
760 ch
= (buffer
[0] << 8) | buffer
[1];
762 ch
= (buffer
[1] << 8) | buffer
[0];
764 if (ch
>= 0xd800 && ch
<= 0xdbff)
766 // Handle multi-word encoding...
769 if (cupsFileRead(fp
, (char *)buffer
, 2) != 2)
772 if (cs
== PPDC_CS_UTF16BE
)
773 lch
= (buffer
[0] << 8) | buffer
[1];
775 lch
= (buffer
[1] << 8) | buffer
[0];
777 if (lch
< 0xdc00 || lch
>= 0xdfff)
780 ch
= (((ch
& 0x3ff) << 10) | (lch
& 0x3ff)) + 0x10000;
789 // 'put_utf8()' - Add a UTF-8 character to a string.
792 static int // O - 0 on success, -1 on failure
793 put_utf8(int ch
, // I - Unicode character
794 char *&ptr
, // IO - String pointer
795 char *end
) // I - End of buffer
808 if ((ptr
+ 1) >= end
)
811 *ptr
++ = 0xc0 | (ch
>> 6);
812 *ptr
++ = 0x80 | (ch
& 0x3f);
814 else if (ch
< 0x10000)
816 // Three-byte UTF-8...
817 if ((ptr
+ 2) >= end
)
820 *ptr
++ = 0xe0 | (ch
>> 12);
821 *ptr
++ = 0x80 | ((ch
>> 6) & 0x3f);
822 *ptr
++ = 0x80 | (ch
& 0x3f);
826 // Four-byte UTF-8...
827 if ((ptr
+ 3) >= end
)
830 *ptr
++ = 0xf0 | (ch
>> 18);
831 *ptr
++ = 0x80 | ((ch
>> 12) & 0x3f);
832 *ptr
++ = 0x80 | ((ch
>> 6) & 0x3f);
833 *ptr
++ = 0x80 | (ch
& 0x3f);
841 // 'put_utf16()' - Write a UTF-16 character to a file.
844 static int // O - 0 on success, -1 on failure
845 put_utf16(cups_file_t
*fp
, // I - File to write to
846 int ch
) // I - Unicode character
848 unsigned char buffer
[4]; // Output buffer
853 // One-word UTF-16 big-endian...
857 if (cupsFileWrite(fp
, (char *)buffer
, 2) == 2)
862 // Two-word UTF-16 big-endian...
865 buffer
[0] = 0xd8 | (ch
>> 18);
866 buffer
[1] = ch
>> 10;
867 buffer
[2] = 0xdc | ((ch
>> 8) & 0x03);
870 if (cupsFileWrite(fp
, (char *)buffer
, 4) == 4)