]>
Commit | Line | Data |
---|---|---|
bc44d920 | 1 | /* |
2e4ff8af | 2 | * "$Id: po2strings.c 6921 2007-09-06 13:38:37Z mike $" |
bc44d920 | 3 | * |
0837b7e8 | 4 | * Convert a GNU gettext .po file to an Apple .strings file. |
71e16022 MS |
5 | * |
6 | * Copyright 2007-2010 by Apple Inc. | |
7 | * | |
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/". | |
bc44d920 | 13 | * |
14 | * Usage: | |
15 | * | |
16 | * po2strings filename.strings filename.po | |
17 | * | |
18 | * Compile with: | |
19 | * | |
20 | * gcc -o po2strings po2strings.c `cups-config --libs` | |
21 | * | |
22 | * Contents: | |
23 | * | |
0837b7e8 | 24 | * main() - Convert .po file to .strings. |
bc44d920 | 25 | */ |
26 | ||
71e16022 | 27 | #include <cups/cups-private.h> |
bc44d920 | 28 | |
29 | ||
30 | /* | |
31 | * The .strings file format is simple: | |
32 | * | |
33 | * // comment | |
0837b7e8 | 34 | * "msgid" = "msgstr"; |
bc44d920 | 35 | * |
0837b7e8 MS |
36 | * The GNU gettext .po format is also fairly simple: |
37 | * | |
38 | * #. comment | |
39 | * msgid "some text" | |
40 | * msgstr "localized text" | |
41 | * | |
42 | * The comment, msgid, and msgstr text can span multiple lines using the form: | |
43 | * | |
44 | * #. comment | |
45 | * #. more comments | |
46 | * msgid "" | |
47 | * "some long text" | |
48 | * msgstr "" | |
49 | * "localized text spanning " | |
50 | * "multiple lines" | |
51 | * | |
52 | * Both the msgid and msgstr strings use standard C quoting for special | |
53 | * characters like newline and the double quote character. | |
bc44d920 | 54 | */ |
55 | ||
bc44d920 | 56 | /* |
57 | * main() - Convert .po file to .strings. | |
58 | */ | |
59 | ||
60 | int /* O - Exit code */ | |
61 | main(int argc, /* I - Number of command-line args */ | |
62 | char *argv[]) /* I - Command-line arguments */ | |
63 | { | |
745129be | 64 | int i; /* Looping var */ |
0837b7e8 MS |
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 */ | |
745129be | 75 | int use_msgid; /* Use msgid strings for msgstr? */ |
bc44d920 | 76 | |
77 | ||
0837b7e8 MS |
78 | /* |
79 | * Process command-line arguments... | |
80 | */ | |
745129be | 81 | |
0837b7e8 MS |
82 | pofile = NULL; |
83 | stringsfile = NULL; | |
84 | use_msgid = 0; | |
745129be MS |
85 | |
86 | for (i = 1; i < argc; i ++) | |
0837b7e8 | 87 | { |
745129be MS |
88 | if (!strcmp(argv[i], "-m")) |
89 | use_msgid = 1; | |
90 | else if (argv[i][0] == '-') | |
91 | { | |
92 | puts("Usage: po2strings [-m] filename.po filename.strings"); | |
93 | return (1); | |
94 | } | |
0837b7e8 MS |
95 | else if (!pofile) |
96 | pofile = argv[i]; | |
97 | else if (!stringsfile) | |
98 | stringsfile = argv[i]; | |
745129be MS |
99 | else |
100 | { | |
101 | puts("Usage: po2strings [-m] filename.po filename.strings"); | |
102 | return (1); | |
103 | } | |
0837b7e8 | 104 | } |
745129be | 105 | |
0837b7e8 | 106 | if (!pofile || !stringsfile) |
bc44d920 | 107 | { |
745129be | 108 | puts("Usage: po2strings [-m] filename.po filename.strings"); |
bc44d920 | 109 | return (1); |
110 | } | |
111 | ||
112 | /* | |
0837b7e8 | 113 | * Read strings from the .po file and write to the .strings file... |
bc44d920 | 114 | */ |
115 | ||
0837b7e8 | 116 | if ((po = cupsFileOpen(pofile, "r")) == NULL) |
bc44d920 | 117 | { |
0837b7e8 | 118 | perror(pofile); |
bc44d920 | 119 | return (1); |
120 | } | |
121 | ||
0837b7e8 | 122 | if ((strings = cupsFileOpen(stringsfile, "w")) == NULL) |
bc44d920 | 123 | { |
0837b7e8 MS |
124 | perror(stringsfile); |
125 | cupsFileClose(po); | |
bc44d920 | 126 | return (1); |
127 | } | |
128 | ||
0837b7e8 MS |
129 | msgid = msgstr = NULL; |
130 | ||
131 | while (cupsFileGets(po, s, sizeof(s)) != NULL) | |
bc44d920 | 132 | { |
0837b7e8 MS |
133 | if ((s[0] == '#' && s[0] != '.') || !s[0]) |
134 | { | |
135 | /* | |
136 | * Skip blank and file comment lines... | |
137 | */ | |
bc44d920 | 138 | |
0837b7e8 MS |
139 | continue; |
140 | } | |
141 | else if (s[0] == '#') | |
142 | { | |
143 | /* | |
144 | * Copy comment string... | |
145 | */ | |
bc44d920 | 146 | |
0837b7e8 MS |
147 | cupsFilePrintf(strings, "//%s\n", s + 2); |
148 | } | |
149 | else | |
150 | { | |
151 | /* | |
152 | * Strip the trailing quote... | |
153 | */ | |
bc44d920 | 154 | |
0837b7e8 MS |
155 | if ((ptr = strrchr(s, '\"')) == NULL) |
156 | continue; | |
bc44d920 | 157 | |
0837b7e8 | 158 | *ptr = '\0'; |
bc44d920 | 159 | |
0837b7e8 MS |
160 | /* |
161 | * Find start of value... | |
162 | */ | |
163 | ||
164 | if ((ptr = strchr(s, '\"')) == NULL) | |
165 | continue; | |
bc44d920 | 166 | |
0837b7e8 | 167 | ptr ++; |
bc44d920 | 168 | |
0837b7e8 MS |
169 | /* |
170 | * Create or add to a message... | |
171 | */ | |
172 | ||
173 | if (!strncmp(s, "msgid", 5)) | |
174 | { | |
175 | /* | |
176 | * Output previous message as needed... | |
177 | */ | |
178 | ||
179 | if (msgid && msgstr) | |
180 | { | |
181 | if (*msgid) | |
182 | cupsFilePrintf(strings, "\"%s\" = \"%s\";\n\n", msgid, | |
183 | (use_msgid || !*msgstr) ? msgid : msgstr); | |
184 | ||
185 | free(msgid); | |
186 | free(msgstr); | |
187 | } | |
188 | ||
189 | msgid = strdup(ptr); | |
190 | msgstr = NULL; | |
191 | } | |
192 | else if (s[0] == '\"' ) | |
193 | { | |
194 | /* | |
195 | * Append to current string... | |
196 | */ | |
197 | ||
198 | length = (int)strlen(msgstr ? msgstr : msgid); | |
199 | ||
200 | if ((temp = realloc(msgstr ? msgstr : msgid, | |
201 | length + strlen(ptr) + 1)) == NULL) | |
202 | { | |
203 | perror("Unable to allocate string"); | |
204 | return (1); | |
205 | } | |
206 | ||
207 | if (msgstr) | |
208 | { | |
209 | /* | |
210 | * Copy the new portion to the end of the msgstr string - safe | |
211 | * to use strcpy because the buffer is allocated to the correct | |
212 | * size... | |
213 | */ | |
214 | ||
215 | msgstr = temp; | |
216 | ||
217 | strcpy(msgstr + length, ptr); | |
218 | } | |
219 | else | |
220 | { | |
221 | /* | |
222 | * Copy the new portion to the end of the msgid string - safe | |
223 | * to use strcpy because the buffer is allocated to the correct | |
224 | * size... | |
225 | */ | |
226 | ||
227 | msgid = temp; | |
228 | ||
229 | strcpy(msgid + length, ptr); | |
230 | } | |
231 | } | |
232 | else if (!strncmp(s, "msgstr", 6) && msgid) | |
233 | { | |
234 | /* | |
235 | * Set the string... | |
236 | */ | |
237 | ||
238 | if ((msgstr = strdup(ptr)) == NULL) | |
239 | { | |
240 | perror("Unable to allocate msgstr"); | |
241 | return (1); | |
242 | } | |
243 | } | |
bc44d920 | 244 | } |
0837b7e8 MS |
245 | } |
246 | ||
247 | if (msgid && msgstr) | |
248 | { | |
249 | if (*msgid) | |
250 | cupsFilePrintf(strings, "\"%s\" = \"%s\";\n\n", msgid, | |
251 | (use_msgid || !*msgstr) ? msgid : msgstr); | |
bc44d920 | 252 | |
0837b7e8 MS |
253 | free(msgid); |
254 | free(msgstr); | |
bc44d920 | 255 | } |
256 | ||
0837b7e8 MS |
257 | cupsFileClose(po); |
258 | cupsFileClose(strings); | |
259 | ||
260 | return (0); | |
bc44d920 | 261 | } |
262 | ||
263 | ||
264 | /* | |
2e4ff8af | 265 | * End of "$Id: po2strings.c 6921 2007-09-06 13:38:37Z mike $". |
bc44d920 | 266 | */ |