]> err.no Git - scalable-opengroupware.org/blob - SoObjects/SOGo/NSDictionary+Utilities.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1185 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / SoObjects / SOGo / NSDictionary+Utilities.m
1 /* NSDictionary+Utilities.m - this file is part of SOGo
2  *
3  * Copyright (C) 2007 Inverse groupe conseil
4  *
5  * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6  *
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)
10  * any later version.
11  *
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.
16  *
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.
21  */
22
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSData.h>
25 #import <Foundation/NSString.h>
26
27 #import "NSArray+Utilities.h"
28 #import "NSObject+Utilities.h"
29 #import "NSDictionary+Utilities.h"
30
31 @implementation NSDictionary (SOGoDictionaryUtilities)
32
33 + (NSDictionary *) dictionaryFromStringsFile: (NSString *) file
34 {
35   NSString *serialized;
36   NSMutableData *content;
37   NSDictionary *newDictionary;
38
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];
45   [content release];
46   newDictionary = [serialized propertyList];
47   [serialized release];
48
49   return newDictionary;
50 }
51
52 - (NSString *) jsonRepresentation
53 {
54   NSMutableArray *values;
55   NSString *representation, *currentKey, *currentValue, *currentPair;
56   NSEnumerator *keys;
57
58   values = [NSMutableArray new];
59   keys = [[self allKeys] objectEnumerator];
60   currentKey = [keys nextObject];
61   while (currentKey)
62     {
63       currentValue = [[self objectForKey: currentKey] jsonRepresentation];
64       currentPair = [NSString stringWithFormat: @"%@: %@",
65                               [currentKey jsonRepresentation], currentValue];
66       [values addObject: currentPair];
67       currentKey = [keys nextObject];
68     }
69   representation = [NSString stringWithFormat: @"{%@}",
70                              [values componentsJoinedByString: @", "]];
71   [values release];
72
73   return representation;
74 }
75
76 - (NSString *) keysWithFormat: (NSString *) keyFormat
77 {
78   NSArray *keys, *allKeys;
79   unsigned int count, max;
80   NSMutableString *keysWithFormat;
81   id value;
82
83   keysWithFormat = [NSMutableString stringWithString: keyFormat];
84
85   allKeys = [self allKeys];
86   keys = [allKeys stringsWithFormat: @"%{%@}"];
87
88   max = [allKeys count];
89   for (count = 0; count < max; count++)
90     {
91       value = [self objectForKey: [allKeys objectAtIndex: count]];
92       [keysWithFormat replaceString: [keys objectAtIndex: count]
93                       withString: [value description]];
94     }
95
96   return keysWithFormat;
97 }
98
99 @end