home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 28 / amigaformatcd28.iso / -seriously_amiga- / programming / other / wipeout / source / tools.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-27  |  5.3 KB  |  308 lines

  1. /*
  2.  * $Id: tools.c 1.8 1998/04/12 17:30:16 olsen Exp olsen $
  3.  *
  4.  * :ts=4
  5.  *
  6.  * Wipeout -- Traces and munges memory and detects memory trashing
  7.  *
  8.  * Written by Olaf `Olsen' Barthel <olsen@sourcery.han.de>
  9.  * Public Domain
  10.  */
  11.  
  12. #ifndef _GLOBAL_H
  13. #include "global.h"
  14. #endif    /* _GLOBAL_H */
  15.  
  16. /******************************************************************************/
  17.  
  18. VOID
  19. TimeValToDateStamp(
  20.     const struct timeval *    tv,
  21.     struct DateStamp *        ds)
  22. {
  23.     /* convert a timeval to a DateStamp */
  24.  
  25.     ds->ds_Days        = tv->tv_secs / (24 * 60 * 60);
  26.     ds->ds_Minute    = (tv->tv_secs % (24 * 60 * 60)) / 60;
  27.     ds->ds_Tick        = (tv->tv_secs % 60) * TICKS_PER_SECOND + (tv->tv_micro * TICKS_PER_SECOND) / 1000000;
  28. }
  29.  
  30. /******************************************************************************/
  31.  
  32. VOID
  33. StrcpyN(LONG MaxLen,STRPTR To,const STRPTR From)
  34. {
  35.     ASSERT(To != NULL && From != NULL);
  36.  
  37.     /* copy a string, but only up to MaxLen characters */
  38.  
  39.     if(MaxLen > 0)
  40.     {
  41.         LONG Len = strlen(From);
  42.  
  43.         if(Len >= MaxLen)
  44.             Len = MaxLen - 1;
  45.  
  46.         strncpy(To,From,Len);
  47.         To[Len] = '\0';
  48.     }
  49. }
  50.  
  51. /******************************************************************************/
  52.  
  53. struct FormatContext
  54. {
  55.     STRPTR    Index;
  56.     LONG    Size;
  57.     BOOL    Overflow;
  58. };
  59.  
  60. STATIC VOID ASM
  61. StuffChar(
  62.     REG(a3)    struct FormatContext *    Context,
  63.     REG(d0) UBYTE                    Char)
  64. {
  65.     /* Is there still room? */
  66.     if(Context->Size > 0)
  67.     {
  68.         (*Context->Index) = Char;
  69.  
  70.         Context->Index++;
  71.         Context->Size--;
  72.  
  73.         /* Is there only a single character left? */
  74.         if(Context->Size == 1)
  75.         {
  76.             /* Provide null-termination. */
  77.             (*Context->Index) = '\0';
  78.  
  79.             /* Don't store any further characters. */
  80.             Context->Size = 0;
  81.         }
  82.     }
  83.     else
  84.     {
  85.         Context->Overflow = TRUE;
  86.     }
  87. }
  88.  
  89. BOOL
  90. VSPrintfN(
  91.     LONG            MaxLen,
  92.     STRPTR            Buffer,
  93.     const STRPTR    FormatString,
  94.     const va_list    VarArgs)
  95. {
  96.     BOOL result = FAILURE;
  97.  
  98.     /* format a text, but place only up to MaxLen
  99.      * characters in the output buffer (including
  100.      * the terminating NUL)
  101.      */
  102.  
  103.     ASSERT(Buffer != NULL && FormatString != NULL);
  104.  
  105.     if(MaxLen > 1)
  106.     {
  107.         struct FormatContext Context;
  108.  
  109.         Context.Index        = Buffer;
  110.         Context.Size        = MaxLen;
  111.         Context.Overflow    = FALSE;
  112.  
  113.         RawDoFmt(FormatString,(APTR)VarArgs,(VOID (*)())StuffChar,(APTR)&Context);
  114.  
  115.         if(NO Context.Overflow)
  116.             result = SUCCESS;
  117.     }
  118.  
  119.     return(result);
  120. }
  121.  
  122. BOOL
  123. SPrintfN(
  124.     LONG            MaxLen,
  125.     STRPTR            Buffer,
  126.     const STRPTR    FormatString,
  127.                     ...)
  128. {
  129.     va_list VarArgs;
  130.     BOOL result;
  131.  
  132.     /* format a text, varargs version */
  133.  
  134.     ASSERT(Buffer != NULL && FormatString != NULL);
  135.  
  136.     va_start(VarArgs,FormatString);
  137.     result = VSPrintfN(MaxLen,Buffer,FormatString,VarArgs);
  138.     va_end(VarArgs);
  139.  
  140.     return(result);
  141. }
  142.  
  143. /******************************************************************************/
  144.  
  145. BOOL
  146. DecodeNumber(
  147.     const STRPTR    number,
  148.     LONG *            valuePtr)
  149. {
  150.     BOOL decoded = FALSE;
  151.     LONG value = 0;
  152.  
  153.     /* is this a hexadecimal number? */
  154.     if(Strnicmp(number,"0x",2) == SAME || number[0] == '$')
  155.     {
  156.         STRPTR string;
  157.         UBYTE c;
  158.  
  159.         /* skip the format identifier */
  160.         if(number[0] == '$')
  161.             string = (STRPTR)number + 1;
  162.         else
  163.             string = (STRPTR)number + 2;
  164.  
  165.         decoded = TRUE;
  166.  
  167.         /* decode the number character by character */
  168.         while((c = ToLower(*string++)) != '\0')
  169.         {
  170.             if('0' <= c && c <= '9')
  171.             {
  172.                 value = (value * 16) + (c - '0');
  173.             }
  174.             else if ('a' <= c && c <= 'f')
  175.             {
  176.                 value = (value * 16) + 10 + (c - 'a');
  177.             }
  178.             else
  179.             {
  180.                 /* not in the range 0..9/a..f */
  181.                 decoded = FALSE;
  182.                 break;
  183.             }
  184.         }
  185.     }
  186.     else
  187.     {
  188.         /* decode the decimal number */
  189.         if(StrToLong((STRPTR)number,&value))
  190.         {
  191.             decoded = TRUE;
  192.         }
  193.     }
  194.  
  195.     if(decoded)
  196.     {
  197.         (*valuePtr) = value;
  198.     }
  199.  
  200.     return(decoded);
  201. }
  202.  
  203. /******************************************************************************/
  204.  
  205. VOID
  206. ConvertTimeAndDate(
  207.     const struct timeval *        tv,
  208.     STRPTR                        date,
  209.     STRPTR                        time)
  210. {
  211.     struct DateTime dat;
  212.  
  213.     /* convert a timeval into a human-readable date and time text */
  214.  
  215.     ASSERT(tv != NULL);
  216.  
  217.     memset(&dat,0,sizeof(dat));
  218.  
  219.     TimeValToDateStamp(tv,&dat.dat_Stamp);
  220.  
  221.     dat.dat_Format    = FORMAT_DOS;
  222.     dat.dat_StrDate    = date;
  223.     dat.dat_StrTime    = time;
  224.  
  225.     DateToStr(&dat);
  226. }
  227.  
  228. /******************************************************************************/
  229.  
  230. struct Node *
  231. FindIName(const struct List * list,const STRPTR name)
  232. {
  233.     struct Node * result = NULL;
  234.     struct Node * node;
  235.  
  236.     /* find a name in a list, ignoring case */
  237.  
  238.     for(node = (struct Node *)list->lh_Head ;
  239.         node->ln_Succ != NULL ;
  240.         node = node->ln_Succ)
  241.     {
  242.         if(Stricmp(node->ln_Name,name) == SAME)
  243.         {
  244.             result = node;
  245.             break;
  246.         }
  247.     }
  248.  
  249.     return(result);
  250. }
  251.  
  252. /******************************************************************************/
  253.  
  254. BOOL
  255. IsTaskStillAround(const struct Task * whichTask)
  256. {
  257.     struct Node * node;
  258.     BOOL found;
  259.  
  260.     /* check whether the given task is still active and
  261.      * has not yet exited
  262.      */
  263.  
  264.     Forbid();
  265.  
  266.     found = FALSE;
  267.  
  268.     /* Looking for myself? */
  269.     if(whichTask == FindTask(NULL))
  270.     {
  271.         found = TRUE;
  272.     }
  273.  
  274.     /* Check the list of running tasks. */
  275.     if(NOT found)
  276.     {
  277.         for(node = SysBase->TaskReady.lh_Head ;
  278.             node->ln_Succ != NULL ;
  279.             node = node->ln_Succ)
  280.         {
  281.             if(node == (struct Node *)whichTask)
  282.             {
  283.                 found = TRUE;
  284.                 break;
  285.             }
  286.         }
  287.     }
  288.  
  289.     /* Check the list of waiting tasks. */
  290.     if(NOT found)
  291.     {
  292.         for(node = SysBase->TaskWait.lh_Head ;
  293.             node->ln_Succ != NULL ;
  294.             node = node->ln_Succ)
  295.         {
  296.             if(node == (struct Node *)whichTask)
  297.             {
  298.                 found = TRUE;
  299.                 break;
  300.             }
  301.         }
  302.     }
  303.  
  304.     Permit();
  305.  
  306.     return(found);
  307. }
  308.