-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetting.c
212 lines (179 loc) · 4.84 KB
/
setting.c
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
// Pithesiser - a software synthesiser for Raspberry Pi
// Copyright (C) 2015 Nicholas Tuckett
//
// 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 3 of the License, or
// (at your option) any later version.
//
// 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/>.
/*
* setting.c
*
* Created on: 14 Sep 2013
* Author: ntuckett
*/
#include "system_constants.h"
#include "setting.h"
#include <string.h>
#include <stdlib.h>
#include <assert.h>
//------------------------------------------------------------------------------
// Internal link list structure for keeping track of settings
//
typedef struct link_double_t link_double_t;
struct link_double_t
{
link_double_t* next;
link_double_t* prev;
};
static void setting_set_name(setting_t* setting, const char* name)
{
strncpy(setting->name, name, SETTING_MAX_NAME_LEN);
setting->name[SETTING_MAX_NAME_LEN - 1] = 0;
}
static link_double_t* setting_list_head = NULL;
static void setting_list_add(setting_t* setting)
{
link_double_t* link = ((link_double_t*)setting) - 1;
assert(link->next == NULL);
assert(link->prev == NULL);
link->next = setting_list_head;
if (link->next != NULL)
{
link->next->prev = link;
}
setting_list_head = link;
}
static link_double_t* setting_list_remove(setting_t* setting)
{
link_double_t* link = ((link_double_t*)setting) - 1;
if (link->next != NULL)
{
link->next->prev = link->prev;
}
if (link->prev != NULL)
{
link->prev->next = link->next;
}
else
{
setting_list_head = link->next;
}
return link;
}
//------------------------------------------------------------------------------
// Public API
//
setting_t* setting_create(const char* name)
{
link_double_t* link = (link_double_t*)calloc(1, sizeof(link_double_t) + sizeof(setting_t));
setting_t* setting = NULL;
if (link != NULL)
{
link->next = NULL;
link->prev = NULL;
setting = (setting_t*)(link + 1);
setting_set_name(setting, name);
setting->type = SETTING_TYPE_UNKNOWN;
setting_list_add(setting);
}
return setting;
}
void setting_destroy(setting_t* setting)
{
link_double_t* link = setting_list_remove(setting);
free(link);
}
setting_t* setting_find(const char* name)
{
link_double_t* link = setting_list_head;
while (link != NULL)
{
setting_t* setting = (setting_t*)(link + 1);
if (strncmp(name, setting->name, SETTING_MAX_NAME_LEN) == 0)
{
return setting;
}
else
{
link = link->next;
}
}
return NULL;
}
void setting_init_as_int(setting_t* setting, int value)
{
assert(setting->type == SETTING_TYPE_UNKNOWN);
setting->type = SETTING_TYPE_INT;
setting->value.integer = value;
}
void setting_init_as_float(setting_t* setting, float value)
{
assert(setting->type == SETTING_TYPE_UNKNOWN);
setting->type = SETTING_TYPE_FLOAT;
setting->value.real = value;
}
void setting_init_as_enum(setting_t* setting, int value, enum_type_info_t* enum_type)
{
assert(setting->type == SETTING_TYPE_UNKNOWN);
assert(value >= 0 && value < enum_type->max_index);
setting->type = SETTING_TYPE_ENUM;
setting->value.integer = value;
setting->type_info.enum_type_info = enum_type;
}
void setting_set_value_int(setting_t* setting, int value)
{
assert(setting->type != SETTING_TYPE_UNKNOWN);
switch (setting->type)
{
case SETTING_TYPE_INT:
setting->value.integer = value;
break;
case SETTING_TYPE_FLOAT:
setting_set_value_float(setting, value);
break;
case SETTING_TYPE_ENUM:
setting_set_value_enum_as_int(setting, value);
break;
case SETTING_TYPE_UNKNOWN:
break;
}
}
void setting_set_value_float(setting_t* setting, float value)
{
assert(setting->type == SETTING_TYPE_FLOAT);
setting->value.real = value;
}
void setting_set_value_enum_as_int(setting_t* setting, int value)
{
assert(setting->type == SETTING_TYPE_ENUM);
assert(value >= 0 && value <= setting->type_info.enum_type_info->max_index);
setting->value.integer = value;
}
int setting_get_value_int(setting_t* setting)
{
assert(setting->type == SETTING_TYPE_INT);
return setting->value.integer;
}
float setting_get_value_float(setting_t* setting)
{
assert(setting->type == SETTING_TYPE_FLOAT);
return setting->value.real;
}
const char* setting_get_value_enum(setting_t* setting)
{
assert(setting->type == SETTING_TYPE_ENUM);
return setting->type_info.enum_type_info->values[setting->value.integer];
}
int setting_get_value_enum_as_int(setting_t* setting)
{
assert(setting->type == SETTING_TYPE_ENUM);
return setting->value.integer;
}