Skip to content

Instantly share code, notes, and snippets.

@MaddTheSane
Created August 1, 2016 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaddTheSane/8f284883416b677358836753b8287c9f to your computer and use it in GitHub Desktop.
Save MaddTheSane/8f284883416b677358836753b8287c9f to your computer and use it in GitHub Desktop.
A callback for CGDataProvider around the file pointer
#include <CoreGraphics/CoreGraphics.h>
static size_t FILEGetBytesCallback(void * __nullable info, void * buffer, size_t count)
{
if (info == NULL) {
return 0;
}
FILE *fInfo = info;
size_t retVal = fread(buffer, 1, count, fInfo);
return retVal;
}
static off_t FILESkipForwardCallback(void * __nullable info, off_t count)
{
if (info == NULL) {
return 0;
}
FILE *fInfo = info;
off_t start_position = ftello(fInfo);
fseeko(fInfo, count, SEEK_CUR);
off_t end_position = ftello(fInfo);
return (end_position - start_position);
}
static void FILERewindCallback(void *info)
{
if (info == NULL) {
return;
}
FILE *fInfo = info;
rewind(fInfo);
}
static void FILEReleaseCallback(void *info)
{
if (info == NULL) {
return;
}
FILE *fInfo = info;
fclose(fInfo);
}
const CGDataProviderSequentialCallbacks FILECallback = {
0,
FILEGetBytesCallback,
FILESkipForwardCallback,
FILERewindCallback,
FILEReleaseCallback
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment