Skip to content

Commit

Permalink
Port fmemopen to NimbusKit.
Browse files Browse the repository at this point in the history
  • Loading branch information
jverkoey committed Feb 20, 2014
1 parent 44f91de commit fc285af
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
1 change: 1 addition & 0 deletions deps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"NimbusKit":[],"Frameworks":[]}
28 changes: 28 additions & 0 deletions src/NimbusMemoryMapping.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// Copyright 2011-2014 NimbusKit
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#ifdef NIMBUSKIT_FRAMEWORK
#import <NimbusKit/fmemopen.h>
#else
#import "fmemopen.h"
#endif

/**
* @defgroup NimbusMemoryMappping Nimbus Memory Mapping
*
* <div id="github" feature="memorymapping"></div>
*
*/
20 changes: 17 additions & 3 deletions fmemopen.c → src/fmemopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,23 @@ static fpos_t seekfn(void *handler, fpos_t offset, int whence) {
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;
case SEEK_SET: {
if (offset >= 0) {
pos = (size_t)offset;
} else {
pos = 0;
}
break;
}
case SEEK_CUR: {
if (offset >= 0 || (size_t)(-offset) <= mem->pos) {
pos = mem->pos + (size_t)offset;
} else {
pos = 0;
}
break;
}
case SEEK_END: pos = mem->size + (size_t)offset; break;
default: return -1;
}

Expand Down
18 changes: 9 additions & 9 deletions fmemopen.h → src/fmemopen.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
#ifndef FMEMOPEN_H_
#define FMEMOPEN_H_

#ifdef __cplusplus
extern "C"
{
#if defined __cplusplus
extern "C" {
#endif

/**
Expand All @@ -36,12 +35,13 @@ extern "C"
*
* You must call fclose on the returned file pointer or memory will be leaked.
*
* @param buf The data that will be used to back the FILE* methods. Must be at least
* @c size bytes.
* @param size The size of the @c buf data.
* @param mode The permitted stream operation modes.
* @returns A pointer that can be used in the fread/fwrite/fseek/fclose family of methods.
* If a failure occurred NULL will be returned.
* @param buf The data that will be used to back the FILE* methods. Must be at least
* @c size bytes.
* @param size The size of the @c buf data.
* @param mode The permitted stream operation modes.
* @return A pointer that can be used in the fread/fwrite/fseek/fclose family of methods.
* If a failure occurred NULL will be returned.
* @ingroup NimbusMemoryMappping
*/
FILE *fmemopen(void *buf, size_t size, const char *mode);

Expand Down

0 comments on commit fc285af

Please sign in to comment.