Skip to content
This repository has been archived by the owner on Mar 8, 2019. It is now read-only.

Commit

Permalink
Merge pull request #8 from xing/add_fmemopen
Browse files Browse the repository at this point in the history
Inhibit warnings and add fmemopen to this project
  • Loading branch information
daehn committed Mar 26, 2015
2 parents 959ba4f + 60b36b8 commit b06b13b
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 17 deletions.
11 changes: 1 addition & 10 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,4 @@ DerivedData

Pods/

.idea/workspace.xml
.idea/tasks.xml

.idea/dataSources.ids
.idea/dataSources.xml
.idea/sqlDataSources.xml
.idea/dynamic.xml
*.ipr
*.iws
*.iml
.idea/
7 changes: 2 additions & 5 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
PODS:
- Expecta (0.3.1)
- FBSnapshotTestCase (1.4)
- fmemopen (0.0.1)
- XNGMarkdownParser (0.3.0):
- fmemopen
- XNGMarkdownParser (0.3.0)

DEPENDENCIES:
- Expecta (~> 0.3.1)
Expand All @@ -17,7 +15,6 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
Expecta: 03aabd0a89d8dea843baecb19a7fd7466a69a31d
FBSnapshotTestCase: f9f225b5ba11c8d8c09075590490df16314e4d62
fmemopen: 54fc0cc9d9063de6103b40a24849818a53c8f1e5
XNGMarkdownParser: 562999edfa0e0913dc345f0931bdd78be59f826e
XNGMarkdownParser: 8ad6d21f972f2b2301637d842da2f7942faed5a0

COCOAPODS: 0.36.0
3 changes: 1 addition & 2 deletions XNGMarkdownParser.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Pod::Spec.new do |s|
s.author = { "XING AG" => "[email protected]" }
s.platform = :ios, '7.0'
s.source = { :git => "https://github.com/xing/XNGMarkdownParser.git", :tag => s.version.to_s }
s.source_files = 'src/*.{h,m}'
s.dependency 'fmemopen'
s.source_files = 'src/*.{h,m,c}'
s.requires_arc = true
end
81 changes: 81 additions & 0 deletions src/fmemopen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>

struct fmem {
size_t pos;
size_t size;
char *buffer;
};
typedef struct fmem fmem_t;

static int readfn(void *handler, char *buf, int size) {
fmem_t *mem = handler;
size_t available = mem->size - mem->pos;

if (size > available) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wshorten-64-to-32"
size = available;
#pragma clang diagnostic pop
}
memcpy(buf, mem->buffer + mem->pos, sizeof(char) * size);
mem->pos += size;

return size;
}

static int writefn(void *handler, const char *buf, int size) {
fmem_t *mem = handler;
size_t available = mem->size - mem->pos;

if (size > available) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wshorten-64-to-32"
size = available;
#pragma clang diagnostic pop
}
memcpy(mem->buffer + mem->pos, buf, sizeof(char) * size);
mem->pos += size;

return size;
}

static fpos_t seekfn(void *handler, fpos_t offset, int whence) {
size_t pos;
fmem_t *mem = handler;

switch (whence) {
case SEEK_SET: pos = offset; break;
case SEEK_CUR: pos = mem->pos + offset; break;
case SEEK_END: pos = mem->size + offset; break;
default: return -1;
}

if (pos > mem->size) {
return -1;
}

mem->pos = pos;
return (fpos_t)pos;
}

static int closefn(void *handler) {
free(handler);
return 0;
}

FILE *fmemopen(void *buf, size_t size, const char *mode) {
// This data is released on fclose.
fmem_t* mem = (fmem_t *) malloc(sizeof(fmem_t));

// Zero-out the structure.
memset(mem, 0, sizeof(fmem_t));

mem->size = size;
mem->buffer = buf;

// funopen's man page: https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/funopen.3.html
return funopen(mem, readfn, writefn, seekfn, closefn);
}
15 changes: 15 additions & 0 deletions src/fmemopen.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef FMEMOPEN_H_
#define FMEMOPEN_H_

#ifdef __cplusplus
extern "C"
{
#endif

FILE *fmemopen(void *buf, size_t size, const char *mode);

#ifdef __cplusplus
}
#endif

#endif

0 comments on commit b06b13b

Please sign in to comment.