+struct sort_term {
+ zint sysno;
+ zint section_id;
+ zint length;
+ char term[SORT_MAX_MULTI];
+};
+
+
+static void sort_term_log_item(int level, const void *b, const char *txt)
+{
+ struct sort_term a1;
+
+ memcpy(&a1, b, sizeof(a1));
+
+ yaz_log(level, "%s " ZINT_FORMAT " " ZINT_FORMAT " %.*s", txt, a1.sysno,
+ a1.section_id, (int) a1.length-1, a1.term);
+}
+
+static int sort_term_compare(const void *a, const void *b)
+{
+ struct sort_term a1, b1;
+
+ memcpy(&a1, a, sizeof(a1));
+ memcpy(&b1, b, sizeof(b1));
+
+ if (a1.sysno > b1.sysno)
+ return 1;
+ else if (a1.sysno < b1.sysno)
+ return -1;
+ if (a1.section_id > b1.section_id)
+ return 1;
+ else if (a1.section_id < b1.section_id)
+ return -1;
+
+ return 0;
+}
+
+static void *sort_term_code_start(void)
+{
+ return 0;
+}
+
+static void sort_term_encode1(void *p, char **dst, const char **src)
+{
+ struct sort_term a1;
+
+ memcpy(&a1, *src, sizeof(a1));
+ *src += sizeof(a1);
+
+ zebra_zint_encode(dst, a1.sysno); /* encode record id */
+ strcpy(*dst, a1.term); /* then sort term, 0 terminated */
+ *dst += strlen(a1.term) + 1;
+}
+
+static void sort_term_encode2(void *p, char **dst, const char **src)
+{
+ struct sort_term a1;
+
+ memcpy(&a1, *src, sizeof(a1));
+ *src += sizeof(a1);
+
+ zebra_zint_encode(dst, a1.sysno);
+ zebra_zint_encode(dst, a1.section_id);
+ zebra_zint_encode(dst, a1.length); /* encode length */
+ memcpy(*dst, a1.term, a1.length);
+ *dst += a1.length;
+}
+
+static void sort_term_decode1(void *p, char **dst, const char **src)
+{
+ struct sort_term a1;
+ size_t slen;
+
+ zebra_zint_decode(src, &a1.sysno);
+ a1.section_id = 0;
+
+ strcpy(a1.term, *src);
+ slen = 1 + strlen(a1.term);
+ *src += slen;
+ a1.length = slen;
+
+ memcpy(*dst, &a1, sizeof(a1));
+ *dst += sizeof(a1);
+}
+
+static void sort_term_decode2(void *p, char **dst, const char **src)
+{
+ struct sort_term a1;
+
+ zebra_zint_decode(src, &a1.sysno);
+ zebra_zint_decode(src, &a1.section_id);
+ zebra_zint_decode(src, &a1.length);
+
+ memcpy(a1.term, *src, a1.length);
+ *src += a1.length;
+
+ memcpy(*dst, &a1, sizeof(a1));
+ *dst += sizeof(a1);
+}
+
+static void sort_term_code_reset(void *p)
+{
+}
+
+static void sort_term_code_stop(void *p)
+{
+}
+
+struct sort_term_stream {
+ int no;
+ int insert_flag;
+ struct sort_term st;
+};
+
+static int sort_term_code_read(void *vp, char **dst, int *insertMode)
+{
+ struct sort_term_stream *s = (struct sort_term_stream *) vp;
+
+ if (s->no == 0)
+ return 0;
+
+ (s->no)--;
+
+ *insertMode = s->insert_flag;
+ memcpy(*dst, &s->st, sizeof(s->st));
+ *dst += sizeof(s->st);
+ return 1;
+}
+