Skip to content

Commit bcb7099

Browse files
committed
Add fmemopen source.
1 parent 88fb4ba commit bcb7099

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

fmemopen.c

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//
2+
// Copyright 2012 Jeff Verkoeyen
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, 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, buf, sizeof(char) * size);
51+
52+
return size;
53+
}
54+
55+
static fpos_t seekfn(void *handler, fpos_t offset, int whence) {
56+
size_t pos;
57+
fmem_t *mem = handler;
58+
59+
switch (whence) {
60+
case SEEK_SET: pos = offset; break;
61+
case SEEK_CUR: pos = mem->pos + offset; break;
62+
case SEEK_END: pos = mem->size + offset; break;
63+
default: return -1;
64+
}
65+
66+
if (pos > mem->size) {
67+
return -1;
68+
}
69+
70+
mem->pos = pos;
71+
return (fpos_t)pos;
72+
}
73+
74+
static int closefn(void *handler) {
75+
free(handler);
76+
return 0;
77+
}
78+
79+
FILE *fmemopen(void *buf, size_t size, const char *mode) {
80+
// This data is released on fclose.
81+
fmem_t* mem = (fmem_t *) malloc(sizeof(fmem_t));
82+
83+
// Zero-out the structure.
84+
memset(mem, 0, sizeof(fmem_t));
85+
86+
mem->size = size;
87+
mem->buffer = buf;
88+
89+
// funopen's man page: https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/funopen.3.html
90+
return funopen(mem, readfn, writefn, seekfn, closefn);
91+
}

fmemopen.h

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// Copyright 2012 Jeff Verkoeyen
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+
#ifndef FMEMOPEN_H_
19+
#define FMEMOPEN_H_
20+
21+
/**
22+
* A BSD port of the fmemopen Linux method using funopen.
23+
*
24+
* man docs for fmemopen:
25+
* http://linux.die.net/man/3/fmemopen
26+
*
27+
* man docs for funopen:
28+
* https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/funopen.3.html
29+
*
30+
* This method is ported from ingenuitas' python-tesseract project.
31+
*
32+
* You must call fclose on the returned file pointer or memory will be leaked.
33+
*
34+
* @param buf The data that will be used to back the FILE* methods. Must be at least
35+
* @c size bytes.
36+
* @param size The size of the @c buf data.
37+
* @param mode The permitted stream operation modes.
38+
* @returns A pointer that can be used in the fread/fwrite/fseek/fclose family of methods.
39+
* If a failure occurred NULL will be returned.
40+
*/
41+
FILE *fmemopen(void *buf, size_t size, const char *mode);
42+
43+
#endif // #ifndef FMEMOPEN_H_

0 commit comments

Comments
 (0)