0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
fmemopen.c
Go to the documentation of this file.
1 //
2 // Copyright 2011-2014 NimbusKit
3 // Originally ported from https://github.com/ingenuitas/python-tesseract/blob/master/fmemopen.c
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/mman.h>
22 
23 struct fmem {
24  size_t pos;
25  size_t size;
26  char *buffer;
27 };
28 typedef struct fmem fmem_t;
29 
30 static int readfn(void *handler, char *buf, int size) {
31  fmem_t *mem = handler;
32  size_t available = mem->size - mem->pos;
33 
34  if (size > available) {
35  size = available;
36  }
37  memcpy(buf, mem->buffer + mem->pos, sizeof(char) * size);
38  mem->pos += size;
39 
40  return size;
41 }
42 
43 static int writefn(void *handler, const char *buf, int size) {
44  fmem_t *mem = handler;
45  size_t available = mem->size - mem->pos;
46 
47  if (size > available) {
48  size = available;
49  }
50  memcpy(mem->buffer + mem->pos, buf, sizeof(char) * size);
51  mem->pos += size;
52 
53  return size;
54 }
55 
56 static fpos_t seekfn(void *handler, fpos_t offset, int whence) {
57  size_t pos;
58  fmem_t *mem = handler;
59 
60  switch (whence) {
61  case SEEK_SET: {
62  if (offset >= 0) {
63  pos = (size_t)offset;
64  } else {
65  pos = 0;
66  }
67  break;
68  }
69  case SEEK_CUR: {
70  if (offset >= 0 || (size_t)(-offset) <= mem->pos) {
71  pos = mem->pos + (size_t)offset;
72  } else {
73  pos = 0;
74  }
75  break;
76  }
77  case SEEK_END: pos = mem->size + (size_t)offset; break;
78  default: return -1;
79  }
80 
81  if (pos > mem->size) {
82  return -1;
83  }
84 
85  mem->pos = pos;
86  return (fpos_t)pos;
87 }
88 
89 static int closefn(void *handler) {
90  free(handler);
91  return 0;
92 }
93 
94 FILE *fmemopen(void *buf, size_t size, const char *mode) {
95  // This data is released on fclose.
96  fmem_t* mem = (fmem_t *) malloc(sizeof(fmem_t));
97 
98  // Zero-out the structure.
99  memset(mem, 0, sizeof(fmem_t));
100 
101  mem->size = size;
102  mem->buffer = buf;
103 
104  // funopen's man page: https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/funopen.3.html
105  return funopen(mem, readfn, writefn, seekfn, closefn);
106 }
static fpos_t seekfn(void *handler, fpos_t offset, int whence)
Definition: fmemopen.c:56
size_t size
Definition: fmemopen.c:25
size_t pos
Definition: fmemopen.c:24
static int readfn(void *handler, char *buf, int size)
Definition: fmemopen.c:30
static int writefn(void *handler, const char *buf, int size)
Definition: fmemopen.c:43
FILE * fmemopen(void *buf, size_t size, const char *mode)
A BSD port of the fmemopen Linux method using funopen.
Definition: fmemopen.c:94
char * buffer
Definition: fmemopen.c:26
Definition: fmemopen.c:23
static int closefn(void *handler)
Definition: fmemopen.c:89