Skip to content

Instantly share code, notes, and snippets.

@mapedd
Last active December 14, 2015 20:49
Show Gist options
  • Save mapedd/5146483 to your computer and use it in GitHub Desktop.
Save mapedd/5146483 to your computer and use it in GitHub Desktop.
Short program to get all objects with key = Title from Root.plist and save them to Root.strings file for localizing app settings
#import <Foundation/Foundation.h>
BOOL isDictionary(id object);
BOOL isString(id object);
BOOL isArray(id object);
void addObjectForKeyToArray(NSString *key, id collection, NSMutableArray *array);
void addArrayObjectsForKeyToArray(NSString *searchKey, id collection, NSMutableArray *array);
void printHelp();
int main(int argc, char *argv[]) {
NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];
if (argc < 2) {
printHelp();
exit(1);
}
NSString *inputString,*plistDir,*outputFile;
BOOL havePlistDir = NO;
BOOL haveOutputFilename = NO;
NSUInteger i = 1;
BOOL wrongPath = NO;
while (i<argc)
{
if (argv[i][0]!='-')
{
// not a parameter, treat as file name
inputString = [NSString stringWithUTF8String:argv[i]];
if(havePlistDir){
havePlistDir = NO;
plistDir = [inputString copy];
}
if(haveOutputFilename){
haveOutputFilename = NO;
outputFile = [inputString copy];
}
}
else if (!strcmp("-f", argv[i]))
{
havePlistDir = YES;
}
else if (!strcmp("-o", argv[i]))
{
haveOutputFilename = YES;
}
else if (!strcmp("-?", argv[i]))
{
printHelp();
exit(1);
}
i++;
}
if(plistDir){
NSLog(@"plist directory :%@", plistDir);
}
if(outputFile){
NSLog(@"output filename :%@", outputFile);
}
{
NSString *plistDirectory = [NSString stringWithFormat:@"%@/Root.plist",plistDir];
NSString *plistPath = [plistDirectory stringByExpandingTildeInPath];
NSDictionary *plistData = [NSDictionary dictionaryWithContentsOfFile:plistPath];
// NSLog(@"plist data \r%@",plistData);
NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
addObjectForKeyToArray(@"Title",plistData,array);
addArrayObjectsForKeyToArray(@"Titles", plistData, array);
if(array.count){
NSMutableString *string = [NSMutableString stringWithCapacity:0];
for (id object in array) {
if(isString(object)){
[string appendFormat:@"\"%@\" = \"%@\"\r\n\n",object,object];
}
}
if(string.length){
NSString *fileName = outputFile ?: @"Root.strings";
NSError *error;
if(![string writeToFile:[fileName stringByExpandingTildeInPath] atomically:YES encoding:NSUTF8StringEncoding error:&error]){
NSLog(@"error occured during writing to file %@", fileName);
}else{
NSLog(@"successfully written all titles to file :%@", fileName);
}
}
}
}
[p release];
return 0;
}
BOOL isDictionary(id object){
return [object isKindOfClass:[NSDictionary class]];
}
BOOL isString(id object){
return [object isKindOfClass:[NSString class]];
}
BOOL isArray(id object){
return [object isKindOfClass:[NSArray class]];
}
void addArrayObjectsForKeyToArray(NSString *searchKey, id collection, NSMutableArray *array){
if(isArray(collection)){
for (id object in collection) {
if(isDictionary(object))
addArrayObjectsForKeyToArray(searchKey,object,array);
}
}
else if (isDictionary(collection)) {
for (id key in collection) {
id object = [collection objectForKey:key];
if(isString(key) && isArray(object)){
if([key isEqualToString:searchKey]){
for (id arrayObj in object) {
[array addObject:arrayObj];
}
}
else{
addArrayObjectsForKeyToArray(searchKey,object,array);
}
}
}
}
}
void addObjectForKeyToArray(NSString *searchKey, id collection, NSMutableArray *array){
if(isArray(collection)){
for (id object in collection) {
if(isDictionary(object))
addObjectForKeyToArray(searchKey,object,array);
}
}
else if (isDictionary(collection)) {
for (id key in collection) {
id object = [collection objectForKey:key];
if(isString(object)){
if([key isEqualToString:searchKey]) {[array addObject:object];}
}
else if (isDictionary(object)) {
addObjectForKeyToArray(key,object,array);
}
else if (isArray(object)) {
addObjectForKeyToArray(searchKey,object,array);
}
}
}
}
void printHelp(){
printf("Usage: Root.plist Title getter [OPTIONS] ...\n\n");
printf(" Options\n");
printf(" -o filename Output string file name, default Root.strings.\n");
printf(" -f path Path to Root.plist file, default '.' \n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment