1- #include "io.h"
1+ /*
2+ Copyright (c) 2016, PyData Development Team
3+ All rights reserved.
4+
5+ Distributed under the terms of the BSD Simplified License.
6+
7+ The full license is in the LICENSE file, distributed with this software.
8+ */
29
3- /*
4- On-disk FILE, uncompressed
5- */
10+ #include "io.h"
611
12+ /*
13+ On-disk FILE, uncompressed
14+ */
715
816void * new_file_source (char * fname , size_t buffer_size ) {
9- file_source * fs = (file_source * ) malloc (sizeof (file_source ));
17+ file_source * fs = (file_source * )malloc (sizeof (file_source ));
1018 fs -> fp = fopen (fname , "rb" );
1119
1220 if (fs -> fp == NULL ) {
@@ -18,7 +26,7 @@ void *new_file_source(char *fname, size_t buffer_size) {
1826 fs -> initial_file_pos = ftell (fs -> fp );
1927
2028 // Only allocate this heap memory if we are not memory-mapping the file
21- fs -> buffer = (char * ) malloc ((buffer_size + 1 ) * sizeof (char ));
29+ fs -> buffer = (char * ) malloc ((buffer_size + 1 ) * sizeof (char ));
2230
2331 if (fs -> buffer == NULL ) {
2432 return NULL ;
@@ -27,33 +35,19 @@ void *new_file_source(char *fname, size_t buffer_size) {
2735 memset (fs -> buffer , 0 , buffer_size + 1 );
2836 fs -> buffer [buffer_size ] = '\0' ;
2937
30- return (void * ) fs ;
38+ return (void * )fs ;
3139}
3240
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 ));
41+ void * new_rd_source (PyObject * obj ) {
42+ rd_source * rds = (rd_source * )malloc (sizeof (rd_source ));
4943
5044 /* hold on to this object */
5145 Py_INCREF (obj );
5246 rds -> obj = obj ;
5347 rds -> buffer = NULL ;
5448 rds -> position = 0 ;
5549
56- return (void * ) rds ;
50+ return (void * ) rds ;
5751}
5852
5953/*
@@ -63,9 +57,7 @@ void* new_rd_source(PyObject *obj) {
6357 */
6458
6559int del_file_source (void * fs ) {
66- // fseek(FS(fs)->fp, FS(fs)->initial_file_pos, SEEK_SET);
67- if (fs == NULL )
68- return 0 ;
60+ if (fs == NULL ) return 0 ;
6961
7062 /* allocated on the heap */
7163 free (FS (fs )-> buffer );
@@ -89,27 +81,23 @@ int del_rd_source(void *rds) {
8981
9082 */
9183
92-
93- void * buffer_file_bytes (void * source , size_t nbytes ,
94- size_t * bytes_read , int * status ) {
84+ void * buffer_file_bytes (void * source , size_t nbytes , size_t * bytes_read ,
85+ int * status ) {
9586 file_source * src = FS (source );
9687
97- * bytes_read = fread ((void * ) src -> buffer , sizeof (char ), nbytes ,
98- src -> fp );
88+ * bytes_read = fread ((void * )src -> buffer , sizeof (char ), nbytes , src -> fp );
9989
10090 if (* bytes_read == 0 ) {
10191 * status = REACHED_EOF ;
10292 } else {
10393 * status = 0 ;
10494 }
10595
106- return (void * ) src -> buffer ;
107-
96+ return (void * )src -> buffer ;
10897}
10998
110-
111- void * buffer_rd_bytes (void * source , size_t nbytes ,
112- size_t * bytes_read , int * status ) {
99+ void * buffer_rd_bytes (void * source , size_t nbytes , size_t * bytes_read ,
100+ int * status ) {
113101 PyGILState_STATE state ;
114102 PyObject * result , * func , * args , * tmp ;
115103
@@ -125,21 +113,18 @@ void* buffer_rd_bytes(void *source, size_t nbytes,
125113 args = Py_BuildValue ("(i)" , nbytes );
126114
127115 func = PyObject_GetAttrString (src -> obj , "read" );
128- /* printf("%s\n", PyBytes_AsString(PyObject_Repr(func))); */
129116
130117 /* TODO: does this release the GIL? */
131118 result = PyObject_CallObject (func , args );
132119 Py_XDECREF (args );
133120 Py_XDECREF (func );
134121
135- /* PyObject_Print(PyObject_Type(result), stdout, 0); */
136122 if (result == NULL ) {
137123 PyGILState_Release (state );
138124 * bytes_read = 0 ;
139125 * status = CALLING_READ_FAILED ;
140126 return NULL ;
141- }
142- else if (!PyBytes_Check (result )) {
127+ } else if (!PyBytes_Check (result )) {
143128 tmp = PyUnicode_AsUTF8String (result );
144129 Py_XDECREF (result );
145130 result = tmp ;
@@ -154,8 +139,7 @@ void* buffer_rd_bytes(void *source, size_t nbytes,
154139
155140 /* hang on to the Python object */
156141 src -> buffer = result ;
157- retval = (void * ) PyBytes_AsString (result );
158-
142+ retval = (void * )PyBytes_AsString (result );
159143
160144 PyGILState_Release (state );
161145
@@ -165,42 +149,38 @@ void* buffer_rd_bytes(void *source, size_t nbytes,
165149 return retval ;
166150}
167151
168-
169152#ifdef HAVE_MMAP
170153
171- #include <sys/stat.h>
172154#include <sys/mman.h>
155+ #include <sys/stat.h>
173156
174- void * new_mmap (char * fname )
175- {
157+ void * new_mmap (char * fname ) {
176158 struct stat buf ;
177159 int fd ;
178160 memory_map * mm ;
179- /* off_t position; */
180161 off_t filesize ;
181162
182- mm = (memory_map * ) malloc (sizeof (memory_map ));
163+ mm = (memory_map * )malloc (sizeof (memory_map ));
183164 mm -> fp = fopen (fname , "rb" );
184165
185166 fd = fileno (mm -> fp );
186167 if (fstat (fd , & buf ) == -1 ) {
187168 fprintf (stderr , "new_file_buffer: fstat() failed. errno =%d\n" , errno );
188169 return NULL ;
189170 }
190- filesize = buf .st_size ; /* XXX This might be 32 bits. */
191-
171+ filesize = buf .st_size ; /* XXX This might be 32 bits. */
192172
193173 if (mm == NULL ) {
194174 /* XXX Eventually remove this print statement. */
195175 fprintf (stderr , "new_file_buffer: malloc() failed.\n" );
196176 return NULL ;
197177 }
198- mm -> size = (off_t ) filesize ;
178+ mm -> size = (off_t )filesize ;
199179 mm -> line_number = 0 ;
200180
201181 mm -> fileno = fd ;
202182 mm -> position = ftell (mm -> fp );
203- mm -> last_pos = (off_t ) filesize ;
183+ mm -> last_pos = (off_t )filesize ;
204184
205185 mm -> memmap = mmap (NULL , filesize , PROT_READ , MAP_SHARED , fd , 0 );
206186 if (mm -> memmap == NULL ) {
@@ -210,30 +190,20 @@ void *new_mmap(char *fname)
210190 mm = NULL ;
211191 }
212192
213- return (void * ) mm ;
193+ return (void * ) mm ;
214194}
215195
216-
217- int del_mmap (void * src )
218- {
196+ int del_mmap (void * src ) {
219197 munmap (MM (src )-> memmap , MM (src )-> size );
220198
221199 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- /* } */
230200 free (src );
231201
232202 return 0 ;
233203}
234204
235- void * buffer_mmap_bytes (void * source , size_t nbytes ,
236- size_t * bytes_read , int * status ) {
205+ void * buffer_mmap_bytes (void * source , size_t nbytes , size_t * bytes_read ,
206+ int * status ) {
237207 void * retval ;
238208 memory_map * src = MM (source );
239209
@@ -264,19 +234,15 @@ void* buffer_mmap_bytes(void *source, size_t nbytes,
264234
265235/* kludgy */
266236
267- void * new_mmap (char * fname ) {
268- return NULL ;
269- }
237+ void * new_mmap (char * fname ) { return NULL ; }
270238
271- int del_mmap (void * src ) {
272- return 0 ;
273- }
239+ int del_mmap (void * src ) { return 0 ; }
274240
275241/* don't use this! */
276242
277- void * buffer_mmap_bytes (void * source , size_t nbytes ,
278- size_t * bytes_read , int * status ) {
279- return NULL ;
243+ void * buffer_mmap_bytes (void * source , size_t nbytes , size_t * bytes_read ,
244+ int * status ) {
245+ return NULL ;
280246}
281247
282248#endif
0 commit comments