-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmkproject_mpfile.mata
105 lines (92 loc) · 1.71 KB
/
mkproject_mpfile.mata
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
mata:
mata set matastrict on
void mpfile::mpferror(real scalar errcode)
{
errprintf("%s\n", ferrortext(errcode))
exit(freturncode(errcode))
}
real scalar mpfile::mpfopen(string scalar fn, string scalar mode)
{
real scalar fh
fh = _fopen(fn, mode)
if (fh < 0 ) {
mpferror(fh)
}
fhs.put(fh,"open")
return(fh)
}
real scalar mpfile::nlines(string scalar fn)
{
real scalar fh, i
string scalar EOF, line
EOF = J(0,0,"")
fh = mpfopen(fn,"r")
i = 0
while ((line=fget(fh))!=EOF) i++
mpfclose(fh)
return(i)
}
void mpfile::mpfread(string scalar fn)
{
if (reading.open == 1) {
errprintf("{p}trying to read " + fn + " while " + reading.fn + " is still open{p_end}")
exit(198)
}
reading.fn = fn
reading.fh = mpfopen(fn, "r")
reading.lnr = 0
reading.label = ""
reading.fversion = J(1,3,.)
reading.sversion = ""
reading.type = ""
reading.open = 1
reading.reqs = J(0,1,"")
reading.description = J(0,1,"")
}
void mpfile::mpfclose(real scalar fh)
{
real scalar errcode
errcode = _fclose(fh)
if (errcode < 0 ) {
mpferror(errcode)
}
if (fh == reading.fh) reading.open = 0
fhs.put(fh,"closed")
}
void mpfile::mpfput(real scalar fh, string scalar s)
{
real scalar errcode
errcode = _fput(fh, s)
if (errcode < 0 ) {
mpferror(errcode)
}
}
string matrix mpfile::mpfget()
{
real scalar errcode
string scalar result
result = _fget(reading.fh)
errcode = fstatus(reading.fh)
if (errcode < -1 ) {
where_err()
mpferror(errcode)
}
return(result)
}
void mpfile::new()
{
fhs.reinit("real")
}
void mpfile::mpfclose_all()
{
real colvector K
real scalar i, fh
K = fhs.keys()
for (i=1; i<=rows(K); i++) {
fh = K[i]
if (fhs.get(fh)!="closed") {
mpfclose(fh)
}
}
}
end