summaryrefslogtreecommitdiffstats
path: root/private/crt32/mbstring/mbstok.c
blob: 9c1093fb61af8fc5a019c8ffd7fbdba5ce02e57b (plain) (blame)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*** 
*mbstok.c - Break string into tokens (MBCS)
*
*	Copyright (c) 1985-1993, Microsoft Corporation.	All rights reserved.
*
*Purpose:
*	Break string into tokens (MBCS)
*
*Revision History:
*	11-19-92  KRS	Ported from 16-bit sources.
*	12-04-92  KRS	Added MTHREAD support.
*	02-17-93  GJF	Changed for new _getptd().
*	07-14-93  KRS	Fix: all references should be to _mtoken, not _token.
*
*******************************************************************************/

#ifdef _MBCS
#include <cruntime.h>
#include <mbdata.h>
#include <mbctype.h>
#include <mbstring.h>
#include <stddef.h>
#ifdef	MTHREAD
#include <os2dll.h>
#endif

#define _MBSSPNP(p,s)  _mbsspnp(p,s)
#define _MBSPBRK(q,s) _mbspbrk(q,s);

/***
* _mbstok - Break string into tokens (MBCS)
*
*Purpose:
*	strtok considers the string to consist of a sequence of zero or more
*	text tokens separated by spans of one or more control chars. the first
*	call, with string specified, returns a pointer to the first char of the
*	first token, and will write a null char into string immediately
*	following the returned token. subsequent calls with zero for the first
*	argument (string) will work thru the string until no tokens remain. the
*	control string may be different from call to call. when no tokens remain
*	in string a NULL pointer is returned. remember the control chars with a
*	bit map, one bit per ascii char. the null char is always a control char.
*
*	MBCS chars supported correctly.
*
*Entry:
*	char *string = string to break into tokens.
*	char *sepset = set of characters to use as seperators
*
*Exit:
*       returns pointer to token, or NULL if no more tokens
*
*Exceptions:
*
*******************************************************************************/

unsigned char * _CRTAPI1 _mbstok( string, sepset )
unsigned char * string;
const unsigned char * sepset;

{
	unsigned char *nextsep;

#ifdef	MTHREAD
#ifdef	_CRUISER_

	struct _tiddata * tdata;

	tdata = _gettidtab();	/* init tid's data address */

#else	/* ndef _CRUISER_ */
#ifdef	_WIN32_

	_ptiddata ptd = _getptd();

#else	/* ndef _WIN32_ */

#error ERROR - ONLY CRUISER OR WIN32 TARGET SUPPORTED!

#endif	/* _WIN32_ */
#endif	/* _CRUISER_ */

	unsigned char *nextoken;

#else	/* MTHREAD */

	static unsigned char *nextoken;

#endif	/* MTHREAD */


	/* init start of scan */

	if (string)
		nextoken = string;
	else
	/* If string==NULL, continue with previous string */
		{

#ifdef	MTHREAD
#ifdef	_CRUISER_

		nextoken = tdata->_mtoken;

#else	/* ndef _CRUISER_ */
#ifdef	_WIN32_

		nextoken = ptd->_mtoken;

#else	/* ndef _WIN32_ */

#error ERROR - ONLY CRUISER OR WIN32 TARGET SUPPORTED!

#endif	/* _WIN32_ */
#endif	/* _CRUISER_ */
#endif  /* MTHREAD */

		if (!nextoken)
		    return NULL;
		}

	/* skip over lead seperators */

	if ((string = _MBSSPNP(nextoken, sepset)) == NULL)
		return(NULL);


	/* test for end of string */

	if ( (*string == '\0') ||
	     ( (_ISLEADBYTE(*string)) && (string[1] == '\0') )
	   )
		return(NULL);


	/* find next seperator */

	nextsep = _MBSPBRK(string, sepset);

	if ((nextsep == NULL) || (*nextsep == '\0'))
		nextoken = NULL;
        else {
		if (_ISLEADBYTE(*nextsep))
			*nextsep++ = '\0';
		*nextsep = '\0';
		nextoken = ++nextsep;
             }

#ifdef	MTHREAD
	/* Update the corresponding field in the per-thread data * structure */

#ifdef _CRUISER_

	tdata->_mtoken = nextoken;

#else	/* _CRUISER_ */

#ifdef	_WIN32_

	ptd->_mtoken = nextoken;

#else	/* ndef _WIN32_ */

#error ERROR - ONLY CRUISER OR WIN32 TARGET SUPPORTED!

#endif	/* _WIN32_ */

#endif	/* _CRUISER_ */

#endif	/* MTHREAD */

	return(string);
}
#endif	/* _MBCS */