forked from frmeier/psi46test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.cpp
135 lines (105 loc) · 2.49 KB
/
scanner.cpp
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
// scanner.cpp
#include <string.h>
#include "scanner.h"
// === CFile =============================================================
bool CFile::open(const char filename[])
{
init();
buffer = new char[FILEBUFFERSIZE];
if (!buffer) return false;
m_stream = fopen(filename,"r");
if (m_stream == NULL) return false;
getNextChar();
return true;
}
bool CFile::rewind()
{
if (!m_stream) return false;
fseek(m_stream, 0, SEEK_SET);
getNextChar();
return true;
}
char CFile::loadBuffer()
{
if (!m_stream) return 0;
count = fread(buffer,1,FILEBUFFERSIZE,m_stream);
pos = 0;
if (pos >= count) m_LastChar = 0;
else m_LastChar = buffer[pos++];
return m_LastChar;
}
void CFile::close()
{
if (buffer) { delete[] buffer; buffer=NULL; }
if (m_stream) { fclose(m_stream); m_stream = NULL; }
}
// === CScanner ==========================================================
bool CScanner::open(const char filename[])
{
if (m_logf.open(filename)) { getNextSection(); return true; }
return false;
}
bool CScanner::rewind()
{
if (m_logf.rewind()) { getNextSection(); return true; }
return false;
}
void CScanner::close()
{
m_logf.close();
}
char* CScanner::getNextSection()
{
m_logf.skipToChar('[');
int pos = 0;
char ch = m_logf.getNextChar();
while (ch && ch!=']')
{
if (pos<MAXSECTIONLEN) m_Section[pos++] = ch;
ch = m_logf.getNextChar();
}
m_Section[pos] = 0;
m_logf.getNextChar();
return m_Section;
}
bool CScanner::getNextSection(const char name[])
{
do
{
getNextSection();
if (m_Section[0] == 0) return false;
}
while (strcmp(m_Section, name) != 0);
return true;
}
bool CScanner::getNextSection(const char name[], const char stop[])
{
if (strcmp(m_Section,stop) == 0) return strcmp(name,stop) == 0;
do
{
getNextSection();
if (m_Section[0] == 0) return false;
if (strcmp(m_Section,name) == 0) return true;
} while(strcmp(m_Section,stop) != 0);
return false;
}
char* CScanner::getNextLine()
{
int pos = 0;
char ch = m_logf.getChar();
while (ch && ch!='[' && ch!='\r' && ch!='\n')
{
if (pos<MAXLINELEN) m_Line[pos++] = ch;
ch = m_logf.getNextChar();
}
m_Line[pos] = 0;
while (ch=='\r' || ch=='\n') ch = m_logf.getNextChar();
return m_Line;
}
void CScanner::skipLine()
{
char ch = m_logf.getChar();
while (ch && ch!='[' && ch!='\r' && ch!='\n')
ch = m_logf.getNextChar();
while (ch=='\r' || ch=='\n') ch = m_logf.getNextChar();
}