/* 
   Copyright (C) 1996 
	Ovidiu Predescu <ovidiu@bx.logicnet.ro>
	Mircea Oancea <mircea@jupiter.elcom.pub.ro>

   Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>

   This file is part of the FoundationExtensions library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; see the file COPYING.LIB.
   If not, write to the Free Software Foundation,
   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include <stdarg.h>
#include <stdio.h>
#include <Foundation/NSString.h>

#include <extensions/FormatScanner.h>
#include <extensions/PrintfScannerHandler.h>
#include <extensions/support.h>

@implementation PrintfScannerHandler

- init
{
    [super init];

    specHandler['d'] = [self methodForSelector:@selector(convertInt:scanner:)];
    specHandler['i'] = [self methodForSelector:@selector(convertInt:scanner:)];
    specHandler['o'] = [self methodForSelector:@selector(convertInt:scanner:)];
    specHandler['x'] = [self methodForSelector:@selector(convertInt:scanner:)];
    specHandler['X'] = [self methodForSelector:@selector(convertInt:scanner:)];
    specHandler['u'] = [self methodForSelector:@selector(convertInt:scanner:)];
    specHandler['c']
	    = [self methodForSelector:@selector(convertChar:scanner:)];
    specHandler['s']
	    = [self methodForSelector:@selector(convertString:scanner:)];
    specHandler['f']
	    = [self methodForSelector:@selector(convertFloat:scanner:)];
    specHandler['e']
	    = [self methodForSelector:@selector(convertFloat:scanner:)];
    specHandler['E']
	    = [self methodForSelector:@selector(convertFloat:scanner:)];
    specHandler['g']
	    = [self methodForSelector:@selector(convertFloat:scanner:)];
    specHandler['G']
	    = [self methodForSelector:@selector(convertFloat:scanner:)];
    specHandler['p']
	    = [self methodForSelector:@selector(convertPointer:scanner:)];
    return self;
}

- (NSString*)convertInt:(va_list*)pInt scanner:(FormatScanner*)scanner
{
    char buffer[256];
    sprintf(buffer, [scanner currentSpecifier], va_arg(*pInt, int));
    return [NSString stringWithCString:buffer];
}

- (NSString*)convertChar:(va_list*)pChar scanner:(FormatScanner*)scanner
{
    char buffer[256];
    sprintf(buffer, [scanner currentSpecifier], va_arg(*pChar, char));
    return [NSString stringWithCString:buffer];
}

- (NSString*)convertString:(va_list*)pString scanner:(FormatScanner*)scanner
{
    char* string = va_arg(*pString, char*);
    return string ? [NSString stringWithCString:string] : @"";
}

- (NSString*)convertFloat:(va_list*)pFloat scanner:(FormatScanner*)scanner
{
    char buffer[256];
    sprintf(buffer, [scanner currentSpecifier], va_arg(*pFloat, double));
    return [NSString stringWithCString:buffer];
}

- (NSString*)convertPointer:(va_list*)pPointer scanner:(FormatScanner*)scanner
{
    char buffer[256];
    sprintf(buffer, [scanner currentSpecifier], va_arg(*pPointer, void*));
    return [NSString stringWithCString:buffer];
}

@end /* PrintfScannerHandler */


@implementation FSObjectFormat

- init
{
    [super init];
    specHandler['@']
	    = [self methodForSelector:@selector(convertObject:scanner:)];
    return self;
}

- (NSString*)convertObject:(va_list*)pId scanner:scanner
{
    id object = va_arg(*pId, id);
    return [object description];
}

@end /* FSObjectFormat */
