1 /*********************************************************************
2 Blosc - Blocked Shuffling and Compression Library
3
4 Copyright (C) 2021 The Blosc Developers <blosc@blosc.org>
5 https://blosc.org
6 License: BSD 3-Clause (see LICENSE.txt)
7
8 See LICENSE.txt for details about copyright and rights to use.
9 **********************************************************************/
10
11
12 #include "blosc2/blosc2-stdio.h"
13
blosc2_stdio_open(const char * urlpath,const char * mode,void * params)14 void *blosc2_stdio_open(const char *urlpath, const char *mode, void *params) {
15 FILE *file = fopen(urlpath, mode);
16 if (file == NULL)
17 return NULL;
18 blosc2_stdio_file *my_fp = malloc(sizeof(blosc2_stdio_file));
19 my_fp->file = file;
20 return my_fp;
21 }
22
blosc2_stdio_close(void * stream)23 int blosc2_stdio_close(void *stream) {
24 blosc2_stdio_file *my_fp = (blosc2_stdio_file *) stream;
25 int err = fclose(my_fp->file);
26 free(my_fp);
27 return err;
28 }
29
blosc2_stdio_tell(void * stream)30 int64_t blosc2_stdio_tell(void *stream) {
31 blosc2_stdio_file *my_fp = (blosc2_stdio_file *) stream;
32 int64_t pos;
33 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
34 pos = _ftelli64(my_fp->file);
35 #else
36 pos = (int64_t)ftell(my_fp->file);
37 #endif
38 return pos;
39 }
40
blosc2_stdio_seek(void * stream,int64_t offset,int whence)41 int blosc2_stdio_seek(void *stream, int64_t offset, int whence) {
42 blosc2_stdio_file *my_fp = (blosc2_stdio_file *) stream;
43 int rc;
44 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
45 rc = _fseeki64(my_fp->file, offset, whence);
46 #else
47 rc = fseek(my_fp->file, (long) offset, whence);
48 #endif
49 return rc;
50 }
51
blosc2_stdio_write(const void * ptr,int64_t size,int64_t nitems,void * stream)52 int64_t blosc2_stdio_write(const void *ptr, int64_t size, int64_t nitems, void *stream) {
53 blosc2_stdio_file *my_fp = (blosc2_stdio_file *) stream;
54
55 size_t nitems_ = fwrite(ptr, (size_t) size, (size_t) nitems, my_fp->file);
56 return (int64_t) nitems_;
57 }
58
blosc2_stdio_read(void * ptr,int64_t size,int64_t nitems,void * stream)59 int64_t blosc2_stdio_read(void *ptr, int64_t size, int64_t nitems, void *stream) {
60 blosc2_stdio_file *my_fp = (blosc2_stdio_file *) stream;
61 size_t nitems_ = fread(ptr, (size_t) size, (size_t) nitems, my_fp->file);
62 return (int64_t) nitems_;
63 }
64
blosc2_stdio_truncate(void * stream,int64_t size)65 int blosc2_stdio_truncate(void *stream, int64_t size) {
66 blosc2_stdio_file *my_fp = (blosc2_stdio_file *) stream;
67 int rc;
68 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
69 rc = _chsize_s(_fileno(my_fp->file), size);
70 #else
71 rc = ftruncate(fileno(my_fp->file), size);
72 #endif
73 return rc;
74 }
75