+/* Decode the Z_SortAttributes struct and store the whole thing into the
+ * hash by reference
+ */
+int simpleserver_ExpandSortAttributes (HV *sort_spec, Z_SortAttributes *sattr)
+{
+ WRBUF attrset_wr = wrbuf_alloc();
+ AV *list = newAV();
+ Z_AttributeList *attr_list = sattr->list;
+ int i;
+
+ oid2str(sattr->id, attrset_wr);
+ hv_store(sort_spec, "ATTRSET", 7,
+ newSVpv(attrset_wr->buf, attrset_wr->pos), 0);
+ wrbuf_free(attrset_wr, 1);
+
+ hv_store(sort_spec, "SORT_ATTR", 9, newRV( sv_2mortal( (SV*) list ) ), 0);
+
+ for (i = 0; i < attr_list->num_attributes; i++)
+ {
+ Z_AttributeElement *attr = *attr_list->attributes++;
+ HV *attr_spec = newHV();
+
+ av_push(list, newRV( sv_2mortal( (SV*) attr_spec ) ));
+ hv_store(attr_spec, "ATTR_TYPE", 9, newSViv(*attr->attributeType), 0);
+
+ if (attr->which == Z_AttributeValue_numeric)
+ {
+ hv_store(attr_spec, "ATTR_VALUE", 10,
+ newSViv(*attr->value.numeric), 0);
+ } else {
+ return 0;
+ }
+ }
+
+ return 1;
+}
+
+
+/* Decode the Z_SortKeySpec struct and store the whole thing in a perl hash */
+int simpleserver_SortKeySpecToHash (HV *sort_spec, Z_SortKeySpec *spec)
+{
+ Z_SortElement *element = spec->sortElement;
+
+ hv_store(sort_spec, "RELATION", 8, newSViv(*spec->sortRelation), 0);
+ hv_store(sort_spec, "CASE", 4, newSViv(*spec->caseSensitivity), 0);
+ hv_store(sort_spec, "MISSING", 7, newSViv(spec->which), 0);
+
+ if (element->which == Z_SortElement_generic)
+ {
+ Z_SortKey *key = element->u.generic;
+
+ if (key->which == Z_SortKey_sortField)
+ {
+ hv_store(sort_spec, "SORTFIELD", 9,
+ newSVpv((char *) key->u.sortField, 0), 0);
+ }
+ else if (key->which == Z_SortKey_elementSpec)
+ {
+ Z_Specification *zspec = key->u.elementSpec;
+
+ hv_store(sort_spec, "ELEMENTSPEC_TYPE", 16,
+ newSViv(zspec->which), 0);
+
+ if (zspec->which == Z_Schema_oid)
+ {
+ WRBUF elementSpec = wrbuf_alloc();
+
+ oid2str(zspec->schema.oid, elementSpec);
+ hv_store(sort_spec, "ELEMENTSPEC_VALUE", 17,
+ newSVpv(elementSpec->buf, elementSpec->pos), 0);
+ wrbuf_free(elementSpec, 1);
+ }
+ else if (zspec->which == Z_Schema_uri)
+ {
+ hv_store(sort_spec, "ELEMENTSPEC_VALUE", 17,
+ newSVpv((char *) zspec->schema.uri, 0), 0);
+ }
+ }
+ else if (key->which == Z_SortKey_sortAttributes)
+ {
+ return simpleserver_ExpandSortAttributes(sort_spec,
+ key->u.sortAttributes);
+ }
+ else
+ {
+ return 0;
+ }
+ }
+ else
+ {
+ return 0;
+ }
+
+ return 1;
+}
+
+