1 /*********************************************************************
3 * stptok() -- public domain by Ray Gardner, modified by Bob Stout and
7 * Description: This file contains the stptok function
9 *********************************************************************/
12 /******************************************************************
13 * DESCRIPTION: You pass this function a string to parse,
14 * a buffer to receive the "token" that gets scanned,
15 * the length of the buffer, and a string of "break"
16 * characters that stop the scan.
17 * It will copy the string into the buffer up to
18 * any of the break characters, or until the buffer
19 * is full, and will always leave the buffer
20 * null-terminated. It will return a pointer to the
21 * first non-breaking character after the one that
23 * RETURN: It will return a pointer to the
24 * first non-breaking character after the one that
25 * stopped the scan or NULL on error or end of string.
27 ******************************************************************/
29 const char *s
,/* string to parse */
30 char *tok
, /* buffer that receives the "token" that gets scanned */
31 size_t toklen
,/* length of the buffer */
32 const char *brk
)/* string of break characters that will stop the scan */
34 char *lim
; /* limit of token */
35 const char *b
; /* current break character */
38 /* check for invalid pointers */
39 if (!s
|| !tok
|| !brk
)
42 /* check for empty string */
46 lim
= tok
+ toklen
- 1;
47 while ( *s
&& tok
< lim
)
49 for ( b
= brk
; *b
; b
++ )
54 for (++s
, b
= brk
; *s
&& *b
; ++b
)
81 void testTokens(Test
* pTest
)
83 char *pCmd
= "I Love You\r\n";
87 pCmd
= stptok(pCmd
,token
, sizeof(token
), " \r\n");
89 ct_test(pTest
, strcmp(token
,"I") == 0);
90 ct_test(pTest
, strcmp(pCmd
,"Love You\r\n") == 0);
92 pCmd
= stptok(pCmd
,token
, sizeof(token
), " \r\n");
94 ct_test(pTest
, strcmp(token
,"Love") == 0);
95 ct_test(pTest
, strcmp(pCmd
,"You\r\n") == 0);
97 pCmd
= stptok(pCmd
,token
, sizeof(token
), " \r\n");
99 ct_test(pTest
, strcmp(token
,"You") == 0);
100 ct_test(pTest
, pCmd
== NULL
);
111 pTest
= ct_create("stptok test", NULL
);
113 /* individual tests */
114 rc
= ct_addTestFunction(pTest
, testTokens
);
117 ct_setStream(pTest
, stdout
);
119 (void)ct_report(pTest
);
125 #endif /* LOCAL_TEST */