forked from paulfitz/daff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTableStream.hx
84 lines (76 loc) · 2.01 KB
/
TableStream.hx
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
// -*- mode:java; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
#if !TOPLEVEL
package coopy;
#end
class TableStream implements RowStream {
private var t : Table;
private var at : Int;
private var h : Int;
private var src : RowStream;
private var columns : Array<String>;
private var row : Map<String, Dynamic>;
public function new(t: Table) {
this.t = t;
at = -1;
h = t.height;
src = null;
if (h<0) {
var meta = t.getMeta();
if (meta==null) {
throw("Cannot get meta information for table");
}
src = meta.getRowStream();
if (src==null) {
throw("Cannot iterate table");
}
}
}
public function fetchColumns() : Array<String> {
if (columns!=null) return columns;
if (src!=null) {
columns = src.fetchColumns();
return columns;
}
columns = new Array<String>();
for (i in 0...t.width) {
columns.push(t.getCell(i,0));
}
return columns;
}
public function fetchRow() : Map<String, Dynamic> {
if (src!=null) return src.fetchRow();
if (at>=h) return null;
var row = new Map<String,Dynamic>();
for (i in 0...columns.length) {
row[columns[i]] = t.getCell(i,at);
}
return row;
}
public function fetch() : Bool {
if (at==-1) {
at++;
if (src!=null) fetchColumns();
return true;
}
if (src!=null) {
at = 1;
row = fetchRow();
return row!=null;
}
at++;
return at<h;
}
public function getCell(x: Int) : Dynamic {
if (at==0) {
return columns[x];
}
if (row!=null) {
return row[columns[x]];
}
return t.getCell(x,at);
}
public function width() : Int {
fetchColumns();
return columns.length;
}
}