/**
 * \file    ctype.h
 * \brief   ctype definition of system adaptation layer
 *
 * \par     Copyright (c) 2018 UWE Technologies Corp., Ltd. All Rights Reserved.
 * \author  James
 * \date    2018-03-05
 * \version 
 * 
 * \history 
 *  Date        Note
 *  2018-03-05  Original Creation
 * 
 */
 
#ifndef __CTYPE_H_
#define __CTYPE_H_

#pragma pack(1)

#define _U    0x01    ///< upper
#define _L    0x02    ///< lower
#define _D    0x04    ///< digit
#define _C    0x08    ///< cntrl
#define _P    0x10    ///< punct
#define _S    0x20    ///< white space (space/lf/tab)
#define _X    0x40    ///< hex digit
#define _SP    0x80    ///< hard space (0x20)

#pragma pack()

/** ctype table */
extern const unsigned char g_ctype_tb[];

/** check if x is mask in ctype table */
#define __ismask(x) (g_ctype_tb[(int)(unsigned char)(x)])

/** check c if it is an alphanumeric character ('A'-'Z', 'a'-'z', or '0'-'9') */
#define isalnum(c)    ((__ismask(c)&(_U|_L|_D)) != 0)

/** check c if it is an alphabetic character ('A'-'Z', 'a'-'z') */
#define isalpha(c)    ((__ismask(c)&(_U|_L)) != 0)

/** check c if it is a control character (0x00-0x1F or 0x7F) */
#define iscntrl(c)    ((__ismask(c)&(_C)) != 0)

/** check c if it is a printable character (0x21-0x7E) not including space (' ') */
#define isgraph(c)    ((__ismask(c)&(_P|_U|_L|_D)) != 0)

/** check c if it is an lowercase alphabetic character ('a'-'z') */
#define islower(c)    ((__ismask(c)&(_L)) != 0)

/** check c if it is a printable character (0x20-0x7E) */
#define isprint(c)    ((__ismask(c)&(_P|_U|_L|_D|_SP)) != 0)

/** check c if it is a punctuation character */
#define ispunct(c)    ((__ismask(c)&(_P)) != 0)

/** check c if it is a whitespace character (0x09-0x0D or 0x20) */
#define isspace(c)    ((__ismask(c)&(_S)) != 0)

/** check c if it is an uppercase alphabetic character ('A'-'Z') */
#define isupper(c)    ((__ismask(c)&(_U)) != 0)

/** check c if it is a digit character ('0'-'9') */
#define isdigit(c)    ((__ismask(c)&(_D)) != 0)

/** check c if it is a hexadecimal character ('A'-'F', 'a'-'f', or '0'-'9') */
#define isxdigit(c)    ((__ismask(c)&(_D|_X)) != 0)

/** check c if it is an ascii character (0~127) */
#define isascii(c)    (((unsigned char)(c))<=0x7f)

/** converts c to an ascii character */
#define toascii(c)    (((unsigned char)(c))&0x7f)

/** converts c to an uppercase character */
#define toupper(c)    ((('a' <= (c))&&((c) <= 'z')) ? ((c) - 'a' + 'A') : (c))

/** converts c to a lowercase character */
#define tolower(c)    ((('A' <= (c))&&((c) <= 'Z')) ? ((c) - 'A' + 'a') : (c))

//#define toxdigit(c)   (isxdigit(c) ? (isdigit(c) ? ((c)-'0') : ((('a' <= (c))&&((c) <= 'f')) ? ((c)-'a'+10) : ((c)-'A'+10))) : (0xFF))
//#define toxdigit(c)   (isdigit(c) ? ((c)-'0') : ((('a' <= (c))&&((c) <= 'f')) ? ((c)-'a'+10) : (((('A' <= (c))&&((c) <= 'F')) ? ((c)-'A'+10) : (0xFF)))))
unsigned char toxdigit(char c);


#endif /* #ifndef __CTYPE_H_ */

