From 60b36b87e88b2638aa3631b2a791a0d0d08ab0e3 Mon Sep 17 00:00:00 2001 From: Piet Brauer Date: Thu, 26 Mar 2015 16:17:36 +0100 Subject: [PATCH] Inhibit warnings and add fmemopen to this project --- .gitignore | 11 +----- Example/Podfile.lock | 7 +--- XNGMarkdownParser.podspec | 3 +- src/fmemopen.c | 81 +++++++++++++++++++++++++++++++++++++++ src/fmemopen.h | 15 ++++++++ 5 files changed, 100 insertions(+), 17 deletions(-) create mode 100644 src/fmemopen.c create mode 100644 src/fmemopen.h diff --git a/.gitignore b/.gitignore index c98932f..f15bdf8 100644 --- a/.gitignore +++ b/.gitignore @@ -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/ diff --git a/Example/Podfile.lock b/Example/Podfile.lock index c934d02..3108182 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -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) @@ -17,7 +15,6 @@ EXTERNAL SOURCES: SPEC CHECKSUMS: Expecta: 03aabd0a89d8dea843baecb19a7fd7466a69a31d FBSnapshotTestCase: f9f225b5ba11c8d8c09075590490df16314e4d62 - fmemopen: 54fc0cc9d9063de6103b40a24849818a53c8f1e5 - XNGMarkdownParser: 562999edfa0e0913dc345f0931bdd78be59f826e + XNGMarkdownParser: 8ad6d21f972f2b2301637d842da2f7942faed5a0 COCOAPODS: 0.36.0 diff --git a/XNGMarkdownParser.podspec b/XNGMarkdownParser.podspec index fd8249a..76e9659 100644 --- a/XNGMarkdownParser.podspec +++ b/XNGMarkdownParser.podspec @@ -14,7 +14,6 @@ Pod::Spec.new do |s| s.author = { "XING AG" => "iosdev@xing.com" } 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 diff --git a/src/fmemopen.c b/src/fmemopen.c new file mode 100644 index 0000000..9ff8a4d --- /dev/null +++ b/src/fmemopen.c @@ -0,0 +1,81 @@ +#include +#include +#include +#include + +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); +} diff --git a/src/fmemopen.h b/src/fmemopen.h new file mode 100644 index 0000000..cfc53c0 --- /dev/null +++ b/src/fmemopen.h @@ -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