#include "sysheaders.h"
#include "CompactPlayer.h"

/* This is a neat little function for reading tooltypes from an icon as if they
 * were command line arguments. It needs some special setup like an oversized
 * argument array... */

struct RDArgs *
ReadToolArgs( STRPTR template, LONG *argarray )
{
	struct DiskObject *icon;
	struct RDArgs *rda = AllocDosObject( DOS_RDARGS, NULL );
	STRPTR mytemplate = AllocVec( strlen(template)+16, MEMF_CLEAR );
	
	if (rda && mytemplate)
	{
		rda->RDA_ExtHelp = NULL;
		
		strcpy( mytemplate, template );
		
		if (_WBenchMsg)
		{
			CurrentDir( _WBenchMsg->sm_ArgList->wa_Lock );
			
			if (icon = GetDiskObject( _WBenchMsg->sm_ArgList->wa_Name ))
			{
				STRPTR *toolarray, toolstring;
				
				if (toolarray = icon->do_ToolTypes)
				while( *toolarray )
				{
					rda->RDA_Source.CS_Length += strlen(*toolarray++) + 1;
				}
				
				if (rda->RDA_Source.CS_Length)
				{
					STRPTR s;
					rda->RDA_Source.CS_Buffer = AllocVec( rda->RDA_Source.CS_Length + 1, MEMF_CLEAR );
					toolstring = rda->RDA_Source.CS_Buffer;
				
					/* copy all tool types to a single string */
					
					for ( toolarray = icon->do_ToolTypes ;
						s = *toolarray ; toolarray++ )
					{
						while (*toolstring = *s++)
							toolstring++;
						*toolstring++ = ' ';
					}
					*(toolstring - 1)= '\n';
					*toolstring = 0;
				}
				FreeDiskObject( icon );
			}
			strcat( mytemplate, ",ThrowAway/M" );
		}
		rda = ReadArgs( mytemplate, argarray, rda );
	}
	if (mytemplate)
		FreeVec( mytemplate );
	return rda;
}

void
FreeToolArgs( struct RDArgs *rda )
{
	if (rda)
	{
		if ( rda->RDA_Source.CS_Buffer )
			FreeVec( rda->RDA_Source.CS_Buffer );
		FreeArgs( rda );
		FreeDosObject( DOS_RDARGS, rda );
	}
}
