1 /*
2   Copyright 2016 David Robillard <http://drobilla.net>
3 
4   Permission to use, copy, modify, and/or distribute this software for any
5   purpose with or without fee is hereby granted, provided that the above
6   copyright notice and this permission notice appear in all copies.
7 
8   THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9   WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10   MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11   ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16 
17 /**
18    @defgroup util Utilities
19    @ingroup lv2core
20    @{
21 */
22 
23 #include "lv2/core/lv2.h"
24 
25 #include <stdarg.h>
26 #include <stdbool.h>
27 #include <string.h>
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 /**
34    Return the data for a feature in a features array.
35 
36    If the feature is not found, NULL is returned.  Note that this function is
37    only useful for features with data, and can not detect features that are
38    present but have NULL data.
39 */
40 static inline void*
lv2_features_data(const LV2_Feature * const * features,const char * const uri)41 lv2_features_data(const LV2_Feature*const* features,
42                   const char* const        uri)
43 {
44 	if (features) {
45 		for (const LV2_Feature*const* f = features; *f; ++f) {
46 			if (!strcmp(uri, (*f)->URI)) {
47 				return (*f)->data;
48 			}
49 		}
50 	}
51 	return NULL;
52 }
53 
54 /**
55    Query a features array.
56 
57    This function allows getting several features in one call, and detect
58    missing required features, with the same caveat of lv2_features_data().
59 
60    The arguments should be a series of const char* uri, void** data, bool
61    required, terminated by a NULL URI.  The data pointers MUST be initialized
62    to NULL.  For example:
63 
64    @code
65    LV2_URID_Log* log = NULL;
66    LV2_URID_Map* map = NULL;
67    const char* missing = lv2_features_query(
68         features,
69         LV2_LOG__log,  &log, false,
70         LV2_URID__map, &map, true,
71         NULL);
72    @endcode
73 
74    @return NULL on success, otherwise the URI of this missing feature.
75 */
76 static inline const char*
lv2_features_query(const LV2_Feature * const * features,...)77 lv2_features_query(const LV2_Feature* const* features, ...)
78 {
79 	va_list args;
80 	va_start(args, features);
81 
82 	const char* uri = NULL;
83 	while ((uri = va_arg(args, const char*))) {
84 		void** data     = va_arg(args, void**);
85 		bool   required = va_arg(args, int);
86 
87 		*data = lv2_features_data(features, uri);
88 		if (required && !*data) {
89 			return uri;
90 		}
91 	}
92 
93 	return NULL;
94 }
95 
96 #ifdef __cplusplus
97 }  /* extern "C" */
98 #endif
99 
100 /**
101    @}
102    @}
103 */
104