1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
// extract the next token in srce into token.
// return the number of characters processed in srce
// invariant: non-NULL pointers
size_t extract_next_token( const char* srce, char* token )
{
size_t n = 0 ;
while( *srce && isspace(*srce) ) { ++srce ; ++n ; } // skip leading white space
for( ; *srce && !isspace(*srce) ; ++n ) *token++ = *srce++ ; // copy non-ws characters
*token = 0 ; // null terminate
return n ; // number of characters processed
}
// extract up to a maximum of sz tokens. return number of tokens extracted
// invariant: non-NULL top-level and non-top-level pointers
size_t extract_tokens( const char* srce, char* tokens[], size_t sz )
{
for( size_t i = 0 ; i < sz ; ++i )
{
size_t offset = extract_next_token( srce, tokens[i] ) ;
if( tokens[i][0] == 0 ) return i ; // return number of tokens extracted
srce += offset ;
}
return sz ;
}
// extract up to three tokens into a, b and c. return number of tokens extracted
// invariant: non-NULL pointers
size_t break_up( const char* srce, char* token_a, char* token_b, char* token_c )
{
char* tokens[3] = { token_a, token_b, token_c } ;
return extract_tokens( srce, tokens, 3 ) ;
}
int main()
{
const char text[] = " C is quirky, flawed, \t and an enormous success.\n"
" - Dennis M. Ritchie.\n " ;
enum { MAX_TOK_SZ = 128, NUM_TOKS = 16 };
char tokens[NUM_TOKS][MAX_TOK_SZ] ;
char* tok_ptrs[NUM_TOKS] ;
for( size_t i = 0 ; i < NUM_TOKS ; ++i ) tok_ptrs[i] = tokens[i] ;
size_t n = extract_tokens( text, tok_ptrs, NUM_TOKS ) ;
for( size_t i = 0 ; i < n ; ++i ) printf( "%s ", tokens[i] ) ;
char a[MAX_TOK_SZ], b[MAX_TOK_SZ], c[MAX_TOK_SZ] ;
n = break_up( text, a, b, c ) ;
assert( n == 3 ) ;
printf( "\n\n1. %s\n2. %s\n3. %s\n", a, b, c ) ;
}
|