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

📄 npp_ncl.c

📁 Nucleus Common Library for Integrator using ARM Developer Suite
💻 C
字号:
/*************************************************************************
*
*               Copyright Mentor Graphics Corporation 2002              
*                         All Rights Reserved.                          
*                                                                       
* THIS WORK CONTAINS TRADE SECRET AND PROPRIETARY INFORMATION WHICH IS  
* THE PROPERTY OF MENTOR GRAPHICS CORPORATION OR ITS LICENSORS AND IS   
* SUBJECT TO LICENSE TERMS.                                             
*
*************************************************************************/

/*************************************************************************
* FILE NAME                                     VERSION
*
*       npp_ncl.c                               Nucleus Common Library 1.1
*
* DESCRIPTION
*
*       This file contains the implementations of NCL_ultoa, NCL_ltoa and
*       NCL_stricmp.
*
* DATA STRUCTURES
*
*       None.
*
* FUNCTIONS
*
*       NCL_ultoa
*       NCL_ltoa
*       NCL_stricmp
*       numbertoaWorker
*
* DEPENDENCIES
*
*       ncl.h
*       stdlib.h
*       ctype.h
*       convert.h
*       nucleus.h
*
************************************************************************/

#define NU_NCL_SOURCE_FILE

#include "ncl\inc\ncl.h"
#include "ncl\inc\stdlib.h"
#include "ncl\inc\ctype.h"
#include "ncl\inc\convert.h"
#include "plus\nucleus.h"                                /* MMU Support */

void numbertoaWorker(unsigned long value, char *destination, int radix, 
				     BOOLEAN isNegative)
{
    char* firstDigitLocation = destination;

    /* Is the value negative? */
    if(isNegative)
    {
        /* ... yes. Output the minus sign. */
        *destination++ = '-';

        /* Take the absolute value of the value for the rest of the algorithm. */
        value = ((unsigned long) ((long)(((long)0) - ((long)value))));
    }
    /* We will build the number in the reverse order, then swap at end. */
    /* So, we need to save a pointer to the first digit's location. */

    do
    {
        /* Get the remainder after dividing by the radix. */

        unsigned remainder = (unsigned)(value % radix);

        /* Get 'rid' of the remainder, getting the next digit. */

        value /= radix;

        /* Convert the remainder to ascii and store it in the destination. */
        /* Is the remainder a 'digit' (non decimal)? */

        if( remainder > 9 )
        {
            /* ... yes. Store it as a letter. */
            *destination++ = (char)(remainder - 10 + 'a');
        }
        else
        {
            /* ... no. Store it as a number. */
            *destination++ = (char)(remainder + '0');
        }

        /* Continue to do the above algorithm while the value is positive. */

    } while(value > 0);

    /* We now have the number in the reverse direction, swap it around. */
    /* While we are at the end of the string, terminate it and back up. */
    *destination-- = '\0';

    do
    {
        /* Continue to swap until we are at the first digit location.  */
        /* While we swap, back up the destination pointer and advance  */
        /* the first digit location. We will essentially meet halfway. */

        char swap = *destination;

        *destination--          = *firstDigitLocation;
        *firstDigitLocation++   = swap;

    } while(firstDigitLocation < destination);
}

/*************************************************************************
*
*   FUNCTION
*
*       NCL_ultoa
*
*   DESCRIPTION
*
*       Return the ascii representation of an unsigned long number.
*
*   INPUTS
*
*       value               Value to convert.
*       destination         Character in question.
*       radix               Radix to convert to.
*
*   OUTPUTS
*
*       char*               returns pointer to string representing value
*
*************************************************************************/

char* NCL_ultoa(unsigned long value, char *destination, int radix)
{
    /* We no the value is not negative. */
    numbertoaWorker(value, destination, radix, NU_FALSE);

    /* Return pointer to beginning of the stuffed string. */
    return( destination );
}

/*************************************************************************
*
*   FUNCTION
*
*       NCL_ltoa
*
*   DESCRIPTION
*
*       Return the ascii representation of a long number.
*
*   INPUTS
*
*       value               Value to convert.
*       destination         Character in question.
*       radix               Radix to convert to.
*
*   OUTPUTS
*
*       char*               returns pointer to string representing value
*
*************************************************************************/

char* NCL_ltoa(long value, char* destination, int radix)
{
    /* Do we have a negative number? */

    if ((radix == 10) && (value < 0))
    {
        /* ... yes.  */
        numbertoaWorker((unsigned long)value, destination, radix, NU_TRUE);
    }
    else
    {
        /* ...no. */
        numbertoaWorker((unsigned long)value, destination, radix, NU_FALSE);
    }

    /* Return pointer to beginning of the stuffed string. */
    return(destination);
}

/*************************************************************************
*
*   FUNCTION
*
*       NCL_ltoa
*
*   DESCRIPTION
*
*       Compares strings until chars are not equal or end of string.
*       Converts strings to uppercase as it goes.
*
*   INPUTS
*
*       value               Value to convert.
*       destination         Character in question.
*       radix               Radix to convert to.
*
*   OUTPUTS
*
*       string1             Converted to uppercase
*                           (passed by reference)
*       string2             Converted to uppercase
*                           (passed by reference)
*       int                 Returns pointer to string representing value
*
*************************************************************************/

int NCL_stricmp(const char* string1, const char* string2)
{
    /* Keep comparing until the characters are not equal or we are at the */
    /* end of one of the strings, converting to upper case along the way. */

    while((NCL_toupper(*string1) == NCL_toupper(*string2)) && (*string1))
    {
        /* Advance the pointers. */

        string1++;
        string2++;
    }

    /* We are different or at the end. Return the difference of the */
    /* contents of the string (cast to int for signed return). */

    return((int)((unsigned char)*string1) - (int)((unsigned char)*string2));
}

⌨️ 快捷键说明

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