This repository was archived by the owner on Jan 27, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathtokenizer.cpp
More file actions
83 lines (72 loc) · 2.38 KB
/
Copy pathtokenizer.cpp
File metadata and controls
83 lines (72 loc) · 2.38 KB
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
/*----------------------------------------------------------------------------+
| Name: Jerome Richards |
| Course: CNS-1250 Section F01 |
| Instructor: Dr. Afsaneh Minaie |
| |
| File: tokenizer.cpp |
| |
| Purpose: String Tokenizer implementation |
+----------------------------------------------------------------------------*/
#include "tokenizer.h"
#define NULL 0
/**
* Purpose: Overloaded constructor
*
* Parameters: theString a pointer to a NULL terminated character
* array
*
* theDelimiter the delimiting character for tokens
*/
StringTokenizer::StringTokenizer( char* theString, char theDelimiter )
{
itsString = NULL;
itsDelimiter = ' ';
if( theString )
{
itsString = theString;
itsDelimiter = theDelimiter;
}
}
/**
* Purpose: Returns the next token from the character stream
*
* Parameters: none
* Pre-conditions: none
* Post-conditions: none
*
* Returns : a character pointer to the next token and
* NULL if no other tokens exist in the stream
*/
char* StringTokenizer::next()
{
char* token = NULL;
// make sure that a valid pointer is being used
if( itsString )
{
// skip past any delimiter characters BUT not past the NULL terminator
while( *itsString == itsDelimiter && *itsString )
itsString++;
// if there is a token - process it
if( *itsString )
{
// save the new starting point
token = itsString;
// Look for the end of the token and NULL terminate it
while( *itsString && *itsString != itsDelimiter )
itsString++;
if( *itsString )
{
// if not at the NULL - terminate the token
*itsString = NULL;
// and Prepare for next token
itsString++;
}
}
else
{
// otherwise inform the caller there are no more tokens
itsString = NULL;
}
}
return token;
}