-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStyleParser.cpp
137 lines (106 loc) · 2.12 KB
/
StyleParser.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
136
137
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "StyleParser.h"
StyleParser::StyleParser()
: fData(""),
fBuffer(""),
fPosition(0)
{
}
StyleParser::StyleParser(BString data)
: fData(data),
fBuffer(""),
fPosition(0)
{
}
StyleParser::StyleParser(const char *data)
: fData(data),
fBuffer(""),
fPosition(0)
{
}
StyleParser::~StyleParser()
{
}
void
StyleParser::SetData(BString &data)
{
fData.SetTo(data);
}
void
StyleParser::SetData(const char *data)
{
fData.SetTo(data);
}
void
StyleParser::AddData(BString &data)
{
fData.Append(data);
}
void
StyleParser::AddData(const char *data)
{
fData.Append(data);
}
void
StyleParser::Reset()
{
fData.SetTo("");
Rewind();
}
attribute_s *
StyleParser::GetNext()
{
int length = fData.Length();
if (length <= 0 || fPosition >= length)
return NULL;
int pos = fData.FindFirst(':', fPosition);
if (pos < 0)
return NULL;
attribute_s *result = new attribute_s;
fData.CopyInto(result->id, fPosition, pos++ - fPosition);
int nextpos = fData.FindFirst(';', pos);
if (nextpos < 0)
nextpos = length;
fData.CopyInto(result->value, pos, nextpos++ - pos);
fPosition = nextpos;
result->id.ReplaceSet(" \t\n", "");
result->value.ReplaceSet("\r\t\n", " ");
//printf("next: '%s' '%s'\n", result->id.String(), result->value.String());
return result;
}
void
StyleParser::Rewind()
{
fPosition = 0;
}
attribute_s *
StyleParser::Find(BString id)
{
int pos = fData.FindFirst(id);
if (pos < 0)
return NULL;
int sep = fData.FindFirst(':', pos);
if (sep < 0)
return NULL;
attribute_s *result = new attribute_s;
fData.CopyInto(result->id, pos, sep - pos);
int end = fData.FindFirst(';', sep);
if (end < 0)
end = fData.Length();
//printf("l: %d; p: %d; s: %d; e: %d\n", fData.Length(), pos, sep, end);
fData.CopyInto(result->value, sep + 1, end - sep - 1);
return result;
}
attribute_s *
StyleParser::Find(const char *id)
{
return Find(BString(id));
}
bool
StyleParser::FindAndAsign(const char *id, const char *format, void *to_value)
{
attribute_s *style = Find(BString(id));
return sscanf(style->value.String(), format, to_value) == 1;
}