-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokens.cpp
More file actions
95 lines (78 loc) · 1.53 KB
/
Tokens.cpp
File metadata and controls
95 lines (78 loc) · 1.53 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
#include "Tokens.h"
#if !defined NULL
#define NULL 0
#endif
#include <iostream>
using namespace std;
Tokens::Tokens(String* str, char delimiter)
{
max_tokens = 1;
sz = 0;
tokens = new String*[max_tokens];
for (int i = 0; i < max_tokens; i++)
{
tokens[i] = NULL;
}
int str_len = str->length();
int current_loc = 0;
int count = 0;
while(current_loc < str_len)
{
int next_loc = str->find(delimiter, current_loc);
if (next_loc > 0) //a delimiter as the first character is a problem
{
String* token = str->substr(current_loc, next_loc - 1);
addToken(token);
count++;
}
current_loc = next_loc + 1;
}
}
Tokens::~Tokens()
{
delete[] tokens;
}
int Tokens::getNumTokens()
{
return sz;
}
void Tokens::displayTokens()
{
int num_tokens = sz;
String** strings = tokens;
for (int i = 0; i < num_tokens; i++)
{
String* str = strings[i];
str->displayString();
cout << endl;
}
}
void Tokens::resize()
{
String** resize_strings = new String*[2*max_tokens];
for (int i = 0; i < sz; i++)
{
resize_strings[i] = tokens[i];
}
for (int i = sz; i < 2*sz; i++)
{
resize_strings[i] = NULL;
}
delete[] tokens;
tokens = resize_strings;
max_tokens = 2*max_tokens;
}
void Tokens::addToken(String* str)
{
if (sz == max_tokens)
{
resize();
}
tokens[sz] = str;
sz++;
}
String* Tokens::getToken(int index)
{
if (index < 0 && index >= sz) return NULL;
return tokens[index];
}