]>
git.ipfire.org Git - thirdparty/cups.git/blob - locale/po2strings.c
4 * Convert a GNU gettext .po file to an Apple .strings file.
6 * Copyright 2007-2015 by Apple Inc.
8 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
11 * which should have been included with this file. If this file is
12 * file is missing or damaged, see the license at "http://www.cups.org/".
16 * po2strings filename.strings filename.po
20 * gcc -o po2strings po2strings.c `cups-config --libs`
23 #include <cups/cups-private.h>
27 * The .strings file format is simple:
32 * The GNU gettext .po format is also fairly simple:
36 * msgstr "localized text"
38 * The comment, msgid, and msgstr text can span multiple lines using the form:
45 * "localized text spanning "
48 * Both the msgid and msgstr strings use standard C quoting for special
49 * characters like newline and the double quote character.
52 static char *normalize_string(const char *idstr
, char *buffer
, size_t bufsize
);
56 * main() - Convert .po file to .strings.
59 int /* O - Exit code */
60 main(int argc
, /* I - Number of command-line args */
61 char *argv
[]) /* I - Command-line arguments */
63 int i
; /* Looping var */
64 const char *pofile
, /* .po filename */
65 *stringsfile
; /* .strings filename */
66 cups_file_t
*po
, /* .po file */
67 *strings
; /* .strings file */
68 char s
[4096], /* String buffer */
69 *ptr
, /* Pointer into buffer */
70 *temp
, /* New string */
71 *msgid
, /* msgid string */
72 *msgstr
, /* msgstr string */
73 normalized
[8192];/* Normalized msgid string */
74 size_t length
; /* Length of combined strings */
75 int use_msgid
; /* Use msgid strings for msgstr? */
79 * Process command-line arguments...
86 for (i
= 1; i
< argc
; i
++)
88 if (!strcmp(argv
[i
], "-m"))
90 else if (argv
[i
][0] == '-')
92 puts("Usage: po2strings [-m] filename.po filename.strings");
97 else if (!stringsfile
)
98 stringsfile
= argv
[i
];
101 puts("Usage: po2strings [-m] filename.po filename.strings");
106 if (!pofile
|| !stringsfile
)
108 puts("Usage: po2strings [-m] filename.po filename.strings");
113 * Read strings from the .po file and write to the .strings file...
116 if ((po
= cupsFileOpen(pofile
, "r")) == NULL
)
122 if ((strings
= cupsFileOpen(stringsfile
, "w")) == NULL
)
129 msgid
= msgstr
= NULL
;
131 while (cupsFileGets(po
, s
, sizeof(s
)) != NULL
)
133 if (s
[0] == '#' && s
[1] == '.')
136 * Copy comment string...
142 * First output the last localization string...
146 cupsFilePrintf(strings
, "\"%s\" = \"%s\";\n", msgid
,
147 (use_msgid
|| !*msgstr
) ? msgid
: msgstr
);
151 msgid
= msgstr
= NULL
;
154 cupsFilePrintf(strings
, "//%s\n", s
+ 2);
156 else if (s
[0] == '#' || !s
[0])
159 * Skip blank and file comment lines...
167 * Strip the trailing quote...
170 if ((ptr
= strrchr(s
, '\"')) == NULL
)
176 * Find start of value...
179 if ((ptr
= strchr(s
, '\"')) == NULL
)
185 * Create or add to a message...
188 if (!strncmp(s
, "msgid", 5))
191 * Output previous message as needed...
197 cupsFilePrintf(strings
, "\"%s\" = \"%s\";\n", msgid
, normalize_string((use_msgid
|| !*msgstr
) ? msgid
: msgstr
, normalized
, sizeof(normalized
)));
209 else if (s
[0] == '\"' && (msgid
|| msgstr
))
212 * Append to current string...
215 size_t ptrlen
= strlen(ptr
); /* Length of string */
217 length
= strlen(msgstr
? msgstr
: msgid
);
219 if ((temp
= realloc(msgstr
? msgstr
: msgid
,
220 length
+ ptrlen
+ 1)) == NULL
)
225 perror("Unable to allocate string");
232 * Copy the new portion to the end of the msgstr string - safe
233 * to use strcpy because the buffer is allocated to the correct
239 memcpy(msgstr
+ length
, ptr
, ptrlen
+ 1);
244 * Copy the new portion to the end of the msgid string - safe
245 * to use strcpy because the buffer is allocated to the correct
251 memcpy(msgid
+ length
, ptr
, ptrlen
+ 1);
254 else if (!strncmp(s
, "msgstr", 6) && msgid
)
263 if ((msgstr
= strdup(ptr
)) == NULL
)
266 perror("Unable to allocate msgstr");
276 cupsFilePrintf(strings
, "\"%s\" = \"%s\";\n", msgid
, normalize_string((use_msgid
|| !*msgstr
) ? msgid
: msgstr
, normalized
, sizeof(normalized
)));
286 cupsFileClose(strings
);
293 * 'normalize_string()' - Normalize a msgid string.
295 * This function converts ASCII ellipsis and double quotes to their Unicode
299 static char * /* O - Normalized string */
300 normalize_string(const char *idstr
, /* I - msgid string */
301 char *buffer
, /* I - Normalized string buffer */
302 size_t bufsize
) /* I - Size of string buffer */
304 char *bufptr
= buffer
, /* Pointer into buffer */
305 *bufend
= buffer
+ bufsize
- 3; /* End of buffer */
306 int quote
= 0; /* Quote direction */
309 while (*idstr
&& bufptr
< bufend
)
311 if (*idstr
== '.' && idstr
[1] == '.' && idstr
[2] == '.')
314 * Convert "..." to Unicode ellipsis...
317 *bufptr
++ = (char)0xE2;
318 *bufptr
++ = (char)0x80;
319 *bufptr
++ = (char)0xA6;
322 else if (*idstr
== '\"')
327 * Convert " to right quote.
330 *bufptr
++ = (char)0xE2;
331 *bufptr
++ = (char)0x80;
332 *bufptr
++ = (char)0x9D;
337 * Convert " to left quote.
340 *bufptr
++ = (char)0xE2;
341 *bufptr
++ = (char)0x80;
342 *bufptr
++ = (char)0x9C;