-
Notifications
You must be signed in to change notification settings - Fork 5
/
fileio.spin
189 lines (158 loc) · 5.95 KB
/
fileio.spin
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
'******************************************************************************
' Author: Dave Hein
' Version 1.0
' Copyright (c) 2010, 2011
' See end of file for terms of use.
'******************************************************************************
{{
This object provide file I/O functions for SpinSim. It currently implements only popen, pclose and pread.
The methods a similar to the ones the FSRW object. A dummy mount routine is provide for compatibility.
}}
con
SYS_COMMAND = $12340000
SYS_LOCKNUM = $12340002
SYS_PARM = $12340004
SYS_FILE_OPEN = 3
SYS_FILE_CLOSE = 4
SYS_FILE_READ = 5
SYS_FILE_WRITE = 6
SYS_FILE_OPENDIR = 7
SYS_FILE_CLOSEDIR = 8
SYS_FILE_READDIR = 9
SYS_FILE_SEEK = 10
SYS_FILE_TELL = 11
SYS_FILE_REMOVE = 12
SYS_FILE_CHDIR = 13
SYS_FILE_GETCWD = 14
SYS_FILE_MKDIR = 15
SYS_FILE_GETMOD = 16
dat
handle0 long 0
dirhand0 long 0
direntbuf long 0[20]
'****************************
' FSRW Routines
'****************************
pub mount(pin)
ifnot word[SYS_LOCKNUM]
word[SYS_LOCKNUM] := locknew + 1
pub mount_explicit(DO, CLK, DI, CS)
ifnot word[SYS_LOCKNUM]
word[SYS_LOCKNUM] := locknew + 1
pub popen(fname, mode)
pclose
handle0 := hopen(fname, mode)
ifnot handle0
return -1
pub pclose
if handle0
hclose(handle0)
handle0~
pub pread(buffer, num)
return hread(handle0, buffer, num)
pub pwrite(buffer, num)
return hwrite(handle0, buffer, num)
pub opendir
if dirhand0
hclosedir(dirhand0)
dirhand0 := hopendir
ifnot dirhand0
return -1
pub nextfile(fbuf)
return hnextfile(dirhand0, fbuf)
pub seek(position)
result := hseek(handle0, position)
pub tell
result := htell(handle0)
pub get_filesize
result := hget_filesize(handle0)
'****************************
' handle Routines
'****************************
pub hopen(fname, mode)
if mode => "a" and mode =< "z"
mode -= "a" - "A"
if mode == "R"
mode := string("rb")
elseif mode == "W"
mode := string("wb")
elseif mode == "A"
mode := string("ab")
elseif mode == "D"
return SystemCall(SYS_FILE_REMOVE, fname)
else
return 0
result := SystemCall(SYS_FILE_OPEN, @fname)
pub hclose(handle)
result := SystemCall(SYS_FILE_CLOSE, handle)
pub hread(handle, buffer, num)
result := SystemCall(SYS_FILE_READ, @handle)
pub hwrite(handle, buffer, num)
result := SystemCall(SYS_FILE_WRITE, @handle)
pub hopendir
result := SystemCall(SYS_FILE_OPENDIR, 0)
pub hclosedir(handle)
result := SystemCall(SYS_FILE_CLOSEDIR, handle)
pub hnextfile(handle, fbuf) | num
result := hreaddir(handle)
if result
num := strsize(@direntbuf[2])
if num > 31
num := 31
bytemove(fbuf, @direntbuf[2], num)
byte[fbuf][num] := 0
pub hreaddir(handle) | pdirent
pdirent := @direntbuf
result := SystemCall(SYS_FILE_READDIR, @handle)
if result
result := @direntbuf
pub hseek(handle, position) | whence
whence~
result := SystemCall(SYS_FILE_SEEK, @handle)
pub htell(handle)
result := SystemCall(SYS_FILE_TELL, handle)
pub hget_filesize(handle) | offset, whence, position, filesize
position := SystemCall(SYS_FILE_TELL, handle)
offset~
whence := 2
SystemCall(SYS_FILE_SEEK, @handle)
filesize := SystemCall(SYS_FILE_TELL, handle)
offset := position
whence~
SystemCall(SYS_FILE_SEEK, @handle)
return filesize
pub chdir(path)
result := SystemCall(SYS_FILE_CHDIR, path)
pub getcwd(str, num)
result := SystemCall(SYS_FILE_CHDIR, @str)
pub mkdir(path)
result := SystemCall(SYS_FILE_MKDIR, path)
pub getmod(fname)
result := SystemCall(SYS_FILE_GETMOD, fname)
pub SystemCall(command, parm) | locknum
locknum := word[SYS_LOCKNUM] - 1
if locknum == -1
return -1
repeat until not lockset(locknum)
long[SYS_PARM] := parm
word[SYS_COMMAND] := command
repeat while word[SYS_COMMAND]
result := long[SYS_PARM]
lockclr(locknum)
{{
+------------------------------------------------------------------------------------------------------------------------------+
| TERMS OF USE: MIT License |
+------------------------------------------------------------------------------------------------------------------------------+
|Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation |
|files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, |
|modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software|
|is furnished to do so, subject to the following conditions: |
| |
|The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.|
| |
|THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
|WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
|COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
|ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
+------------------------------------------------------------------------------------------------------------------------------+
}}