Is this the right form to update hooks to OS4?

8 posts / 0 new
Last post
jabirulo
jabirulo's picture
Offline
Last seen: 5 hours 10 min ago
Joined: 2013-05-30 00:53
Is this the right form to update hooks to OS4?

Hi, just updated hooktest.c to use OS4 hooks "mode". It works fine. IS this the right way if updating/coding? Or must I for every AllocSys.. add FreeSys..?

TIA

  1. ;/*
  2. gcc hooktest2.c -o HookTest2 -Wall -lauto -gstabs
  3. quit
  4. */
  5. #include <proto/dos.h>
  6. #include <proto/locale.h>
  7. #include <proto/exec.h>
  8.  
  9.  
  10. void fmtfunct(struct Hook *hook, struct Locale *locale, ULONG ch)
  11. {
  12. char *buffer = (char *)hook->h_Data;
  13. *buffer++ = ch;
  14. hook->h_Data = (APTR)buffer;
  15. }
  16.  
  17.  
  18. int main(void)
  19. {
  20. struct DateStamp ds;
  21. char buffer[30];
  22. struct Hook *prhook;
  23.  
  24. IDOS->DateStamp(&ds);
  25.  
  26. // fmthook.h_Entry = (HOOKFUNC)fmtfunct;
  27. // fmthook.h_Data = (APTR)buffer;
  28. prhook = (struct Hook *)IExec->AllocSysObjectTags(ASOT_HOOK,
  29. ASOHOOK_Entry, fmtfunct,
  30. ASOHOOK_Data, buffer, TAG_DONE);
  31. ILocale->FormatDate(NULL,"%d-%m-%Y", &ds, prhook);
  32. IDOS->Printf("Fecha: %s\n",buffer);
  33.  
  34. // fmthook.h_Entry = (HOOKFUNC)fmtfunct;
  35. // fmthook.h_Data = (APTR)buffer;
  36. prhook = (struct Hook *)IExec->AllocSysObjectTags(ASOT_HOOK,
  37. ASOHOOK_Entry, fmtfunct,
  38. ASOHOOK_Data, buffer, TAG_DONE);
  39. ILocale->FormatDate(NULL,"%d", &ds, prhook);
  40. IDOS->Printf("Day: %s\n",buffer);
  41.  
  42. // fmthook.h_Entry = (HOOKFUNC)fmtfunct;
  43. // fmthook.h_Data = (APTR)buffer;
  44. prhook = (struct Hook *)IExec->AllocSysObjectTags(ASOT_HOOK,
  45. ASOHOOK_Entry, fmtfunct,
  46. ASOHOOK_Data, buffer, TAG_DONE);
  47. ILocale->FormatDate(NULL,"%m", &ds, prhook);
  48. IDOS->Printf("Month: %s ",buffer);
  49. // fmthook.h_Entry = (HOOKFUNC)fmtfunct;
  50. // fmthook.h_Data = (APTR)buffer;
  51. prhook = (struct Hook *)IExec->AllocSysObjectTags(ASOT_HOOK,
  52. ASOHOOK_Entry, fmtfunct,
  53. ASOHOOK_Data, buffer, TAG_DONE);
  54. ILocale->FormatDate(NULL,"%B", &ds, prhook);
  55. IDOS->Printf("(%s)\n",buffer);
  56.  
  57. // fmthook.h_Entry = (HOOKFUNC)fmtfunct;
  58. // fmthook.h_Data = (APTR)buffer;
  59. prhook = (struct Hook *)IExec->AllocSysObjectTags(ASOT_HOOK,
  60. ASOHOOK_Entry, fmtfunct,
  61. ASOHOOK_Data, buffer, TAG_DONE);
  62. ILocale->FormatDate(NULL,"%Y", &ds, prhook);
  63. IDOS->Printf("Year: %s\n",buffer);
  64.  
  65. IExec->FreeSysObject(ASOT_HOOK, prhook);
  66. return(RETURN_OK);
  67. }
trixie
trixie's picture
Offline
Last seen: 5 months 7 hours ago
Joined: 2011-02-03 13:58
Or must I for every

Or must I for every AllocSys.. add FreeSys..?

Of course! Each AllocSysObject() is an allocation so it must be deallocated, regardless of your using a single variable for the hook pointer.

AmigaOne X5000-020 / 2GB RAM / Sapphire Pulse Radeon RX 560 / AmigaOS 4.1 Final Edition Update 2

jabirulo
jabirulo's picture
Offline
Last seen: 5 hours 10 min ago
Joined: 2013-05-30 00:53
OK, THX.

OK, THX.

AOS4.1/SAM460ex/PPC460EX-1155MHZ/2048MB/RadeonHD6570/SSD120GB/DVDRW :-P

thomas
thomas's picture
Offline
Last seen: 10 hours 29 min ago
Joined: 2011-05-16 14:23
I would call AllocSysObject

I would call AllocSysObject only once in the beginning and then only poke the new buffer pointer into hook->h_Data. You do this in your fmtfunct routine anyway, so it should be legal for the main routine, too.

jabirulo
jabirulo's picture
Offline
Last seen: 5 hours 10 min ago
Joined: 2013-05-30 00:53
Hi, as I'm not very good

Hi, as I'm not very good coder.
How do I "poke the new buffer pointer into hook->h_Data"?

BTW I found another way to get Year/Month/Day values:

  1. int32 D_today, M_today, Y_today;
  2. struct DateTime dt;
  3. char dt_str[11], buffer[11];
  4. // Get current date
  5. IDOS->DateStamp(&dt.dat_Stamp);
  6. dt.dat_Format = FORMAT_ISO; // YYYY-MM-DD (dt_str "positions view" 0123-56-89)
  7. dt.dat_Flags = 0;
  8. dt.dat_StrDay = NULL;
  9. dt.dat_StrDate = dt_str;
  10. dt.dat_StrTime = NULL;
  11. IDOS->DateToStr(&dt);
  12.  
  13. IUtility->Strlcpy(buffer, dt_str, 5); // copy only the YEAR
  14. IDOS->StrToLong(buffer, &Y_today);
  15.  
  16. IUtility->Strlcpy(buffer, dt_str+5, 3); // "strange" mode to copy only the MONTH
  17. IDOS->StrToLong(buffer, &M_today);
  18.  
  19. IUtility->Strlcpy(buffer, dt_str+8, 3); // "strange" mode to copy only the DAY
  20. IDOS->StrToLong(buffer, &D_today);

Is there a better/proper way to do what I'm trying to achieve (get Date and split YYYY MM DD into integer variables)?

TIA

AOS4.1/SAM460ex/PPC460EX-1155MHZ/2048MB/RadeonHD6570/SSD120GB/DVDRW :-P

gazelle
gazelle's picture
Offline
Last seen: 1 year 1 month ago
Joined: 2011-04-13 12:52
uint32 seconds;struct
  1. uint32 seconds;
  2. struct DateStamp ds;
  3. struct ClockData cd;
  4.  
  5. IDOS->DateStamp(&ds);
  6. seconds = IDOS->DateStampToSeconds(&ds);
  7. IUtility->Amiga2Date(seconds, &cd);

From utility/date.h:

  1. struct ClockData
  2. {
  3. UWORD sec; /* 0..59 */
  4. UWORD min; /* 0..59 */
  5. UWORD hour; /* 0..23 */
  6. UWORD mday; /* 1..31 */
  7. UWORD month; /* 1..12 */
  8. UWORD year; /* 1978.. */
  9. UWORD wday; /* 0..6; 0 == Sunday */
  10. };
thomas
thomas's picture
Offline
Last seen: 10 hours 29 min ago
Joined: 2011-05-16 14:23
How do I "poke the new buffer

How do I "poke the new buffer pointer into hook->h_Data"?

hook->h_Data = (APTR)buffer;

jabirulo
jabirulo's picture
Offline
Last seen: 5 hours 10 min ago
Joined: 2013-05-30 00:53
@gazelle & @thomas THX both

@gazelle & @thomas THX both modes working fine.

AOS4.1/SAM460ex/PPC460EX-1155MHZ/2048MB/RadeonHD6570/SSD120GB/DVDRW :-P

Log in or register to post comments