4 Copyright (C) 1996 Free Software Foundation, Inc.
6 Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>
9 This file is part of the GNUstep Database Library.
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.
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.
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.
27 // $Id: EOCustomValues.m 1 2004-08-20 10:38:46Z znek $
30 #import "EOCustomValues.h"
32 @implementation NSString(EOCustomValues)
34 + stringWithString:(NSString*)string type:(NSString*)type {
35 // If mutable return a copy if not return self
37 if ([string isKindOfClass:[NSMutableString class]])
38 return [[NSString alloc] initWithString:string type:type];
43 - initWithString:(NSString*)string type:(NSString*)type {
44 return [self initWithString:string];
47 - (NSString*)stringForType:(NSString*)type {
48 // If mutable return a copy if not return self (handled in NSString)
49 return [self copyWithZone:[self zone]];
52 - (id)initWithData:(NSData*)data type:(NSString*)type {
53 return [self initWithCString:[data bytes] length:[data length]];
56 - (NSData *)dataForType:(NSString *)type {
57 unsigned len = [self cStringLength];
59 [self getCString:buf];
60 return [NSData dataWithBytes:buf length:len];
63 @end /* NSString(EOCustomValues) */
66 @implementation NSData(EOCustomValues)
68 - (id)initWithString:(NSString*)string type:(NSString*)type {
69 unsigned len = [string cStringLength];
71 [string getCString:buf];
73 return [self initWithBytes:buf length:len];
76 - (NSString*)stringForType:(NSString*)type {
77 return [NSString stringWithCString:[self bytes] length:[self length]];
80 - initWithData:(NSData*)data type:(NSString*)type {
81 return [self initWithBytes:[data bytes] length:[data length]];
84 - (NSData*)dataForType:(NSString*)type {
85 return [self copyWithZone:[self zone]];
88 @end /* NSData(EOCustomValues) */
91 @implementation NSNumber(EOCustomValues)
93 + (id)numberWithString:(NSString*)string type:(NSString*)type {
94 char buf[[string cStringLength] + 1];
97 [string getCString:buf];
100 if ([type cStringLength] == 1)
101 switch ((unsigned char)[type characterAtIndex:0]) {
103 char value = atoi(cstring);
104 return [NSNumber numberWithChar:value];
107 unsigned char value = atoi(cstring);
108 return [NSNumber numberWithUnsignedChar:value];
111 short value = atoi(cstring);
112 return [NSNumber numberWithShort:value];
115 unsigned short value = atoi(cstring);
116 return [NSNumber numberWithUnsignedShort:value];
119 int value = atoi(cstring);
120 return [NSNumber numberWithInt:value];
123 unsigned int value = atoi(cstring);
124 return [NSNumber numberWithUnsignedInt:value];
127 long value = atol(cstring);
128 return [NSNumber numberWithLong:value];
131 unsigned long value = atol(cstring);
132 return [NSNumber numberWithUnsignedLong:value];
135 long long value = atol(cstring);
136 return [NSNumber numberWithLongLong:value];
139 unsigned long long value = atol(cstring);
140 return [NSNumber numberWithUnsignedLongLong:value];
143 float value = atof(cstring);
144 return [NSNumber numberWithFloat:value];
147 double value = atof(cstring);
148 return [NSNumber numberWithDouble:value];
152 [NSException raise:NSInvalidArgumentException
153 format:@"invalid type `%@' for NSNumber in "
154 @"numberWithString:type:", type];
158 - initWithString:(NSString*)string type:(NSString*)type {
159 (void)AUTORELEASE(self);
160 return RETAIN([NSNumber numberWithString:string type:type]);
163 - (NSString*)stringForType:(NSString*)type {
164 return [self stringValue];
167 @end /* NSNumber(EOCustomValues) */
169 void EOAccess_EOCustomValues_link(void) {
170 EOAccess_EOCustomValues_link();