1 /* 2 * Copyright (c) 2006-2021, RT-Thread Development Team 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Change Logs: 7 * Date Author Notes 8 * 2022-05-25 WangQiang the first verion for msh parse 9 */ 10 11 #include <rtthread.h> 12 #include <ctype.h> 13 14 #define forstrloop(str) for (; '\0' != *(str); (str)++) 15 16 /** 17 * This function will check integer. 18 * 19 * @param strvalue string 20 * 21 * @return true or false 22 */ msh_isint(char * strvalue)23rt_bool_t msh_isint(char *strvalue) 24 { 25 if ((RT_NULL == strvalue) || ('\0' == strvalue[0])) 26 { 27 return RT_FALSE; 28 } 29 if (('+' == *strvalue) || ('-' == *strvalue)) 30 { 31 strvalue++; 32 } 33 forstrloop(strvalue) 34 { 35 if (!isdigit((int)(*strvalue))) 36 { 37 return RT_FALSE; 38 } 39 } 40 return RT_TRUE; 41 } 42 43 /** 44 * This function will check hex. 45 * 46 * @param strvalue string 47 * 48 * @return true or false 49 */ msh_ishex(char * strvalue)50rt_bool_t msh_ishex(char *strvalue) 51 { 52 int c; 53 if ((RT_NULL == strvalue) || ('\0' == strvalue[0])) 54 { 55 return RT_FALSE; 56 } 57 if ('0' != *(strvalue++)) 58 { 59 return RT_FALSE; 60 } 61 if ('x' != *(strvalue++)) 62 { 63 return RT_FALSE; 64 } 65 66 forstrloop(strvalue) 67 { 68 c = tolower(*strvalue); 69 if (!isxdigit(c)) 70 { 71 return RT_FALSE; 72 } 73 } 74 return RT_TRUE; 75 } 76 77 /** 78 * This function will transform for string to hex. 79 * 80 * @param strvalue string 81 * 82 * @return integer transformed from string 83 */ msh_strtohex(char * strvalue)84int msh_strtohex(char *strvalue) 85 { 86 char c = 0; 87 int value = 0; 88 strvalue += 2; 89 forstrloop(strvalue) 90 { 91 value *= 16; 92 c = tolower(*strvalue); 93 value += isdigit(c) ? c - '0' : c - 'a' + 10; 94 } 95 return value; 96 } 97