-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigfile.c
199 lines (168 loc) · 3.88 KB
/
bigfile.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
/*
* Copyright (C) 2002 John Todd Larason <jtl@molehill.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <stdlib.h>
#include <stdio.h>
#include "rtv.h"
#include "bigfile.h"
#ifdef WIN32
#define BIGFILE_BY_WINDOWS_UNIXISH_LL 1
#endif
#if BIGFILE_BY_STDIO
#include <errno.h>
struct big_file
{
FILE * fp;
};
BIGFILE * bfopen(const char * filename, const char * mode)
{
BIGFILE * stream;
stream = malloc(sizeof *stream);
if (!stream)
return NULL;
stream->fp = fopen(filename, mode);
if (!stream->fp) {
int e = errno;
free(stream);
errno = e;
return NULL;
}
return stream;
}
BIGFILE * bfreopen(FILE *fp)
{
BIGFILE * stream;
stream = malloc(sizeof *stream);
if (!stream)
return NULL;
stream->fp = fp;
return stream;
}
int bfseek(BIGFILE * stream, s64 offset, int whence)
{
return fseeko(stream->fp, offset, whence);
}
u64 bftell(BIGFILE * stream)
{
return ftello(stream->fp);
}
size_t bfread(void * ptr, size_t size, size_t nmemb, BIGFILE * stream)
{
return fread(ptr, size, nmemb, stream->fp);
}
size_t bfwrite(const void * ptr, size_t size, size_t nmemb, BIGFILE * stream)
{
return fwrite(ptr, size, nmemb, stream->fp);
}
int bfclose(BIGFILE *stream)
{
int r, e;
r = fclose(stream->fp);
e = errno;
free(stream);
errno = e;
return r;
}
#else
#ifdef BIGFILE_BY_WINDOWS_UNIXISH_LL
#include <IO.H>
#include <SYS\TYPES.H>
#include <FCNTL.H>
#include <SYS\STAT.H>
struct big_file
{
int fd;
};
BIGFILE * bfopen(const char * filename, const char * mode)
{
BIGFILE * stream;
int flag = 0;
stream = malloc(sizeof *stream);
if (!stream)
return NULL;
if (strcmp(mode, "r") == 0) {
flag |= _O_RDONLY;
} else if (strcmp(mode, "w") == 0) {
flag |= _O_WRONLY | _O_CREAT;
} else if (strcmp(mode, "w+") == 0) {
flag |= _O_RDWR | _O_CREAT;
} else {
fprintf(stderr, "bfopen: mode '%s' not implented\n", mode);
flag = O_RDWR;
}
flag |= _O_BINARY;
stream->fd = _open(filename, flag, _S_IREAD | _S_IWRITE);
if (stream->fd < 0) {
int e = errno;
free(stream);
errno = e;
return NULL;
}
return stream;
}
BIGFILE * bfreopen(FILE *fp)
{
BIGFILE * stream;
stream = malloc(sizeof *stream);
if (!stream)
return NULL;
stream->fd = _fileno(fp);
return stream;
}
int bfseek(BIGFILE * stream, s64 offset, int whence)
{
__int64 r;
r = _lseeki64(stream->fd, offset, whence);
if (r < 0)
return -1;
return 0;
}
u64 bftell(BIGFILE * stream)
{
return _telli64(stream->fd);
}
size_t bfread(void * ptr, size_t size, size_t nmemb, BIGFILE * stream)
{
size_t m;
unsigned char * p = ptr;
for (m = 0; m < nmemb; m++) {
if (_read(stream->fd, p, size) != size)
return m;
p += size;
}
return m;
}
size_t bfwrite(const void * ptr, size_t size, size_t nmemb, BIGFILE * stream)
{
size_t m;
unsigned char * p = ptr;
for (m = 0; m < nmemb; m++) {
if (_write(stream->fd, p, size) != size)
return m;
p += size;
}
return m;
}
int bfclose(BIGFILE *stream)
{
int r, e;
r = _close(stream->fd);
e = errno;
free(stream);
errno = e;
return r;
}
#else
#error "You need to choose a BIGFILE implementation or write one"
#endif
#endif