aboutsummaryrefslogtreecommitdiff
path: root/board/MAI/bios_emulator/scitech/src/pm/vdd/fileio.c
blob: dbbaf37dfa135f0e3a493f24efe1061ac0477b67 (plain)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/****************************************************************************
*
*                   SciTech OS Portability Manager Library
*
*  ========================================================================
*
*    The contents of this file are subject to the SciTech MGL Public
*    License Version 1.0 (the "License"); you may not use this file
*    except in compliance with the License. You may obtain a copy of
*    the License at http://www.scitechsoft.com/mgl-license.txt
*
*    Software distributed under the License is distributed on an
*    "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
*    implied. See the License for the specific language governing
*    rights and limitations under the License.
*
*    The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc.
*
*    The Initial Developer of the Original Code is SciTech Software, Inc.
*    All Rights Reserved.
*
*  ========================================================================
*
* Language:     ANSI C
* Environment:  32-bit OS/2 VDD
*
* Description:  C library compatible I/O functions for use within a VDD.
*
****************************************************************************/

#include "pmapi.h"
#include "vddfile.h"

/*------------------------ Main Code Implementation -----------------------*/

#define EOF -1

/* NB: none of the file VDHs are available during the DOS session          */
/* initialzation context!                                                  */

/* Macros for Open/Close APIs to allow using this module in both VDDs and  */
/* normal OS/2 applications. Unfortunately VDHRead/Write/Seek don't map to */
/* their Dos* counterparts so cleanly.                                     */
#ifdef __OS2_VDD__
#define _OS2Open    VDHOpen
#define _OS2Close   VDHClose
#else
#define _OS2Open    DosOpen
#define _OS2Close   DosClose
#endif

/****************************************************************************
REMARKS:
VDD implementation of the ANSI C fopen function.
****************************************************************************/
FILE * fopen(
    const char *filename,
    const char *mode)
{
    FILE    *f = PM_malloc(sizeof(FILE));
    long    oldpos;
    ULONG   rc, ulAction;
    ULONG   omode, oflags;

    if (f != NULL) {
        f->offset = 0;
        f->text = (mode[1] == 't' || mode[2] == 't');
        f->writemode = (mode[0] == 'w') || (mode[0] == 'a');
        f->unputc = EOF;
        f->endp = f->buf + sizeof(f->buf);
        f->curp = f->startp = f->buf;

        if (mode[0] == 'r') {
            #ifdef __OS2_VDD__
            omode  = VDHOPEN_ACCESS_READONLY | VDHOPEN_SHARE_DENYNONE;
            oflags = VDHOPEN_ACTION_OPEN_IF_EXISTS | VDHOPEN_ACTION_FAIL_IF_NEW;
            #else
            omode  = OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE;
            oflags = OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW;
            #endif
            }
        else if (mode[0] == 'w') {
            #ifdef __OS2_VDD__
            omode  = VDHOPEN_ACCESS_WRITEONLY | VDHOPEN_SHARE_DENYWRITE;
            oflags = VDHOPEN_ACTION_REPLACE_IF_EXISTS | VDHOPEN_ACTION_CREATE_IF_NEW;
            #else
            omode  = OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYWRITE;
            oflags = OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW;
            #endif
            }
        else {
            #ifdef __OS2_VDD__
            omode  = VDHOPEN_ACCESS_READWRITE | VDHOPEN_SHARE_DENYWRITE;
            oflags = VDHOPEN_ACTION_OPEN_IF_EXISTS | VDHOPEN_ACTION_CREATE_IF_NEW;
            #else
            omode  = OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYWRITE;
            oflags = OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW;
            #endif
            }
        rc = _OS2Open((PSZ)filename, (PHFILE)&f->handle, &ulAction, 0, VDHOPEN_FILE_NORMAL, oflags, omode, NULL);
        if (rc != 0) {
            PM_free(f);
            return NULL;
            }

        #ifdef __OS2_VDD__
        f->filesize = VDHSeek((HFILE)f->handle, 0, VDHSK_END_OF_FILE);
        #else
        rc = DosSetFilePtr((HFILE)f->handle, 0, FILE_END, &f->filesize);
        #endif

        if (mode[0] == 'a')
            fseek(f,0,2);
    }
    return f;
}

/****************************************************************************
REMARKS:
VDD implementation of the ANSI C fread function. Note that unlike Windows VxDs,
OS/2 VDDs are not limited to 64K reads or writes.
****************************************************************************/
size_t fread(
    void *ptr,
    size_t size,
    size_t n,
    FILE *f)
{
    char    *buf = ptr;
    int     bytes,readbytes,totalbytes = 0;

    /* First copy any data already read into our buffer */
    if ((bytes = (f->curp - f->startp)) > 0) {
        memcpy(buf,f->curp,bytes);
        f->startp = f->curp = f->buf;
        buf += bytes;
        totalbytes += bytes;
        bytes = (size * n) - bytes;
        }
    else
        bytes = size * n;
    if (bytes) {
        #ifdef __OS2_VDD__
        readbytes = VDHRead((HFILE)f->handle, buf, bytes);
        #else
        DosRead((HFILE)f->handle, buf, bytes, &readbytes);
        #endif
        totalbytes += readbytes;
        f->offset += readbytes;
        }
    return totalbytes / size;
}

/****************************************************************************
REMARKS:
VDD implementation of the ANSI C fwrite function.
****************************************************************************/
size_t fwrite(
    void *ptr,
    size_t size,
    size_t n,
    FILE *f)
{
    char        *buf = ptr;
    int         bytes,writtenbytes,totalbytes = 0;

    /* Flush anything already in the buffer */
    if (!f->writemode)
        return 0;
    fflush(f);
    bytes = size * n;
    #ifdef __OS2_VDD__
    writtenbytes = VDHWrite((HFILE)f->handle, buf, bytes);
    #else
    DosWrite((HFILE)f->handle, buf, bytes, &writtenbytes);
    #endif
    totalbytes += writtenbytes;
    f->offset += writtenbytes;
    if (f->offset > f->filesize)
        f->filesize = f->offset;
    return totalbytes / size;
}

/****************************************************************************
REMARKS:
VxD implementation of the ANSI C fflush function.
****************************************************************************/
int fflush(
    FILE *f)
{
    ULONG     bytes;

    /* First copy any data already written into our buffer */
    if (f->writemode && (bytes = (f->curp - f->startp)) > 0) {
        #ifdef __OS2_VDD__
        bytes = VDHWrite((HFILE)f->handle, f->startp, bytes);
        #else
        DosWrite((HFILE)f->handle, f->startp, bytes, &bytes);
        #endif
        f->offset += bytes;
        if (f->offset > f->filesize)
            f->filesize = f->offset;
        f->startp = f->curp = f->buf;
        }
    return 0;
}

/****************************************************************************
REMARKS:
VDD implementation of the ANSI C fseek function.
****************************************************************************/
int fseek(
    FILE *f,
    long int offset,
    int whence)
{
    fflush(f);

    if (whence == 0)
        f->offset = offset;
    else if (whence == 1)
        f->offset += offset;
    else if (whence == 2)
        f->offset = f->filesize + offset;

    #ifdef __OS2_VDD__
    VDHSeek((HFILE)f->handle, f->offset, VDHSK_ABSOLUTE);
    #else
    DosSetFilePtr((HFILE)f->handle, f->offset, FILE_BEGIN, NULL);
    #endif

    return 0;
}

/****************************************************************************
REMARKS:
VDD implementation of the ANSI C ftell function.
****************************************************************************/
long ftell(
    FILE *f)
{
    long    offset;

    offset = (f->curp - f->startp);
    offset += f->offset;
    return offset;
}

/****************************************************************************
REMARKS:
VDD implementation of the ANSI C feof function.
****************************************************************************/
int feof(
    FILE *f)
{
    return (f->offset == f->filesize);
}

/****************************************************************************
REMARKS:
Read a single character from the input file buffer, including translation
of the character in text transation modes.
****************************************************************************/
static int __getc(
    FILE *f)
{
    int c;

    if (f->unputc != EOF) {
        c = f->unputc;
        f->unputc = EOF;
        }
    else {
        if (f->startp == f->curp) {
            int bytes = fread(f->buf,1,sizeof(f->buf),f);
            if (bytes == 0)
                return EOF;
            f->curp = f->startp + bytes;
            }
        c = *f->startp++;
        if (f->text && c == '\r') {
            int nc = __getc(f);
            if (nc != '\n')
                f->unputc = nc;
            }
        }
    return c;
}

/****************************************************************************
REMARKS:
Write a single character from to input buffer, including translation of the
character in text transation modes.
****************************************************************************/
static int __putc(int c,FILE *f)
{
    int count = 1;
    if (f->text && c == '\n') {
        __putc('\r',f);
        count = 2;
        }
    if (f->curp == f->endp)
        fflush(f);
    *f->curp++ = c;
    return count;
}

/****************************************************************************
REMARKS:
VxD implementation of the ANSI C fgets function.
****************************************************************************/
char *fgets(
    char *s,
    int n,
    FILE *f)
{
    int c = 0;
    char *cs;

    cs = s;
    while (--n > 0 && (c = __getc(f)) != EOF) {
        *cs++ = c;
        if (c == '\n')
            break;
        }
    if (c == EOF && cs == s)
        return NULL;
    *cs = '\0';
    return s;
}

/****************************************************************************
REMARKS:
VxD implementation of the ANSI C fputs function.
****************************************************************************/
int fputs(
    const char *s,
    FILE *f)
{
    int r = 0;
    int c;

    while ((c = *s++) != 0)
        r = __putc(c, f);
    return r;
}

/****************************************************************************
REMARKS:
VxD implementation of the ANSI C fclose function.
****************************************************************************/
int fclose(
    FILE *f)
{
    fflush(f);
    _OS2Close((HFILE)f->handle);
    PM_free(f);
    return 0;
}