]> err.no Git - sope/blob - sope-xml/XmlRpc/NSNumber+XmlRpcCoding.m
fixed Xcode build
[sope] / sope-xml / XmlRpc / NSNumber+XmlRpcCoding.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21 // $Id$
22
23 #include "XmlRpcMethodResponse.h"
24 #include "NSObject+XmlRpc.h"
25 #include "XmlRpcCoder.h"
26 #include "common.h"
27
28 #if LIB_FOUNDATION_LIBRARY
29 #  include <Foundation/NSConcreteNumber.h>
30 #endif
31
32 #define BOOLEAN_TYPE 0
33 #define INTEGER_TYPE 1
34 #define DOUBLE_TYPE  2
35
36 @implementation NSNumber(XmlRpcCoding)
37
38 - (unsigned int)_xmlRpcNumberType {
39   static Class BoolClass = Nil;
40
41   if (BoolClass == Nil)
42     BoolClass = NSClassFromString(@"NSBoolNumber");
43
44   if ([self isKindOfClass:BoolClass])
45     return BOOLEAN_TYPE;
46   
47   switch (*[self objCType]) {
48     case 'i':
49     case 'I':
50     case 'c':
51     case 'C':
52     case 's':
53     case 'S':
54     case 'l':
55     case 'L':
56       return INTEGER_TYPE;
57       
58     case 'f':
59     case 'd':
60     default:
61       return DOUBLE_TYPE;
62   }
63 }
64
65 - (NSString *)xmlRpcType {
66   switch ([self _xmlRpcNumberType]) {
67     case BOOLEAN_TYPE:
68       return @"boolean";
69     case DOUBLE_TYPE:
70       return @"double";
71     default: /* INTEGER_TYPE */
72       return @"i4";
73   }
74 }
75
76 - (void)encodeWithXmlRpcCoder:(XmlRpcEncoder *)_coder {
77   switch ([self _xmlRpcNumberType]) {
78     case BOOLEAN_TYPE:
79       [_coder encodeBoolean:[self boolValue]];
80       break;
81     case DOUBLE_TYPE:
82       [_coder encodeDouble:[self doubleValue]];
83       break;
84     default: /* INTEGER_TYPE */
85       [_coder encodeInt:[self intValue]];
86   }
87 }
88
89 + (id)decodeObjectWithXmlRpcCoder:(XmlRpcDecoder *)_coder {
90   return [NSNumber numberWithInt:[_coder decodeInt]];
91 }
92
93 @end /* NSNumber(XmlRpcCoding) */
94
95 #if LIB_FOUNDATION_LIBRARY
96
97 @implementation NSBoolNumber(XmlRpcCoding)
98
99 + (id)decodeObjectWithXmlRpcCoder:(XmlRpcDecoder *)_coder {
100   return [[[NSNumber alloc] initWithBool:[_coder decodeBoolean]] autorelease];
101 }
102
103 - (NSString *)xmlRpcType {
104   return @"boolean";
105 }
106
107 @end /* NSBoolNumber(XmlRpcCoding) */
108
109 // nicht notwendig, nur BOOL muss speziell abgefangen werden ??? :
110 @implementation NSFloatNumber(XmlRpcCoding)
111 + (id)decodeObjectWithXmlRpcCoder:(XmlRpcDecoder *)_coder {
112   return [[[NSNumber alloc] initWithDouble:[_coder decodeDouble]] autorelease];
113 }
114
115 - (NSString *)xmlRpcType {
116   return @"double";
117 }
118
119 @end /* NSFloatNumber(XmlRpcCoding) */
120
121 @implementation NSDoubleNumber(XmlRpcCoding)
122 + (id)decodeObjectWithXmlRpcCoder:(XmlRpcDecoder *)_coder {
123   return [[[NSNumber alloc] initWithDouble:[_coder decodeDouble]] autorelease];
124 }
125
126 - (NSString *)xmlRpcType {
127   return @"double";
128 }
129
130 @end /* NSDoubleNumber(XmlRpcCoding) */
131
132 #endif