zfl_config_t * zfl_config_load (char *filename) { // Open file as specified and make sure we can read it FILE *file; if (streq (filename, "-")) file = stdin; // "-" means read from stdin else { file = fopen (filename, "r"); if (!file) return NULL; // File missing or not readable } // Load file data into a memory blob zfl_blob_t *blob = zfl_blob_new (NULL, 0); assert (blob); assert (zfl_blob_load (blob, file)); fclose (file); // Autodetect whether it's JSON or ZPL text char *data = (char *) zfl_blob_data (blob); while (isspace (*data)) data++; zfl_config_t *self; if (*data == '{') self = zfl_config_json ((char *) zfl_blob_data (blob)); else self = zfl_config_zpl ((char *) zfl_blob_data (blob)); zfl_blob_destroy (&blob); return self; }
// -------------------------------------------------------------------------- // 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; }
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; }
int zfl_config_set_string (zfl_config_t *self, char *string) { assert (self); zfl_blob_t *blob = zfl_blob_new (NULL, 0); zfl_blob_set_dptr (blob, (byte *) string, string? strlen (string) + 1: 0); zfl_config_set_value (self, blob); zfl_blob_destroy (&blob); return 0; }
// Starts a device as configured by a full ZDCF config file // static void s_start_configured_device (char *filename) { FILE *file; // JSON config file stream if (streq (filename, "-")) file = stdin; // "-" means read from stdin else { file = fopen (filename, "rb"); if (!file) { printf ("E: '%s' doesn't exist or can't be read\n", filename); exit (EXIT_FAILURE); } } // Load config data into blob zfl_blob_t *blob = zfl_blob_new (); assert (blob); zfl_blob_load (blob, file); fclose (file); // Create a new config from the blob data zfl_config_t *config = zfl_config_new (zfl_blob_data (blob)); assert (config); // Find first device char *device = zfl_config_device (config, 0); if (!*device) { printf ("E: No device specified, please read http://rfc.zeromq.org/spec:3\n"); exit (EXIT_FAILURE); } // Process device type char *type = zfl_config_device_type (config, device); int device_type; // 0MQ defined type int frontend_type; // Socket types depending int backend_type; // on the device type s_parse_device_type (type, &device_type, &frontend_type, &backend_type); // Create and configure sockets void *frontend = zfl_config_socket (config, device, "frontend", frontend_type); assert (frontend); void *backend = zfl_config_socket (config, device, "backend", backend_type); assert (backend); // Start the device now if (zfl_config_verbose (config)) printf ("I: Starting device...\n"); // Will not actually ever return zmq_device (device_type, frontend, backend); zfl_config_destroy (&config); }
int zfl_config_set_printf (zfl_config_t *self, char *format, ...) { char value [255 + 1]; va_list args; assert (self); va_start (args, format); vsnprintf (value, 255, format, args); va_end (args); zfl_blob_t *blob = zfl_blob_new (NULL, 0); zfl_blob_set_dptr (blob, (byte *) value, strlen (value) + 1); zfl_config_set_value (self, blob); zfl_blob_destroy (&blob); return 0; }