]> err.no Git - sope/blob - sope-gdl1/GDLAccess/EOCustomValues.m
minor changes to Xcode project layout
[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 @implementation NSString(EOCustomValues)
33
34 + stringWithString:(NSString*)string type:(NSString*)type {
35   // If mutable return a copy if not return self
36     
37   if ([string isKindOfClass:[NSMutableString class]])
38     return [[NSString alloc] initWithString:string type:type];
39   else
40     return RETAIN(self);
41 }
42
43 - initWithString:(NSString*)string type:(NSString*)type {
44   return [self initWithString:string];
45 }
46
47 - (NSString*)stringForType:(NSString*)type {
48   // If mutable return a copy if not return self (handled in NSString)
49   return [self copyWithZone:[self zone]];
50 }
51
52 - (id)initWithData:(NSData*)data type:(NSString*)type {
53   return [self initWithCString:[data bytes] length:[data length]];
54 }
55
56 - (NSData *)dataForType:(NSString *)type {
57   unsigned len = [self cStringLength];
58   char buf[len + 1];
59   [self getCString:buf];
60   return [NSData dataWithBytes:buf length:len];
61 }
62
63 @end /* NSString(EOCustomValues) */
64
65
66 @implementation NSData(EOCustomValues)
67
68 - (id)initWithString:(NSString*)string type:(NSString*)type {
69   unsigned len = [string cStringLength];
70   char buf[len + 1];
71   [string getCString:buf];
72   
73   return [self initWithBytes:buf length:len];
74 }
75
76 - (NSString*)stringForType:(NSString*)type {
77   return [NSString stringWithCString:[self bytes] length:[self length]];
78 }
79
80 - initWithData:(NSData*)data type:(NSString*)type {
81   return [self initWithBytes:[data bytes] length:[data length]];
82 }
83
84 - (NSData*)dataForType:(NSString*)type {
85   return [self copyWithZone:[self zone]];
86 }
87
88 @end /* NSData(EOCustomValues) */
89
90
91 @implementation NSNumber(EOCustomValues)
92
93 + (id)numberWithString:(NSString*)string type:(NSString*)type {
94   char buf[[string cStringLength] + 1];
95   const char *cstring;
96
97   [string getCString:buf];
98   cstring = buf;
99   
100   if ([type cStringLength] == 1)
101     switch ((unsigned char)[type characterAtIndex:0]) {
102       case 'c' : {
103         char value = atoi(cstring);
104         return [NSNumber numberWithChar:value];
105       }
106       case 'C' : {
107         unsigned char value = atoi(cstring);
108         return [NSNumber numberWithUnsignedChar:value];
109       }
110       case 's' : {
111         short value = atoi(cstring);
112         return [NSNumber numberWithShort:value];
113       }
114       case 'S' : {
115         unsigned short value = atoi(cstring);
116         return [NSNumber numberWithUnsignedShort:value];
117       }
118       case 'i' : {
119         int value = atoi(cstring);
120         return [NSNumber numberWithInt:value];
121       }
122       case 'I' : {
123         unsigned int value = atoi(cstring);
124         return [NSNumber numberWithUnsignedInt:value];
125       }
126       case 'l' : {
127         long value = atol(cstring);
128         return [NSNumber numberWithLong:value];
129       }
130       case 'L' : {
131         unsigned long value = atol(cstring);
132         return [NSNumber numberWithUnsignedLong:value];
133       }
134       case 'q' : {
135         long long value = atol(cstring);
136         return [NSNumber numberWithLongLong:value];
137       }
138       case 'Q' : {
139         unsigned long long value = atol(cstring);
140         return [NSNumber numberWithUnsignedLongLong:value];
141       }
142       case 'f' : {
143         float value = atof(cstring);
144         return [NSNumber numberWithFloat:value];
145       }
146       case 'd' : {
147         double value = atof(cstring);
148         return [NSNumber numberWithDouble:value];
149       }
150     }
151
152   [NSException raise:NSInvalidArgumentException
153                format:@"invalid type `%@' for NSNumber in "
154                  @"numberWithString:type:", type];
155   return nil;
156 }
157
158 - initWithString:(NSString*)string type:(NSString*)type {
159   (void)AUTORELEASE(self);
160   return RETAIN([NSNumber numberWithString:string type:type]);
161 }
162
163 - (NSString*)stringForType:(NSString*)type {
164   return [self stringValue];
165 }
166
167 @end /* NSNumber(EOCustomValues) */
168
169 void EOAccess_EOCustomValues_link(void) {
170   EOAccess_EOCustomValues_link();
171 }