11#include "io.h"
22
3- /*
4- On-disk FILE, uncompressed
5- */
6-
3+ /*
4+ On-disk FILE, uncompressed
5+ */
76
87void * new_file_source (char * fname , size_t buffer_size ) {
9- file_source * fs = (file_source * ) malloc (sizeof (file_source ));
8+ file_source * fs = (file_source * )malloc (sizeof (file_source ));
109 fs -> fp = fopen (fname , "rb" );
1110
1211 if (fs -> fp == NULL ) {
@@ -18,7 +17,7 @@ void *new_file_source(char *fname, size_t buffer_size) {
1817 fs -> initial_file_pos = ftell (fs -> fp );
1918
2019 // Only allocate this heap memory if we are not memory-mapping the file
21- fs -> buffer = (char * ) malloc ((buffer_size + 1 ) * sizeof (char ));
20+ fs -> buffer = (char * ) malloc ((buffer_size + 1 ) * sizeof (char ));
2221
2322 if (fs -> buffer == NULL ) {
2423 return NULL ;
@@ -27,33 +26,19 @@ void *new_file_source(char *fname, size_t buffer_size) {
2726 memset (fs -> buffer , 0 , buffer_size + 1 );
2827 fs -> buffer [buffer_size ] = '\0' ;
2928
30- return (void * ) fs ;
29+ return (void * )fs ;
3130}
3231
33-
34- // XXX handle on systems without the capability
35-
36-
37- /*
38- * void *new_file_buffer(FILE *f, int buffer_size)
39- *
40- * Allocate a new file_buffer.
41- * Returns NULL if the memory allocation fails or if the call to mmap fails.
42- *
43- * buffer_size is ignored.
44- */
45-
46-
47- void * new_rd_source (PyObject * obj ) {
48- rd_source * rds = (rd_source * ) malloc (sizeof (rd_source ));
32+ void * new_rd_source (PyObject * obj ) {
33+ rd_source * rds = (rd_source * )malloc (sizeof (rd_source ));
4934
5035 /* hold on to this object */
5136 Py_INCREF (obj );
5237 rds -> obj = obj ;
5338 rds -> buffer = NULL ;
5439 rds -> position = 0 ;
5540
56- return (void * ) rds ;
41+ return (void * ) rds ;
5742}
5843
5944/*
@@ -63,9 +48,7 @@ void* new_rd_source(PyObject *obj) {
6348 */
6449
6550int del_file_source (void * fs ) {
66- // fseek(FS(fs)->fp, FS(fs)->initial_file_pos, SEEK_SET);
67- if (fs == NULL )
68- return 0 ;
51+ if (fs == NULL ) return 0 ;
6952
7053 /* allocated on the heap */
7154 free (FS (fs )-> buffer );
@@ -89,27 +72,23 @@ int del_rd_source(void *rds) {
8972
9073 */
9174
92-
93- void * buffer_file_bytes (void * source , size_t nbytes ,
94- size_t * bytes_read , int * status ) {
75+ void * buffer_file_bytes (void * source , size_t nbytes , size_t * bytes_read ,
76+ int * status ) {
9577 file_source * src = FS (source );
9678
97- * bytes_read = fread ((void * ) src -> buffer , sizeof (char ), nbytes ,
98- src -> fp );
79+ * bytes_read = fread ((void * )src -> buffer , sizeof (char ), nbytes , src -> fp );
9980
10081 if (* bytes_read == 0 ) {
10182 * status = REACHED_EOF ;
10283 } else {
10384 * status = 0 ;
10485 }
10586
106- return (void * ) src -> buffer ;
107-
87+ return (void * )src -> buffer ;
10888}
10989
110-
111- void * buffer_rd_bytes (void * source , size_t nbytes ,
112- size_t * bytes_read , int * status ) {
90+ void * buffer_rd_bytes (void * source , size_t nbytes , size_t * bytes_read ,
91+ int * status ) {
11392 PyGILState_STATE state ;
11493 PyObject * result , * func , * args , * tmp ;
11594
@@ -125,21 +104,18 @@ void* buffer_rd_bytes(void *source, size_t nbytes,
125104 args = Py_BuildValue ("(i)" , nbytes );
126105
127106 func = PyObject_GetAttrString (src -> obj , "read" );
128- /* printf("%s\n", PyBytes_AsString(PyObject_Repr(func))); */
129107
130108 /* TODO: does this release the GIL? */
131109 result = PyObject_CallObject (func , args );
132110 Py_XDECREF (args );
133111 Py_XDECREF (func );
134112
135- /* PyObject_Print(PyObject_Type(result), stdout, 0); */
136113 if (result == NULL ) {
137114 PyGILState_Release (state );
138115 * bytes_read = 0 ;
139116 * status = CALLING_READ_FAILED ;
140117 return NULL ;
141- }
142- else if (!PyBytes_Check (result )) {
118+ } else if (!PyBytes_Check (result )) {
143119 tmp = PyUnicode_AsUTF8String (result );
144120 Py_XDECREF (result );
145121 result = tmp ;
@@ -154,8 +130,7 @@ void* buffer_rd_bytes(void *source, size_t nbytes,
154130
155131 /* hang on to the Python object */
156132 src -> buffer = result ;
157- retval = (void * ) PyBytes_AsString (result );
158-
133+ retval = (void * )PyBytes_AsString (result );
159134
160135 PyGILState_Release (state );
161136
@@ -165,42 +140,38 @@ void* buffer_rd_bytes(void *source, size_t nbytes,
165140 return retval ;
166141}
167142
168-
169143#ifdef HAVE_MMAP
170144
171- #include <sys/stat.h>
172145#include <sys/mman.h>
146+ #include <sys/stat.h>
173147
174- void * new_mmap (char * fname )
175- {
148+ void * new_mmap (char * fname ) {
176149 struct stat buf ;
177150 int fd ;
178151 memory_map * mm ;
179- /* off_t position; */
180152 off_t filesize ;
181153
182- mm = (memory_map * ) malloc (sizeof (memory_map ));
154+ mm = (memory_map * )malloc (sizeof (memory_map ));
183155 mm -> fp = fopen (fname , "rb" );
184156
185157 fd = fileno (mm -> fp );
186158 if (fstat (fd , & buf ) == -1 ) {
187159 fprintf (stderr , "new_file_buffer: fstat() failed. errno =%d\n" , errno );
188160 return NULL ;
189161 }
190- filesize = buf .st_size ; /* XXX This might be 32 bits. */
191-
162+ filesize = buf .st_size ; /* XXX This might be 32 bits. */
192163
193164 if (mm == NULL ) {
194165 /* XXX Eventually remove this print statement. */
195166 fprintf (stderr , "new_file_buffer: malloc() failed.\n" );
196167 return NULL ;
197168 }
198- mm -> size = (off_t ) filesize ;
169+ mm -> size = (off_t )filesize ;
199170 mm -> line_number = 0 ;
200171
201172 mm -> fileno = fd ;
202173 mm -> position = ftell (mm -> fp );
203- mm -> last_pos = (off_t ) filesize ;
174+ mm -> last_pos = (off_t )filesize ;
204175
205176 mm -> memmap = mmap (NULL , filesize , PROT_READ , MAP_SHARED , fd , 0 );
206177 if (mm -> memmap == NULL ) {
@@ -210,30 +181,20 @@ void *new_mmap(char *fname)
210181 mm = NULL ;
211182 }
212183
213- return (void * ) mm ;
184+ return (void * ) mm ;
214185}
215186
216-
217- int del_mmap (void * src )
218- {
187+ int del_mmap (void * src ) {
219188 munmap (MM (src )-> memmap , MM (src )-> size );
220189
221190 fclose (MM (src )-> fp );
222-
223- /*
224- * With a memory mapped file, there is no need to do
225- * anything if restore == RESTORE_INITIAL.
226- */
227- /* if (restore == RESTORE_FINAL) { */
228- /* fseek(FB(fb)->file, FB(fb)->current_pos, SEEK_SET); */
229- /* } */
230191 free (src );
231192
232193 return 0 ;
233194}
234195
235- void * buffer_mmap_bytes (void * source , size_t nbytes ,
236- size_t * bytes_read , int * status ) {
196+ void * buffer_mmap_bytes (void * source , size_t nbytes , size_t * bytes_read ,
197+ int * status ) {
237198 void * retval ;
238199 memory_map * src = MM (source );
239200
@@ -264,19 +225,15 @@ void* buffer_mmap_bytes(void *source, size_t nbytes,
264225
265226/* kludgy */
266227
267- void * new_mmap (char * fname ) {
268- return NULL ;
269- }
228+ void * new_mmap (char * fname ) { return NULL ; }
270229
271- int del_mmap (void * src ) {
272- return 0 ;
273- }
230+ int del_mmap (void * src ) { return 0 ; }
274231
275232/* don't use this! */
276233
277- void * buffer_mmap_bytes (void * source , size_t nbytes ,
278- size_t * bytes_read , int * status ) {
279- return NULL ;
234+ void * buffer_mmap_bytes (void * source , size_t nbytes , size_t * bytes_read ,
235+ int * status ) {
236+ return NULL ;
280237}
281238
282239#endif
0 commit comments