]> err.no Git - sope/blob - sope-xml/XmlRpc/NSNumber+XmlRpcCoding.m
fixed a Tiger warning
[sope] / sope-xml / XmlRpc / NSNumber+XmlRpcCoding.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE 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   SOPE 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 SOPE; 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
22 #include "XmlRpcMethodResponse.h"
23 #include "NSObject+XmlRpc.h"
24 #include "XmlRpcCoder.h"
25 #include "common.h"
26
27 #if LIB_FOUNDATION_LIBRARY
28 #  include <Foundation/NSConcreteNumber.h>
29 #endif
30
31 #define BOOLEAN_TYPE 0
32 #define INTEGER_TYPE 1
33 #define DOUBLE_TYPE  2
34
35 @implementation NSNumber(XmlRpcCoding)
36
37 - (unsigned int)_xmlRpcNumberType {
38   static Class BoolClass = Nil;
39
40   if (BoolClass == Nil)
41     BoolClass = NSClassFromString(@"NSBoolNumber");
42
43   if ([self isKindOfClass:BoolClass])
44     return BOOLEAN_TYPE;
45   
46   switch (*[self objCType]) {
47     case 'i':
48     case 'I':
49     case 'c':
50     case 'C':
51     case 's':
52     case 'S':
53     case 'l':
54     case 'L':
55       return INTEGER_TYPE;
56       
57     case 'f':
58     case 'd':
59     default:
60       return DOUBLE_TYPE;
61   }
62 }
63
64 - (NSString *)xmlRpcType {
65   switch ([self _xmlRpcNumberType]) {
66     case BOOLEAN_TYPE:
67       return @"boolean";
68     case DOUBLE_TYPE:
69       return @"double";
70     default: /* INTEGER_TYPE */
71       return @"i4";
72   }
73 }
74
75 - (void)encodeWithXmlRpcCoder:(XmlRpcEncoder *)_coder {
76   switch ([self _xmlRpcNumberType]) {
77     case BOOLEAN_TYPE:
78       [_coder encodeBoolean:[self boolValue]];
79       break;
80     case DOUBLE_TYPE:
81       [_coder encodeDouble:[self doubleValue]];
82       break;
83     default: /* INTEGER_TYPE */
84       [_coder encodeInt:[self intValue]];
85   }
86 }
87
88 + (id)decodeObjectWithXmlRpcCoder:(XmlRpcDecoder *)_coder {
89   return [NSNumber numberWithInt:[_coder decodeInt]];
90 }
91
92 @end /* NSNumber(XmlRpcCoding) */
93
94 #if LIB_FOUNDATION_LIBRARY
95
96 @implementation NSBoolNumber(XmlRpcCoding)
97
98 + (id)decodeObjectWithXmlRpcCoder:(XmlRpcDecoder *)_coder {
99   return [[[NSNumber alloc] initWithBool:[_coder decodeBoolean]] autorelease];
100 }
101
102 - (NSString *)xmlRpcType {
103   return @"boolean";
104 }
105
106 @end /* NSBoolNumber(XmlRpcCoding) */
107
108 // nicht notwendig, nur BOOL muss speziell abgefangen werden ??? :
109 @implementation NSFloatNumber(XmlRpcCoding)
110 + (id)decodeObjectWithXmlRpcCoder:(XmlRpcDecoder *)_coder {
111   return [[[NSNumber alloc] initWithDouble:[_coder decodeDouble]] autorelease];
112 }
113
114 - (NSString *)xmlRpcType {
115   return @"double";
116 }
117
118 @end /* NSFloatNumber(XmlRpcCoding) */
119
120 @implementation NSDoubleNumber(XmlRpcCoding)
121 + (id)decodeObjectWithXmlRpcCoder:(XmlRpcDecoder *)_coder {
122   return [[[NSNumber alloc] initWithDouble:[_coder decodeDouble]] autorelease];
123 }
124
125 - (NSString *)xmlRpcType {
126   return @"double";
127 }
128
129 @end /* NSDoubleNumber(XmlRpcCoding) */
130
131 #endif