-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtestGWSJSONCoder.m
executable file
·180 lines (155 loc) · 4.35 KB
/
testGWSJSONCoder.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
/**
Copyright (C) 2011 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <[email protected]>
Date: may 2011
This file is part of the WebServices package.
This is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
$Date: 2007-09-14 13:54:55 +0100 (Fri, 14 Sep 2007) $ $Revision: 25485 $
*/
#import <Foundation/Foundation.h>
#import "GWSPrivate.h"
int
main()
{
NSAutoreleasePool *pool;
NSUserDefaults *defs;
NSString *file;
BOOL decode = NO;
pool = [NSAutoreleasePool new];
defs = [NSUserDefaults standardUserDefaults];
file = [defs stringForKey: @"Encode"];
if (nil == file)
{
decode = YES;
file = [defs stringForKey: @"Decode"];
}
if (file == nil)
{
GSPrintf(stderr, @"Usage ... testGWSJSONCoder -Decode filename\n");
GSPrintf(stderr, @"or ... testGWSJSONCoder -Encode filename\n");
GSPrintf(stderr, @" -Record filename (to store results)\n");
GSPrintf(stderr, @" -Compare filename (to check results)\n");
[pool release];
return 1;
}
if (YES == decode)
{
GWSJSONCoder *coder;
NSData *data;
NSMutableDictionary *result;
data = [NSData dataWithContentsOfFile: file];
if (data == nil)
{
GSPrintf(stderr, @"Unable to load data from file '%@'\n", file);
[pool release];
return 1;
}
coder = [[GWSJSONCoder new] autorelease];
[coder setDebug: [defs boolForKey: @"Debug"]];
result = [coder parseMessage: data];
if (nil == result)
{
GSPrintf(stderr, @"Failed to decode data from file '%@'\n", file);
[pool release];
return 1;
}
file = [defs objectForKey: @"Record"];
if (file != nil)
{
if (NO == [result writeToFile: file atomically: NO])
{
GSPrintf(stderr, @"Failed to record result to file '%@'\n", file);
[pool release];
return 1;
}
}
file = [defs objectForKey: @"Compare"];
if (file == nil)
{
GSPrintf(stdout, @"%@", result);
}
else
{
NSDictionary *old;
old = [NSDictionary dictionaryWithContentsOfFile: file];
if (old == nil)
{
GSPrintf(stderr, @"Failed to load dictionary from file '%@'\n",
file);
[pool release];
return 1;
}
if ([old isEqual: result] == NO)
{
GSPrintf(stderr, @"Decode result does not match file '%@'\n",
file);
[pool release];
return 1;
}
}
}
else
{
GWSJSONCoder *coder;
id object;
NSData *result;
object = [[NSString stringWithContentsOfFile: file] propertyList];
if (nil == object)
{
GSPrintf(stderr, @"Unable to read object to encode from '%@'\n",
file);
[pool release];
return 1;
}
coder = [[GWSJSONCoder new] autorelease];
result = [coder buildRequest: nil
parameters: object
order: nil];
file = [defs objectForKey: @"Record"];
if (file != nil)
{
if (NO == [result writeToFile: file atomically: NO])
{
GSPrintf(stderr, @"Failed to record result to file '%@'\n", file);
[pool release];
return 1;
}
}
file = [defs objectForKey: @"Compare"];
if (file == nil)
{
GSPrintf(stdout, @"%@", result);
}
else
{
NSData *old;
old = [NSData dataWithContentsOfFile: file];
if (old == nil)
{
GSPrintf(stderr, @"Failed to load data from file '%@'\n",
file);
[pool release];
return 1;
}
if ([old isEqual: result] == NO)
{
GSPrintf(stderr, @"Decode result does not match file '%@'\n",
file);
[pool release];
return 1;
}
}
}
[pool release];
return 0;
}