/* SelectorBrowsingFunctions.m
 * Written By:  Thomas Burkholder
 *
 * You may freely copy, distribute, and reuse the code in this example.
 * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
 * fitness for any particular use.
 */

#import <ctype.h>
#import "SelectorBrowsingFunctions.h"
#import "../Utilities.subproj/SortedStorage.h"

int argumentsFor(SEL aSelector)
{
	// would really like to use method_getNumberOfArguments...
	// however, need a class to do that.  So, we do it the ugly way...

	const char *selName = sel_getName(aSelector);
	int i, args = 0, n = strlen(selName);

	for(i=0;(i<n);i++) {
		if (selName[i] == ':') args++;
	}
	return args;
}

int hasOneArgument(id src, SEL aSelector)
{
	return (1 == argumentsFor(aSelector));
}

int hasNoArguments(id src, SEL aSelector)
{
	return (0 == argumentsFor(aSelector));
}

int isAccessor(id src, SEL aSelector)
{
	return ([src instancesRespondTo:modifierFor(aSelector)]);
}

int isModifier(id src, SEL aSelector)
{
	return ([src instancesRespondTo:accessorFor(aSelector)]);
}

SEL modifierFor(SEL aSelector)
{
	const char *str;
	static char *buf;

	if (0!=argumentsFor(aSelector))
		return 0;
	str = sel_getName(aSelector);
	if (buf) NX_FREE(buf);
	NX_MALLOC(buf,char,strlen(str)+6);
	sprintf(buf,"set%s:",str);
	if (islower(buf[3]))
		buf[3] = toupper(buf[3]);
	return sel_getUid(buf);
}

SEL accessorFor(SEL aSelector)
{
	const char *str;
	static char *buf;

	if (1!=argumentsFor(aSelector))
		return 0;
	str = sel_getName(aSelector);
	if (strncmp(str,"set",3))
		return 0;
	if (buf) NX_FREE(buf);
	buf = NXCopyStringBuffer(&(str[3]));
	if (isupper(buf[0]))
		buf[0] = tolower(buf[0]);
	buf[strlen(buf)-1] = '\0';
	return sel_getUid(buf);
}
