1 : /* SLV2
2 : * Copyright (C) 2007-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 : #include <string.h>
21 : #include <limits.h>
22 : #include <stdio.h>
23 : #include <stdlib.h>
24 : #include <assert.h>
25 : #include <librdf.h>
26 : #include "slv2/types.h"
27 : #include "slv2/plugin.h"
28 : #include "slv2/collections.h"
29 : #include "slv2/util.h"
30 : #include "slv2_internal.h"
31 :
32 :
33 : SLV2Plugins
34 : slv2_plugins_new()
35 14 : {
36 : //return raptor_new_sequence((void (*)(void*))&slv2_plugin_free, NULL);
37 14 : return raptor_new_sequence(NULL, NULL);
38 : }
39 :
40 :
41 : void
42 : slv2_plugins_free(SLV2World world, SLV2Plugins list)
43 8 : {
44 8 : if (list && list != world->plugins)
45 4 : raptor_free_sequence(list);
46 8 : }
47 :
48 : #if 0
49 : void
50 : slv2_plugins_filter(SLV2Plugins dest, SLV2Plugins source, bool (*include)(SLV2Plugin))
51 : {
52 : assert(dest);
53 :
54 : for (int i=0; i < raptor_sequence_size(source); ++i) {
55 : SLV2Plugin p = raptor_sequence_get_at(source, i);
56 : if (include(p))
57 : raptor_sequence_push(dest, slv2_plugin_duplicate(p));
58 : }
59 : }
60 : #endif
61 :
62 :
63 : unsigned
64 : slv2_plugins_size(SLV2Plugins list)
65 595 : {
66 595 : return (list ? raptor_sequence_size(list) : 0);
67 : }
68 :
69 :
70 : SLV2Plugin
71 : slv2_plugins_get_by_uri(SLV2Plugins list, SLV2Value uri)
72 7432 : {
73 : // good old fashioned binary search
74 :
75 7432 : int lower = 0;
76 7432 : int upper = raptor_sequence_size(list) - 1;
77 : int i;
78 :
79 63051 : while (upper >= lower) {
80 52984 : i = lower + ((upper - lower) / 2);
81 :
82 52984 : SLV2Plugin p = raptor_sequence_get_at(list, i);
83 :
84 : const int cmp = strcmp(slv2_value_as_uri(slv2_plugin_get_uri(p)),
85 52984 : slv2_value_as_uri(uri));
86 :
87 52984 : if (cmp == 0)
88 4797 : return p;
89 48187 : else if (cmp > 0)
90 43 : upper = i - 1;
91 : else
92 48144 : lower = i + 1;
93 : }
94 :
95 2635 : return NULL;
96 : }
97 :
98 :
99 : SLV2Plugin
100 : slv2_plugins_get_at(SLV2Plugins list, unsigned index)
101 586 : {
102 586 : if (index > INT_MAX)
103 1 : return NULL;
104 : else
105 585 : return (SLV2Plugin)raptor_sequence_get_at(list, (int)index);
106 : }
107 :
|