]> sigrok.org Git - libsigrok.git/blobdiff - src/input/input.c
input: sr_input_new() always allocates the instance buffer.
[libsigrok.git] / src / input / input.c
index c7855434e56ff9eaaf41660a19f0f437252f3336..16185aaab39c916c0941bd4ed96e8a82cfb4855b 100644 (file)
@@ -157,39 +157,52 @@ SR_API const struct sr_input_module *sr_input_find(char *id)
  *
  * @since 0.4.0
  */
-SR_API const struct sr_option *sr_input_options_get(const struct sr_input_module *o)
+SR_API const struct sr_option **sr_input_options_get(const struct sr_input_module *imod)
 {
+       const struct sr_option *mod_opts, **opts;
+       int size, i;
 
-       if (!o || !o->options)
+       if (!imod || !imod->options)
                return NULL;
 
-       return o->options();
+       mod_opts = imod->options();
+
+       for (size = 0; mod_opts[size].id; size++)
+               ;
+       opts = g_malloc((size + 1) * sizeof(struct sr_option *));
+
+       for (i = 0; i < size; i++)
+               opts[i] = &mod_opts[i];
+       opts[i] = NULL;
+
+       return opts;
 }
 
 /**
  * After a call to sr_input_options_get(), this function cleans up all
- * resources allocated by that call.
+ * resources returned by that call.
  *
  * @since 0.4.0
  */
-SR_API void sr_input_options_free(const struct sr_input_module *o)
+SR_API void sr_input_options_free(const struct sr_option **options)
 {
-       struct sr_option *opt;
+       int i;
 
-       if (!o || !o->options)
+       if (!options)
                return;
 
-       for (opt = o->options(); opt->id; opt++) {
-               if (opt->def) {
-                       g_variant_unref(opt->def);
-                       opt->def = NULL;
+       for (i = 0; options[i]; i++) {
+               if (options[i]->def) {
+                       g_variant_unref(options[i]->def);
+                       ((struct sr_option *)options[i])->def = NULL;
                }
 
-               if (opt->values) {
-                       g_slist_free_full(opt->values, (GDestroyNotify)g_variant_unref);
-                       opt->values = NULL;
+               if (options[i]->values) {
+                       g_slist_free_full(options[i]->values, (GDestroyNotify)g_variant_unref);
+                       ((struct sr_option *)options[i])->values = NULL;
                }
        }
+       g_free(options);
 }
 
 /**
@@ -257,12 +270,12 @@ SR_API struct sr_input *sr_input_new(const struct sr_input_module *imod,
        }
 
        if (in->module->init && in->module->init(in, new_opts) != SR_OK) {
-               g_hash_table_destroy(new_opts);
                g_free(in);
                in = NULL;
        }
        if (new_opts)
                g_hash_table_destroy(new_opts);
+       in->buf = g_string_sized_new(128);
 
        return in;
 }
@@ -344,7 +357,7 @@ SR_API const struct sr_input *sr_input_scan_buffer(GString *buf)
 
                /* Found a matching module. */
                in = sr_input_new(imod, NULL);
-               in->buf = g_string_new_len(buf->str, buf->len);
+               g_string_insert_len(in->buf, 0, buf->str, buf->len);
                break;
        }
 
@@ -443,7 +456,7 @@ SR_API const struct sr_input *sr_input_scan_file(const char *filename)
 
                /* Found a matching module. */
                in = sr_input_new(imod, NULL);
-               in->buf = g_string_new_len(buf->str, buf->len);
+               g_string_insert_len(in->buf, 0, buf->str, buf->len);
                break;
        }
        if (!in && buf)