Add/Change tooltype

4 posts / 0 new
Last post
jabirulo
jabirulo's picture
Offline
Last seen: 2 weeks 4 days ago
Joined: 2013-05-30 00:53
Add/Change tooltype

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:

  1. void SaveNewIMAGEPATH(void)
  2. {
  3. STRPTR *oldttp = NULL;
  4. int i;
  5. char newttp[128]="IMAGEPATH=";
  6. struct DiskObject *micon = NULL;
  7.  
  8. if(WBenchMsg) // get tool(Mixer) name, maybe user changed 'Mixer' to 'XXX'
  9. snprintf(textbuf, sizeof(textbuf), "PROGDIR:%s",WBenchMsg->sm_ArgList->wa_Name);
  10. else GetCliProgramName( textbuf, sizeof(textbuf) );
  11.  
  12. micon = GetDiskObject(textbuf);
  13. if(micon)
  14. {
  15. strcat(newttp, ARG("IMAGEPATH") );
  16. #ifdef _DEBUG_
  17. Printf("'%s'\n",newttp);
  18. #endif
  19. for(i = 0; micon->do_ToolTypes[i] != NULL; i++) // parse tootlypes
  20. {
  21. if( !strncmp(micon->do_ToolTypes[i], "IMAGEPATH=", 10) ) // is it IMAGEPATH ?
  22. {
  23. #ifdef _DEBUG_
  24. Printf("1'%s'\n",micon->do_ToolTypes[i]);
  25. #endif
  26. if( stricmp(micon->do_ToolTypes[i], newttp) || strlen(micon->do_ToolTypes[i]) != strlen(newttp) )
  27. {// skin changed -> modify IMAGEPATH tooltype and END
  28. oldttp = micon->do_ToolTypes;
  29. micon->do_ToolTypes[i] = newttp;
  30. PutDiskObject(textbuf,micon);
  31. micon->do_ToolTypes = oldttp;
  32. #ifdef _DEBUG_
  33. Printf("(%s) modified.\n",newttp);
  34. #endif
  35. }
  36. break;
  37.  
  38. }
  39.  
  40. }
  41.  
  42. if( !micon->do_ToolTypes[i] && stricmp("PROGDIR:Images", ARG("IMAGEPATH")) )
  43. {// no IMAGEPATH tooltype AND skin changed -> create IMAGEPATH tooltype and END
  44. #ifdef _DEBUG_
  45. Printf("2IMAGEPATH='%s' i=%ld\n",ARG("IMAGEPATH"),i );
  46. #endif
  47. oldttp = micon->do_ToolTypes;
  48. micon->do_ToolTypes[i] = newttp;
  49. micon->do_ToolTypes[i+1] = NULL;
  50. PutDiskObject(textbuf,micon);
  51. micon->do_ToolTypes = oldttp;
  52. #ifdef _DEBUG_
  53. Printf("(%s) created.\n",newttp);
  54. #endif
  55. }
  56.  
  57. FreeDiskObject(micon);
  58. }
  59. #ifdef _DEBUG_
  60. Printf("END SaveNewIMAGEPATH()\n");
  61. #endif
  62. }

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

thomas
thomas's picture
Offline
Last seen: 1 week 3 days ago
Joined: 2011-05-16 14:23
I don't think there is any

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:

  1. oldtts = micon->do_ToolTypes;
  2. newtts = malloc((i+2) * sizeof(STRPTR));
  3. memcpy (newtts,oldtts,i * sizeof(STRPTR);
  4. newtts[i] = newttp;
  5. newtts[i+1] = NULL;
  6. micon->do_ToolTypes = newtts;
  7. PutDiskObject (textbuf,micon);
  8. micon->do_ToolTypes = oldtts;
  9. free (newtts);
  10. FreeDiskObject (micon);
jabirulo
jabirulo's picture
Offline
Last seen: 2 weeks 4 days ago
Joined: 2013-05-30 00:53
You're right it worked

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

gazelle
gazelle's picture
Offline
Last seen: 1 year 2 months ago
Joined: 2011-04-13 12:52
The lines 28 - 31 also don't

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.

Log in or register to post comments