/***********************************************************************************************
* Company: Microsemi Corporation
*
* File: char_to_int.c
* File history:
*      Revision: 1.0 Date: July 29, 2011
*
* Description: This code converts integer to character
*
* Author: Pavan Marisetti
*         pavan.marisetti@microsemi.com
*         Corporate Applications Engineering
*
************************************************************************************************/

#include "../CMSIS/a2fxxxm3.h"

int xatoi (                                             /* 0:Failed, 1:Successful */
		       uint8_t **str,                          /* Pointer to pointer to the string */
		       uint32_t *res                             /* Pointer to the valiable to store the value */
)
{
                unsigned long val;
                unsigned char c, r, s = 0;


                *res = 0;

                while ((c = **str) == ' ') (*str)++;               /* Skip leading spaces */

                if (c == '-') {                         /* negative? */
                                s = 1;
                                c = *(++(*str));
                }

                if (c == '0') {
                                c = *(++(*str));
                                switch (c) {
                                case 'x':                                /* hexdecimal */
                                                r = 16; c = *(++(*str));
                                                break;
                                case 'b':                                /* binary */
                                                r = 2; c = *(++(*str));
                                                break;
                                default:
                                                if (c <= ' ') return 1;          /* single zero */
                                                if (c < '0' || c > '9') return 0;          /* invalid char */
                                                r = 8;                      /* octal */
                                }
                } else {
                                if (c < '0' || c > '9') return 0;          /* EOL or invalid char */
                                r = 10;                                    /* decimal */
                }

                val = 0;
                while (c > ' ') {
                                if (c >= 'a') c -= 0x20;
                                c -= '0';
                                if (c >= 17) {
                                                c -= 7;
                                                if (c <= 9) return 0;           /* invalid char */
                                }
                                if (c >= r) return 0;                           /* invalid char for current radix */
                                val = val * r + c;
                                c = *(++(*str));
                }
                if (s) val = 0 - val;                                               /* apply sign if needed */

                *res = val;
                return 1;
}
