funkalicious 0.1

read_binary_data.h File Reference

#include <glib-2.0/glib.h>
#include <gio/gio.h>
#include <libdotcode/binary_data_common.h>
Include dependency graph for read_binary_data.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

struct dotcode_datadotcode_read_binary_data (GFile *file, GError **err)
char * dotcode_read_sstring (gboolean little_endian, GInputStream *is, GError **error)
int dotcode_read_int (gboolean little_endian, GInputStream *is, GError **error)
double dotcode_read_float8 (gboolean little_endian, GInputStream *is, GError **error)
float dotcode_read_float4 (gboolean little_endian, GInputStream *is, GError **error)
struct dotcode_vectordotcode_read_vector (gboolean little_endian, enum dotcode_type t, GInputStream *is, GError **error)
struct dotcode_arraydotcode_read_array (gboolean little_endian, enum dotcode_type t, GInputStream *is, GError **error)
struct dotcode_griddotcode_read_grid (gboolean little_endian, GInputStream *is, GError **err)
struct dotcode_tensordotcode_read_tensor (gboolean little_endian, GInputStream *is, GError **err)
void * read_dotcode_typed_file (enum dotcode_type t, GFile *infile, GError **err)
void * dotcode_read_generic (enum dotcode_type t, gboolean little_endian, GInputStream *is, GError **err)
void * read_dotcode_generic_file (enum dotcode_type *t, GFile *infile, GError **err)

Function Documentation

struct dotcode_array* dotcode_read_array ( gboolean  little_endian,
enum dotcode_type  t,
GInputStream *  is,
GError **  error 
) [read]

Definition at line 100 of file read_binary_data.c.

References DOTCODE_FLOAT4, DOTCODE_FLOAT8, DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, dotcode_read_float4(), dotcode_read_float8(), dotcode_read_int(), e, dotcode_array::imax, dotcode_array::imin, dotcode_array::storage, dotcode_array::t, and unlikely.

Referenced by dotcode_internal_read_gridspec(), and dotcode_read_generic().

                                                                                                                        {
  struct dotcode_array *arr = (struct dotcode_array*)malloc(sizeof(struct dotcode_array));
  if(arr == NULL) {
    *error = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, 1, "dotcode::read_binary_data::read_array: ERROR failed to allocate storage for a new array.");
    return NULL;
  }
  arr->t = t;
  GError *e = NULL;
  arr->imin = dotcode_read_int(little_endian, is, &e);
  if(unlikely(e != NULL)) {
    free(arr);
    *error = e;
    return NULL;
  }
  arr->imax = dotcode_read_int(little_endian, is, &e);
  if(unlikely(e != NULL)) {
    free(arr);
    *error = e;
    return NULL;
  }
  /*Error checking: make sure min/max are sane.*/
  if(arr->imin >= arr->imax) {
    *error = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, 1, "dotcode::read_binary_data::read_array: ERROR invalid min/max: imin=%d >= imax=%d.", arr->imin, arr->imax);
    free(arr);
    return NULL;
  }
  //fprintf(stderr, "read_array: Got new array: imin=%d imax=%d:\n", arr->imin, arr->imax);
  /*Allocate the storage*/
  switch(t) {
  case DOTCODE_FLOAT8:
    arr->storage = malloc(sizeof(double)*(arr->imax-arr->imin + 1));
    break;
  case DOTCODE_FLOAT4:
    arr->storage = malloc(sizeof(float)*(arr->imax-arr->imin + 1));
    break;
  default:
    free(arr);
    *error = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, 3, "dotcode::read_binary_data::read_array_data: ERROR couldn't recognize type %d.", t);
    return NULL;
  }
  if(arr->storage == NULL) {
    *error = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, 1, "dotcode::read_binary_data::read_array: ERROR failed to allocate storage for the array data.");
    free(arr);
    return NULL;
  }
  for(int i=0; i<(arr->imax - arr->imin + 1); i++) {
    switch(t) {
    case DOTCODE_FLOAT8:
      ((double*)(arr->storage))[i] = dotcode_read_float8(little_endian, is, &e);
      //fprintf(stderr, "read_array: Got double value %d of %g\n", i, ((double*)(arr->storage))[i]);
      break;
    case DOTCODE_FLOAT4:
      ((float*)(arr->storage))[i] = dotcode_read_float4(little_endian, is, &e);
      //fprintf(stderr, "read_array: Got float value %d of %g\n", i, ((float*)(arr->storage))[i]);
      break;
    default:
      break;
    }
    if(unlikely(e != NULL)) {
      free(arr->storage);
      free(arr);
      *error = e;
      return NULL;
    }
  }
  return arr;
}

Here is the call graph for this function:

Here is the caller graph for this function:

struct dotcode_data* dotcode_read_binary_data ( GFile *  file,
GError **  err 
) [read]

Definition at line 39 of file read_binary_data.c.

                                                                         {
  return NULL;
             }
float dotcode_read_float4 ( gboolean  little_endian,
GInputStream *  is,
GError **  error 
)

Definition at line 254 of file read_binary_data.c.

References DOTCODE_FLOAT4_SIZE, DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, e, and flip_endian().

Referenced by dotcode_read_array(), dotcode_read_generic(), dotcode_read_tensor(), and dotcode_read_vector().

                                                                                    {
  gsize size;
  union float4_union_ {
    char buffer[DOTCODE_FLOAT4_SIZE];
    float fval;
  } float4_union;
  GError *e = NULL;
  if(!g_input_stream_read_all(is, float4_union.buffer, DOTCODE_FLOAT4_SIZE, &size, NULL, &e)) {
    *error = e;
    return -1;
  }
  if(size != DOTCODE_FLOAT4_SIZE) {
    *error = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, 1, "dotcode::read_binary_data::read_int: ERROR wanted %d bytes, but got %lu.", DOTCODE_FLOAT4_SIZE, (long unsigned int)size);
    return -1;
  }
#ifdef WORDS_BIGENDIAN
  if(little_endian)
#else
    if(!little_endian)
#endif
      {
  /*Other endianness*/
  flip_endian(float4_union.buffer, DOTCODE_FLOAT4_SIZE);
      }
  return float4_union.fval;
}

Here is the call graph for this function:

Here is the caller graph for this function:

double dotcode_read_float8 ( gboolean  little_endian,
GInputStream *  is,
GError **  error 
)

Definition at line 227 of file read_binary_data.c.

References DOTCODE_FLOAT8_SIZE, DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, e, and flip_endian().

Referenced by dotcode_grid1_get_from_gfile(), dotcode_read_array(), dotcode_read_generic(), dotcode_read_tensor(), and dotcode_read_vector().

                                                                                     {
  gsize size;
  union float8_union_ {
    char buffer[DOTCODE_FLOAT8_SIZE];
    double dval;
  } float8_union;
  GError *e = NULL;
  if(!g_input_stream_read_all(is, float8_union.buffer, DOTCODE_FLOAT8_SIZE, &size, NULL, &e)) {
    *error = e;
    return -1;
  }
  if(size != DOTCODE_FLOAT8_SIZE) {
    *error = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, 1, "dotcode::read_binary_data::read_int: ERROR wanted %d bytes, but got %lu.", DOTCODE_FLOAT8_SIZE, (long unsigned int)size);
    return -1;
  }
#ifdef WORDS_BIGENDIAN
  if(little_endian)
#else
    if(!little_endian)
#endif
      {
  /*Other endianness*/
  flip_endian(float8_union.buffer, DOTCODE_FLOAT8_SIZE);
      }
  return float8_union.dval;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void* dotcode_read_generic ( enum dotcode_type  t,
gboolean  little_endian,
GInputStream *  is,
GError **  err 
)

Definition at line 696 of file read_binary_data.c.

References DOTCODE_ARRAY, DOTCODE_ERROR_MALLOCFAIL, DOTCODE_FLOAT4, DOTCODE_FLOAT8, DOTCODE_GRID, DOTCODE_INT, dotcode_read_array(), DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, dotcode_read_float4(), dotcode_read_float8(), dotcode_read_grid(), dotcode_read_int(), dotcode_read_tensor(), dotcode_read_vector(), dotcode_string_from_type2(), DOTCODE_TENSOR, DOTCODE_VECTOR, and unlikely.

Referenced by read_dotcode_generic_file(), and read_dotcode_typed_file().

                                                                                                        {
  double* i = NULL;
  float* j = NULL;
  int* k = NULL;
  switch(t) {
  case DOTCODE_FLOAT8:
    i = (double*)malloc(sizeof(double));
    if(unlikely(i==NULL)) {
      *err = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, DOTCODE_ERROR_MALLOCFAIL, "libdotcode::read_binary_data::dotcode_read_generic: ERROR: failed to allocate memory to hold a double.");
      return NULL;
    }
    *i = dotcode_read_float8(little_endian, is, err);
    return (void*)i;
  case DOTCODE_FLOAT4:
    j = (float*)malloc(sizeof(float));
    if(unlikely(j==NULL)) {
      *err = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, DOTCODE_ERROR_MALLOCFAIL, "libdotcode::read_binary_data::dotcode_read_generic: ERROR: failed to allocate memory to hold a float.");
      return NULL;
    }
    *j = dotcode_read_float4(little_endian, is, err);
    return (void*)j;
  case DOTCODE_INT:
    k = (int*)malloc(sizeof(int));
    if(unlikely(k==NULL)) {
      *err = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, DOTCODE_ERROR_MALLOCFAIL, "libdotcode::read_binary_data::dotcode_read_generic: ERROR: failed to allocate memory to hold an int.");
      return NULL;
    }
    *k = dotcode_read_int(little_endian, is, err);
    return (void*)k;
  case DOTCODE_VECTOR:
    return (void*)dotcode_read_vector(little_endian, t, is, err);
  case DOTCODE_ARRAY:
    return (void*)dotcode_read_array(little_endian, t, is, err);
  case DOTCODE_TENSOR:
    return (void*)dotcode_read_tensor(little_endian, is, err);
  case DOTCODE_GRID:
    return (void*)dotcode_read_grid(little_endian, is, err);
  default:
    *err = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, 171, "libdotcode::read_binary_data::read_dotcode_read_generic: Can't recognize dotcode data type (%d/%s)", t, dotcode_string_from_type2(t));
    return NULL;
  }
  return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

struct dotcode_grid* dotcode_read_grid ( gboolean  little_endian,
GInputStream *  is,
GError **  err 
) [read]

Definition at line 501 of file read_binary_data.c.

References dotcode_grid::data, DOTCODE_ARRAY, DOTCODE_ERROR_MALLOCFAIL, dotcode_free_type(), dotcode_internal_read_gridspec(), DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, dotcode_read_tensor(), e, dotcode_gridspec::gridsites, dotcode_grid::gridsites, dotcode_grid::Ng, dotcode_gridspec::Ng_, and unlikely.

Referenced by dotcode_read_generic().

                                                                                               {
  GError *e = NULL;
  //fprintf(stderr, "dotcode_read_grid()\n");
  struct dotcode_grid* g = (struct dotcode_grid*)malloc(sizeof(struct dotcode_grid));
  if(g == NULL) {
    *err = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, DOTCODE_ERROR_MALLOCFAIL, "dotcode::read_binary_data::read_grid: ERROR failed to allocate storage for a new sgrid.");
    return NULL;
  }
  //fprintf(stderr, "reading gridspec\n");
  struct dotcode_gridspec* gs = dotcode_internal_read_gridspec(little_endian, is, &e);
  if(unlikely(e != NULL)) {
    g_propagate_prefixed_error(err, e, "libdotcode::read_binary_data.c::dotcode_read_gridspec: error getting GridSpec.");
    free(g);
    return NULL;
  }
  /*Copy over the data that are needed, then free the structure.*/ 
 for(int i=0; i<3; i++) {
    g->Ng[i] = gs->Ng_[i];
    g->gridsites[i] = gs->gridsites[i];
    //fprintf(stderr, "copied Ng[%d], gridsites[%d]\n", i, i);
  }
  free(gs);
  g->data = dotcode_read_tensor(little_endian, is, &e);
  if(unlikely(e != NULL)) {
    GError *e2 = NULL;
    g_propagate_prefixed_error(&e2, e, "libdotcode::read_binary_data.c::dotcode_read_grid: Error reading tensor from input stream.");
    e=NULL;
    for(int i=0; i<3; i++) dotcode_free_type(DOTCODE_ARRAY, g->gridsites[i], 1, &e, TRUE);
    /*We could error in freeing the arrays; propagate error if this
      failed, else do a simple copy.*/
    if(unlikely(e != NULL)) {
      g_propagate_prefixed_error(err, e2, "libdotcode::read_binary_data.c::dotcode_read_grid: Failure while freeing structure after failing to get tensor (error message from free: %s)", e->message);
      g_error_free(e);
    }else{
      *err = e2;
    }
    free(g);
    return NULL;
  }
  return g;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int dotcode_read_int ( gboolean  little_endian,
GInputStream *  is,
GError **  error 
)

Definition at line 197 of file read_binary_data.c.

References DOTCODE_INT_SIZE, DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, e, and flip_endian().

Referenced by dotcode_grid1_get_from_gfile(), dotcode_internal_read_gridspec(), dotcode_read_array(), dotcode_read_generic(), dotcode_read_sstring(), dotcode_read_tensor(), and dotcode_read_vector().

                                                                               {
  gsize size;
  union int_union_ {
    char buffer[DOTCODE_INT_SIZE];
    int ival;
  } int_union;
  GError *e = NULL;
  if(!g_input_stream_read_all(is, int_union.buffer, DOTCODE_INT_SIZE, &size, NULL, &e)) {
    *error = e;
    return -1;
  }
  if(size != DOTCODE_INT_SIZE) {
    if(e != NULL) {
      g_propagate_prefixed_error(error, e, "dotcode::read_binary_data::read_int: ERROR wanted %d bytes, but got %lu.", DOTCODE_INT_SIZE, (long unsigned int)size);
    }else{
      *error = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, 1, "dotcode::read_binary_data::read_int: ERROR wanted %d bytes, but got %lu.", DOTCODE_INT_SIZE, (long unsigned int)size);    }
    return -1;
  }
#ifdef WORDS_BIGENDIAN
  if(little_endian)
#else
    if(!little_endian)
#endif
      {
  /*Other endianness*/
  flip_endian(int_union.buffer, DOTCODE_INT_SIZE);
      }
  return int_union.ival;
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* dotcode_read_sstring ( gboolean  little_endian,
GInputStream *  is,
GError **  error 
)

Definition at line 168 of file read_binary_data.c.

References DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, dotcode_read_int(), e, and unlikely.

Referenced by dotcode_grid1_get_from_gfile(), dotcode_internal_read_gridspec(), dotcode_read_tensor(), and read_dotcode_generic_file().

                                                                                     {
  GError *e = NULL;
  int N = dotcode_read_int(little_endian, is, &e);
  if(unlikely(e != NULL)) {
    *error = e;
    return NULL;
  }
  char *buffer = (char*)malloc(N+1);
  if(buffer == NULL) {
    *error = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, 1, "dotcode::read_binary_data::read_sstring: ERROR failed to allocate storage for a new sstring.");
    return NULL;
  }
  gsize size;
  e = NULL;
  if(!g_input_stream_read_all(is, buffer, N, &size, NULL, &e)) {
    *error = e;
    free(buffer);
    return NULL;
  }
  if(size != N) {
    *error = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, 1, "dotcode::read_binary_data::read_sstring: ERROR wanted %d bytes, but got %lu.", N, (long unsigned int)size);
    free(buffer);
    return NULL;
  }
  /*Ensure that we're NULL-terminated.*/
  buffer[N] = '\0';
  return buffer;
}

Here is the call graph for this function:

Here is the caller graph for this function:

struct dotcode_tensor* dotcode_read_tensor ( gboolean  little_endian,
GInputStream *  is,
GError **  err 
) [read]

Definition at line 281 of file read_binary_data.c.

References dotcode_tensor::data, dotcode_determine_templated_type(), DOTCODE_ERROR_MALLOCFAIL, DOTCODE_FLOAT4, DOTCODE_FLOAT8, DOTCODE_INT, DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, dotcode_read_float4(), dotcode_read_float8(), dotcode_read_int(), dotcode_read_sstring(), dotcode_string_from_type2(), DOTCODE_TENSOR, e, dotcode_tensor::indices, dotcode_tensor::metadata, dotcode_tensor::Nd, str, dotcode_tensor::t, and unlikely.

Referenced by dotcode_read_generic(), and dotcode_read_grid().

                                                                                                   {
  struct dotcode_tensor* t = (struct dotcode_tensor*)malloc(sizeof(struct dotcode_tensor));
  if(t == NULL) {
    *err = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, DOTCODE_ERROR_MALLOCFAIL, "dotcode::read_binary_data::read_tensor: ERROR failed to allocate storage for a new stensor.");
    return NULL;
  }
  char* str;
  GError *e = NULL;
  str = dotcode_read_sstring(little_endian, is, &e);
  if((str == NULL) || (e != NULL)) {
    g_propagate_prefixed_error(err, e, "dotcode::read_binary_data::read_tensor: ERROR: failed to read tensor type information from input stream.");
    free(t);
    return NULL;
  }
  //fprintf(stderr, "Tensor=%s\n", str);
  /*This is all just to parse out what type we have. Ugh.*/
  /*Rewrote to remove the annoying bit :)*/
  GList *typelist = NULL;
  dotcode_determine_templated_type(str, &typelist, &e);
  if(e != NULL) {
    g_propagate_prefixed_error(err, e, "libdotcode::read_binary_data.c::dotcode_read_tensor: Failed to get type from type template string.");
    free(t);
    free(str);
    return NULL;
  }
  if((*(enum dotcode_type*)(typelist->data)) != DOTCODE_TENSOR) {
    *err = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, 159, "dotcode::read_binary_data::dotcode_read_tensor: ERROR: main type in templated type string (%s) was of type %s, not Tensor!", str, dotcode_string_from_type2(*(enum dotcode_type*)(typelist->data)));
    free(t);
    free(str);
  }
  if(typelist->next == NULL) {
    *err = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, 160, "dotcode::read_binary_data::dotcode_read_tensor: ERROR: Tensor type in templated type string (%s) was not given!", str);
    free(t);
    free(str);
  }
  free(str);
  t->t = (*(enum dotcode_type*)(typelist->next->data));
  for(GList *l = typelist; l!=NULL; l=l->next) free(l->data);
  g_list_free(typelist);
  t->metadata = dotcode_read_sstring(little_endian, is, &e);
  if(unlikely(e != NULL)) {
    g_propagate_prefixed_error(err, e, "Failed to read tensor data type: first type of templated type (%s) was not a tensor! (it was %s)", str, dotcode_string_from_type2(*(enum dotcode_type*)(typelist->data)));
    free(t);
    return NULL;
  }
  //fprintf(stderr, "\tmetadata=(%s)\n", t->metadata);
  t->Nd = dotcode_read_int(little_endian, is, &e);
  if(unlikely(e != NULL)) {
    g_propagate_prefixed_error(err, e, "Failed to read in number of indices: ");
    free(t->metadata);
    free(t);
    return NULL;
  }
  t->indices = (int*)malloc(2*(t->Nd)*sizeof(int));
  if(t->indices == NULL) {
    *err = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, 157, "dotcode::read_binary_data::read_tensor: ERROR: failed to allocate memory for tensor indices.");
    free(t->metadata);
    free(t);
    return NULL;
  }
  long int n_items = 1;
  for(int i=0; i<(t->Nd); i++) {
    //fprintf(stderr, "t->Nd=%d i=%d\n", t->Nd, i);
    (t->indices)[2*i] = dotcode_read_int(little_endian, is, &e);
    //fprintf(stderr, "t->Nd=%d i=%d index[%d]=%d\n", t->Nd, i, 2*i, (t->indices)[2*i]);
    if(unlikely(e != NULL)) {
      g_propagate_prefixed_error(err, e, "Failed to read in tensor minimum index %d: ", i);
      free(t->indices);
      free(t->metadata);
      free(t);
      return NULL;
    }
    (t->indices)[2*i+1] = dotcode_read_int(little_endian, is, &e);
    //fprintf(stderr, "t->Nd=%d i=%d index[%d]=%d\n", t->Nd, i, 2*i+1, (t->indices)[2*i+1]); 
   if(unlikely(e != NULL)) {
      g_propagate_prefixed_error(err, e, "Failed to read in tensor maximum index %d: ", i);
      free(t->indices);
      free(t->metadata);
      free(t);
      return NULL;
    }
    //fprintf(stderr, "\t%d.min=%d\n", i, (t->indices)[2*i]);
    //fprintf(stderr, "\t%d.max=%d\n", i, (t->indices)[2*i+1]);
    n_items *= ((t->indices)[2*i+1] - (t->indices)[2*i] + 1);
    //fprintf(stderr, "\t%d.n_items=%ld\n", i, n_items);
  }
  size_t datum_size;
  switch(t->t) {
  case DOTCODE_FLOAT8:
    datum_size = sizeof(double);
    break;
  case DOTCODE_FLOAT4:
    datum_size = sizeof(float);
    break;
  case DOTCODE_INT:
    datum_size = sizeof(int);
    break;
  default:
    *err = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, 161, "dotcode::read_binary_data::read_tensor: (incident 1) ERROR: invalid tensor data type (%d)", t->t);
    free(t->indices);
    free(t->metadata);
    free(t);
    return NULL;
  }
  //fprintf(stderr, "Datum size for type %s: %ld times %ld data = %ld bytes\n", dotcode_string_from_type2(t->t), (long int) datum_size, n_items, n_items*datum_size);
  t->data = malloc(n_items*datum_size);
  if(t->data == NULL) {
    *err = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, 160, "dotcode::read_binary_data::read_tensor: ERROR: failed to allocate memory for tensor data.");
    free(t->indices);
    free(t->metadata);
    free(t);
    return NULL;
  }
  for(long int i=0; i<n_items; i++) {
    switch(t->t) {
    case DOTCODE_FLOAT8:
      ((double*)(t->data))[i] = dotcode_read_float8(little_endian, is, &e);
      //fprintf(stderr, "\tindex %ld=%g\n", i, ((double*)(t->data))[i]);
      break;
    case DOTCODE_FLOAT4:
      ((float*)(t->data))[i] = dotcode_read_float4(little_endian, is, &e);
      //fprintf(stderr, "\tindex %ld=%f\n", i, ((float*)(t->data))[i]);
      break;
    case DOTCODE_INT:
      ((int*)(t->data))[i] = dotcode_read_int(little_endian, is, &e);
      //fprintf(stderr, "\tindex %ld=%d\n", i, ((int*)(t->data))[i]);
      break;
    default:
      *err = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, 162, "dotcode::read_binary_data::read_tensor: (incident 2) ERROR: invalid tensor data type (%d)", t->t);
      free(t->indices);
      free(t->metadata);
      free(t);
      return NULL;
    }
    if(unlikely(e != NULL)) {
      g_propagate_prefixed_error(err, e, "Failed to read in %ld%s tensor datum: ", i, (i==1)?"st":(i==2)?"nd":(i==3)?"rd":"th");
      free(t->indices);
      free(t->metadata);
      free(t);
      return NULL;
    }
  }
  return t;
}

Here is the call graph for this function:

Here is the caller graph for this function:

struct dotcode_vector* dotcode_read_vector ( gboolean  little_endian,
enum dotcode_type  t,
GInputStream *  is,
GError **  error 
) [read]

Definition at line 43 of file read_binary_data.c.

References dotcode_vector::data, DOTCODE_FLOAT4, DOTCODE_FLOAT8, DOTCODE_INT, DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, dotcode_read_float4(), dotcode_read_float8(), dotcode_read_int(), e, dotcode_vector::t, and unlikely.

Referenced by dotcode_read_generic().

                                                                                                                          {
  struct dotcode_vector *vec = (struct dotcode_vector*)malloc(sizeof(struct dotcode_vector));
  if(vec == NULL) {
    *error = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, 1, "dotcode::read_binary_data::read_vector: ERROR failed to allocate storage for a new vector.");
    return NULL;
  }
  vec->t = t;
  GError *e = NULL;
  size_t datum_size;
  /*Allocate the storage*/
  switch(t) {
  case DOTCODE_FLOAT8:
    datum_size = sizeof(double);
    break;
  case DOTCODE_FLOAT4:
    datum_size = sizeof(float);
    break;
  case DOTCODE_INT:
    datum_size = sizeof(int);
    break;
  default:
    free(vec);
    *error = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, 3, "dotcode::read_binary_data::read_float: ERROR couldn't recognize type %d.", t);
    return NULL;
  }
  vec->data = malloc(3*datum_size);
  if(vec->data == NULL) {
    *error = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, 1, "dotcode::read_binary_data::read_vector: ERROR failed to allocate storage for the vector data.");
    free(vec);
    return NULL;
  }
  for(int i=0; i<3; i++) {
    switch(t) {
    case DOTCODE_FLOAT8:
      ((double*)(vec->data))[i] = dotcode_read_float8(little_endian, is, &e);
      break;
    case DOTCODE_FLOAT4:
      ((float*)(vec->data))[i] = dotcode_read_float4(little_endian, is, &e);
      break;
    case DOTCODE_INT:
      ((int*)(vec->data))[i] = dotcode_read_int(little_endian, is, &e);
      break;
    default:
      break;
      *error = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, 27, "dotcode::read_binary_data::read_vector: ERROR unkown dotcode type %d", t);
    }
    if(unlikely(e != NULL)) break;
  }
  if(unlikely(e != NULL)) {
    free(vec->data);
    free(vec);
    *error = e;
    return NULL;
  }
  return vec;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void* read_dotcode_generic_file ( enum dotcode_type t,
GFile *  infile,
GError **  err 
)

Call read_dotcode_generic_file if the data type header has been written.

Definition at line 600 of file read_binary_data.c.

References c, DOTCODE_ARRAY, dotcode_determine_templated_type(), DOTCODE_FLOAT8, dotcode_free_type(), DOTCODE_GRID, DOTCODE_INVALID_TYPE, DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, dotcode_read_generic(), dotcode_read_sstring(), dotcode_string_from_type2(), DOTCODE_TENSOR, e, str, and unlikely.

Referenced by main(), read_mone_10x10x10_float8_grid(), read_one_10x10x10_float8_grid(), read_random_10x10x10_float8_grid(), and read_zero_10x10x10_float8_grid().

                                                                                   {
  GError *e = NULL;
  GFileInputStream *fis = g_file_read(infile, NULL, &e);
  //fprintf(stderr, "read_dotcode_generic_file()\n");
  if(unlikely(e != NULL)) {
    g_propagate_prefixed_error(err, e, "libdotcode::read_binary_data::read_dotcode_generic_file: Error opening dotcode file for reading.");
    return NULL;
  }
  char c;
  gboolean little_endian;
  gsize b_read;
  g_input_stream_read_all((GInputStream*)fis, &c, 1, &b_read, NULL, &e);
  if(unlikely((e != NULL) || (b_read != 1))) {
    if(e == NULL) {
      e = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, 172, "libdotcode::read_binary_data::read_dotcode_generic_file: Error reading endianness from file: read non-1 (more than or less than 1) bytes from file (read %ld bytes).", (long int)b_read);
    }
    g_propagate_prefixed_error(err, e, "libdotcode::read_binary_data::read_dotcode_generic_file: Error reading endianness from file.");
    return NULL;
  }
  switch(c) {
  case 'l':
    little_endian = TRUE;
    break;
  case 'b':
    little_endian = FALSE;
    break;
  default:
    *err = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, 170, "libdotcode::read_binary_data::read_dotcode_generic_file: Invalid file endianness: %c (should be l or b)\n", c);
    return NULL;
  }
  //fprintf(stderr, "little_endian: %s\n", (little_endian)?"TRUE":"FALSE");
  char* typestring = dotcode_read_sstring(little_endian, (GInputStream*)fis, &e);
  //fprintf(stderr, "got typestring: %s\n", typestring);
  if(e != NULL) {
    g_propagate_prefixed_error(err, e, "libdotcode::read_binary_data::read_dotcode_generic_file: ERROR getting type string from the dotcode file.");
    return NULL;
  }
  GList *tlist = NULL;
  dotcode_determine_templated_type(typestring, &tlist, &e);
  if(((*(enum dotcode_type*)(tlist->data)) == DOTCODE_INVALID_TYPE)
     || (tlist->next == NULL)
     || (e != NULL)) {
     char *str = NULL;
     if(e != NULL) {
       str = e->message;
     }
     *err = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, 175, "libdotcode::read_binary_data::read_dotcode_generic_file: Invalid dotcode data type header: %s. (error info from parser: %s)", typestring, str);
    free(typestring);
    for(GList *l = tlist; l!=NULL; l=l->next) free(l->data);
    g_list_free(tlist);
    if(e != NULL) g_error_free(e);
    return NULL;
  }
  *t = *(enum dotcode_type*)(tlist->data);
  switch(*t) {
  case DOTCODE_ARRAY:
  case DOTCODE_TENSOR:
  case DOTCODE_GRID:
    if((*(enum dotcode_type*)(tlist->next->data)) != DOTCODE_FLOAT8) {
      fprintf(stderr, "NOT DOTCODE_FLOAT8!!\n");
     *err = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, 175, "libdotcode::read_binary_data::read_dotcode_generic_file: Invalid dotcode data type header: %s: templated subtype was not float8!)", typestring);
     free(typestring);
     for(GList *l = tlist; l!=NULL; l=l->next) free(l->data);
     g_list_free(tlist);
     return NULL;
    }
  default:
    break;
  }
  for(GList *l = tlist; l!=NULL; l=l->next) free(l->data);
  g_list_free(tlist);
  free(typestring);
  void* val = dotcode_read_generic(*t, little_endian, (GInputStream*)fis, &e);
  if(unlikely(e != NULL)) {
    g_propagate_prefixed_error(err, e, "libdotcode::read_binary_data::read_dotcode_generic_file: ERROR reading file type (%s) from file.", dotcode_string_from_type2(*t));
    return NULL;
  }
  g_input_stream_close((GInputStream*)fis, NULL, &e);
  if(unlikely(e != NULL)) {
    GError *e2;
    g_propagate_prefixed_error(&e2, e, "libdotcode::read_binary_data::read_dotcode_generic_file: ERROR closing file (with type %s).", dotcode_string_from_type2(*t));
    e = NULL;
    dotcode_free_type(*t, val, 1, &e, TRUE);
    if(e != NULL) {
      g_propagate_prefixed_error(err, e2, "libdotcode::read_binary_data::read_dotcode_generic_file: ERROR freeing value (type %d/%s) after failing to close the input stream (free error was %s)", *t, dotcode_string_from_type2(*t), e->message);
      g_error_free(e);
    }else{
      *err = e2;
    }
    return NULL;
  }
  g_object_unref(fis);
  return val;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void* read_dotcode_typed_file ( enum dotcode_type  t,
GFile *  infile,
GError **  err 
)

Call read_dotcode_typed_file if the data type header hasn't been written.

Definition at line 546 of file read_binary_data.c.

References c, dotcode_free_type(), DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, dotcode_read_generic(), dotcode_string_from_type2(), e, and unlikely.

                                                                                {
  GError *e = NULL;
  GFileInputStream *fis = g_file_read(infile, NULL, &e);
  if(unlikely(e != NULL)) {
    g_propagate_prefixed_error(err, e, "libdotcode::read_binary_data::read_dotcode_typed_file: Error opening dotcode file for reading.");
    return NULL;
  }
  char c;
  gboolean little_endian;
  gsize b_read;
  g_input_stream_read_all((GInputStream*)fis, &c, 1, &b_read, NULL, &e);
  if(unlikely((e != NULL) || (b_read != 1))) {
    if(e == NULL) {
      e = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, 172, "libdotcode::read_binary_data::read_dotcode_typed_file: Error reading endianness from file: read non-1 (more than or less than 1) bytes from file (read %ld bytes).", (long int)b_read);
    }
    g_propagate_prefixed_error(err, e, "libdotcode::read_binary_data::read_dotcode_typed_file: Error reading endianness from file.");
    return NULL;
  }
  switch(c) {
  case 'l':
    little_endian = TRUE;
    break;
  case 'b':
    little_endian = FALSE;
    break;
  default:
    *err = g_error_new(DOTCODE_READ_BINARY_DATA_GERROR_DOMAIN, 170, "libdotcode::read_binary_data::read_dotcode_typed_file: libdotcode::read_dotcode_typed_file:: Invalid file endianness: %c (should be l or b)\n", c);
    return NULL;
  }
  void* val = dotcode_read_generic(t, little_endian, (GInputStream*)fis, &e);
  if(unlikely(e != NULL)) {
    g_propagate_prefixed_error(err, e, "libdotcode::read_binary_data::read_dotcode_typed_file: ERROR reading file type (%s) from file.", dotcode_string_from_type2(t));
    return NULL;
  }
  g_input_stream_close((GInputStream*)fis, NULL, &e);
  if(unlikely(e != NULL)) {
    GError *e2;
    g_propagate_prefixed_error(&e2, e, "libdotcode::read_binary_data::read_dotcode_typed_file: ERROR closing file (with type %s).", dotcode_string_from_type2(t));
    e = NULL;
    dotcode_free_type(t, val, 1, &e, TRUE);
    if(e != NULL) {
      g_propagate_prefixed_error(err, e2, "libdotcode::read_binary_data::read_dotcode_typed_file: ERROR freeing value (type %d/%s) after failing to close the input stream (free error was %s)", t, dotcode_string_from_type2(t), e->message);
      g_error_free(e);
    }else{
      *err = e2;
    }
    return NULL;
  }
  g_object_unref(fis);
  return val;
}

Here is the call graph for this function:

 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines