File: xml.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 (140 lines) | stat: -rw-r--r-- 3,901 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
/* -*- Mode: C ; c-basic-offset: 4 -*- */
/*
    Copyright (C) 2007,2008,2011 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 <string.h>
#include <stdio.h>
#include <assert.h>
#include <dbus/dbus.h>

#include "controller_internal.h"

void
jack_controller_deserialize_parameter_value(
    struct jack_controller *controller_ptr,
    const char * const * address,
    const char * option_value)
{
    const struct jack_parameter * param_ptr;
    union jackctl_parameter_value value;
    size_t size;

    param_ptr = jack_params_get_parameter(controller_ptr->params, address);
    if (param_ptr == NULL)
    {
        jack_error("Unknown parameter");
        goto ignore;
    }

    jack_info("setting parameter '%s':'%s':'%s' to value \"%s\"", address[0], address[1], address[2], option_value);

    switch (param_ptr->type)
    {
    case JackParamInt:
        value.i = atoi(option_value);
        break;
    case JackParamUInt:
        value.ui = strtoul(option_value, NULL, 10);
        break;
    case JackParamChar:
        if (option_value[0] == 0 || option_value[1] != 0)
        {
            jack_error("invalid char option value \"%s\"", option_value);
            goto ignore;
        }
        value.c = *option_value;
        break;
    case JackParamString:
        size = strlen(option_value);
        if (size >= sizeof(value.str))
        {
            jack_error("string option value \"%s\" is too long, max is %zu chars (including terminating zero)", option_value, sizeof(value.str));
            goto ignore;
        }

        strcpy(value.str, option_value);
        break;
    case JackParamBool:
        if (strcmp(option_value, "true") == 0)
        {
            value.b = true;
        }
        else if (strcmp(option_value, "false") == 0)
        {
            value.b = false;
        }
        else
        {
            jack_error("ignoring unknown bool value \"%s\"", option_value);
            goto ignore;
        }
        break;
    default:
        jack_error("Unknown type %d", (int)param_ptr->type);
        goto ignore;
    }

    if (param_ptr->vtable.set_value(param_ptr->obj, &value))
    {
        return;
    }

    jack_error("Parameter set failed");

ignore:
    jack_error("Ignoring restore attempt of parameter '%s':'%s':'%s'", address[0], address[1], address[2]);
}

void
jack_controller_serialize_parameter_value(
    const struct jack_parameter * param_ptr,
    char * value_buffer)
{
    union jackctl_parameter_value value;

    value = param_ptr->vtable.get_value(param_ptr->obj);

    switch (param_ptr->type)
    {
    case JackParamInt:
        sprintf(value_buffer, "%d", (int)value.i);
        return;
    case JackParamUInt:
        sprintf(value_buffer, "%u", (unsigned int)value.ui);
        return;
    case JackParamChar:
        sprintf(value_buffer, "%c", (char)value.c);
        return;
    case JackParamString:
        strcpy(value_buffer, value.str);
        return;
    case JackParamBool:
        strcpy(value_buffer, value.b ? "true" : "false");
        return;
    }

    jack_error("parameter of unknown type %d", (int)param_ptr->type);
    assert(false);
    *value_buffer = 0;
}