# HG changeset patch
# User Dan Smith <danms(a)us.ibm.com>
# Date 1194042578 25200
# Node ID 0df048ad2e99c8eda6c843f148168a9fa0161399
# Parent 05b01d03f0b801afe266d8d012bc464be6a2d165
Add cu_compare_ref()
This provides a generic way to easily compare a given ref against an instance,
which should be useful in GetInstance functions for making sure the returned
instance matches what was asked.
Signed-off-by: Dan Smith <danms(a)us.ibm.com>
diff -r 05b01d03f0b8 -r 0df048ad2e99 instance_util.c
--- a/instance_util.c Sun Feb 15 22:01:53 2015 -0800
+++ b/instance_util.c Fri Nov 02 15:29:38 2007 -0700
@@ -19,6 +19,7 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdbool.h>
+#include <string.h>
#include "libcmpiutil.h"
@@ -68,6 +69,59 @@ unsigned int cu_return_instance_names(co
return c;
}
+static bool _compare_data(const CMPIData *a, const CMPIData *b)
+{
+ if (a->type != b->type)
+ return false;
+
+ if (a->type & CMPI_string) {
+ const char *as = CMGetCharPtr(a->value.string);
+ const char *bs = CMGetCharPtr(b->value.string);
+
+ return STREQ(as, bs);
+ } else if (a->type & CMPI_INTEGER) {
+ return memcmp(&a->value, &b->value, sizeof(a->value)) ==
0;
+ }
+
+ CU_DEBUG("Unhandled CMPI type: `%i'", a->type);
+
+ return false;
+}
+
+const struct cu_property *cu_compare_ref(const CMPIObjectPath *ref,
+ const CMPIInstance *inst,
+ const struct cu_property *props)
+{
+ const struct cu_property *p = NULL;
+ int i;
+ CMPIStatus s;
+
+ for (i = 0; props[i].name != NULL; i++) {
+ CMPIData kd, pd;
+
+ p = &props[i];
+
+ kd = CMGetKey(ref, p->name, &s);
+ if (s.rc != CMPI_RC_OK) {
+ if (p->required)
+ goto out;
+ else
+ continue;
+ }
+
+ pd = CMGetProperty(inst, p->name, &s);
+ if (s.rc != CMPI_RC_OK)
+ goto out;
+
+ if (!_compare_data(&kd, &pd))
+ goto out;
+ }
+
+ p = NULL;
+ out:
+ return p;
+}
+
/*
* Local Variables:
* mode: C
diff -r 05b01d03f0b8 -r 0df048ad2e99 libcmpiutil.h
--- a/libcmpiutil.h Sun Feb 15 22:01:53 2015 -0800
+++ b/libcmpiutil.h Fri Nov 02 15:29:38 2007 -0700
@@ -336,6 +336,27 @@ void inst_list_free(struct inst_list *li
* @returns nonzero on success, zero on failure
*/
int inst_list_add(struct inst_list *list, CMPIInstance *inst);
+
+struct cu_property {
+ const char *name;
+ bool required;
+};
+
+/**
+ * Compare key values in a reference to properties in an instance,
+ * making sure they are identical. If props identifies a particular
+ * key as not required, then absence in the object path will not
+ * result in failure of this test.
+ *
+ * @param ref The ObjectPath to examine
+ * @param inst The Instance to compare
+ * @param props A NULL-terminated list of properties to compare
+ * @returns A pointer to the property structure of the first
+ * non-matching property, or NULL if all match
+ */
+const struct cu_property *cu_compare_ref(const CMPIObjectPath *ref,
+ const CMPIInstance *inst,
+ const struct cu_property *props);
#define DEFAULT_EIN(pn) \
static CMPIStatus pn##EnumInstanceNames(CMPIInstanceMI *self, \