This repository has been archived by the owner on Mar 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from xing/add_fmemopen
Inhibit warnings and add fmemopen to this project
- Loading branch information
Showing
5 changed files
with
100 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |