-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringTokenizer.cpp
More file actions
150 lines (120 loc) · 3.68 KB
/
Copy pathStringTokenizer.cpp
File metadata and controls
150 lines (120 loc) · 3.68 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
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
//CST 280 - Program Code
//Filename: Tokens/StringTokenizer.cpp
//Winter 2015
//Mon/Wed 8a-10a
//Instructor: T. Klingler
// Class to manage "tokenized" data (comma-separated files)
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
#include "StringTokenizer.h"
// Constructor
// Receive line of comma-delimited data and set index of current
// token to zero
StringTokenizer::StringTokenizer(string line)
{
theLine = line;
currIndex = 0;
delimiterChar = ','; // Default delimiter is comma
}
// Parameterized constructor - accepts a character other than a comma
// as delimiter.
// Precondition: separator is a single character (non-space)
// Postcondtion: data string NOT initialized
StringTokenizer::StringTokenizer(char newDelimiter)
{
currIndex = 0;
delimiterChar = newDelimiter; // Set delimiter to programmer choice
}
// Parameterized constructor - accepts an initial string to parse and
// a character other than a comma as delimiter.
// Precondition: separator is a single character (non-space)
StringTokenizer::StringTokenizer(string line, char newDelimiter)
{
theLine = line;
currIndex = 0;
delimiterChar = newDelimiter; // Set delimiter to programmer choice
}
// "Set" and "Get" functhions
void StringTokenizer::setLine(string line)
{
theLine = line;
}
string StringTokenizer:: getLine()
{
return theLine;
}
// Reset index of current token to zero
void StringTokenizer::reset()
{
currIndex = 0;
}
// Return number of tokens in current string
int StringTokenizer::numberTokens()
{
int lineLength = theLine.length();
int count = 0;
for (int i = 0; i < lineLength; i++)
if (theLine.at(i) == delimiterChar)
count++;
return count + 1; // Return number of tokens as number of commas + 1
}
// To observe if token pointer is at end of string
bool StringTokenizer::atEnd()
{
if (currIndex == theLine.length())
return true;
else
return false;
}
// Return next token as a string object
// Precondition: Token pointer is not at end of string
string StringTokenizer::getStringToken()
{
get_a_Token();
return tempString;
}
// Return next token as a character object
// Precondition: Token pointer is not at end of string
char StringTokenizer::getCharToken()
{
char outChar;
get_a_Token();
outChar = tempString[0]; // Extract one char from string
return outChar;
}
// Return next token as a double value
// Precondition: Token pointer is not at end of string
double StringTokenizer::getDoubleToken()
{
double outValue;
get_a_Token();
outValue = atof(tempString.data()); // Convert to number
return outValue;
}
// Returns an integer value being referenced
int StringTokenizer::getIntToken()
{
int outInt;
get_a_Token();
outInt = atoi(tempString.data()); // Convert to number
return outInt;
}
// This utility function retrieves the next token and returns it to
// be correctly typed before the final return from the object function call
void StringTokenizer::get_a_Token()
{
int newIndex = theLine.find(delimiterChar,currIndex);
if (newIndex >= 0) // If not last token
{
tempString = theLine.substr(currIndex,newIndex - currIndex);
currIndex = newIndex + 1; // Advance to position after next comma
}
else // If last token
{
tempString = theLine.substr(currIndex,theLine.length() - currIndex);
currIndex = theLine.length(); // Set current index to end
}
}
//Tim Klingler | Delta College | 989-686-9140 | teklingl@delta.edu