Back to index

plt-scheme  4.2.1
Classes | Defines | Typedefs | Functions
gd_io_dp.c File Reference
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "gd.h"
#include "gdhelpers.h"

Go to the source code of this file.

Classes

struct  dpStruct
struct  dpIOCtx

Defines

#define TRUE   1
#define FALSE   0

Typedefs

typedef struct dpStruct dynamicPtr
typedef struct dpIOCtx dpIOCtx
typedef struct dpIOCtxdpIOCtxPtr

Functions

static int allocDynamic (dynamicPtr *dp, int initialSize, void *data)
static int appendDynamic (dynamicPtr *dp, const void *src, int size)
static int gdReallocDynamic (dynamicPtr *dp, int required)
static int trimDynamic (dynamicPtr *dp)
static void gdFreeDynamicCtx (struct gdIOCtx *ctx)
static dynamicPtrnewDynamic (int initialSize, void *data)
static int dynamicPutbuf (struct gdIOCtx *, const void *, int)
static void dynamicPutchar (struct gdIOCtx *, int a)
static int dynamicGetbuf (gdIOCtxPtr ctx, void *buf, int len)
static int dynamicGetchar (gdIOCtxPtr ctx)
static int dynamicSeek (struct gdIOCtx *, const int)
static long dynamicTell (struct gdIOCtx *)
gdIOCtxgdNewDynamicCtx (int initialSize, void *data)
voidgdDPExtractData (struct gdIOCtx *ctx, int *size)

Class Documentation

struct dpStruct

Definition at line 33 of file gd_io_dp.c.

Class Members
void * data
int dataGood
int logicalSize
int pos
int realSize
struct dpIOCtx

Definition at line 43 of file gd_io_dp.c.

Collaboration diagram for dpIOCtx:
Class Members
gdIOCtx ctx
dynamicPtr * dp

Define Documentation

#define FALSE   0

Definition at line 30 of file gd_io_dp.c.

#define TRUE   1

Definition at line 29 of file gd_io_dp.c.


Typedef Documentation

Definition at line 50 of file gd_io_dp.c.


Function Documentation

static int allocDynamic ( dynamicPtr dp,
int  initialSize,
void data 
) [static]

Definition at line 316 of file gd_io_dp.c.

{

  if (data == NULL)
    {
      dp->logicalSize = 0;
      dp->dataGood = FALSE;
      dp->data = gdMalloc (initialSize);
    }
  else
    {
      dp->logicalSize = initialSize;
      dp->dataGood = TRUE;
      dp->data = data;
    }

  if (dp->data != NULL)
    {
      dp->realSize = initialSize;
      dp->dataGood = TRUE;
      dp->pos = 0;
      return TRUE;
    }
  else
    {
      dp->realSize = 0;
      return FALSE;
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int appendDynamic ( dynamicPtr dp,
const void src,
int  size 
) [static]

Definition at line 348 of file gd_io_dp.c.

{
  int bytesNeeded;
  char *tmp;

  if (!dp->dataGood)
    return FALSE;

/*  bytesNeeded = dp->logicalSize + size; */
  bytesNeeded = dp->pos + size;

  if (bytesNeeded > dp->realSize)
    {
      if (!gdReallocDynamic (dp, bytesNeeded * 2))
       {
         dp->dataGood = FALSE;
         return FALSE;
       }
    }

  /* if we get here, we can be sure that we have enough bytes
     to copy safely */
  /*printf("Mem OK Size: %d, Pos: %d\n", dp->realSize, dp->pos); */

  tmp = (char *) dp->data;
  memcpy ((void *) (tmp + (dp->pos)), src, size);
  dp->pos += size;

  if (dp->pos > dp->logicalSize)
    {
      dp->logicalSize = dp->pos;
    };

  return TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int dynamicGetbuf ( gdIOCtxPtr  ctx,
void buf,
int  len 
) [static]

Definition at line 262 of file gd_io_dp.c.

{
  int rlen, remain;
  dpIOCtxPtr dctx;
  dynamicPtr *dp;

  dctx = (dpIOCtxPtr) ctx;
  dp = dctx->dp;

  remain = dp->logicalSize - dp->pos;
  if (remain >= len)
    {
      rlen = len;
    }
  else
    {
      if (remain == 0)
       {
         return EOF;
       }
      rlen = remain;
    }

  memcpy (buf, (void *) ((char *) dp->data + dp->pos), rlen);
  dp->pos += rlen;

  return rlen;
}

Here is the caller graph for this function:

static int dynamicGetchar ( gdIOCtxPtr  ctx) [static]

Definition at line 292 of file gd_io_dp.c.

{
  unsigned char b;
  int rv;

  rv = dynamicGetbuf (ctx, &b, 1);

  if (rv != 1)
    {
      return EOF;
    }
  else
    {
      return b;                    /* (b & 0xff); */
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int dynamicPutbuf ( struct gdIOCtx ctx,
const void buf,
int  size 
) [static]

Definition at line 231 of file gd_io_dp.c.

{
  dpIOCtx *dctx;
  dctx = (dpIOCtx *) ctx;

  appendDynamic (dctx->dp, buf, size);

  if (dctx->dp->dataGood)
    {
      return size;
    }
  else
    {
      return -1;
    };

}

Here is the call graph for this function:

Here is the caller graph for this function:

static void dynamicPutchar ( struct gdIOCtx ctx,
int  a 
) [static]

Definition at line 250 of file gd_io_dp.c.

{
  unsigned char b;
  dpIOCtxPtr dctx;

  b = a;
  dctx = (dpIOCtxPtr) ctx;

  appendDynamic (dctx->dp, &b, 1);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int dynamicSeek ( struct gdIOCtx ctx,
const int  pos 
) [static]

Definition at line 175 of file gd_io_dp.c.

{
  int bytesNeeded;
  dynamicPtr *dp;
  dpIOCtx *dctx;

  dctx = (dpIOCtx *) ctx;
  dp = dctx->dp;

  if (!dp->dataGood)
    return FALSE;

  bytesNeeded = pos;
  if (bytesNeeded > dp->realSize)
    {
      if (!gdReallocDynamic (dp, dp->realSize * 2))
       {
         dp->dataGood = FALSE;
         return FALSE;
       }
    }

  /* if we get here, we can be sure that we have enough bytes
     to copy safely */

  /* Extend the logical size if we seek beyond EOF. */
  if (pos > dp->logicalSize)
    {
      dp->logicalSize = pos;
    };

  dp->pos = pos;

  return TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static long dynamicTell ( struct gdIOCtx ctx) [static]

Definition at line 166 of file gd_io_dp.c.

{
  dpIOCtx *dctx;

  dctx = (dpIOCtx *) ctx;
  return (dctx->dp->pos);
}

Here is the caller graph for this function:

void* gdDPExtractData ( struct gdIOCtx ctx,
int size 
)

Definition at line 107 of file gd_io_dp.c.

{
  dynamicPtr *dp;
  dpIOCtx *dctx;
  void *data;

  dctx = (dpIOCtx *) ctx;
  dp = dctx->dp;

  /* clean up the data block and return it */
  if (dp->dataGood)
    {
      trimDynamic (dp);
      *size = dp->logicalSize;
      data = dp->data;
    }
  else
    {
      *size = 0;
      data = NULL;
      if (dp->data != NULL)
       {
         gdFree (dp->data);
       }
    }

  dp->data = NULL;
  dp->realSize = 0;
  dp->logicalSize = 0;

  return data;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void gdFreeDynamicCtx ( struct gdIOCtx ctx) [static]

Definition at line 141 of file gd_io_dp.c.

{
  dynamicPtr *dp;
  dpIOCtx *dctx;

  dctx = (dpIOCtx *) ctx;
  dp = dctx->dp;

  gdFree (ctx);

  /* clean up the data block and return it */
  if (dp->data != NULL)
    {
      gdFree (dp->data);
      dp->data = NULL;
    }

  dp->realSize = 0;
  dp->logicalSize = 0;

  gdFree (dp);

}

Here is the call graph for this function:

Here is the caller graph for this function:

gdIOCtx* gdNewDynamicCtx ( int  initialSize,
void data 
)

Definition at line 72 of file gd_io_dp.c.

{
  dpIOCtx *ctx;
  dynamicPtr *dp;

  ctx = (dpIOCtx *) gdMalloc (sizeof (dpIOCtx));
  if (ctx == NULL)
    {
      return NULL;
    }

  dp = newDynamic (initialSize, data);
  if (!dp)
    {
      gdFree (ctx);
      return NULL;
    };

  ctx->dp = dp;

  ctx->ctx.getC = dynamicGetchar;
  ctx->ctx.putC = dynamicPutchar;

  ctx->ctx.getBuf = dynamicGetbuf;
  ctx->ctx.putBuf = dynamicPutbuf;

  ctx->ctx.seek = dynamicSeek;
  ctx->ctx.tell = dynamicTell;

  ctx->ctx.gd_free = gdFreeDynamicCtx;

  return (gdIOCtx *) ctx;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int gdReallocDynamic ( dynamicPtr dp,
int  required 
) [static]

Definition at line 386 of file gd_io_dp.c.

{
  void *newPtr;

  /* First try gdRealloc().  If that doesn't work, make a new
     memory block and copy. */
  if ((newPtr = gdRealloc (dp->data, required)))
    {
      dp->realSize = required;
      dp->data = newPtr;
      return TRUE;
    }

  /* create a new pointer */
  newPtr = gdMalloc (required);
  if (!newPtr)
    {
      dp->dataGood = FALSE;
      return FALSE;
    }

  /* copy the old data into it */
  memcpy (newPtr, dp->data, dp->logicalSize);
  gdFree (dp->data);
  dp->data = newPtr;

  dp->realSize = required;
  return TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static dynamicPtr * newDynamic ( int  initialSize,
void data 
) [static]

Definition at line 213 of file gd_io_dp.c.

{
  dynamicPtr *dp;
  dp = (dynamicPtr *) gdMalloc (sizeof (dynamicPtr));
  if (dp == NULL)
    {
      return NULL;
    }

  if (!allocDynamic (dp, initialSize, data))
    return NULL;

  dp->pos = 0;

  return dp;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int trimDynamic ( dynamicPtr dp) [static]

Definition at line 418 of file gd_io_dp.c.

{
  return gdReallocDynamic (dp, dp->logicalSize);
}

Here is the call graph for this function:

Here is the caller graph for this function: