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 #if LIB_FOUNDATION_LIBRARY
33 @implementation NSTemporaryString(EOCustomValues)
35 - initWithString:(NSString*)string type:(NSString*)type {
36 return [self initWithString:string];
42 @implementation NSString(EOCustomValues)
44 + stringWithString:(NSString*)string type:(NSString*)type {
45 // If mutable return a copy if not return self
47 if ([string isKindOfClass:[NSMutableString class]])
48 return [[NSString alloc] initWithString:string type:type];
53 - initWithString:(NSString*)string type:(NSString*)type {
54 return [self initWithString:string];
57 - (NSString*)stringForType:(NSString*)type {
58 // If mutable return a copy if not return self (handled in NSString)
59 return [self copyWithZone:[self zone]];
62 - (id)initWithData:(NSData*)data type:(NSString*)type {
63 return [self initWithCString:[data bytes] length:[data length]];
66 - (NSData *)dataForType:(NSString *)type {
67 unsigned len = [self cStringLength];
69 [self getCString:buf];
70 return [NSData dataWithBytes:buf length:len];
73 @end /* NSString(EOCustomValues) */
76 @implementation NSData(EOCustomValues)
78 - (id)initWithString:(NSString*)string type:(NSString*)type {
79 unsigned len = [string cStringLength];
81 [string getCString:buf];
83 return [self initWithBytes:buf length:len];
86 - (NSString*)stringForType:(NSString*)type {
87 return [NSString stringWithCString:[self bytes] length:[self length]];
90 - initWithData:(NSData*)data type:(NSString*)type {
91 return [self initWithBytes:[data bytes] length:[data length]];
94 - (NSData*)dataForType:(NSString*)type {
95 return [self copyWithZone:[self zone]];
98 @end /* NSData(EOCustomValues) */
101 @implementation NSNumber(EOCustomValues)
103 + (id)numberWithString:(NSString*)string type:(NSString*)type {
104 char buf[[string cStringLength] + 1];
107 [string getCString:buf];
110 if ([type cStringLength] == 1)
111 switch ((unsigned char)[type characterAtIndex:0]) {
113 char value = atoi(cstring);
114 return [NSNumber numberWithChar:value];
117 unsigned char value = atoi(cstring);
118 return [NSNumber numberWithUnsignedChar:value];
121 short value = atoi(cstring);
122 return [NSNumber numberWithShort:value];
125 unsigned short value = atoi(cstring);
126 return [NSNumber numberWithUnsignedShort:value];
129 int value = atoi(cstring);
130 return [NSNumber numberWithInt:value];
133 unsigned int value = atoi(cstring);
134 return [NSNumber numberWithUnsignedInt:value];
137 long value = atol(cstring);
138 return [NSNumber numberWithLong:value];
141 unsigned long value = atol(cstring);
142 return [NSNumber numberWithUnsignedLong:value];
145 long long value = atol(cstring);
146 return [NSNumber numberWithLongLong:value];
149 unsigned long long value = atol(cstring);
150 return [NSNumber numberWithUnsignedLongLong:value];
153 float value = atof(cstring);
154 return [NSNumber numberWithFloat:value];
157 double value = atof(cstring);
158 return [NSNumber numberWithDouble:value];
162 [NSException raise:NSInvalidArgumentException
163 format:@"invalid type `%@' for NSNumber in "
164 @"numberWithString:type:", type];
168 - initWithString:(NSString*)string type:(NSString*)type {
169 (void)AUTORELEASE(self);
170 return RETAIN([NSNumber numberWithString:string type:type]);
173 - (NSString*)stringForType:(NSString*)type {
174 return [self stringValue];
177 @end /* NSNumber(EOCustomValues) */
179 void EOAccess_EOCustomValues_link(void) {
180 EOAccess_EOCustomValues_link();