1 : /* SLV2
2 : * Copyright (C) 2008-2009 Dave Robillard <http://drobilla.net>
3 : *
4 : * This library is free software; you can redistribute it and/or modify it
5 : * under the terms of the GNU General Public License as published by the Free
6 : * Software Foundation; either version 2 of the License, or (at your option)
7 : * any later version.
8 : *
9 : * This library is distributed in the hope that it will be useful, but WITHOUT
10 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 : * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 : * for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License along
15 : * with this program; if not, write to the Free Software Foundation, Inc.,
16 : * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17 : */
18 :
19 : #define _XOPEN_SOURCE 500
20 :
21 : #include <string.h>
22 : #include <limits.h>
23 : #include <librdf.h>
24 : #include "slv2/collections.h"
25 : #include "slv2/pluginclass.h"
26 : #include "slv2/pluginui.h"
27 : #include "slv2_internal.h"
28 :
29 : #define SLV2_COLLECTION_IMPL(CollType, ElemType, prefix, free_func) \
30 : \
31 : CollType \
32 : prefix ## _new() \
33 : { \
34 : return raptor_new_sequence((void (*)(void*))free_func, NULL); \
35 : } \
36 : \
37 : \
38 : void \
39 : prefix ## _free(CollType coll) \
40 : { \
41 : if (coll) \
42 : raptor_free_sequence(coll); \
43 : } \
44 : \
45 : \
46 : unsigned \
47 : prefix ## _size(CollType coll) \
48 : { \
49 : return (coll ? raptor_sequence_size(coll) : 0); \
50 : } \
51 : \
52 : \
53 : ElemType \
54 : prefix ## _get_at(CollType coll, unsigned index) \
55 : { \
56 : if (index > INT_MAX) \
57 : return NULL; \
58 : else \
59 : return (ElemType)raptor_sequence_get_at(coll, (int)index); \
60 : }
61 :
62 37 : SLV2_COLLECTION_IMPL(SLV2PluginClasses, SLV2PluginClass,
63 : slv2_plugin_classes, &slv2_plugin_class_free)
64 7 : SLV2_COLLECTION_IMPL(SLV2ScalePoints, SLV2ScalePoint,
65 : slv2_scale_points, &slv2_scale_point_free)
66 5766 : SLV2_COLLECTION_IMPL(SLV2Values, SLV2Value,
67 : slv2_values, &slv2_value_free)
68 16 : SLV2_COLLECTION_IMPL(SLV2UIs, SLV2UI,
69 : slv2_uis, &slv2_ui_free)
70 :
71 :
72 : /* **** PLUGIN CLASSES **** */
73 :
74 : SLV2PluginClass
75 : slv2_plugin_classes_get_by_uri(SLV2PluginClasses list, SLV2Value uri)
76 3 : {
77 : // good old fashioned binary search
78 :
79 3 : int lower = 0;
80 3 : int upper = raptor_sequence_size(list) - 1;
81 : int i;
82 :
83 17 : while (upper >= lower) {
84 13 : i = lower + ((upper - lower) / 2);
85 :
86 13 : SLV2PluginClass p = raptor_sequence_get_at(list, i);
87 :
88 : const int cmp = strcmp(slv2_value_as_uri(slv2_plugin_class_get_uri(p)),
89 13 : slv2_value_as_uri(uri));
90 :
91 13 : if (cmp == 0)
92 2 : return p;
93 11 : else if (cmp > 0)
94 9 : upper = i - 1;
95 : else
96 2 : lower = i + 1;
97 : }
98 :
99 1 : return NULL;
100 : }
101 :
102 :
103 :
104 : /* **** VALUES **** */
105 :
106 : bool
107 : slv2_values_contains(SLV2Values list, SLV2Value value)
108 10 : {
109 16 : for (unsigned i=0; i < slv2_values_size(list); ++i)
110 13 : if (slv2_value_equals(slv2_values_get_at(list, i), value))
111 7 : return true;
112 :
113 3 : return false;
114 : }
115 :
116 :
117 : void
118 : slv2_values_set_at(SLV2Values list, unsigned index, void* value)
119 16 : {
120 16 : if (index <= INT_MAX)
121 16 : raptor_sequence_set_at(list, index, value);
122 16 : }
123 :
124 :
125 :
126 : /* **** PLUGIN UIS **** */
127 :
128 : SLV2UI
129 : slv2_uis_get_by_uri(SLV2UIs list, SLV2Value uri)
130 4 : {
131 : // good old fashioned binary search
132 :
133 4 : int lower = 0;
134 4 : int upper = raptor_sequence_size(list) - 1;
135 : int i;
136 :
137 14 : while (upper >= lower) {
138 9 : i = lower + ((upper - lower) / 2);
139 :
140 9 : SLV2UI ui = raptor_sequence_get_at(list, i);
141 :
142 : const int cmp = strcmp(slv2_value_as_uri(slv2_ui_get_uri(ui)),
143 9 : slv2_value_as_uri(uri));
144 :
145 9 : if (cmp == 0)
146 3 : return ui;
147 6 : else if (cmp > 0)
148 3 : upper = i - 1;
149 : else
150 3 : lower = i + 1;
151 : }
152 :
153 1 : return NULL;
154 : }
155 :
|