陋居

淡薄名利,修身养性

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  31 Posts :: 1 Stories :: 4 Comments :: 0 Trackbacks

常用链接

留言簿

我参与的团队

搜索

  •  

最新评论

阅读排行榜

评论排行榜

;***************************************************************************************************************
;       strlen returns the length of a null-terminated string in bytes, not including the null byte itself.
;       Algorithm:
;       int strlen (const char * str)
;       {
;           int length = 0;
;
;           while( *str++ )
;                   ++length;
;
;           return( length );
;       }

;***************************************************************************************************************
;       memcpy() copies a source memory buffer to a destination buffer.
;       Overlapping buffers are not treated specially, so propogation may occur.
;       Algorithm:
;       void * memcpy(void * dst, void * src, size_t count)
;       {
;               void * ret = dst;
;               /*
;                * copy from lower addresses to higher addresses
;                */
;               while (count--)
;                       *dst++ = *src++;
;
;               return(ret);
;       }

;***************************************************************************************************************
;       memmove() copies a source memory buffer to a destination memory buffer.
;       This routine recognize overlapping buffers to avoid propogation.
;       For cases where propogation is not a problem, memcpy() can be used.
;       Algorithm:
;       void * memmove(void * dst, void * src, size_t count)
;       {
;               void * ret = dst;
;               if (dst <= src || dst >= (src + count)) {
;                       /*
;                        * Non-Overlapping Buffers
;                        * copy from lower addresses to higher addresses
;                        */
;                       while (count--)
;                               *dst++ = *src++;
;                       }
;               else {
;                       /*
;                        * Overlapping Buffers
;                        * copy from higher addresses to lower addresses
;                        */
;                       dst += count - 1;
;                       src += count - 1;
;
;                       while (count--)
;                               *dst-- = *src--;
;                       }
;
;               return(ret);
;       }

;***************************************************************************************************************
int strcmp(const char *str1,const char *str2)
{
while((*str1==*str2)&&(*str1))
{
str1++;
str2++;
}
if((*str1==*str2)&&(!*str1)) //Same strings
return 0;
else if((*str1)&&(!*str2))  //Same but str1 longer
return -1;
else if((*str2)&&(!*str1)) //Same but str2 longer
return 1;
else
return((*str1>*str2)?-1:1);
}

;***************************************************************************************************************
char *strstr1(const char *str1, const char *str2)
{
     char *cp = (char *)str1;  //type transfer
     char *s1, *s2;
     if(!str2) 
      return (char *)str1;
     while (cp)
     {
      s1 = cp;
      s2 = (char *)str2;
      while( !s1 && !s2 && !(*s1-*s2))
      {
       s1++, s2++;
      }
      if(!s2) 
       return cp;
      else
       cp++;
     }
     return NULL;
}

;***************************************************************************************************************
*char *_itoa, *_ltoa, *_ultoa(val, buf, radix) - convert binary int to ASCII string
static void __cdecl xtoa (unsigned long val, char *buf, unsigned radix, int is_neg )
{
        char *p;                /* pointer to traverse string */
        char *firstdig;         /* pointer to first digit */
        char temp;              /* temp char */
        unsigned digval;        /* value of digit */

        p = buf;

        if (is_neg) {
            /* negative, so output '-' and negate */
            *p++ = '-';
            val = (unsigned long)(-(long)val);
        }

        firstdig = p;           /* save pointer to first digit */

        do {
            digval = (unsigned) (val % radix);
            val /= radix;       /* get next digit */

            /* convert to ascii and store */
            if (digval > 9)
                *p++ = (char) (digval - 10 + 'a');  /* a letter */
            else
                *p++ = (char) (digval + '0');       /* a digit */
        } while (val > 0);

        /* We now have the digit of the number in the buffer, but in reverse
           order.  Thus we reverse them now. */

        *p-- = '\0';            /* terminate string; p points to last digit */

        do {
            temp = *p;
            *p = *firstdig;
            *firstdig = temp;   /* swap *p and *firstdig */
            --p;
            ++firstdig;         /* advance to next two digits */
        } while (firstdig < p); /* repeat until halfway */
}
char * __cdecl _itoa ( int val, char *buf, int radix )
{
        if (radix == 10 && val < 0)
            xtoa((unsigned long)val, buf, radix, 1);
        else
            xtoa((unsigned long)(unsigned int)val, buf, radix, 0);
        return buf;
}
char * __cdecl _ltoa ( long val, char *buf, int radix )
{
        xtoa((unsigned long)val, buf, radix, (radix == 10 && val < 0));
        return buf;
}

;***************************************************************************************************************
*long atol(char *nptr) - Convert string to long. Overflow is not detected.
long __cdecl _tstol(const _TCHAR *nptr )
{
        int c;              /* current char */
        long total;         /* current total */
        int sign;           /* if '-', then negative, otherwise positive */
        while ( _istspace((int)(_TUCHAR)*nptr) )
                 ++nptr;   /* skip whitespace */

        c = (int)(_TUCHAR)*nptr++;
        sign = c;           /* save sign indication */
        if (c == _T('-') || c == _T('+'))
            c = (int)(_TUCHAR)*nptr++;    /* skip sign */

        total = 0;

        while ( (c = _tchartodigit(c)) != -1 ) {
            total = 10 * total + c;     /* accumulate digit */
            c = (_TUCHAR)*nptr++;    /* get next char */
        }

        if (sign == '-')
            return -total;
        else
            return total;   /* return result, negated if necessary */
}
int __cdecl _tstoi( const _TCHAR *nptr )
{
        return (int)_tstol(nptr);
}

;***************************************************************************************************************
;performs a byteswap on an unsigned integer.
unsigned short __cdecl _byteswap_ushort(unsigned short i)
{
    unsigned short j;
    j =  (i << 8) ;
    j += (i >> 8) ;
    return j;
}
unsigned long __cdecl _byteswap_ulong(unsigned long i)
{
    unsigned int j;
    j =  (i << 24);
    j += (i <<  8) & 0x00FF0000;
    j += (i >>  8) & 0x0000FF00;
    j += (i >> 24);
    return j;
}

;***************************************************************************************************************
*char *bsearch() - do a binary search on an array
*Entry:
*       const char *key    - key to search for const char *base   - base of sorted array to search unsigned int num   - number of *elements in array unsigned int width - number of bytes per element int (*compare)()   - pointer to function that compares two
*array elements, returning neg when #1 < #2, pos when #1 > #2, and 0 when they are equal. Function is passed pointers to two
*array elements.
*Exit:
*       if key is found: returns pointer to occurrence of key in array
*       if key is not found:returns NULL

void * __cdecl bsearch ( REG4 const void *key, const void *base, size_t num, size_t width, int (__cdecl *compare)(const void *, const void *)   )
{
        REG1 char *lo = (char *)base;
        REG2 char *hi = (char *)base + (num - 1) * width;
        REG3 char *mid;
        size_t half;
        int result;

        while (lo <= hi)
                if (half = num / 2)
                {
                        mid = lo + (num & 1 ? half : (half - 1)) * width;
                        if (!(result = (*compare)(key,mid)))
                                return(mid);
                        else if (result < 0)
                        {
                                hi = mid - width;
                                num = num & 1 ? half : half-1;
                        }
                        else    {
                                lo = mid + width;
                                num = half;
                        }
                }
                else if (num)
                        return((*compare)(key,lo) ? NULL : lo);
                else
                        break;

        return(NULL);
}

;***************************************************************************************************************
void __cdecl _tmakepath (register _TSCHAR *path, const _TSCHAR *drive, const _TSCHAR *dir,
const _TSCHAR *fname, const _TSCHAR *ext  )
{
     register const _TSCHAR *p;
     /* copy drive */
     if (drive && *drive) {
                *path++ = *drive;
                *path++ = _T(':');
        }

        /* copy dir */
        if ((p = dir) && *p) {
                do {
                        *path++ = *p++;
                }while (*p);
                if (*(p-1) != _T('/') && *(p-1) != _T('\\')) {
                        *path++ = _T('\\');
                }
        }

        /* copy fname */
        if (p = fname) {
                while (*p) {
                        *path++ = *p++;
                }
        }

        /* copy ext, including 0-terminator - check to see if a '.' needs to be inserted. */
        if (p = ext) {
                if (*p && *p != _T('.')) {
                        *path++ = _T('.');
                }
                while (*path++ = *p++)
                        ;
        }
        else {
                /* better add the 0-terminator */
                *path = _T('\0');
        }
}


posted on 2010-08-23 14:42 eircQ 阅读(388) 评论(0)  编辑 收藏 引用 所属分类: Arithmetic

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理