Index: branches/z3950-branch/gsdl/src/recpt/z3950server.cpp
===================================================================
--- branches/z3950-branch/gsdl/src/recpt/z3950server.cpp	(revision 1191)
+++ branches/z3950-branch/gsdl/src/recpt/z3950server.cpp	(revision 1342)
@@ -18,4 +18,6 @@
   // for now, assume that all records will have text associated with them.
   meta["hastxt"]="1";
+  // for now, assume we don't want ANY DocumentButtons.
+  format["DocumentButtons"]="";
 }
 
@@ -74,5 +76,58 @@
 }
 
-text_tarray *z3950_server::getbriefrecords(const text_t &query, 
+void z3950_server::parseQuery(const text_t &query, 
+			      const text_t &fields,
+			      text_t &parsed_query) {
+  /****** FIXME *****/
+  // We need to format the query string into RPN - 
+  // by just passing it like this, it will only work for simple queries.
+  // This will require us to actually come up with a query syntax and 
+  // a parser. For now, we'll just do an "AND" query for all terms
+
+  // we need to count number of terms separated by a space
+  char *ptr=query.getcstr();
+  int strlength=strlen(ptr);
+  bool inword=false;
+  int num_terms=0;
+  for (int i=0;i<strlength;i++) {
+    if (*(ptr+i)!=' ') {
+      if (inword==false) {
+	inword=true;
+	num_terms++;
+      }
+    }
+    else { // ptr+i is a space
+      inword=false;
+      }
+  }
+  
+  // set the field(s) to search on - main ones (that I've found) include:
+  //  1016  => Any
+  //  1     => (Personal) Name
+  //  4     => Title
+  //  21    => Subject Heading
+  //  45    => Subject precis
+  // Note I have no idea how these actually work - I think some servers
+  // only have limited fields, and map all subject-type requests into that
+  // subject field, etc.
+
+  parsed_query="@attr 1=";
+  if (fields==".author")
+    parsed_query+="1 ";
+  else if (fields==".title")
+    parsed_query+="4 ";
+  else // fields==".any"
+    parsed_query+="1016 ";
+
+  // append "@and" for each term after the first
+  for (int i=1;i<num_terms;i++)
+    parsed_query+="@and ";
+  // append the actual query
+  parsed_query+=query;
+
+}
+
+text_tarray *z3950_server::getrecordTitles(const text_t &query, 
+					   const text_t &fields,
 					   int first, int count,
 					   int *nummatches, comerror_t &err) {
@@ -83,4 +138,5 @@
   titles=new text_tarray;
 
+  /* check if connected */
   if (connected==false) 
     if (connect()==false) {
@@ -91,19 +147,28 @@
   
 
-  // We need to format the query string into RPN - 
-  // by just passing it like this, it will only work for simple queries.
-  /* check if connected */
-  /*  titles->push_back("first dummy title");
-      titles->push_back("Second dummy title");
-      return titles;
-  */
+  text_t expanded_query="";
+  parseQuery(query,fields,expanded_query);
+
   // following functions defined in yaz_zclient.c
-  *nummatches=z_cmd_dosearch(query.getcstr()); // returns # found, -1 on err.
-  // could just check if (*nummatches)==0.
-
+  *nummatches=z_cmd_dosearch(expanded_query.getcstr()); // returns # found, -1 on err.
+  if (*nummatches<=0) {
+    if (*nummatches==0) {
+      // no matches
+      return (NULL);
+    } else  if (*nummatches==-1) {
+      // prefix query error
+      err=protocolError;
+      return (NULL);
+    }  else if (*nummatches==-2) {
+      // sendsearchRequest not answered by searchResponse
+      err=protocolError;
+      return (NULL);
+    }
+  }
   // could do a sort eventually, eg on date, title, etc.
   // (non-existent function)    z_sort(field, asc|desc);
   /* min of (count, first + (*nummatches) ) */
-  c_str_titles=z_getbriefrecords(first,count);
+  // z_getrecordTitles ( first, howmany )
+  c_str_titles=z_getrecordTitles(first,min(count,*nummatches-first+1));
   if (c_str_titles==NULL) {
     // an error occurred. we need a logout/err as an arg
@@ -124,6 +189,8 @@
 }
 
-bool z3950_server::getfullrecord(const int ID, text_t &rettitle, 
-				text_t &rettext, comerror_t &err) {
+bool z3950_server::getfullrecord(const text_t &query, const text_t &fields,
+				 const int ID, 
+				 text_t &rettitle, 
+				 text_t &rettext, comerror_t &err) {
   if (connected==false) {
     if (connect()==false) {
@@ -134,27 +201,37 @@
     // since we have just re-connected, we need to do the
     // query again.
+
+    text_t expanded_query="";
+    parseQuery(query,fields,expanded_query);
+
+    int returned=z_cmd_dosearch(expanded_query.getcstr());
+    if (returned<=0) {
+      // 0 => none.
+      // <0 => error
+      err=protocolError;
+      return (false);
+    }
   }
 
   if (rettitle!="unneeded") {
-    /*char **c_str_titles;
-      int dummy;
-      c_str_titles=z_getbriefrecords("the",ID,1,&dummy); // check this return value.
-      if (c_str_titles!=NULL && (int)c_str_titles[0]==1) {
+    char **c_str_titles;
+    //int dummy;
+    c_str_titles=z_getrecordTitles(ID,1); // check this return value.
+    if (c_str_titles!=NULL && (int)c_str_titles[0]==1) {
       rettitle.setcstr(c_str_titles[1]); // and check this 
       free (c_str_titles);
-    */
-    //    rettitle=(*titles)[ID]; // this isn't quite right, as ID isn't the offset
-    rettitle="Dummy Title";
-  }
-  
+      // rettitle="Dummy Title";
+    } else {
+      // we didn't get something....
+      rettitle="Nothing Returned...";
+    }
+  }
+
   if (rettext!="unneeded") {
     // get the text
-    rettext="Dummy Text. This is here as a placeholder until I work out";
-    rettext+=" a way to get the query again when running in simple cgi\n";
-    rettext+="as after each page is completed, the connection is lost to the";
-    rettext+=" server, and we forget what the query was\n";
+    rettext.setcstr(z_getfullRecord(ID));
   }
   return (true);
-}
+  }
 
 text_t &z3950_server::getzAbout() {
