Here's how I do it in my AOrganiser program.
LONG ArgArray[] =
{
(LONG)"YES", /* CX_POPUP */
(LONG)"ctrl alt d", /* CX_POPKEY */
(LONG)0, /* CX_PRIORITY */
0
};
/* process args with read args or read tooltypes */
/* return a pinter to a tooltype array for CX */
BOOL FromWB = FALSE;
struct DiskObject *dob = NULL;
static struct RDArgs *rdargs;
STRPTR template = "CX_POPUP/K,CX_POPKEY/K,CX_PRIORITY/K/N,FILE";
static STRPTR *setupArgs(int argc, STRPTR* argv)
{
STRPTR *ttarray = NULL;
if(argc == 0)
{
/* We were invoked from WB */
int i = 0;
FromWB = TRUE;
struct WBStartup *wbs = (struct WBStartup *)argv;
if(wbs->sm_NumArgs > 1)
{
i = 1;
}
BPTR oldcd = IDOS->CurrentDir(wbs->sm_ArgList[i].wa_Lock);
dob=IIcon->GetDiskObject(wbs->sm_ArgList[i].wa_Name);
if(wbs->sm_NumArgs > 1)
{
if((Filename = IExec->AllocVec(sizeof(BYTE) * 1024, MEMF_ANY|MEMF_CLEAR)))
{
IDOS->NameFromLock(wbs->sm_ArgList[i].wa_Lock,Filename,1024);
IDOS->AddPart(Filename,wbs->sm_ArgList[i].wa_Name,1024);
}
}
if(dob)
{
if(IIcon->FindToolType(dob->do_ToolTypes, "CX_POPUP"))
{
ArgArray[0] = (LONG)IIcon->FindToolType(dob->do_ToolTypes, "CX_POPUP");
}
ArgArray[3] = (LONG)IIcon->FindToolType(dob->do_ToolTypes, "FILE");
ttarray = dob->do_ToolTypes;
}
else
{
if((ttarray = IExec->AllocVec(sizeof(UBYTE *)*4,MEMF_ANY|MEMF_CLEAR)))
{
ttarray[0] = NULL;
}
}
IDOS->CurrentDir(oldcd);
}
else
{
/* Invoked from CLI use readargs */
rdargs = IDOS->ReadArgs(template,ArgArray,NULL);
/* Create a "fake" ttarray */
if((ttarray = IExec->AllocVec(sizeof(UBYTE *)*4,MEMF_ANY|MEMF_CLEAR)))
{
ULONG i=0;
ULONG length =0;
length = strlen((char *)ArgArray[0]) + 10; // "CX_POPUP=....\0"
if((ttarray[i] = IExec->AllocVec(length,MEMF_ANY)))
{
sprintf(ttarray[i++],"CX_POPUP=%s",(UBYTE *)ArgArray[0]);
}
length = strlen((char *)ArgArray[1]) + 11; // "CX_POPKEY=....\0"
if((ttarray[i] = IExec->AllocVec(length,MEMF_ANY)))
{
sprintf(ttarray[i++],"CX_POPKEY=%s",(UBYTE *)ArgArray[1]);
}
length = 7 + 13; // "CX_PRIORITY=....\0"
if((ttarray[i] = IExec->AllocVec(length,MEMF_ANY)))
{
sprintf(ttarray[i++],"CX_PRIORITY=%d",(int)ArgArray[2]);
}
ttarray[i] = NULL;
}
}
return ttarray;
}
void releaseArgs(STRPTR *ttarray)
{
if(FromWB)
{
if(dob)IIcon->FreeDiskObject(dob);
if(Filename) IExec->FreeVec(Filename);
}
else
{
IDOS->FreeArgs(rdargs);
if(ttarray)
{
ULONG i = 0;
while(ttarray[i])
{
IExec->FreeVec(ttarray[i++]);
}
IExec->FreeVec(ttarray);
}
}
}
then
int main(int argc, char **argv)
{
STRPTR *ttypes;
if(openLibraries())
{
if((CALENDAR_Class = initCalendarClass()))
{
ttypes = setupArgs(argc, argv);
if(ArgArray[3])
{
int error;
if((error = Loadfile(&Events,(STRPTR)ArgArray[3])) <0)
{
// Reporterror(error);
}
if(Filename)
{
IExec->FreeVec(Filename);
}
Filename = Dupstr((STRPTR)ArgArray[3],-1);
}
else if (Filename)
{
int error;
if((error = Loadfile(&Events,Filename)) <0)
{
// Reporterror(error);
}
}
else
{
Filename = Dupstr("Unnamed",-1);
}
// do a whole heap of stuff here...
releaseArgs(ttypes);
freeCalendarClass(CALENDAR_Class);
}
}
closeLibraries();
return(0);
}
[edit]
Added some more code for clarity
[/edit]
Edited by broadblues on 2011/7/7 16:20:11