|
funkalicious 0.1
|
#include <string.h>#include <glib-2.0/glib.h>#include <gio/gio.h>#include "config.h"#include <libdotcode/dotcode_generic.h>#include <libdotcode/binary_data_common.h>#include "write_binary_data.h"#include "gcc_hints.h"
Include dependency graph for write_binary_data.c:Go to the source code of this file.
Defines | |
| #define | DOTCODE_WRITE_BINARY_DATA_GERROR_DOMAIN 7032 |
Functions | |
| GError * | dotcode_write_binary_data (const struct dotcode_data *data, GFile *file) |
| GError * | dotcode_write_typed_file (const enum dotcode_type t, const void *data, GFile *outfile) |
| GError * | dotcode_write_generic (const enum dotcode_type t, const void *data, gboolean write_header, GOutputStream *os) |
| GError * | dotcode_write_sstring (const char *str, gboolean write_header, GOutputStream *os) |
| GError * | dotcode_write_int (int val, gboolean write_header, GOutputStream *os) |
| GError * | dotcode_write_float8 (double val, gboolean write_header, GOutputStream *os) |
| GError * | dotcode_write_float4 (float val, gboolean write_header, GOutputStream *os) |
| GError * | dotcode_write_vector (const struct dotcode_vector *val, gboolean write_header, GOutputStream *os) |
| GError * | dotcode_write_array (const struct dotcode_array *val, gboolean write_header, GOutputStream *os) |
| GError * | dotcode_write_grid (const struct dotcode_grid *grid, gboolean write_header, GOutputStream *os) |
| GError * | dotcode_write_tensor (const struct dotcode_tensor *tensor, gboolean write_header, GOutputStream *os) |
| #define DOTCODE_WRITE_BINARY_DATA_GERROR_DOMAIN 7032 |
Definition at line 36 of file write_binary_data.c.
Referenced by dotcode_write_array(), dotcode_write_generic(), dotcode_write_tensor(), dotcode_write_typed_file(), and dotcode_write_vector().
| GError* dotcode_write_array | ( | const struct dotcode_array * | val, |
| gboolean | write_header, | ||
| GOutputStream * | os | ||
| ) |
Definition at line 217 of file write_binary_data.c.
References DOTCODE_ARRAY, DOTCODE_FLOAT4, DOTCODE_FLOAT8, DOTCODE_INT, dotcode_string_from_type(), dotcode_string_from_type2(), DOTCODE_WRITE_BINARY_DATA_GERROR_DOMAIN, dotcode_write_float4(), dotcode_write_float8(), dotcode_write_int(), dotcode_write_sstring(), e, dotcode_array::imax, dotcode_array::imin, dotcode_array::storage, str, and dotcode_array::t.
Referenced by dotcode_write_generic(), and dotcode_write_grid().
{
GError *err = NULL;
GError *e = NULL;
if(write_header) {
GString *s = g_string_new("");
g_string_printf(s, "%s<%s>", dotcode_string_from_type2(DOTCODE_ARRAY), dotcode_string_from_type2(val->t));
e = dotcode_write_sstring(s->str, FALSE, os);
g_string_free(s, TRUE);
if(e != NULL) return err;
}
e = dotcode_write_int(val->imin, FALSE, os);
if(e != NULL) {
g_propagate_prefixed_error(&err, e, "libdotcode::write_binary_data::write_array: error writing integer imin (%d) to output stream.", val->imin);
return err;
}
e = dotcode_write_int(val->imax, FALSE, os);
if(e != NULL) {
g_propagate_prefixed_error(&err, e, "libdotcode::write_binary_data::write_array: error writing integer imax (%d) to output stream.", val->imax);
return err;
}
char* str;
for(int i=0; i<((val->imax)-(val->imin)+1); i++) {
switch(val->t) {
case DOTCODE_FLOAT8:
e = dotcode_write_float8(((double*)(val->storage))[i], FALSE, os);
break;
case DOTCODE_FLOAT4:
e = dotcode_write_float4(((float*)(val->storage))[i], FALSE, os);
break;
case DOTCODE_INT:
e = dotcode_write_int(((int*)(val->storage))[i], FALSE, os);
break;
default:
return g_error_new(DOTCODE_WRITE_BINARY_DATA_GERROR_DOMAIN, 1, "libdotcode::write_binary_data::write_array: error writing array data to stream: unknown dotcode type %d (%s)", val->t, str=dotcode_string_from_type(val->t));
g_free(str);
}
if(e != NULL) {
g_propagate_prefixed_error(&err, e, "libdotcode::write_binary_data::write_array: error writing %dth (array index %d) to output stream.", i, i+val->imin);
return err;
}
}
return NULL;
}
Here is the call graph for this function:
Here is the caller graph for this function:| GError* dotcode_write_binary_data | ( | const struct dotcode_data * | data, |
| GFile * | file | ||
| ) |
Definition at line 38 of file write_binary_data.c.
{
return NULL;
}
| GError* dotcode_write_float4 | ( | float | val, |
| gboolean | write_header, | ||
| GOutputStream * | os | ||
| ) |
Definition at line 188 of file write_binary_data.c.
References DOTCODE_FLOAT4, DOTCODE_FLOAT4_SIZE, dotcode_string_from_type2(), dotcode_write_sstring(), and e.
Referenced by dotcode_write_array(), dotcode_write_generic(), and dotcode_write_tensor().
{
GError *err = NULL;
GError *e = NULL;
union float4_union_ {
char buffer[DOTCODE_FLOAT4_SIZE];
float val;
} the_union;
the_union.val = val;
gsize written;
if(write_header) {
e = dotcode_write_sstring(dotcode_string_from_type2(DOTCODE_FLOAT4), FALSE, os);
if(e != NULL) return err;
}
if((!g_output_stream_write_all(os, the_union.buffer, DOTCODE_FLOAT4_SIZE, &written, NULL, &e)) || (DOTCODE_FLOAT4_SIZE != written) || (e != NULL)) {
if(DOTCODE_FLOAT4_SIZE != written) {
g_propagate_prefixed_error(&err, e, "libdotcode::write_binary_data::write_float8: error writing float8: (%g) len(%d) and amount written(%lu) differ!", val, DOTCODE_FLOAT4_SIZE, (long unsigned int)written);
}else{
g_propagate_prefixed_error(&err, e, "libdotcode::write_binary_data::write_float8: error writing float8 %g!", val);
}
return err;
}
return NULL;
}
Here is the call graph for this function:
Here is the caller graph for this function:| GError* dotcode_write_float8 | ( | double | val, |
| gboolean | write_header, | ||
| GOutputStream * | os | ||
| ) |
Definition at line 163 of file write_binary_data.c.
References DOTCODE_FLOAT8, DOTCODE_FLOAT8_SIZE, dotcode_string_from_type2(), dotcode_write_sstring(), and e.
Referenced by dotcode_write_array(), dotcode_write_generic(), and dotcode_write_tensor().
{
GError *err = NULL;
GError *e = NULL;
union float8_union_ {
char buffer[DOTCODE_FLOAT8_SIZE];
double val;
} the_union;
the_union.val = val;
gsize written;
if(write_header) {
e = dotcode_write_sstring(dotcode_string_from_type2(DOTCODE_FLOAT8), FALSE, os);
if(e != NULL) return err;
}
if((!g_output_stream_write_all(os, the_union.buffer, DOTCODE_FLOAT8_SIZE, &written, NULL, &e)) || (DOTCODE_FLOAT8_SIZE != written) || (e != NULL)) {
if(DOTCODE_FLOAT8_SIZE != written) {
g_propagate_prefixed_error(&err, e, "libdotcode::write_binary_data::write_float8: error writing float8: (%g) len(%d) and amount written(%lu) differ!", val, DOTCODE_FLOAT8_SIZE, (long unsigned int)written);
}else{
g_propagate_prefixed_error(&err, e, "libdotcode::write_binary_data::write_float8: error writing float8 %g!", val);
}
return err;
}
return NULL;
}
Here is the call graph for this function:
Here is the caller graph for this function:| GError* dotcode_write_generic | ( | const enum dotcode_type | t, |
| const void * | data, | ||
| gboolean | write_header, | ||
| GOutputStream * | os | ||
| ) |
Definition at line 76 of file write_binary_data.c.
References DOTCODE_ARRAY, DOTCODE_FLOAT4, DOTCODE_FLOAT8, DOTCODE_GRID, DOTCODE_INT, dotcode_string_from_type2(), DOTCODE_TENSOR, DOTCODE_VECTOR, dotcode_write_array(), DOTCODE_WRITE_BINARY_DATA_GERROR_DOMAIN, dotcode_write_float4(), dotcode_write_float8(), dotcode_write_grid(), dotcode_write_int(), dotcode_write_tensor(), and dotcode_write_vector().
Referenced by dotcode_write_typed_file().
{
GError *err = NULL;
switch(t) {
case DOTCODE_FLOAT8:
err = dotcode_write_float8(*((double*)data), write_header, os);
break;
case DOTCODE_FLOAT4:
err = dotcode_write_float4((*(float*)data), write_header, os);
break;
case DOTCODE_INT:
err = dotcode_write_int(*((int*)data), write_header, os);
break;
case DOTCODE_VECTOR:
err = dotcode_write_vector((struct dotcode_vector*)data, write_header, os);
break;
case DOTCODE_ARRAY:
err = dotcode_write_array((struct dotcode_array*)data, write_header, os);
break;
case DOTCODE_TENSOR:
err = dotcode_write_tensor((struct dotcode_tensor*)data, write_header, os);
break;
case DOTCODE_GRID:
err = dotcode_write_grid((struct dotcode_grid*)data, write_header, os);
break;
default:
err = g_error_new(DOTCODE_WRITE_BINARY_DATA_GERROR_DOMAIN, 171, "libdotcode::read_binary_data::read_dotcode_typed_file: Can't recognize dotcode data type (%d/%s)", t, dotcode_string_from_type2(t));
return err;
}
return err;
}
Here is the call graph for this function:
Here is the caller graph for this function:| GError* dotcode_write_grid | ( | const struct dotcode_grid * | grid, |
| gboolean | write_header, | ||
| GOutputStream * | os | ||
| ) |
Definition at line 261 of file write_binary_data.c.
References dotcode_grid::data, dotcode_string_from_type2(), dotcode_write_array(), dotcode_write_int(), dotcode_write_sstring(), dotcode_write_tensor(), e, dotcode_grid::gridsites, dotcode_grid::Ng, and dotcode_array::t.
Referenced by dotcode_write_generic().
{
GError *err;
GError *e = NULL;
if(write_header) {
GString *s = g_string_new("");
g_string_printf(s, "Grid<%s>", dotcode_string_from_type2(grid->gridsites[0]->t));
e = dotcode_write_sstring(s->str, FALSE, os);
g_string_free(s, TRUE);
if(e != NULL) return err;
}
/*First, the gridspec*/
e = dotcode_write_sstring("GridSpec", FALSE, os);
if(e != NULL) {
g_propagate_prefixed_error(&err, e, "libdotcode::write_binary_data::write_grid: error writing \"GridSpec\" to output stream.");
return err;
}
e = dotcode_write_sstring("2.0", FALSE, os);
if(e != NULL) {
g_propagate_prefixed_error(&err, e, "libdotcode::write_binary_data::write_grid: error writing \"2.0\" to output stream.");
return err;
}
for(int i=0; i<3; i++) {
e = dotcode_write_array(grid->gridsites[i], FALSE, os);
if(e != NULL) {
g_propagate_prefixed_error(&err, e, "libdotcode::write_binary_data::write_grid: error writing index array %d to output stream.", i);
return err;
}
}
for(int i=0; i<3; i++) {
e = dotcode_write_int(grid->Ng[i], FALSE, os);
if(e != NULL) {
g_propagate_prefixed_error(&err, e, "libdotcode::write_binary_data::write_grid: error writing Ng[%d] to output stream.", i);
return err;
}
}
/*Then, the tensor*/
e = dotcode_write_tensor(grid->data, TRUE, os);
if(e != NULL) {
g_propagate_prefixed_error(&err, e, "libdotcode::write_binary_data::write_grid: error writing tensor to output stream.");
return err;
}
return NULL;
}
Here is the call graph for this function:
Here is the caller graph for this function:| GError* dotcode_write_int | ( | int | val, |
| gboolean | write_header, | ||
| GOutputStream * | os | ||
| ) |
Definition at line 138 of file write_binary_data.c.
References DOTCODE_INT, DOTCODE_INT_SIZE, dotcode_string_from_type2(), dotcode_write_sstring(), and e.
Referenced by dotcode_write_array(), dotcode_write_generic(), dotcode_write_grid(), dotcode_write_sstring(), and dotcode_write_tensor().
{
GError *err = NULL;
GError *e = NULL;
union int_union_ {
char buffer[DOTCODE_INT_SIZE];
int val;
} int_union;
int_union.val = val;
if(write_header) {
e = dotcode_write_sstring(dotcode_string_from_type2(DOTCODE_INT), FALSE, os);
if(e != NULL) return err;
}
gsize written;
if((!g_output_stream_write_all(os, int_union.buffer, DOTCODE_INT_SIZE, &written, NULL, &e)) || (DOTCODE_INT_SIZE != written) || (e != NULL)) {
if(DOTCODE_INT_SIZE != written) {
g_propagate_prefixed_error(&err, e, "libdotcode::write_binary_data::write_int: error writing int: (%d) len(%d) and amount written(%lu) differ!", val, DOTCODE_INT_SIZE, (long unsigned int)written);
}else{
g_propagate_prefixed_error(&err, e, "libdotcode::write_binary_data::write_int: error writing int %d!", val);
}
return err;
}
return NULL;
}
Here is the call graph for this function:
Here is the caller graph for this function:| GError* dotcode_write_sstring | ( | const char * | str, |
| gboolean | write_header, | ||
| GOutputStream * | os | ||
| ) |
Definition at line 108 of file write_binary_data.c.
References DOTCODE_SSTRING, dotcode_string_from_type2(), dotcode_write_int(), dotcode_write_sstring(), and e.
Referenced by dotcode_write_array(), dotcode_write_float4(), dotcode_write_float8(), dotcode_write_grid(), dotcode_write_int(), dotcode_write_sstring(), and dotcode_write_tensor().
{
GError* err=NULL;
/*We need to write out the trailing NULL. It's what Craig's code
does. WE DON'T RELY ON THE TRAILING NULL BEING IN THE STREAM,
THO. That way lies security vulnerabilities!*/
int len = strlen(str) + 1;
GError *e = NULL;
if(write_header) {
e = dotcode_write_sstring(dotcode_string_from_type2(DOTCODE_SSTRING), FALSE, os);
if(e != NULL) return err;
}
e = dotcode_write_int(len, FALSE, os);
if(e != NULL) {
g_propagate_prefixed_error(&err, e, "libdotcode::write_binary_data::write_sstring: error writing string length %d (%s)", len, str);
g_error_free(e);
return err;
}
gsize written;
if((!g_output_stream_write_all(os, str, len, &written, NULL, &e)) || (len != written) || (e != NULL)) {
if(len != written) {
g_propagate_prefixed_error(&err, e, "libdotcode::write_binary_data::write_sstring: error writing string: (%s) len(%d) and amount written(%lu) differ!", str, len, (long unsigned int)written);
}else{
g_propagate_prefixed_error(&err, e, "libdotcode::write_binary_data::write_sstring: error writing string %s!", str);
}
return err;
}
return NULL;
}
Here is the call graph for this function:
Here is the caller graph for this function:| GError* dotcode_write_tensor | ( | const struct dotcode_tensor * | tensor, |
| gboolean | write_header, | ||
| GOutputStream * | os | ||
| ) |
Definition at line 305 of file write_binary_data.c.
References dotcode_tensor::data, DOTCODE_FLOAT4, DOTCODE_FLOAT8, DOTCODE_INT, dotcode_string_from_type(), dotcode_string_from_type2(), DOTCODE_TENSOR, DOTCODE_WRITE_BINARY_DATA_GERROR_DOMAIN, dotcode_write_float4(), dotcode_write_float8(), dotcode_write_int(), dotcode_write_sstring(), e, dotcode_tensor::indices, dotcode_tensor::metadata, dotcode_tensor::Nd, str, and dotcode_tensor::t.
Referenced by dotcode_write_generic(), and dotcode_write_grid().
{
GError *err = NULL;
GError *e = NULL;
char* str;
if(write_header) {
GString *s = g_string_new("");
g_string_printf(s, "%s<%s>", dotcode_string_from_type2(DOTCODE_TENSOR), dotcode_string_from_type2(tensor->t));
e = dotcode_write_sstring(s->str, FALSE, os);
g_string_free(s, TRUE);
if(e != NULL) return err;
}
e = dotcode_write_sstring(tensor->metadata, FALSE, os);
if(e != NULL) {
g_propagate_prefixed_error(&err, e, "libdotcode::write_binary_data::write_tensor: error writing metadata (%s) to output stream", tensor->metadata);
return err;
}
e = dotcode_write_int(tensor->Nd, FALSE, os);
if(e != NULL) {
g_propagate_prefixed_error(&err, e, "libdotcode::write_binary_data::write_tensor: error writing number of dimensions (%d) to output stream", tensor->Nd);
return err;
}
for(int d=0; d<tensor->Nd; d++) {
for(int m=0; m<2; m++) {
e = dotcode_write_int(tensor->indices[m+2*d], FALSE, os);
if(e != NULL) {
g_propagate_prefixed_error(&err, e, "libdotcode::write_binary_data::write_tensor: error writing %s for dimension %d to output stream", m?"imax":"imin", tensor->Nd);
return err;
}
}
}
/*Walk through the array, writing the values. Since our in-memory
representation is identical to the on-disk representation, we're
good to go.*/
long unsigned int size=1;
for(int d=0; d<tensor->Nd; d++) size *= tensor->indices[1+2*d] - tensor->indices[2*d] + 1;
for(long unsigned int i=0; i<size; i++) {
switch(tensor->t) {
case DOTCODE_FLOAT8:
e = dotcode_write_float8(((double*)(tensor->data))[i], FALSE, os);
break;
case DOTCODE_FLOAT4:
e = dotcode_write_float4(((float*)(tensor->data))[i], FALSE, os);
break;
case DOTCODE_INT:
e = dotcode_write_int(((int*)(tensor->data))[i], FALSE, os);
break;
default:
return g_error_new(DOTCODE_WRITE_BINARY_DATA_GERROR_DOMAIN, 1, "libdotcode::write_binary_data::write_tensor: error writing tensor data to stream: unknown dotcode type %d (%s)", tensor->t, str=dotcode_string_from_type(tensor->t));
g_free(str);
}
if(e != NULL) {
g_propagate_prefixed_error(&err, e, "libdotcode::write_binary_data::write_tensor: error writing %luth datum to output stream.", i);
return err;
}
}
return NULL;
}
Here is the call graph for this function:
Here is the caller graph for this function:| GError* dotcode_write_typed_file | ( | const enum dotcode_type | t, |
| const void * | data, | ||
| GFile * | outfile | ||
| ) |
Definition at line 43 of file write_binary_data.c.
References dotcode_string_from_type2(), DOTCODE_WRITE_BINARY_DATA_GERROR_DOMAIN, dotcode_write_generic(), e, str, and unlikely.
Referenced by main(), and write_specified_10x10x10_float8_grid().
{
GError *e = NULL;
GError *err = NULL;
char* str;
GFileIOStream *gfios = g_file_replace_readwrite(outfile, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &e);
if(unlikely(e != NULL)) {
g_propagate_prefixed_error(&err, e, "libdotcode::write_binary_data::write_dotcode_typed_file: Error opening dotcode file (%s) for writing a %d(%s).", str=g_file_get_uri(outfile), t, dotcode_string_from_type2(t));
g_free(str);
return err;
}
GOutputStream *os = g_io_stream_get_output_stream((GIOStream*)gfios);
char b;
#ifdef WORDS_BIGENDIAN
b = 'b';
#else
b = 'l';
#endif
gsize s;
g_output_stream_write_all(os, &b, 1, &s, NULL, &e);
if((e != NULL) || (s != 1)) {
if(e != NULL) {
g_propagate_prefixed_error(&err, e, "libdotcode::write_binary_data::write_dotcode_typed_file: Error writing endianness (%c) to output file (%s).", b, g_file_get_uri(outfile));
}else{
err = g_error_new(DOTCODE_WRITE_BINARY_DATA_GERROR_DOMAIN, 180, "libdotcode::write_binary_data::write_dotcode_typed_file: Error writing endianness (%c) to output file (%s): more or less than 1 byte written (%ld bytes were written)", b, g_file_get_uri(outfile), (long int)s);
}
return err;
}
err = dotcode_write_generic(t, data, TRUE, os);
g_object_unref(gfios);
return err;
}
Here is the call graph for this function:
Here is the caller graph for this function:| GError* dotcode_write_vector | ( | const struct dotcode_vector * | val, |
| gboolean | write_header, | ||
| GOutputStream * | os | ||
| ) |
Definition at line 213 of file write_binary_data.c.
References DOTCODE_ERROR_NOTIMPLEMENTED, and DOTCODE_WRITE_BINARY_DATA_GERROR_DOMAIN.
Referenced by dotcode_write_generic().
{
return g_error_new(DOTCODE_WRITE_BINARY_DATA_GERROR_DOMAIN, DOTCODE_ERROR_NOTIMPLEMENTED, "libdotcode::write_binary_data::write_vector: function not yet implmented.\n");
}
Here is the caller graph for this function: