/* 
   signature.m

   Copyright (C) 1995, 1996, 1997 Ovidiu Predescu and Mircea Oancea.
   All rights reserved.

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

   This file is part of libFoundation.

   Permission to use, copy, modify, and distribute this software and its
   documentation for any purpose and without fee is hereby granted, provided
   that the above copyright notice appear in all copies and that both that
   copyright notice and this permission notice appear in supporting
   documentation.

   We disclaim all warranties with regard to this software, including all
   implied warranties of merchantability and fitness, in no event shall
   we be liable for any special, indirect or consequential damages or any
   damages whatsoever resulting from loss of use, data or profits, whether in
   an action of contract, negligence or other tortious action, arising out of
   or in connection with the use or performance of this software.
*/

#include <config.h>

#include <ctype.h>

#if HAVE_STRING_H
# include <string.h>
#endif

#if HAVE_MEMORY_H
# include <memory.h>
#endif

#include <Foundation/NSMethodSignature.h>
#include <Foundation/NSString.h>

#include <extensions/objc-runtime.h>
#include <extensions/NSException.h>
#include <extensions/exceptions/GeneralExceptions.h>

@interface NSMethodSignature (FullTypeEncoding)
@end

@implementation NSMethodSignature (FullTypeEncoding)

static const char* fill_with_type(char* buffer, const char* types)
{
    const char* initial = types;

    switch (*types) {
	case _C_ID:
	case _C_CLASS:
	case _C_SEL:
	case _C_CHR:
	case _C_UCHR:
	case _C_CHARPTR:
	case _C_ATOM:
	case _C_SHT:
	case _C_USHT:
	case _C_INT:
	case _C_UINT:
	case _C_LNG:
	case _C_ULNG:
	case _C_FLT:
	case _C_DBL:
	case _C_VOID:
	case _C_UNDEF:
	    buffer[0] = *types; buffer[1] = 0;
	    break;
	case _C_ARY_B:
	    while (isdigit (*++types));
	    types = objc_skip_typespec(types);
	    if (*types != _C_ARY_E)
		THROW([InvalidArgumentException new]);
	    strncpy (buffer, initial, types - initial);
	    buffer [types - initial] = 0;
	    break;
	case _C_STRUCT_B:
	    while (*types != _C_STRUCT_E && *types++ != '=');
	    while (*types != _C_STRUCT_E)
		types = objc_skip_typespec (types);
	    types++;
	    strncpy (buffer, initial, types - initial);
	    buffer [types - initial] = 0;
	    break;
	case _C_UNION_B:    
	    while (*types != _C_UNION_E && *types++ != '=');
	    while (*types != _C_UNION_E)
		types = objc_skip_typespec (types);
	    types++;
	    strncpy (buffer, initial, types - initial);
	    buffer [types - initial] = 0;
	    break;
	case _C_PTR:    
	    buffer[0] = *types;
	    fill_with_type(buffer + 1, ++types);
	    break;
	default:
	    THROW([InvalidArgumentException new]);
    }
    return buffer;
}

- (NSString*)fullMethodEncoding
{
    NSMutableString* encoding = [[NSMutableString new] autorelease];
    int i, count = [self numberOfArguments];
    char buffer[256];

    [encoding appendFormat:@"%s%d",
		fill_with_type(buffer, [self methodReturnType]),
		[self frameLength]];
    for (i = 0; i < count; i++) {
	NSArgumentInfo argInfo = [self argumentInfoAtIndex:i];
	[encoding appendFormat:@"%s%d",
		    fill_with_type(buffer, argInfo.type),
		    argInfo.offset];
    }
    return encoding;
}

@end
