2 * "$Id: po2strings.c 10379 2012-03-23 22:16:22Z mike $"
4 * Convert a GNU gettext .po file to an Apple .strings file.
6 * Copyright 2007-2012 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`
24 * main() - Convert .po file to .strings.
27 #include <cups/cups-private.h>
31 * The .strings file format is simple:
36 * The GNU gettext .po format is also fairly simple:
40 * msgstr "localized text"
42 * The comment, msgid, and msgstr text can span multiple lines using the form:
49 * "localized text spanning "
52 * Both the msgid and msgstr strings use standard C quoting for special
53 * characters like newline and the double quote character.
57 * main() - Convert .po file to .strings.
60 int /* O - Exit code */
61 main(int argc, /* I - Number of command-line args */
62 char *argv[]) /* I - Command-line arguments */
64 int i; /* Looping var */
65 const char *pofile, /* .po filename */
66 *stringsfile; /* .strings filename */
67 cups_file_t *po, /* .po file */
68 *strings; /* .strings file */
69 char s[4096], /* String buffer */
70 *ptr, /* Pointer into buffer */
71 *temp, /* New string */
72 *msgid, /* msgid string */
73 *msgstr; /* msgstr string */
74 int 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,
198 (use_msgid || !*msgstr) ? msgid : msgstr);
210 else if (s[0] == '\"' && (msgid || msgstr))
213 * Append to current string...
216 length = (int)strlen(msgstr ? msgstr : msgid);
218 if ((temp = realloc(msgstr ? msgstr : msgid,
219 length + strlen(ptr) + 1)) == NULL)
224 perror("Unable to allocate string");
231 * Copy the new portion to the end of the msgstr string - safe
232 * to use strcpy because the buffer is allocated to the correct
238 strcpy(msgstr + length, ptr);
243 * Copy the new portion to the end of the msgid string - safe
244 * to use strcpy because the buffer is allocated to the correct
250 strcpy(msgid + length, ptr);
253 else if (!strncmp(s, "msgstr", 6) && msgid)
262 if ((msgstr = strdup(ptr)) == NULL)
265 perror("Unable to allocate msgstr");
275 cupsFilePrintf(strings, "\"%s\" = \"%s\";\n", msgid,
276 (use_msgid || !*msgstr) ? msgid : msgstr);
286 cupsFileClose(strings);
293 * End of "$Id: po2strings.c 10379 2012-03-23 22:16:22Z mike $".