⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utility.c

📁 gif图像文件软件解码器的arm版本的源代码程序
💻 C
字号:
///////////////////////////////////////////////////////////////////////////////
//
//  utility.c
//
//  DESCRIPTION
//        Define utility functions.
//
//
///////////////////////////////////////////////////////////////////////////////


/*
  Include declarations.
*/

#include "gifcommon.h"

/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   L o c a l e N C o m p a r e                                               %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Method LocaleNCompare compares two strings byte-by-byte, according to
%  the ordering of the currnet locale encoding. LocaleNCompare returns an
%  integer greater than, equal to, or less than 0, if the string pointed
%  to by p is greater than, equal to, or less than the string pointed to
%  by q respectively.  The sign of a non-zero return value is determined
%  by the sign of the difference between the values of the first pair of
%  bytes that differ in the strings being compared.  The LocaleNCompare
%  method makes the same comparison as LocaleCompare but looks at a
%  maximum of n bytes.  Bytes following a null byte are not compared.
%
%  The format of the LocaleNCompare method is:
%
%      int LocaleNCompare(const char *p,const char *q,unsigned int n)
%
%  A description of each parameter follows:
%
%    o p: A pointer to a character string.
%
%    o q: A pointer to a character string to compare to p.
%
%    o n: The number of characters to compare in strings p & q.
%
%
*/
int LocaleNCompare
(
    const char      *p,
    const char      *q,
    unsigned int    n
)
{
    register int    i;
    register int    j;

    if (p == q)
    {
        return(0);
    }
    if (p == (char *) OP_NULL)
    {
        return(-1);
    }
    if (q == (char *) OP_NULL)
    {
        return(1);
    }
    while ((*p != '\0') && (*q != '\0'))
    {
        if ((*p == '\0') || (*q == '\0'))
        {
            break;
        }
        i = (*p);
        if (islower(i))
        {
            i = toupper(i);
        }
        j = (*q);
        if (islower(j))
        {
            j = toupper(j);
        }
        if (i != j)
        {
            break;
        }
        n--;
        if (n == 0)
        {
            break;
        }
        p++;
        q++;
    }
    return(toupper(*p)-toupper(*q));
}

/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   A l l o c a t e S t r i n g                                               %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Method AllocateString allocates memory for a string and copies the source
%  string to that memory location (and returns it).
%
%  The format of the AllocateString method is:
%
%      char *AllocateString(const char *source)
%
%  A description of each parameter follows:
%
%    o allocated_string:  Method AllocateString returns a copy of the source
%      string.
%
%    o source: A character string.
%
%
*/
char *AllocateString
(
    const char          *source, 
    AllocateMemory      getmemory
)
{
    char            *destination;
    unsigned int    length;

    length = MaxTextExtent;

    if (source != (char *) OP_NULL)
    {
        length += strlen(source);
    }

    destination = (char *) getmemory(length+MaxTextExtent);

    if (destination == (char *) OP_NULL)
    {
        return OP_NULL;
    }

    *destination = '\0';

    if (source != (char *) OP_NULL)
    {
        (void) strcpy(destination, source);
    }

    return(destination);
}

/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   C o n c a t e n a t e S t r i n g                                         %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Method ConcatenateString appends a copy of string source, including
%  the terminating null character, to the end of string destination.
%
%  The format of the ConcatenateString method is:
%
%      unsigned int ConcatenateString(char **destination,const char *source)
%
%  A description of each parameter follows:
%
%    o status:  Method ConcatenateString returns True is the string is cloned,
%      otherwise False is returned.
%
%    o destination:  A pointer to a character string.
%
%    o source: A character string.
%
%
*/
unsigned int ConcatenateString
(
    char                **destination,
    const char          *source, 
    AllocateMemory      getmemory, 
    LiberateMemory      freememory
)
{
    if (source == (const char *) OP_NULL)
    {
        return(OP_TRUE);
    }

    ReacquireMemory((void **) &(*destination),
            strlen(*destination)+strlen(source)+MaxTextExtent, getmemory, freememory);

    if (*destination == (char *) OP_NULL)
    {
        return(OP_FALSE);
    }

    (void) strcat(*destination, source);
    
    return(OP_TRUE);
}

/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   R e a c q u i r e M e m o r y                                             %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Regetmemory() changes the size of the memory and returns a
%  pointer to the (possibly moved) block.  The contents will be unchanged
%  up to the lesser of the new and old sizes.
%
%  The format of the Regetmemory method is:
%
%      void Regetmemory(void **memory,const unsigned int size)
%
%  A description of each parameter follows:
%
%    o memory: A pointer to a memory allocation.  On return the pointer
%      may change but the contents of the original allocation will not.
%
%    o size: The new size of the allocated memory.
%
%
*/
void ReacquireMemory
(
    void                    **memory,
    const unsigned int      size, 
    AllocateMemory          getmemory, 
    LiberateMemory          freememory
)
{
    void    *allocation;

    if (*memory == (void *) OP_NULL)
    {
        *memory = getmemory(size);
        return;
    }
    allocation = realloc(*memory,size);
    if (allocation == (void *) OP_NULL)
    {
        freememory((void *) *memory);
    }
    *memory = allocation;
}

/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   L o c a l e C o m p a r e                                                 %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Method LocaleCompare compares two strings byte-by-byte, according to
%  the ordering of the current locale encoding. LocaleCompare returns an
%  integer greater than, equal to, or less than 0, if the string pointed
%  to by p is greater than, equal to, or less than the string pointed to
%  by q respectively.  The sign of a non-zero return value is determined
%  by the sign of the difference between the values of the first pair of
%  bytes that differ in the strings being compared.
%
%  The format of the LocaleCompare method is:
%
%      int LocaleCompare(const char *p,const char *q)
%
%  A description of each parameter follows:
%
%    o p: A pointer to a character string.
%
%    o q: A pointer to a character string to compare to p.
%
%
*/
int LocaleCompare
(
    const char      *p,
    const char      *q
)
{
    register int    i;
    register int    j;

    if (p == q)
    {
        return(0);
    }
    if (p == (char *) OP_NULL)
    {
        return(-1);
    }
    if (q == (char *) OP_NULL)
    {
        return(1);
    }
    while ((*p != '\0') && (*q != '\0'))
    {
        i=(*p);
        
        if (islower(i))
        {
            i=toupper(i);
        }
        
        j=(*q);
        
        if (islower(j))
        {
            j=toupper(j);
        }
        
        if (i != j)
        {
            break;
        }
        
        p++;
        q++;
    }
    
    return(toupper(*p)-toupper(*q));
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -