File: extract_token.c

package info (click to toggle)
garlic 1.6-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,516 kB
  • sloc: ansic: 52,465; makefile: 2,254
file content (118 lines) | stat: -rw-r--r-- 2,730 bytes parent folder | download | duplicates (6)
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
/* Copyright (C) 2000 Damir Zucic */

/*=============================================================================

				extract_token.c

Purpose:
	Extract token (substring) from string. Up to max_length - 1
	characters are copied.

Input:
	(1) Pointer to the buffer where token will be stored.
	(2) The maximal length of the token (counting '\0').
	(2) Pointer to the input string.
	(3) Pointer to the list (string) of separators.

Output:
	(1) Token extracted and copied.
	(2) Return value.

Return value:
	(1) Pointer to the next token, on success.
	(2) NULL on failure.

Notes:
	(1) Input overflow is not allowed in this function.

	(2) The output token is always terminated properly.

========includes:============================================================*/

#include <stdio.h>

/*======extract token:=======================================================*/

char *ExtractToken_ (char *tokenP, int max_length,
		     char *stringP, char *separatorsP)
{
int		current_char, current_separator;
int		separator_foundF, token_foundF;
char		*P;
int		token_length = 0;

/* Initialize the output string: */
*tokenP = '\0';

/* Check the input string pointer: */
if (stringP == NULL) return NULL;

/* Skip leading separators: */
token_foundF = 0;
while ((current_char = *stringP++) != '\0')
	{
	/** Search the set of separators: **/
	P = separatorsP;
	separator_foundF = 0;
	while ((current_separator = *P++) != '\0')
		{
		if (current_char == current_separator)
			{
			separator_foundF = 1;
			break;
			}
		}

	/** If the current character  does not belong to **/
	/** the set of separators, the token is reached: **/
	if (!separator_foundF)
		{
		token_foundF = 1;
		break;
		}
	}

/* Pointer to the last character; this is either */
/* zero or  the first  character  of the  token: */
stringP--;

/* If token was not reached at all, return NULL: */
if (*stringP == '\0') return NULL;

/* Copy the token: */
while ((current_char = *stringP++) != '\0')
	{
	/** Search the set of separators: **/
	P = separatorsP;
	separator_foundF = 0;
	while ((current_separator = *P++) != '\0')
		{
		if (current_char == current_separator)
			{
			separator_foundF = 1;
			break;
			}
		}

	/** If the current character belongs to the set **/
	/** of separators, the end of token is reached: **/
	if (separator_foundF) break;

	/** If end of token is not reached, copy the current character: **/
	*tokenP++ = current_char;

	/** Update and check the token length: **/
	token_length++;
	if (token_length >= max_length - 1) break;
	}

/* Terminate the token: */
*tokenP = '\0';

/* Return the pointer to the remainder of the string: */
return --stringP;
}

/*===========================================================================*/