File: preset_xml.c

package info (click to toggle)
gdigi 0.4.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,168 kB
  • sloc: ansic: 13,659; makefile: 41
file content (261 lines) | stat: -rw-r--r-- 8,424 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 *  Copyright (c) 2009 Tomasz Moń <desowin@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; under version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses>.
 */

#include <gio/gio.h>
#include <expat.h>
#include <libxml/encoding.h>
#include <libxml/xmlreader.h>
#include <libxml/xmlwriter.h>
#include <string.h>
#include "preset.h"
#include "gdigi.h"
#include "gui.h"
#include "gdigi_xml.h"

#ifndef DOXYGEN_SHOULD_SKIP_THIS
// FIXME: Bring the xml settings into this module?
extern XmlSettings xml_settings[];
extern guint n_xml_settings;
extern EffectValues values_on_off;

/**
 *  \param id modifier ID
 *  \param position modifier position
 *
 *  Gets modifier info.
 *
 *  \return Modifier which must not be freed, or NULL if no matching Modifier has been found.
*/
XmlSettings *get_xml_settings (guint id, guint position)
{
    gint x;

    for (x=0; x< n_xml_settings; x++) {
        if ((xml_settings[x].id == id) && (xml_settings[x].position == position)) {
            return xml_settings + x;
        }
    }

    return NULL;
}

gchar *
map_xml_value (XmlSettings *xml, EffectValues *values,  gint value)
{
    switch (values->type) {
    case VALUE_TYPE_LABEL:
        if ((values == &values_on_off) && (value > 1)) {
            g_warning("Skipping modifier->label %s\n", xml->label);
            return NULL;
        }   

        if (value > values->max || value < values->min) {
            g_warning("%s value %d out of range %0.1f %0.1f",
                      xml->label, value, xml->values->min, xml->values->max);
        } 
        {
            XmlLabel *labels = xml->xml_labels;
            guint labels_amt = xml->xml_labels_amt;
            gint i;

            // Maybe this can be a computation: i = xml->values->min + val
            for (i = 0; i < labels_amt; i++) {
                if (labels[i].type  == value) {
                    return (labels[i].label);
                }
            }
        }
        break;

    default:
        break;
    }
    return NULL;
}

gboolean value_is_extra (EffectValues *val, int value)
{
    if ((value < val->min) || (value > val->max)) {
        return TRUE;
    }
    return FALSE;
}

#define GDIGI_ENCODING "utf-8"
void
write_preset_to_xml(Preset *preset, gchar *filename)
{

    int rc;
    xmlTextWriterPtr writer;
    GList *iter_params = preset->params;
    guint last_id = 0;
    guint last_position = 0;

    printf("Creating a new xml doc\n");
    /* Create a new XmlWriter for uri, with no compression. */
    writer = xmlNewTextWriterFilename(filename, 0);
    if (writer == NULL) {
        printf("testXmlwriterFilename: Error creating the xml writer\n");
        return;
    }

    /*
     * Start the document with the xml default for the version,
     * encoding and the default for the standalone declaration.
     */
    rc = xmlTextWriterStartDocument(writer, NULL, GDIGI_ENCODING, NULL);
    if (rc < 0) {
        printf
            ("testXmlwriterFilename: Error at xmlTextWriterStartDocument\n");
        return;
    }

    rc = xmlTextWriterSetIndent(writer, 1);
    rc = xmlTextWriterSetIndentString(writer, BAD_CAST "  ");
    /* Write the tag identifying type of prefix, schema version and ns. */
    rc = xmlTextWriterStartElement(writer, BAD_CAST get_preset_filename(product_id));

    rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "SchemaVersion",
                                     BAD_CAST "1.2");


    rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "xmlns",
                                     BAD_CAST "http://www.digitech.com/xml/preset");

    /* Write the Name tag. */
    rc = xmlTextWriterWriteElement(writer, BAD_CAST "Name", BAD_CAST preset->name);

    rc = xmlTextWriterStartElement(writer, BAD_CAST "Params");

    while (iter_params) {
        XmlSettings *xml;
        SettingParam *param = (SettingParam *) iter_params->data;

        if (param->id == last_id && param->position == last_position) {
            g_warning("Skipping duplicate parameter id %d position %d",
                       last_id, last_position);
            iter_params  = iter_params->next;
            continue;
        }

        rc = xmlTextWriterStartElement(writer, BAD_CAST "Param");

        rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ID",
                                             "%d", param->id);
        rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "Position",
                                             "%d", param->position);
        rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "Value",
                                             "%d", param->value);

        last_id = param->id;
        last_position = param->position;
        xml  = get_xml_settings(param->id, param->position);
        if (!xml) {
            printf("Failed to get xml settings for id %d position %d\n",
                    param->id, param->position);
        } else {
            ValueType type;
            gchar *suffix = "";
            gdouble step = 1.0;
            gint offset = 0;
            gboolean        decimal = FALSE;
            EffectValues *values = NULL;

            rc = xmlTextWriterWriteElement(writer, BAD_CAST "Name",
                                                   BAD_CAST xml->label);
            values = xml->values;
            type = values->type;
            while ((type & VALUE_TYPE_EXTRA) && value_is_extra(values, param->value)) {
                values = values->extra;
                type = values->type;
            }
            type &= ~VALUE_TYPE_EXTRA;

            if (type & VALUE_TYPE_OFFSET) {
                offset = values->offset;
                type &= ~VALUE_TYPE_OFFSET;
            }

            if (type & VALUE_TYPE_STEP) {
                step = values->step;
                type &= ~VALUE_TYPE_STEP;
            }

            if (type & VALUE_TYPE_SUFFIX) {
                suffix = values->suffix;
                type &= ~VALUE_TYPE_SUFFIX;
            }

            if (type & VALUE_TYPE_DECIMAL) {
                decimal = TRUE;
                type &= ~VALUE_TYPE_DECIMAL;
            }

            switch (type) {
            case VALUE_TYPE_LABEL:
            {
                char *textp = map_xml_value(xml, values, param->value);
                if (!textp) {
                    g_warning("Unable to map %s value %d for id %d position %d",
                              xml->label, param->value, param->id,
                              param->position);
                    textp = "";
                }
                rc = xmlTextWriterWriteElement(writer, BAD_CAST "Text",
                                                       BAD_CAST textp);
                break;
            }

            case VALUE_TYPE_PLAIN:
            {
                if (decimal) {
                    double value = (param->value + offset) * step;
                    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "Text",
                            "%0.2f%s", value, suffix);
                } else {
                    gint value = (param->value + offset) * step;
                    rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "Text",
                            "%d%s", value, suffix);
                }
                break;

            }
            case VALUE_TYPE_NONE:
                rc = xmlTextWriterStartElement(writer, BAD_CAST "Text");
                rc = xmlTextWriterEndElement(writer);
                break;

            default:
                g_warning("Unhandled value type %d", type);
                break;
            }
        }

        rc = xmlTextWriterEndElement(writer);

        iter_params  = iter_params->next;
    }

    rc = xmlTextWriterEndDocument(writer);
    if (rc < 0) {
        printf("testXmlwriterFilename: Error at xmlTextWriterEndDocument\n");
        return;
    }

    xmlFreeTextWriter(writer);
}
#endif /* DOXYGEN_SHOULD_SKIP_THIS */