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; }
char * zfl_config_string (zfl_config_t *self) { assert (self); if (self->blob) return (char *) zfl_blob_data (self->blob); else return ""; }
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; }
// -------------------------------------------------------------------------- // Returns tree string value // char * zfl_tree_string (zfl_tree_t *self) { assert (self); if (self->blob) return zfl_blob_data (self->blob); else return ""; }
// 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); }
// -------------------------------------------------------------------------- // Dump tree to stdout in ZPL format // static int s_tree_dump (zfl_tree_t *self, void *context, int level) { assert (self); if (level > 0) { if (self->blob) printf ("%*s%s = %s\n", (level - 1) * 4, "", self->name? self->name: "(Unnamed)", (char *) zfl_blob_data (self->blob)); else printf ("%*s%s\n", (level - 1) * 4, "", self->name? self->name: "(Unnamed)"); } return 0; }
static int s_config_save (zfl_config_t *self, void *arg, int level) { assert (self); assert (arg); FILE *file = (FILE *) arg; if (level > 0) { if (self->blob) fprintf (file, "%*s%s = %s\n", (level - 1) * 4, "", self->name? self->name: "(Unnamed)", (char *) zfl_blob_data (self->blob)); else fprintf (file, "%*s%s\n", (level - 1) * 4, "", self->name? self->name: "(Unnamed)"); } return 0; }