1 /* NSDictionary+Utilities.m - this file is part of SOGo
3 * Copyright (C) 2007 Inverse groupe conseil
5 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
7 * This file is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This file is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSData.h>
25 #import <Foundation/NSString.h>
27 #import "NSArray+Utilities.h"
28 #import "NSObject+Utilities.h"
29 #import "NSDictionary+Utilities.h"
31 @implementation NSDictionary (SOGoDictionaryUtilities)
33 + (NSDictionary *) dictionaryFromStringsFile: (NSString *) file
36 NSMutableData *content;
37 NSDictionary *newDictionary;
39 content = [NSMutableData new];
40 [content appendBytes: "{" length: 1];
41 [content appendData: [NSData dataWithContentsOfFile: file]];
42 [content appendBytes: "}" length: 1];
43 serialized = [[NSString alloc] initWithData: content
44 encoding: NSUTF8StringEncoding];
46 newDictionary = [serialized propertyList];
52 - (NSString *) jsonRepresentation
54 NSMutableArray *values;
55 NSString *representation, *currentKey, *currentValue, *currentPair;
58 values = [NSMutableArray new];
59 keys = [[self allKeys] objectEnumerator];
60 currentKey = [keys nextObject];
63 currentValue = [[self objectForKey: currentKey] jsonRepresentation];
64 currentPair = [NSString stringWithFormat: @"%@: %@",
65 [currentKey jsonRepresentation], currentValue];
66 [values addObject: currentPair];
67 currentKey = [keys nextObject];
69 representation = [NSString stringWithFormat: @"{%@}",
70 [values componentsJoinedByString: @", "]];
73 return representation;
76 - (NSString *) keysWithFormat: (NSString *) keyFormat
78 NSArray *keys, *allKeys;
79 unsigned int count, max;
80 NSMutableString *keysWithFormat;
83 keysWithFormat = [NSMutableString stringWithString: keyFormat];
85 allKeys = [self allKeys];
86 keys = [allKeys stringsWithFormat: @"%{%@}"];
88 max = [allKeys count];
89 for (count = 0; count < max; count++)
91 value = [self objectForKey: [allKeys objectAtIndex: count]];
92 [keysWithFormat replaceString: [keys objectAtIndex: count]
93 withString: [value description]];
96 return keysWithFormat;