+ struct rset_between_rfd *p=(struct rset_between_rfd *)rfd;
+ struct rset_between_info *info =p->info;
+ int cmp;
+ int thisterm=0;
+ int which;
+ *term=0; /* just in case, should not be necessary */
+#if RSBETWEEN_DEBUG
+ yaz_log(log_level,"btw: == read: term=%p",term);
+#endif
+ while ( read_anded(p,buf,&thisterm,&which) )
+ {
+#if RSBETWEEN_DEBUG
+ yaz_log(log_level,"btw: read loop term=%p d=%d ad=%d",
+ term, p->depth, p->attrdepth);
+#endif
+ if (p->hits<0)
+ {/* first time? */
+ memcpy(p->recbuf,buf,info->key_size);
+ p->hits=0;
+ cmp=2;
+ }
+ else {
+ cmp=(*info->cmp)(buf,p->recbuf);
+#if RSBETWEEN_DEBUG
+ yaz_log(log_level, "btw: cmp=%d",cmp);
+#endif
+ }
+
+ if (cmp>=2)
+ {
+#if RSBETWEEN_DEBUG
+ yaz_log(log_level,"btw: new record");
+#endif
+ p->depth=0;
+ p->attrdepth=0;
+ memcpy(p->recbuf,buf,info->key_size);
+ }
+
+#if RSBETWEEN_DEBUG
+ yaz_log(log_level,"btw: which: %d", which);
+#endif
+ if (which==WHICH_L)
+ {
+ p->depth++;
+#if RSBETWEEN_DEBUG
+ yaz_log(log_level,"btw: read start tag. d=%d",p->depth);
+#endif
+ memcpy(p->startbuf,buf,info->key_size);
+ p->startbufok=1;
+ checkattr(rfd); /* in case we already saw the attr here */
+ }
+ else if (which==WHICH_R)
+ {
+ if (p->depth == p->attrdepth)
+ p->attrdepth=0; /* ending the tag with attr match */
+ p->depth--;
+#if RSBETWEEN_DEBUG
+ yaz_log(log_level,"btw: read end tag. d=%d ad=%d",
+ p->depth, p->attrdepth);
+#endif
+ }
+ else if (which==WHICH_A)
+ {
+#if RSBETWEEN_DEBUG
+ yaz_log(log_level,"btw: read attr");
+#endif
+ memcpy(p->attrbuf,buf,info->key_size);
+ p->attrbufok=1;
+ checkattr(rfd); /* in case the start tag came first */
+ }
+ else
+ { /* mut be a real hit */
+ if (p->depth && p->attrdepth)
+ {
+ p->hits++;
+#if RSBETWEEN_DEBUG
+ yaz_log(log_level,"btw: got a hit h=%d d=%d ad=%d t=%d+%d",
+ p->hits,p->depth,p->attrdepth,
+ info->rset_m->no_rset_terms,thisterm);
+#endif
+ *term= info->rset_m->no_rset_terms + thisterm;
+ return 1; /* everything else is in place already */
+ } else
+ {
+#if RSBETWEEN_DEBUG
+ yaz_log(log_level, "btw: Ignoring hit. h=%d d=%d ad=%d",
+ p->hits,p->depth,p->attrdepth);
+#endif
+ }
+ }
+ } /* while read */
+
+ return 0;
+
+} /* r_read */