gpib_parser_validators_numbers.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #ifndef GPIB_PARSER_VALIDATORS_NUMBERS_H
  2. #define GPIB_PARSER_VALIDATORS_NUMBERS_H
  3. #include <ctype.h> // isalpha, isdigit
  4. #include "gpib_parser_casts.h"
  5. #define IsDigit(ch) MAKE_BOOL(isdigit(ch))
  6. #define IsNUMBER(ch) IsDigit(ch)
  7. #define IsNumber(ch) IsDigit(ch)
  8. /* UNUSED
  9. #define IsSIGN(character) MAKE_BOOL((character)=='+' || (character)=='-')
  10. #define IsEXPCHAR(character) MAKE_BOOL(((character)&0xDF)=='E')
  11. #define IsHEXOnly(character) MAKE_BOOL(((character) & 0xDF)<='F' && ((character) & 0xDF)>='A')
  12. #define IsHEXorDEC(character) MAKE_BOOL(IsHEX(character) || IsDEC(character))
  13. */
  14. //------------------------------------------------------------------------------
  15. // IsExponentialChar:
  16. // Check whether the character @ch is a valid charater for a exponential number format
  17. static inline BOOL IsExponentialChar( const char ch )
  18. {
  19. return MAKE_BOOL(
  20. isdigit( ch ) // Allow a demical digit
  21. || 'E' == ch // Allow a 'E' character
  22. || 'e' == ch // Allow a 'e' character
  23. || '.' == ch // Allow a dot character
  24. || '+' == ch // Allow a '+' sign
  25. || '-' == ch // Allow a '-' sign
  26. );
  27. /* #define IsEXP(character) MAKE_BOOL(IsEXPCHAR(character) || IsFloatChar(character) || IsSIGN(character)) */
  28. }
  29. //------------------------------------------------------------------------------
  30. // IsFloatChar:
  31. // Check whether the character @ch is a valid charater for a float number
  32. static inline BOOL IsFloatChar( const char ch )
  33. {
  34. return MAKE_BOOL(
  35. isdigit( ch ) // Allow a demical digit
  36. || '.' == ch // Allow a dot character
  37. || '+' == ch // Allow a '+' sign
  38. || '-' == ch // Allow a '-' sign
  39. );
  40. /* #define IsPOINT(character) MAKE_BOOL((character)=='.') */
  41. /* #define IsFLOAT(character) MAKE_BOOL(IsDEC(character) || IsPOINT(character)) */
  42. }
  43. //------------------------------------------------------------------------------
  44. // IsBinaryChar:
  45. // Check whether the character @ch is a valid charater for a binary number format
  46. static inline BOOL IsBinaryChar( const char ch )
  47. {
  48. return MAKE_BOOL( '0' == ch // Allow '0' character
  49. || '1' == ch // Allow '1' character
  50. );
  51. /* #define IsBIN(character) MAKE_BOOL((character)<='1' && (character)>='0') */
  52. }
  53. //------------------------------------------------------------------------------
  54. // IsOCT_Int32Overflow:
  55. // This is a limited version of IsOctalChar() for the checking of overflowing the 32-bit interger
  56. // This designed to check the most significant character of octal number for 32-bit integer overflowing.
  57. // It is assumed to be applied to '37777777777' octal number.
  58. static inline BOOL IsOCT_Int32Overflow( const char ch )
  59. {
  60. return MAKE_BOOL( ( '0' <= ch ) && ( ch <= '3' ) ); // Allow characters '0'...'3'.
  61. /* #define IsOCTCorrect(character) MAKE_BOOL((character)<='3' && (character)>='0') */
  62. }
  63. //------------------------------------------------------------------------------
  64. // IsOctalChar:
  65. // Check whether the character @ch is a valid charater for a octal number format
  66. static inline BOOL IsOctalChar( char ch )
  67. {
  68. return MAKE_BOOL( ( '0' <= ch ) && ( ch <= '7' ) ); // Allow characters '0', '1', ..., '7'.
  69. /* #define IsOCT(character) MAKE_BOOL((character)<='7' && (character)>='0') */
  70. }
  71. //------------------------------------------------------------------------------
  72. // IsHexademicalChar:
  73. // Check whether the character @ch is a valid charater for a hexademical integer number
  74. static inline BOOL IsHexademicalChar( char ch )
  75. {
  76. ch = tolower( ch ); // Make the @ch to be a lower case
  77. // Note: tolower() for a demical characters ('0', '1', ... '9') is the same character
  78. return MAKE_BOOL(
  79. isdigit( ch ) // Allow a demical digit
  80. || ( ('a' <= ch) && (ch <= 'f') )
  81. );
  82. /* #define IsHEX(character) MAKE_BOOL(IsHEXOnly(character) || IsNUMBER(character)) */
  83. }
  84. //------------------------------------------------------------------------------
  85. // IsDemicalChar:
  86. // Check whether the character @ch is a valid charater for a demical integer number
  87. static inline BOOL IsDemicalChar( const char ch )
  88. {
  89. return MAKE_BOOL(
  90. isdigit( ch ) // Allow a demical digit
  91. || '+' == ch // Allow a '+' sign
  92. || '-' == ch // Allow a '-' sign
  93. );
  94. /* #define IsNUMBER(character) MAKE_BOOL((character)<='9' && (character)>='0') */
  95. /* #define IsDEC(character) MAKE_BOOL(IsNUMBER(character) || (character)=='+' || (character)=='-') */
  96. }
  97. #endif