Hi just created this code to ADD/CHANGE one tooltype (IMAGEPATH) and want to share/show if it's well done or it's so "bad coded (TM)" or maybe if there is an easier way to do it:
void SaveNewIMAGEPATH(void) { STRPTR *oldttp = NULL; int i; char newttp[128]="IMAGEPATH="; struct DiskObject *micon = NULL; if(WBenchMsg) // get tool(Mixer) name, maybe user changed 'Mixer' to 'XXX' snprintf(textbuf, sizeof(textbuf), "PROGDIR:%s",WBenchMsg->sm_ArgList->wa_Name); else GetCliProgramName( textbuf, sizeof(textbuf) ); micon = GetDiskObject(textbuf); if(micon) { strcat(newttp, ARG("IMAGEPATH") ); #ifdef _DEBUG_ Printf("'%s'\n",newttp); #endif for(i = 0; micon->do_ToolTypes[i] != NULL; i++) // parse tootlypes { if( !strncmp(micon->do_ToolTypes[i], "IMAGEPATH=", 10) ) // is it IMAGEPATH ? { #ifdef _DEBUG_ Printf("1'%s'\n",micon->do_ToolTypes[i]); #endif if( stricmp(micon->do_ToolTypes[i], newttp) || strlen(micon->do_ToolTypes[i]) != strlen(newttp) ) {// skin changed -> modify IMAGEPATH tooltype and END oldttp = micon->do_ToolTypes; micon->do_ToolTypes[i] = newttp; PutDiskObject(textbuf,micon); micon->do_ToolTypes = oldttp; #ifdef _DEBUG_ Printf("(%s) modified.\n",newttp); #endif } break; } } if( !micon->do_ToolTypes[i] && stricmp("PROGDIR:Images", ARG("IMAGEPATH")) ) {// no IMAGEPATH tooltype AND skin changed -> create IMAGEPATH tooltype and END #ifdef _DEBUG_ Printf("2IMAGEPATH='%s' i=%ld\n",ARG("IMAGEPATH"),i ); #endif oldttp = micon->do_ToolTypes; micon->do_ToolTypes[i] = newttp; micon->do_ToolTypes[i+1] = NULL; PutDiskObject(textbuf,micon); micon->do_ToolTypes = oldttp; #ifdef _DEBUG_ Printf("(%s) created.\n",newttp); #endif } FreeDiskObject(micon); } #ifdef _DEBUG_ Printf("END SaveNewIMAGEPATH()\n"); #endif }
BTW 'char newttp[128]="IMAGEPATH=";'
can be changed to:
'char *newttp; .. newttp=AllocVecTags(128, AVT_ClearWithValue, 0, TAG_END); .. FreeVec(newttp)'
is it "better"/safer that last mode?
TIA
I don't think there is any reserved space behind the do_ToolTypes array for adding new tooltypes. Therefore you must not write to do_ToolTypes[i+1]. You rather have to allocate a complete new array with additional space, copy the existing array, add your new tooltype and write the pointer of the new array into the do_ToolTypes field.
something like this:
You're right it worked sometimes, crashed some others.
Changed with your code (note to myself: never try to change/add directly structures/objects already read/allocated) :-) and working always fine.
AOS4.1/SAM460ex/PPC460EX-1155MHZ/2048MB/RadeonHD6570/SSD120GB/DVDRW :-P
The lines 28 - 31 also don't look correct. You save/restore the pointer to the string array but override only one of the string pointers, so you save/restore the wrong one.