asm.h: use only .asciz sections of the .s file
[AROS.git] / test / rexx / simplerexx / stptok.c
blobafc0c5d932b4f6ee1546bcbfa1c8ccaf3abeb8e1
1 /*********************************************************************
3 * stptok() -- public domain by Ray Gardner, modified by Bob Stout and
4 * Steve Karg
6 * Functional
7 * Description: This file contains the stptok function
9 *********************************************************************/
10 #include <stddef.h>
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
22 * stopped the scan.
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.
26 * ALGORITHM: none
27 ******************************************************************/
28 char *stptok(
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)
40 return NULL;
42 /* check for empty string */
43 if (!*s)
44 return NULL;
46 lim = tok + toklen - 1;
47 while ( *s && tok < lim )
49 for ( b = brk; *b; b++ )
51 if ( *s == *b )
53 *tok = 0;
54 for (++s, b = brk; *s && *b; ++b)
56 if (*s == *b)
58 ++s;
59 b = brk;
62 if (!*s)
63 return NULL;
64 return (char *)s;
67 *tok++ = *s++;
69 *tok = 0;
71 if (!*s)
72 return NULL;
73 return (char *)s;
76 #ifdef TEST
77 #include <string.h>
78 #include <stdio.h>
79 #include <assert.h>
80 #include "ctest.h"
81 void testTokens(Test* pTest)
83 char *pCmd = "I Love You\r\n";
84 char token[80] = "";
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);
102 return;
105 #ifdef TEST_STPTOK
106 int main(void)
108 Test *pTest;
109 bool rc;
111 pTest = ct_create("stptok test", NULL);
113 /* individual tests */
114 rc = ct_addTestFunction(pTest, testTokens);
115 assert(rc);
117 ct_setStream(pTest, stdout);
118 ct_run(pTest);
119 (void)ct_report(pTest);
121 ct_destroy(pTest);
123 return 0;
125 #endif /* LOCAL_TEST */
127 #endif