1/*
2   EOQualifierScanner.m
3
4   Copyright (C) 1996 Free Software Foundation, Inc.
5
6   Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>
7           Helge Hess <helge.hess@mdlink.de>
8   Date:   September 1996
9           November  1999
10
11   This file is part of the GNUstep Database Library.
12
13   This library is free software; you can redistribute it and/or
14   modify it under the terms of the GNU Library General Public
15   License as published by the Free Software Foundation; either
16   version 2 of the License, or (at your option) any later version.
17
18   This library is distributed in the hope that it will be useful,
19   but WITHOUT ANY WARRANTY; without even the implied warranty of
20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21   Library General Public License for more details.
22
23   You should have received a copy of the GNU Library General Public
24   License along with this library; see the file COPYING.LIB.
25   If not, write to the Free Software Foundation,
26   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27*/
28// $Id: EOQualifierScanner.m 1 2004-08-20 10:38:46Z znek $
29
30#import "common.h"
31#include "EOQualifierScanner.h"
32#include "EOFExceptions.h"
33#include "EOEntity.h"
34#include "EOSQLQualifier.h"
35#include <EOControl/EONull.h>
36
37#if LIB_FOUNDATION_LIBRARY
38#  import <extensions/DefaultScannerHandler.h>
39#  import <extensions/PrintfFormatScanner.h>
40#else
41#  import "DefaultScannerHandler.h"
42#  import "PrintfFormatScanner.h"
43#endif
44
45@implementation EOQualifierScannerHandler
46
47- (id)init {
48    [super init];
49
50    specHandler['d'] = [self methodForSelector:@selector(convertInt:scanner:)];
51    specHandler['f']
52            = [self methodForSelector:@selector(convertFloat:scanner:)];
53    specHandler['s']
54            = [self methodForSelector:@selector(convertCString:scanner:)];
55    specHandler['A']
56            = [self methodForSelector:@selector(convertProperty:scanner:)];
57    specHandler['@']
58            = [self methodForSelector:@selector(convertObject:scanner:)];
59    return self;
60}
61
62- (void)setEntity:(EOEntity *)_entity
63{
64    ASSIGN(self->entity, _entity);
65}
66
67- (void)dealloc {
68    RELEASE(self->entity);
69    [super dealloc];
70}
71
72/* conversions */
73
74- (NSString *)convertInt:(va_list *)pInt scanner:(FormatScanner*)scanner {
75    char buffer[256];
76    sprintf(buffer, [scanner currentSpecifier], va_arg(*pInt, int));
77    return [NSString stringWithCString:buffer];
78}
79
80- (NSString*)convertFloat:(va_list *)pFloat scanner:(FormatScanner*)scanner {
81    char buffer[256];
82    sprintf(buffer, [scanner currentSpecifier], va_arg(*pFloat, double));
83    return [NSString stringWithCString:buffer];
84}
85
86- (NSString*)convertCString:(va_list *)pString scanner:(FormatScanner*)scanner {
87    char *string;
88    string = va_arg(*pString, char*);
89    return string ? [NSString stringWithCString:string] : (id)@"";
90}
91
92- (NSString*)convertProperty:(va_list*)pString scanner:(FormatScanner*)scanner {
93    NSString *propertyName;
94    id property;
95
96    propertyName = va_arg(*pString, id);
97    property     = [entity propertyNamed:propertyName];
98
99    if(property == nil) {
100        [[[InvalidPropertyException alloc]
101                    initWithName:propertyName entity:entity] raise];
102    }
103    return propertyName;
104}
105
106- (NSString *)convertObject:(va_list *)pId scanner:scanner {
107  id object = va_arg(*pId, id);
108  if (object == nil) object = [NSNull null];
109  return [object expressionValueForContext:nil];
110}
111
112@end /* EOQualifierScannerHandler */
113
114@implementation EOQualifierEnumScannerHandler
115
116- (id)init {
117    [super init];
118
119    specHandler['d'] = [self methodForSelector:@selector(convertInt:scanner:)];
120    specHandler['f']
121            = [self methodForSelector:@selector(convertFloat:scanner:)];
122    specHandler['s']
123            = [self methodForSelector:@selector(convertCString:scanner:)];
124    specHandler['A']
125            = [self methodForSelector:@selector(convertProperty:scanner:)];
126    specHandler['@']
127            = [self methodForSelector:@selector(convertObject:scanner:)];
128    return self;
129}
130
131- (void)setEntity:(EOEntity *)_entity {
132    ASSIGN(self->entity, _entity);
133}
134
135- (void)dealloc {
136    RELEASE(self->entity);
137    [super dealloc];
138}
139
140- (NSString *)convertInt:(NSEnumerator **)pInt scanner:(FormatScanner*)scanner {
141    char buffer[256];
142    sprintf(buffer, [scanner currentSpecifier], [[*pInt nextObject] intValue]);
143    return [NSString stringWithCString:buffer];
144}
145
146- (NSString *)convertFloat:(NSEnumerator **)pFloat
147  scanner:(FormatScanner *)scanner
148{
149    char buffer[256];
150    sprintf(buffer, [scanner currentSpecifier],
151            [[*pFloat nextObject] doubleValue]);
152    return [NSString stringWithCString:buffer];
153}
154
155- (NSString *)convertCString:(NSEnumerator **)pString
156  scanner:(FormatScanner *)scanner
157{
158  id str;
159
160  if ((str = [*pString nextObject]) == nil)
161    str = @"";
162  else if ([str isKindOfClass:[NSString class]])
163      ;
164  else if ([str respondsToSelector:@selector(stringValue)])
165    str = [str stringValue];
166  else
167    str = [str description];
168
169  return (str == nil) ? (id)@"" : str;
170}
171
172- (NSString *)convertProperty:(NSEnumerator **)pString
173  scanner:(FormatScanner *)scanner
174{
175    NSString *propertyName;
176    id property;
177
178    propertyName = [*pString nextObject];
179    property     = [entity propertyNamed:propertyName];
180
181    if(property == nil) {
182        [[[InvalidPropertyException alloc]
183                    initWithName:propertyName entity:entity] raise];
184    }
185    return propertyName;
186}
187
188- (NSString *)convertObject:(NSEnumerator **)pId scanner:(id)scanner {
189    id object;
190    object = [*pId nextObject];
191    return [object expressionValueForContext:nil];
192}
193
194@end /* EOQualifierEnumScannerHandler */
195