Changeset 426 for trunk/libs/stdlib.c


Ignore:
Timestamp:
Jan 29, 2018, 6:00:54 PM (6 years ago)
Author:
alain
Message:

The "nostdio" library has been integrated in the stdio library.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libs/stdlib.c

    r416 r426  
    2222 */
    2323
    24 #include <nostdio.h>
    2524#include <stdio.h>
    2625#include <stdarg.h>
     
    4039}
    4140
    42 /////////////////////////
    43 int atoi(const char *str)
    44 {
    45     int res  = 0; // Initialize result
    46     int sign = 1; // Initialize sign as positive
    47     int i    = 0; // Initialize index of first digit
    48 
    49     if (str[0] == '-') //If number is negative, then update sign
    50     {
    51         sign = -1; 
    52         i++;           // Also update index of first digit
    53     }
    54 
    55     for (; str[i] != '\0'; ++i) // Iterate through all digits and update the result
    56     {
    57         res = res*10 + str[i] - '0';
     41//////////////////////////
     42int atoi(const char * str)
     43{
     44    int res  = 0;      // Initialize result
     45    int sign = 1;      // Initialize sign as positive
     46    int i    = 0;      // Initialize index of first digit
     47
     48    if( (str[0] == '0') && ((str[1] == 'x') || (str[1] == 'X')) )   // hexa
     49    {
     50        i = 2;
     51
     52        while( str[i] != 0 )
     53        {
     54            if     ( (str[i] >= '0') && (str[i] <= '9') ) res = (res<<4) + (str[i] - '0');
     55            else if( (str[i] >= 'A') && (str[i] <= 'F') ) res = (res<<4) + (str[i] - 'A');
     56            else if( (str[i] >= 'a') && (str[i] <= 'f') ) res = (res<<4) + (str[i] - 'a');
     57            else return 0;
     58            i++;
     59        }
     60    }
     61    else                                                            // decimal
     62    {
     63        if (str[0] == '-')  //  number is negative, update sign
     64        {
     65            sign = -1; 
     66            i++;           // Also update index of first digit
     67        }
     68
     69        while( str[i] != 0 )
     70        {
     71            if( (str[i] >= '0') && (str[i] <= '9') ) res = (res*10) + (str[i] - '0');
     72            else return 0;
     73            i++;
     74        }
    5875    }
    5976
     
    382399    if ( count == -1 )
    383400    {
    384         panic( "stdlib xprintf failure" );
     401        display_string( "stdlib : xprintf failure" );
    385402        return -1;
    386403    }
     
    495512}  // end getint()
    496513
    497 ///////////////////////////////////////////
    498 int snprintf( char       * string,
     514///////////////////////////////////////
     515int snprintf( char           * string,
    499516              unsigned int     length,
    500               const char * format, ... )
     517              const char     * format, ... )
    501518{
    502519    va_list   args;
Note: See TracChangeset for help on using the changeset viewer.