gpib_parser_strutils.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #ifndef GPIB_PARSER_STRUTILS_H
  2. #define GPIB_PARSER_STRUTILS_H
  3. #include <string.h>
  4. #include <stddef.h> // size_t
  5. #include <stdint.h> // int32_t
  6. #include "gpib_parser_casts.h"
  7. // STRCACHE_BUFFER_SIZE:
  8. // The size of statical buffer for GPIB_StrCopyToCache()
  9. #define STRCACHE_BUFFER_SIZE RAM_SCPIBUFFER_SIZE
  10. BOOL GPIB_StrChkFormat( const char * str, int length, const eStringCheckMode_t mode ); // "gpib_parser_strutils.c"
  11. /* #define strstri(STR1, STR2) strstr((STR1),(STR2)) */
  12. /* #define strcmpi(STR1, STR2) strcmp((STR1),(STR2)) */
  13. // strwhite:
  14. // Looks for a "white" character in the null-terminated string
  15. // and returns the pointer to it.
  16. //
  17. // Note: the function uses "rw" pointers due to it is required by application
  18. static inline char * strwhite( char * string )
  19. {
  20. // Skip first "white" or end-of-line charater
  21. while( !IsWhiteOrEndChar( *string ) )
  22. {
  23. string ++;
  24. }
  25. return string;
  26. }
  27. // searchHeaderEnd:
  28. // Looks for the end of header end by checking the following rules:
  29. // - if the character is a white, it is considered as the end of header
  30. // - if the character is a command separator, it is considered as the end of header
  31. // Returns the pointer to the end.
  32. // Note: the function uses "rw" pointers due to it is required by application
  33. static inline char * searchHeaderEnd( char * string, const char * end )
  34. {
  35. // Skip first "white" or end-of-line charater
  36. while(
  37. !IsWhiteOrEndChar( *string )
  38. && (GPIB_CHAR_CMDSEPARATOR_SIGN != *string)
  39. && (end != string)
  40. )
  41. {
  42. string ++;
  43. }
  44. return string;
  45. }
  46. // strucase:
  47. // Converts @length characters in the string @str into the HIGH register
  48. static inline void strucase( char * str, size_t length )
  49. {
  50. if( NULL != str )
  51. {
  52. while( length > 0 )
  53. {
  54. *str = toupper( *str );
  55. length--;
  56. str++;
  57. }
  58. }
  59. }
  60. // strlcase:
  61. // Converts @length characters in the string @str into the LOW register
  62. static inline void strlcase( char * str, size_t length )
  63. {
  64. if( NULL != str )
  65. {
  66. while( length > 0 )
  67. {
  68. *str = tolower( *str );
  69. length--;
  70. str++;
  71. }
  72. }
  73. }
  74. // GPIB_StrCopyToCache:
  75. // Caches the given string @source into the internal buffer and returns the new pointer
  76. char * GPIB_StrCopyToCache( const char * source, size_t length );
  77. // GPIB_StrChr:
  78. // Searches a character @chr in the string beginning from @str and limited by @end.
  79. // If the function faces a null-character or end-of-line condition, it stops searching.
  80. // Function returns the pointer to the found character in @str, or NULL if not found.
  81. const char * GPIB_StrChr( const char * str, const char chr, const char * end );
  82. // GPIB_GetStringEnd:
  83. // Searches for the end of the string @gpibstr until the end of @gpibstr is reached
  84. // or @length characters is processed
  85. // Note: the string must be quotted in single or double quotes.
  86. const char * GPIB_GetStringEnd( const char * gpibstr, size_t length );
  87. // GPIB_GetParameterEnd:
  88. // Searches for the end signature of the parameter in @string until the
  89. // end of @string string is reached or @length characters is processed.
  90. // The first "white" character is treated as an end signature.
  91. // Does not support SCPI DataBlock format.
  92. const char * GPIB_GetParameterEnd( const char * string, size_t length );
  93. // GPIB_CheckHeader:
  94. // Check if the given @header is valid GPIB header.
  95. // Returns TRUE if it is, and FALSE otherwise.
  96. //BOOL GPIB_CheckHeader( const char * header );
  97. BOOL GPIB_CheckHeader( const char * header, const char * end );
  98. // GPIB_HeaderCompare:
  99. // Compares a couple of GPIB command headers.
  100. // Note: the comparation is case insensitive.
  101. // @headerTree - the original command header, including an optional command
  102. // signature (if required), e.g. optional "[SYSTem]" or mandatory "SYST"
  103. // @headerUser - actually entered command header
  104. // Note: for optional headers it is not required a paired ']' in the end of header.
  105. // Returns TRUE if headers are identical, and FALSE otherwise.
  106. BOOL GPIB_HeaderCompare( const char * headerTree, const char * headerUser );
  107. // GPIB_StrChr_rw:
  108. // Searches a character @chr in the string beginning from @str and limited by @end.
  109. // If the function faces a null-character or end-of-line condition, it stops searching.
  110. // Function returns the pointer to the found character in @str, or NULL if not found.
  111. // Note: the same as GPIB_StrChr() but with read-write pointers
  112. static inline char * GPIB_StrChr_rw( char * str, const char chr, char * end )
  113. {
  114. const char * found = GPIB_StrChr( str, chr, end );
  115. if( NULL != found )
  116. {
  117. return (str + SubPointers( found, str ));
  118. }
  119. return NULL;
  120. }
  121. // GPIB_GetStringEnd_rw:
  122. // Searches for the end of the string @gpibstr until the end of @gpibstr is reached
  123. // or @length characters is processed
  124. // Note: the string must be quotted in single or double quotes.
  125. // Note: the same as GPIB_GetStringEnd() but with read-write pointers
  126. static inline char * GPIB_GetStringEnd_rw( char * gpibstr, size_t length )
  127. {
  128. const char * found = GPIB_GetStringEnd( gpibstr, length );
  129. if( NULL != found )
  130. {
  131. return (gpibstr + SubPointers( found, gpibstr ));
  132. }
  133. return NULL;
  134. }
  135. typedef enum
  136. {
  137. err_IsDataBlock_Success = 0,
  138. err_IsDataBlock_NotFound = -1,
  139. err_IsDataBlock_Invalid = -2,
  140. err_IsDataBlock_Trimmed = -3,
  141. err_IsDataBlock_Huge = -4,
  142. }
  143. err_IsDataBlock_t;
  144. // IsDataBlock:
  145. // The data transfer block validator: Binary transfer format (IEEE488.2)
  146. // Format: the data block consist of the following elements: '#', N, Z, DATA
  147. // '#' - data block signature character;
  148. // N - a demical digit, amount of digits in Z;
  149. // Z - a demical number (multi-digit), the size of binary bytes in DATA.
  150. // DATA - binary data bytes.
  151. // e.g. "#213Hello, world!"
  152. // N=2 - amount of digits in Z
  153. // Z=13 - amount of binary bytes (strlen(DATA)=13)
  154. // DATA = "Hello, world!"
  155. //
  156. // @begin - the string to validate
  157. // @length - the length of string @begin
  158. // @p_nsize - optional pointer which will be filled up with the datablock size in bytes
  159. // @pError - optional pointer to the error variable to be filled with the error code:
  160. // (err_IsDataBlock_Success) - success;
  161. // (err_IsDataBlock_NotFound) - datablock not found;
  162. // (err_IsDataBlock_Invalid) - datablock found, but has invalid format
  163. // (err_IsDataBlock_Trimmed) - datablock found, but it is trimmed
  164. // (err_IsDataBlock_Huge) - datablock found, but it's too large
  165. //
  166. // Returns TRUE only if the datablock format is valid, all the parameter of datablock
  167. // are valid and do not violates any ranges.
  168. BOOL IsDataBlock( const char * begin, size_t length, size_t * p_nsize, int * pError );
  169. #endif