-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathread.cpp
More file actions
134 lines (127 loc) · 2.98 KB
/
Copy pathread.cpp
File metadata and controls
134 lines (127 loc) · 2.98 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
// This file is derived from the following FLTK source file:
// fluid/file.cxx
// It is provided under the terms of the FLTK License, which
// gives you some rights in addition to LGPL v.2
#include <QTextStream>
static bool isBrace(QChar c)
{
return c == '{' || c == '}';
}
static bool unread(QTextStream & in)
{
return in.seek(in.pos() - 1);
}
static int hexdigit(QChar c)
{
if (c.isDigit()) return c.unicode()-'0';
if (c.isUpper()) return c.unicode()-'A'+10;
if (c.isLower()) return c.unicode()-'a'+10;
return 20;
}
static QChar readQuoted(QTextStream & in)
{
QChar ch;
in >> ch;
int c = ch.unicode();
switch (c) {
case '\n': return QChar::Null;
case 'a' : return '\a';
case 'b' : return '\b';
case 'f' : return '\f';
case 'n' : return '\n';
case 'r' : return '\r';
case 't' : return '\t';
case 'v' : return '\v';
case 'x' : {
// read hex
c = 0;
for (int x = 0; x < 3; x++) {
if (in.atEnd()) break;
in >> ch;
int d = hexdigit(ch);
if (d > 15) {
unread(in);
break;
}
c = (c << 4) + d;
}
break;
}
default:
// read octal
if (c<'0' || c>'7') break;
c -= '0';
for (int x=0; x<2; x++) {
if (in.atEnd()) break;
in >> ch;
int d = hexdigit(ch);
if (d > 7) {
unread(in);
break;
}
c = (c << 3) + d;
}
break;
}
return QChar(c);
}
QString readWord(QTextStream & in, bool readBrace)
{
QString result;
QChar c;
// Skip the whitespace
forever {
if (in.atEnd()) return result;
in >> c;
if (c == '#') {
in.readLine();
}
else if (! c.isSpace()) {
break;
}
}
result.reserve(100);
if (c == '{' && ! readBrace) {
// Read between the braces
int level = 1;
forever {
if (in.atEnd()) return result;
in >> c;
if (c == '#') {
in.readLine();
continue;
}
else if (c == '\\') {
c = readQuoted(in);
if (c.isNull()) continue;
}
else if (c == '{') {
++ level;
}
else if (c == '}') {
if (! --level) break;
}
result.append(c);
}
}
else if (isBrace(c)) {
// Read the braces themselves
result = c;
}
else {
// Read a single word
forever {
if (c == '\\') {
c = readQuoted(in);
}
else if (c.isSpace() || isBrace(c) || c == '#') {
unread(in);
break;
}
if (! c.isNull()) result.append(c);
if (in.atEnd()) break;
in >> c;
}
}
return result;
}