#include "EMatoi.h" static char *str_swap(char *str, int size); static void sanitize(char *buff,const char *str, int size); /** \brief * * \param * \param * \return * */ int EMatoi(const char *str) { short unsigned int size = sizeof(int) == 4 ? 11 /*max : 2 147 483 647 (10 characters + 1 for \0)*/: 6/*max : 32 768 (5 characters + 1 for \0)*/; char buff[size]; int val = 0, index = 0; unsigned int power_of_ten = 1; char done = 0; sanitize(buff, str, size); str_swap(buff,strlen(buff)-1); for(;*(buff+index) != '\0' && !done;index++) { switch(*(buff+index)) { case '+': done = 1; break; case '-': val = -val; done = 1; break; default: if(*(buff+index) >= '0' && *(buff+index) <= '9' ) { val += (*(buff+index) - '1'+1) * power_of_ten; power_of_ten *= 10; } else done = 1; break; } } return val; } static char *str_swap(char *str, int size) { int left_pos = 0, right_pos = 0; char c_temp, *p_start = str, *p_end = str + size; for(;left_pos + right_pos < size;left_pos++,right_pos++) { c_temp = *(p_start+left_pos); *(p_start+left_pos) = *(p_end-right_pos); *(p_end-right_pos) = c_temp; } return str; } /** \brief Remove blanks at the beginning of the string and replace the first non numeric character with '\0' except + or - * * \param char *str -> string to be sanitized * \return char *p -> pointing to the string * */ static void sanitize(char *buff,const char *str, int size) { unsigned char number_of_white_space = 0, index = 0; for(;*(str+index) == ' ';index++)number_of_white_space++; EMstrncpy(buff, str+number_of_white_space,size-1); if(*buff == '-' || *buff == '+' || (*(buff) >= '0' && *(buff) <= '9')) { for(index = 1;(*(buff+index) >= '0' && *(buff+index) <= '9');index++); buff[index] = '\0'; } else *buff = '\0'; }