-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtokenizer.cpp
More file actions
78 lines (70 loc) · 1.76 KB
/
tokenizer.cpp
File metadata and controls
78 lines (70 loc) · 1.76 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
#include "tokenizer.h"
#include <stdio.h>
#include <string.h>
void Tokenizer::getLine()
{
// -- Read a line
m_num_tokens = 0;
char* res = fgets(m_line, LINESIZE, m_file);
if (res) {
m_cur_line++;
m_line_saved = m_line;
// -- Break line up into tokens
/// Basically, just replace spaces with \0 and remember the start of each token
// -- Set to true so that the first char is identified as a token
int cur = 0;
bool prev_was_space = true;
while ( (m_line[cur] != '\0') &&
(cur < LINESIZE) ) {
if (isspace(m_line[cur])) {
// -- Replace spaces with \0
prev_was_space = true;
m_line[cur] = '\0';
} else {
// -- Not a space (part of a token)
if (prev_was_space) {
// -- Found the start of t a token
m_tokens[m_num_tokens] = &(m_line[cur]);
m_num_tokens++;
}
prev_was_space = false;
}
cur++;
}
}
m_done = (m_num_tokens == 0);
}
unsigned int Tokenizer::getInt(int i)
{
if (i < m_num_tokens) {
unsigned int value = 0;
sscanf(m_tokens[i], "%X", &value);
return value;
} else {
return 0;
}
}
char* Tokenizer::getString(int i)
{
if (i < m_num_tokens) {
return m_tokens[i];
}
else {
return 0;
}
}
char Tokenizer::getChar(int i)
{
if (i < m_num_tokens) {
return m_tokens[i][0];
}
else {
return 0;
}
}
void Tokenizer::debugCurrent()
{
cout << "DEBUG: current line = " << m_cur_line << endl;
string tmp(m_line_saved);
cout << " ---> " << tmp << endl;
}