]>
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-2014 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.
53 * main() - Convert .po file to .strings.
56 int /* O - Exit code */
57 main(int argc
, /* I - Number of command-line args */
58 char *argv
[]) /* I - Command-line arguments */
60 int i
; /* Looping var */
61 const char *pofile
, /* .po filename */
62 *stringsfile
; /* .strings filename */
63 cups_file_t
*po
, /* .po file */
64 *strings
; /* .strings file */
65 char s
[4096], /* String buffer */
66 *ptr
, /* Pointer into buffer */
67 *temp
, /* New string */
68 *msgid
, /* msgid string */
69 *msgstr
; /* msgstr string */
70 size_t length
; /* Length of combined strings */
71 int use_msgid
; /* Use msgid strings for msgstr? */
75 * Process command-line arguments...
82 for (i
= 1; i
< argc
; i
++)
84 if (!strcmp(argv
[i
], "-m"))
86 else if (argv
[i
][0] == '-')
88 puts("Usage: po2strings [-m] filename.po filename.strings");
93 else if (!stringsfile
)
94 stringsfile
= argv
[i
];
97 puts("Usage: po2strings [-m] filename.po filename.strings");
102 if (!pofile
|| !stringsfile
)
104 puts("Usage: po2strings [-m] filename.po filename.strings");
109 * Read strings from the .po file and write to the .strings file...
112 if ((po
= cupsFileOpen(pofile
, "r")) == NULL
)
118 if ((strings
= cupsFileOpen(stringsfile
, "w")) == NULL
)
125 msgid
= msgstr
= NULL
;
127 while (cupsFileGets(po
, s
, sizeof(s
)) != NULL
)
129 if (s
[0] == '#' && s
[1] == '.')
132 * Copy comment string...
138 * First output the last localization string...
142 cupsFilePrintf(strings
, "\"%s\" = \"%s\";\n", msgid
,
143 (use_msgid
|| !*msgstr
) ? msgid
: msgstr
);
147 msgid
= msgstr
= NULL
;
150 cupsFilePrintf(strings
, "//%s\n", s
+ 2);
152 else if (s
[0] == '#' || !s
[0])
155 * Skip blank and file comment lines...
163 * Strip the trailing quote...
166 if ((ptr
= strrchr(s
, '\"')) == NULL
)
172 * Find start of value...
175 if ((ptr
= strchr(s
, '\"')) == NULL
)
181 * Create or add to a message...
184 if (!strncmp(s
, "msgid", 5))
187 * Output previous message as needed...
193 cupsFilePrintf(strings
, "\"%s\" = \"%s\";\n", msgid
,
194 (use_msgid
|| !*msgstr
) ? msgid
: msgstr
);
206 else if (s
[0] == '\"' && (msgid
|| msgstr
))
209 * Append to current string...
212 size_t ptrlen
= strlen(ptr
); /* Length of string */
214 length
= strlen(msgstr
? msgstr
: msgid
);
216 if ((temp
= realloc(msgstr
? msgstr
: msgid
,
217 length
+ ptrlen
+ 1)) == NULL
)
222 perror("Unable to allocate string");
229 * Copy the new portion to the end of the msgstr string - safe
230 * to use strcpy because the buffer is allocated to the correct
236 memcpy(msgstr
+ length
, ptr
, ptrlen
+ 1);
241 * Copy the new portion to the end of the msgid string - safe
242 * to use strcpy because the buffer is allocated to the correct
248 memcpy(msgid
+ length
, ptr
, ptrlen
+ 1);
251 else if (!strncmp(s
, "msgstr", 6) && msgid
)
260 if ((msgstr
= strdup(ptr
)) == NULL
)
263 perror("Unable to allocate msgstr");
273 cupsFilePrintf(strings
, "\"%s\" = \"%s\";\n", msgid
,
274 (use_msgid
|| !*msgstr
) ? msgid
: msgstr
);
284 cupsFileClose(strings
);