]> err.no Git - sope/blob - sope-gdl1/GDLAccess/EOCustomValues.m
Add libxml2-dev to libsope-xml4.7-dev deps
[sope] / sope-gdl1 / GDLAccess / EOCustomValues.m
1 /* 
2    EOAttributeOrdering.m
3
4    Copyright (C) 1996 Free Software Foundation, Inc.
5
6    Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>
7    Date: 1996
8
9    This file is part of the GNUstep Database Library.
10
11    This library is free software; you can redistribute it and/or
12    modify it under the terms of the GNU Library General Public
13    License as published by the Free Software Foundation; either
14    version 2 of the License, or (at your option) any later version.
15
16    This library is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19    Library General Public License for more details.
20
21    You should have received a copy of the GNU Library General Public
22    License along with this library; see the file COPYING.LIB.
23    If not, write to the Free Software Foundation,
24    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
27 // $Id: EOCustomValues.m 1 2004-08-20 10:38:46Z znek $
28
29 #import "common.h"
30 #import "EOCustomValues.h"
31
32 #if LIB_FOUNDATION_LIBRARY
33 @implementation NSTemporaryString(EOCustomValues)
34
35 - initWithString:(NSString*)string type:(NSString*)type {
36   return [self initWithString:string];
37 }
38
39 @end
40 #endif
41
42 @implementation NSString(EOCustomValues)
43
44 + stringWithString:(NSString*)string type:(NSString*)type {
45   // If mutable return a copy if not return self
46     
47   if ([string isKindOfClass:[NSMutableString class]])
48     return [[NSString alloc] initWithString:string type:type];
49   else
50     return RETAIN(self);
51 }
52
53 - initWithString:(NSString*)string type:(NSString*)type {
54   return [self initWithString:string];
55 }
56
57 - (NSString*)stringForType:(NSString*)type {
58   // If mutable return a copy if not return self (handled in NSString)
59   return [self copyWithZone:[self zone]];
60 }
61
62 - (id)initWithData:(NSData*)data type:(NSString*)type {
63   return [self initWithCString:[data bytes] length:[data length]];
64 }
65
66 - (NSData *)dataForType:(NSString *)type {
67   unsigned len = [self cStringLength];
68   char buf[len + 1];
69   [self getCString:buf];
70   return [NSData dataWithBytes:buf length:len];
71 }
72
73 @end /* NSString(EOCustomValues) */
74
75
76 @implementation NSData(EOCustomValues)
77
78 - (id)initWithString:(NSString*)string type:(NSString*)type {
79   unsigned len = [string cStringLength];
80   char buf[len + 1];
81   [string getCString:buf];
82   
83   return [self initWithBytes:buf length:len];
84 }
85
86 - (NSString*)stringForType:(NSString*)type {
87   return [NSString stringWithCString:[self bytes] length:[self length]];
88 }
89
90 - initWithData:(NSData*)data type:(NSString*)type {
91   return [self initWithBytes:[data bytes] length:[data length]];
92 }
93
94 - (NSData*)dataForType:(NSString*)type {
95   return [self copyWithZone:[self zone]];
96 }
97
98 @end /* NSData(EOCustomValues) */
99
100
101 @implementation NSNumber(EOCustomValues)
102
103 + (id)numberWithString:(NSString*)string type:(NSString*)type {
104   char buf[[string cStringLength] + 1];
105   const char *cstring;
106
107   [string getCString:buf];
108   cstring = buf;
109   
110   if ([type cStringLength] == 1)
111     switch ((unsigned char)[type characterAtIndex:0]) {
112       case 'c' : {
113         char value = atoi(cstring);
114         return [NSNumber numberWithChar:value];
115       }
116       case 'C' : {
117         unsigned char value = atoi(cstring);
118         return [NSNumber numberWithUnsignedChar:value];
119       }
120       case 's' : {
121         short value = atoi(cstring);
122         return [NSNumber numberWithShort:value];
123       }
124       case 'S' : {
125         unsigned short value = atoi(cstring);
126         return [NSNumber numberWithUnsignedShort:value];
127       }
128       case 'i' : {
129         int value = atoi(cstring);
130         return [NSNumber numberWithInt:value];
131       }
132       case 'I' : {
133         unsigned int value = atoi(cstring);
134         return [NSNumber numberWithUnsignedInt:value];
135       }
136       case 'l' : {
137         long value = atol(cstring);
138         return [NSNumber numberWithLong:value];
139       }
140       case 'L' : {
141         unsigned long value = atol(cstring);
142         return [NSNumber numberWithUnsignedLong:value];
143       }
144       case 'q' : {
145         long long value = atol(cstring);
146         return [NSNumber numberWithLongLong:value];
147       }
148       case 'Q' : {
149         unsigned long long value = atol(cstring);
150         return [NSNumber numberWithUnsignedLongLong:value];
151       }
152       case 'f' : {
153         float value = atof(cstring);
154         return [NSNumber numberWithFloat:value];
155       }
156       case 'd' : {
157         double value = atof(cstring);
158         return [NSNumber numberWithDouble:value];
159       }
160     }
161
162   [NSException raise:NSInvalidArgumentException
163                format:@"invalid type `%@' for NSNumber in "
164                  @"numberWithString:type:", type];
165   return nil;
166 }
167
168 - initWithString:(NSString*)string type:(NSString*)type {
169   (void)AUTORELEASE(self);
170   return RETAIN([NSNumber numberWithString:string type:type]);
171 }
172
173 - (NSString*)stringForType:(NSString*)type {
174   return [self stringValue];
175 }
176
177 @end /* NSNumber(EOCustomValues) */
178
179 void EOAccess_EOCustomValues_link(void) {
180   EOAccess_EOCustomValues_link();
181 }