Functions

AScan

Scan an array for a value or until a block returns true

Syntax

Signature
int AScan( const ITEM *array, const ITEM *search, int start, int count, AScanBlock block, void *cargo );

Arguments

array Array to scan.

search Value to find: a number, text, logical value, date, pointer or NIL. NULL looks for NIL. Not used when block is given.

start First position, counted from 1. NIL or a value below 1 starts at 1.

count Number of elements to scan. NIL scans through the end of the array.

block NULL, or a function called for each element: xBool block( const ITEM *element, void *cargo ). The scan stops when it returns T.

cargo Any pointer, passed unchanged to each call of the block. It takes the place of variables outside the block; NULL when not needed.

Return value

The position of the first matching element, or 0 when none matches or array is not an array.

Description

Scan the requested range in order and stop at the first matching element. In a value comparison, integers and floating-point values can match each other. Other supported values match within their type. Arrays use a predicate callback when their contents need to be compared.

String searches are case-sensitive. With exact matching off, a search string matches the beginning of an element. With `Set( _SET_EXACT, T )`, the full strings are compared with trailing spaces ignored.

A callback takes precedence over value comparison and receives a temporary copy of the element plus `cargo`. A subarray in that copy stays shared. The current length is checked after each call, and the scan stops at the end of the current array or the original range.

Example

ascan.c
#include "xdev-wtui.h"
#include "xdev-wtui-set.h"

#include <ctype.h>
#include <stdio.h>

/* Is the element the name in cargo, without regard to case? */
static xBool sameName( const ITEM *element, void *cargo )
{
   const char *text = ItemGetCPtr( element );
   const char *name = cargo;

   while( *text && toupper( ( unsigned char )*text ) == toupper( ( unsigned char )*name ) )
   {
      ++text;
      ++name;
   }

   return *text == *name ? T : F;
}

int main( void )
{
   ITEM *names = Array( 3 );
   ITEM *search = ItemNew( NULL );
   int   exact;

   if( !names )
   {
      ItemRelease( search );
      return 1;
   }

   ArraySetC( names, 1, "Tom" );
   ArraySetC( names, 2, "Maryanne" );
   ArraySetC( names, 3, "Sue" );

   ItemPutC( search, "Mary" );
   printf( "Mary: %d\n", AScan( names, search, NIL, NIL, NULL, NULL ) );

   exact = Set( _SET_EXACT, T );
   printf( "Mary with SET EXACT: %d\n", AScan( names, search, NIL, NIL, NULL, NULL ) );
   Set( _SET_EXACT, exact );

   ItemPutC( search, "mary" );
   printf( "mary: %d\n", AScan( names, search, NIL, NIL, NULL, NULL ) );
   printf( "maryanne with a block: %d\n", AScan( names, NULL, NIL, NIL, sameName, "maryanne" ) );

   ItemRelease( search );
   ItemRelease( names );

   return 0;
}

Output

Mary: 2
Mary with SET EXACT: 0
mary: 0
maryanne with a block: 2

See also