On Wed, Jul 19, 2000 at 10:58:46AM -0700, John Marshall wrote:
> Adam Wozniak <[EMAIL PROTECTED]> wrote:
> >> MemSet() works exactly as documented.
> > 
> > IMHO this is one of the most broken parts of the API.
> 
> Agreed.
> 
> > I'd really like to see this function renamed in some future release of
> > PalmOS.  I think renaming it would prevent some confusion.  MemFlood() ?
> 
> I trust you don't really mean renaming the existing function.  That would
> lead to silent (i.e., impossible to warn about at compile time) changes
> in people's programs.  The only way to really rename it would be if
> you were willing to activate the new MemSet() behaviour by invoking the
> compiler with something like
> 
>       -DFIXED_UP_MEMSET_PLEASE_AND_I_PROMISE_NOT_TO_BLAME_PALM_IF_\
>       THERE_ARE_ANY_OLD_STYLE_REVERSED_MEMSETS_LURKING_ANYWHERE_IN_\
>       MY_CODE_THAT_JUST_SILENTLY_CHANGED_THEIR_MEANINGS
> 
> The only sane thing to do is to add a new function with the standard
> memset() signature.  You can do this yourself today:

Or if you use -O1 or greater with gcc and don't mind the debugger not
actually doing a call and return you can just inline everything.

(not everything has been tested...)

----------------------------------------------------------------
/* linux (posix?) says val should be an int */
//#include <string.h>
#define VALT int
#define SIZET size_t

extern inline void *memset(void *dst, VALT val, SIZET num)
{
  register void *ds = dst;
  while (num--)
    *((char *) dst)++ = val;
  return ds;
}

extern inline void *memcpy(void *dst, const void *src, SIZET num)
{
  register void *ds = dst;
  while (num--)
    *((char *) dst)++ = *((char *) src)++;
  return ds;
}

extern inline int memcmp(void *dst, const void *src, SIZET num)
{

  while (num-- && (*((char *) dst)++ == *((char *) src)++));
  if (num >= 0)
    return *--((char *) dst) - *--((char *) src);
  else
    return 0;
}

extern inline void *memccpy(void *dst, const void *src, int c, SIZET num)
{
  register void *ds = dst;
  while (num-- && *((char *) src) != c)
    *((char *) dst)++ = *((char *) src)++;
  if (num >= 0)
    *((char *) dst)++ = *((char *) src)++;
  return ds;
}

extern inline void *memmove(void *dst, const void *src, SIZET num)
{
  register void *ds = dst;
  if (dst < src || dst >= src + num)
    while (num--)
      *((char *) dst)++ = *((char *) src)++;
  else {
    dst += num;
    src += num;
    while (num--)
      *--((char *) dst) = *--((char *) src);
  }
  return ds;
}

extern inline SIZET strlen(char *dst)
{
  register SIZET num = 0;
  while ((*dst++))
    num++;
  return num;
}

extern inline char *strcat(char *dst, const char *src)
{
  register char *ds = dst;
  while (*dst)
    dst++;
  while ((*dst++ = *src++));
  return ds;
}

extern inline char *strncat(char *dst, const char *src, SIZET num)
{
  register char *ds = dst;
  while (*dst)
    dst++;
  while (num-- && (*dst++ = *src++));
  return ds;
}

extern inline char *strcpy(char *dst, const char *src)
{
  register char *ds = dst;
  while ((*dst++ = *src++));
  return ds;
}

extern inline int strcmp(const char *dst, const char *src)
{
  while (*dst && *src && (*dst++ == *src++));
  if (*dst && *src)
    return *--src - *--dst;
  else
    return *src - *dst;
}

extern inline int strncmp(const char *dst, const char *src, SIZET num)
{
  while (num && *dst && *src && (*dst++ == *src++))
    num--;
  if( !num )
    return 0;
  if (*dst && *src)
    return *--src - *--dst;
  else
    return *src - *dst;
}

extern inline char *strncpy(char *dst, const char *src, SIZET num)
{
  register char *ds = dst;
  while (num-- && (*dst++ = *src++));
  if (num >= 0)
    while (num--)
      *dst++ = 0;
  return ds;
}

extern inline void *memchr(const void *dst, VALT val, SIZET num)
{
  char *dstl = (void *)dst;
  while (num-- && *dstl++ != val);
  if (num >= 0)
    return (void *) --dstl;
  else
    return NULL;
}

extern inline char *strchr(const char *dst, int val)
{
  char *dstl = (char *)dst;
  while (*dstl && *dstl != val)
    dstl++;
  if (*dstl != val)
    return NULL;
  else
    return dstl;
}

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/tech/support/forums/

Reply via email to