File: xml_write_raw.c

package info (click to toggle)
jackd2 1.9.12~dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,632 kB
  • sloc: cpp: 50,117; ansic: 29,194; python: 11,637; sh: 88; makefile: 68; objc: 39
file content (283 lines) | stat: -rw-r--r-- 7,923 bytes parent folder | download | duplicates (8)
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* -*- Mode: C ; c-basic-offset: 4 -*- */
/*
    Copyright (C) 2007,2008 Nedko Arnaudov
    
    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; either version 2 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, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif

#include <stdbool.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <dbus/dbus.h>
#include <time.h>

#include "controller_internal.h"
#include "jackdbus.h"

bool
jack_controller_settings_write_string(int fd, const char * string, void *dbus_call_context_ptr)
{
    size_t len;

    len = strlen(string);

    if (write(fd, string, len) != len)
    {
        jack_dbus_error(dbus_call_context_ptr, JACK_DBUS_ERROR_GENERIC, "write() failed to write config file.");
        return false;
    }

    return true;
}

struct save_context
{
    void * call;
    int fd;
    const char * indent;
    jack_params_handle params;
    const char * address[PARAM_ADDRESS_SIZE];
    const char * str;
};

#define ctx_ptr ((struct save_context *)context)
#define fd (ctx_ptr->fd)

static bool jack_controller_serialize_parameter(void * context, const struct jack_parameter * param_ptr)
{
    char value[JACK_PARAM_STRING_MAX + 1];

    if (!param_ptr->vtable.is_set(param_ptr->obj))
    {
        return true;
    }

    jack_controller_serialize_parameter_value(param_ptr, value);

    return
        jack_controller_settings_write_string(fd, ctx_ptr->indent, ctx_ptr->call) &&
        jack_controller_settings_write_string(fd, "<option name=\"", ctx_ptr->call) &&
        jack_controller_settings_write_string(fd, param_ptr->name, ctx_ptr->call) &&
        jack_controller_settings_write_string(fd, "\">", ctx_ptr->call) &&
        jack_controller_settings_write_string(fd, value, ctx_ptr->call) &&
        jack_controller_settings_write_string(fd, "</option>\n", ctx_ptr->call);
}

bool serialize_modules(void * context, const char * name)
{
    ctx_ptr->indent = "   ";
    ctx_ptr->address[1] = name;
    ctx_ptr->address[2] = NULL;

    return
        jack_controller_settings_write_string(fd, "  <", ctx_ptr->call) &&
        jack_controller_settings_write_string(fd, ctx_ptr->str, ctx_ptr->call) &&
        jack_controller_settings_write_string(fd, " name=\"", ctx_ptr->call) &&
        jack_controller_settings_write_string(fd, name, ctx_ptr->call) &&
        jack_controller_settings_write_string(fd, "\">\n", ctx_ptr->call) &&
        jack_params_iterate_params(ctx_ptr->params, ctx_ptr->address, jack_controller_serialize_parameter, ctx_ptr) &&
        jack_controller_settings_write_string(fd, "  </", ctx_ptr->call) &&
        jack_controller_settings_write_string(fd, ctx_ptr->str, ctx_ptr->call) &&
        jack_controller_settings_write_string(fd, ">\n", ctx_ptr->call);
}

#undef fd
#undef ctx_ptr

bool
jack_controller_settings_save(
    struct jack_controller * controller_ptr,
    void *dbus_call_context_ptr)
{
    char *filename;
    size_t conf_len;
    int fd;
    bool ret;
    time_t timestamp;
    char timestamp_str[26];
    struct save_context context;
    const char * modules[] = {"driver", "internal", NULL};
    char buffer[100];
    unsigned int i;

    time(&timestamp);
    ctime_r(&timestamp, timestamp_str);
    timestamp_str[24] = 0;

    ret = false;

    conf_len = strlen(JACKDBUS_CONF);

    filename = malloc(g_jackdbus_config_dir_len + conf_len + 1);
    if (filename == NULL)
    {
        jack_error("Out of memory.");
        goto exit;
    }

    memcpy(filename, g_jackdbus_config_dir, g_jackdbus_config_dir_len);
    memcpy(filename + g_jackdbus_config_dir_len, JACKDBUS_CONF, conf_len);
    filename[g_jackdbus_config_dir_len + conf_len] = 0;

    jack_info("Saving settings to \"%s\" ...", filename);

    fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
    if (fd == -1)
    {
        jack_error("open() failed to open conf filename. error is %d (%s)", errno, strerror(errno));
        goto exit_free_filename;
    }

    context.fd = fd;
    context.call = dbus_call_context_ptr;

    if (!jack_controller_settings_write_string(fd, "<?xml version=\"1.0\"?>\n", dbus_call_context_ptr))
    {
        goto exit_close;
    }

    if (!jack_controller_settings_write_string(fd, "<!--\n", dbus_call_context_ptr))
    {
        goto exit_close;
    }

    if (!jack_controller_settings_write_string(fd, JACK_CONF_HEADER_TEXT, dbus_call_context_ptr))
    {
        goto exit_close;
    }

    if (!jack_controller_settings_write_string(fd, "-->\n", dbus_call_context_ptr))
    {
        goto exit_close;
    }

    if (!jack_controller_settings_write_string(fd, "<!-- ", dbus_call_context_ptr))
    {
        goto exit_close;
    }

    if (!jack_controller_settings_write_string(fd, timestamp_str, dbus_call_context_ptr))
    {
        goto exit_close;
    }

    if (!jack_controller_settings_write_string(fd, " -->\n", dbus_call_context_ptr))
    {
        goto exit_close;
    }

    if (!jack_controller_settings_write_string(fd, "<jack>\n", dbus_call_context_ptr))
    {
        goto exit_close;
    }
    
    /* engine */

    if (!jack_controller_settings_write_string(fd, " <engine>\n", dbus_call_context_ptr))
    {
        goto exit_close;
    }

    context.indent = "  ";
    context.address[0] = PTNODE_ENGINE;
    context.address[1] = NULL;
    if (!jack_params_iterate_params(controller_ptr->params, context.address, jack_controller_serialize_parameter, &context))
    {
        goto exit_close;
    }

    if (!jack_controller_settings_write_string(fd, " </engine>\n", dbus_call_context_ptr))
    {
        goto exit_close;
    }

    for (i = 0; modules[i] != NULL; i++)
    {
        if (!jack_controller_settings_write_string(fd, " <", dbus_call_context_ptr))
        {
            goto exit_close;
        }

        if (!jack_controller_settings_write_string(fd, modules[i], dbus_call_context_ptr))
        {
            goto exit_close;
        }

        if (!jack_controller_settings_write_string(fd, "s>\n", dbus_call_context_ptr))
        {
            goto exit_close;
        }

        context.indent = "  ";
        context.params = controller_ptr->params;
        context.str = modules[i];
        strcpy(buffer, modules[i]);
        strcat(buffer, "s");
        context.address[0] = buffer;
        context.address[1] = NULL;

        if (!jack_params_iterate_container(controller_ptr->params, context.address, serialize_modules, &context))
        {
            goto exit_close;
        }

        if (!jack_controller_settings_write_string(fd, " </", dbus_call_context_ptr))
        {
            goto exit_close;
        }

        if (!jack_controller_settings_write_string(fd, modules[i], dbus_call_context_ptr))
        {
            goto exit_close;
        }

        if (!jack_controller_settings_write_string(fd, "s>\n", dbus_call_context_ptr))
        {
            goto exit_close;
        }
    }

    if (!jack_controller_settings_write_string(fd, "</jack>\n", dbus_call_context_ptr))
    {
        goto exit_close;
    }

    ret = true;

exit_close:
    close(fd);

exit_free_filename:
    free(filename);

exit:
    return ret;
}

void
jack_controller_settings_save_auto(
    struct jack_controller * controller_ptr)
{
    jack_controller_settings_save(controller_ptr, NULL);
}