-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathJM_IO.C
216 lines (174 loc) · 4.32 KB
/
JM_IO.C
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "id_heads.h"
#include <io.h>
#include "jm_io.h"
#include "jm_cio.h"
#include "jm_lzh.h"
#include "jm_error.h"
//--------------------------------------------------------------------------
// IO_FarRead()
//--------------------------------------------------------------------------
boolean IO_FarRead (int handle, byte far *dest, long length)
{
unsigned readlen,nread;
while (length)
{
if (length > 0xffff)
readlen=0xffff;
else
readlen=length;
_dos_read(handle,dest,readlen,&nread);
if (nread != readlen)
return(false);
length -= readlen;
}
return(true);
}
//--------------------------------------------------------------------------
// IO_FarWrite()
//--------------------------------------------------------------------------
boolean IO_FarWrite (int handle, byte far *source, long length)
{
unsigned writelen,nwritten;
while (length)
{
if (length > 0xffff)
writelen=0xffff;
else
writelen=length;
_dos_write(handle,source,writelen,&nwritten);
if (nwritten != writelen)
return(false);
length -= writelen;
}
return(true);
}
#if DEMOS_EXTERN
//--------------------------------------------------------------------------
// IO_WriteFile()
//--------------------------------------------------------------------------
boolean IO_WriteFile(char *filename, void far *ptr, long length)
{
int handle;
long size;
handle = open(filename,O_CREAT | O_BINARY | O_WRONLY,
S_IREAD | S_IWRITE | S_IFREG);
if (handle == -1)
return(false);
if (!IO_FarWrite (handle,ptr,length))
{
close (handle);
return(false);
}
close (handle);
return(true);
}
#endif
//--------------------------------------------------------------------------
// IO_LoadFile()
//--------------------------------------------------------------------------
long IO_LoadFile (char *filename, memptr *dst)
{
char buffer[5]={0,0,0,0,0};
int handle;
long size=0;
if ((handle = open(filename,O_RDONLY | O_BINARY, S_IREAD)) == -1)
return(size);
read(handle,buffer,4);
if (!strcmp(buffer,"JAMP"))
{
struct JAMPHeader head;
read(handle,&head,sizeof(head));
size = head.OriginalLen;
switch (head.CompType)
{
case ct_LZH:
LZH_Startup();
MM_GetPtr(dst,head.OriginalLen);
if (mmerror)
return(0);
LZH_Decompress((void far *)handle,*dst,size,head.CompressLen,SRC_FILE|DEST_MEM);
LZH_Shutdown();
break;
case ct_LZW:
IO_ERROR(IO_LOADFILE_NO_LZW);
break;
default:
IO_ERROR(IO_LOADFILE_UNKNOWN);
break;
}
}
else
{
lseek(handle,0,SEEK_SET);
size = filelength(handle);
MM_GetPtr(dst,size);
if (!IO_FarRead(handle,*dst,size))
{
close(handle);
return(size);
}
}
close(handle);
return(size);
}
#if 0
//--------------------------------------------------------------------------
// IO_CopyFile()
//--------------------------------------------------------------------------
void IO_CopyFile(char *sFilename, char *dFilename)
{
int sHandle,dHandle;
unsigned length;
// Allocate memory for buffer.
//
if ((sHandle = open(sFilename,O_RDONLY | O_BINARY, S_IREAD)) == -1)
IO_ERROR(IO_COPYFILE_OPEN_SRC);
if ((dHandle=open(dFilename,O_CREAT|O_RDWR|O_BINARY,S_IREAD|S_IWRITE))==-1)
IO_ERROR(IO_COPYFILE_OPEN_DEST);
// Copy that file!
//
IO_CopyHandle(sHandle,dHandle,-1);
// Close files.
//
close(sHandle);
close(dHandle);
}
#endif
//--------------------------------------------------------------------------
// IO_CopyHandle()
//--------------------------------------------------------------------------
void IO_CopyHandle(int sHandle, int dHandle, long num_bytes)
{
extern boolean bombonerror;
#define CF_BUFFER_SIZE 8192
long fsize;
memptr src;
unsigned length;
// Allocate memory for buffer.
//
MM_GetPtr(&src,CF_BUFFER_SIZE);
if (num_bytes == -1)
fsize=filelength(sHandle);
else
fsize=num_bytes;
// Copy that file!
//
do
{
// Determine length to read/write.
//
if (fsize >= CF_BUFFER_SIZE)
length = CF_BUFFER_SIZE;
else
length = fsize;
// Read it, write it and decrement length.
//
IO_FarRead(sHandle,src,length);
IO_FarWrite(dHandle,src,length);
fsize -= length;
}
while (fsize);
// Free buffer.
//
MM_FreePtr(&src);
}