// -------------------------------------------------------------------------- // Selftest // int zfl_blob_test (Bool verbose) { zfl_blob_t *blob; char *string = "This is a string"; FILE *file; printf (" * zfl_blob: "); blob = zfl_blob_new (NULL, 0); assert (blob); assert (zfl_blob_size (blob) == 0); file = fopen ("zfl_blob.c", "r"); assert (file); assert (zfl_blob_load (blob, file)); fclose (file); assert (zfl_blob_size (blob) > 0); zfl_blob_set_data (blob, string, strlen (string)); assert (zfl_blob_size (blob) == strlen (string)); assert (streq ((char *) (zfl_blob_data (blob)), string)); zfl_blob_destroy (&blob); assert (blob == NULL); printf ("OK\n"); return 0; }
// -------------------------------------------------------------------------- // Loads blob from file. Always adds a binary zero to end of blob data so // that it can be parsed as a string if necessary. Returns size of blob // data. Idempotent, does not change current read position in file. If // file cannot be read, returns empty blob. // size_t zfl_blob_load (zfl_blob_t *self, FILE *file) { long posn, // Current position in file size; // Size of file data // Get current position in file so we can come back here afterwards posn = ftell (file); if (fseek (file, 0, SEEK_END) == 0) { // Now determine actual size of blob in file size = ftell (file); assert (size >= 0); // Read file data, and then reset file position char *buffer = malloc (size); fseek (file, 0, SEEK_SET); size_t rc = fread (buffer, 1, size, file); assert (rc == size); fseek (file, posn, SEEK_SET); zfl_blob_set_data (self, buffer, size); free (buffer); } else zfl_blob_set_data (self, NULL, 0); return zfl_blob_size (self); }
int zfl_config_set_value (zfl_config_t *self, zfl_blob_t *blob) { assert (self); zfl_blob_destroy (&self->blob); if (blob) self->blob = zfl_blob_new (zfl_blob_data (blob), zfl_blob_size (blob)); return 0; }