binsearch.h

_ENSURE_LVALUE( x)

Produce a compiler error if x is not an lvalue.

1
((void)sizeof(&(x)))
_ARRAY_STRIDE( arr)

UNSAFE MACRO: Difference in bytes between the addresses of two consecutive array elements.

1
((size_t)((uint8_t *)((arr) + 1) - (uint8_t *)(arr)))
_ARRAY_MEMBER_OFFS( arr, member)

UNSAFE MACRO: Offset in bytes from the start of the array to member “member” of the first element.

1
((size_t)((uint8_t *)(&((arr)->member)) - (uint8_t *)(arr)))
BINSEARCH_STR( arr, nmemb, member, str, n)

Find the index of the array element that contains “str” in member “member”.

1
2
3
4
(_ENSURE_LVALUE(arr), \
     (binsearch_str((arr), _ARRAY_MEMBER_OFFS(arr, member), _ARRAY_STRIDE(arr), \
                    (nmemb), (str), (n))) \
    )

A compile-time error will be raised if arr is not an lvalue. This ensures the macro is safe.

Return values

  • Index of the array element containing the string.
  • (-ENOENT) if it is not found.
BINSEARCH_STR_P( arr, nmemb, member, str, n)

Find a pointer of the array element that contains “str” in member “member”.

1
2
3
4
(_ENSURE_LVALUE(arr), \
     (binsearch_str_p((arr), _ARRAY_MEMBER_OFFS(arr, member), _ARRAY_STRIDE(arr), \
                      (nmemb), (str), (n))) \
    )

Return values

  • Address of the element containing the string (as a void pointer).
  • Null if it is not found.
int binsearch_str(const void * start, msp430_types.h::size_t offset, msp430_types.h::size_t stride, msp430_types.h::size_t nmemb, const char * str, msp430_types.h::size_t n)

Search for an array element containing a string.

This does NOT check for NULL pointers, though start can be NULL of the size (nmemb) is zero.

Parameters

start:Pointer to start of array. The array must be ordered according to the search string.
offset:Offset of member containing string within structure. This can be determined using offsetof.
stride:Difference in bytes between the addresses of two consecutive array elements.
nmemb:Number of elements in the array.
str:String that will be compared against.
n:Compare up to n characters (see strncmp())

Return values

  • Index of the array element containing the string.
  • (-ENOENT) if it is not found.
const void * binsearch_str_p(const void * start, msp430_types.h::size_t offset, msp430_types.h::size_t stride, msp430_types.h::size_t nmemb, const char * str, msp430_types.h::size_t n)

Like binsearch_str but returns the pointer to the element.

Return values

  • Address of the element containing the string.
  • Null if it is not found.