- strcpy(metafile, "sigrok-meta-XXXXXX");
- if ((tmpfile = g_mkstemp(metafile)) == -1)
- return SR_ERR;
- close(tmpfile);
- meta = g_fopen(metafile, "wb");
- fprintf(meta, "[global]\n");
- fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION);
- fprintf(meta, "[device 1]\ncapturefile = logic-1\n");
- fprintf(meta, "total probes = %d\n", g_slist_length(o->sdi->channels));
+ meta = g_key_file_new();
+
+ g_key_file_set_string(meta, "global", "sigrok version",
+ SR_PACKAGE_VERSION_STRING);
+
+ devgroup = "device 1";
+
+ for (l = o->sdi->channels; l; l = l->next) {
+ ch = l->data;
+
+ switch (ch->type) {
+ case SR_CHANNEL_LOGIC:
+ if (ch->enabled)
+ enabled_logic_channels++;
+ logic_channels++;
+ break;
+ case SR_CHANNEL_ANALOG:
+ if (ch->enabled)
+ enabled_analog_channels++;
+ break;
+ }
+ }
+
+ /* When reading the file, the first index of the analog channels
+ * can only be deduced through the "total probes" count, so the
+ * first analog index must follow the last logic one, enabled or not. */
+ if (enabled_logic_channels > 0)
+ outc->first_analog_index = logic_channels + 1;
+ else
+ outc->first_analog_index = 1;
+
+ /* Only set capturefile and probes if we will actually save logic data. */
+ if (enabled_logic_channels > 0) {
+ g_key_file_set_string(meta, devgroup, "capturefile", "logic-1");
+ g_key_file_set_integer(meta, devgroup, "total probes", logic_channels);
+ }
+