/
main.m
244 lines (203 loc) · 8.4 KB
/
main.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//
// main.m
// genstrings2
//
// Created by Oliver Drobnik on 29.12.11.
// Copyright (c) 2011 Drobnik KG. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "DTLocalizableStringScanner.h"
#import "DTLocalizableStringAggregator.h"
#import "DTLocalizableStringEntry.h"
#import "DTLocalizableStringTable.h"
void showUsage(void);
int main (int argc, const char *argv[])
{
@autoreleasepool
{
// default output folder = current working dir
NSURL *outputFolderURL = nil;
// default output encoding
NSStringEncoding outputStringEncoding = NSUTF16StringEncoding;
BOOL wantsPositionalParameters = YES;
BOOL wantsMultipleCommentWarning = YES;
BOOL wantsDecodedUnicodeSequences = NO;
NSMutableSet *tablesToSkip = [NSMutableSet set];
NSString *customMacroPrefix = nil;
NSString *defaultTableName = nil;
// analyze options
BOOL optionsInvalid = NO;
NSUInteger i = 1;
NSMutableArray *files = [NSMutableArray array];
NSStringEncoding inputStringEncoding = NSUTF8StringEncoding;
while (i<argc)
{
if (argv[i][0]!='-')
{
// not a parameter, treat as file name
NSString *fileName = [NSString stringWithUTF8String:argv[i]];
// standardize path
fileName = [fileName stringByStandardizingPath];
NSURL *url = [NSURL fileURLWithPath:fileName];
if (!url)
{
optionsInvalid = YES;
break;
}
[files addObject:url];
}
else if (!strcmp("-noPositionalParameters", argv[i]))
{
// do not add positions to parameters
wantsPositionalParameters = NO;
}
else if (!strcmp("-o", argv[i]))
{
// output folder name
i++;
if (i>=argc)
{
// output folder name is missing
optionsInvalid = YES;
break;
}
// output folder
NSString *fileName = [NSString stringWithUTF8String:argv[i]];
// standardize path
fileName = [fileName stringByStandardizingPath];
// check if output folder exists
if (![[NSFileManager defaultManager] isWritableFileAtPath:fileName])
{
printf("Unable to write to %s\n", [fileName UTF8String]);
exit(1);
}
outputFolderURL = [NSURL fileURLWithPath:fileName];
}
else if (!strcmp("-s", argv[i]))
{
// custom macro prefix
i++;
if (i>=argc)
{
// prefix is missing
optionsInvalid = YES;
break;
}
customMacroPrefix = [NSString stringWithUTF8String:argv[i]];
}
else if (!strcmp("-q", argv[i]))
{
// do not warn if multiple different comments are attached to a token
wantsMultipleCommentWarning = NO;
}
else if (!strcmp("-u", argv[i]))
{
wantsDecodedUnicodeSequences = YES;
}
else if (!strcmp("-skipTable", argv[i]))
{
// tables to be ignored
i++;
if (i>=argc)
{
// table name is missing
optionsInvalid = YES;
break;
}
NSString *tableName = [NSString stringWithUTF8String:argv[i]];
[tablesToSkip addObject:tableName];
}
else if (!strcmp("-littleEndian", argv[i]))
{
outputStringEncoding = NSUTF16LittleEndianStringEncoding;
}
else if (!strcmp("-bigEndian", argv[i]))
{
outputStringEncoding = NSUTF16BigEndianStringEncoding;
}
else if (!strcmp("-utf8", argv[i]))
{
outputStringEncoding = NSUTF8StringEncoding;
}
else if (!strcmp("-macRoman", argv[i]))
{
inputStringEncoding = NSMacOSRomanStringEncoding;
}
else if (!strcmp("-defaultTable", argv[i]))
{
i++;
if (i>=argc)
{
// table name is missing
optionsInvalid = YES;
break;
}
defaultTableName = [NSString stringWithUTF8String:argv[i]];
}
i++;
}
// something is wrong
if (optionsInvalid || ![files count])
{
showUsage();
exit(1);
}
// create the aggregator
DTLocalizableStringAggregator *aggregator = [[DTLocalizableStringAggregator alloc] init];
// set the parameters
aggregator.wantsPositionalParameters = wantsPositionalParameters;
aggregator.inputEncoding = inputStringEncoding;
aggregator.customMacroPrefix = customMacroPrefix;
aggregator.tablesToSkip = tablesToSkip;
aggregator.defaultTableName = defaultTableName;
// go, go, go!
for (NSURL *file in files) {
[aggregator beginProcessingFile:file];
}
NSArray *aggregatedTables = [aggregator aggregatedStringTables];
DTLocalizableStringEntryWriteCallback writeCallback = nil;
if (wantsMultipleCommentWarning) {
writeCallback = ^(DTLocalizableStringEntry *entry) {
NSArray *comments = [entry sortedComments];
if ([comments count] > 1) {
NSString *tmpString = [comments componentsJoinedByString:@"\" & \""];
printf("Warning: Key \"%s\" used with multiple comments \"%s\"\n", [entry.rawKey UTF8String], [tmpString UTF8String]);
}
};
}
// set output dir to current working dir if not set
if (!outputFolderURL) {
NSString *cwd = [[NSFileManager defaultManager] currentDirectoryPath];
outputFolderURL = [NSURL fileURLWithPath:cwd];
}
// output the tables
NSError *error = nil;
for (DTLocalizableStringTable *table in aggregatedTables) {
[table setShouldDecodeUnicodeSequences:wantsDecodedUnicodeSequences];
if (![table writeToFolderAtURL:outputFolderURL encoding:outputStringEncoding error:&error entryWriteCallback:writeCallback]) {
printf("%s\n", [[error localizedDescription] UTF8String]);
exit(1); // exit due to error
}
}
}
return 0;
}
void showUsage(void)
{
printf("Usage: genstrings2 [OPTIONS] file...\n\n");
printf(" Options\n");
// printf(" -j sets the input language to Java.\n");
// printf(" -a append output to the old strings files.\n");
printf(" -s substring substitute 'substring' for NSLocalizedString.\n");
printf(" -skipTable tablename skip over the file for 'tablename'.\n");
printf(" -noPositionalParameters turns off positional parameter support.\n");
printf(" -u allow unicode characters in the values of strings files.\n");
printf(" -macRoman read files as MacRoman not UTF-8.\n");
printf(" -q turns off multiple key/value pairs warning.\n");
printf(" -bigEndian output generated with big endian byte order.\n");
printf(" -littleEndian output generated with little endian byte order.\n");
printf(" -utf8 output generated as UTF-8 not UTF-16.\n");
printf(" -o dir place output files in 'dir'.\n\n");
printf(" -defaultTable tablename use 'tablename' instead of 'Localizable' as default table name.\n");
printf(" Please see the genstrings2(1) man page for full documentation\n");
}