Index: branches/z3950-branch/gsdl/src/recpt/z3950server.cpp
===================================================================
--- branches/z3950-branch/gsdl/src/recpt/z3950server.cpp	(revision 1174)
+++ branches/z3950-branch/gsdl/src/recpt/z3950server.cpp	(revision 1191)
@@ -14,4 +14,8 @@
 z3950_server::z3950_server() {
   info=NULL;
+  connected=false;
+  titles=NULL;
+  // for now, assume that all records will have text associated with them.
+  meta["hastxt"]="1";
 }
 
@@ -28,45 +32,163 @@
 }
 
-void z3950_server::addAbout(const text_t &lang, const text_t &abouttext) {
+void z3950_server::addcfgAbout(const text_t &lang, const text_t &abouttext) {
     about[lang]=abouttext;
 }
 
-const text_t &z3950_server::getAbout(const text_t &lang) {
+bool z3950_server::getcfgAbout(const text_t &lang, text_t &abouttxt) {
   text_tmap::iterator it;
   it=about.find(lang);
-  return ((*it).second);
+  if (it==about.end()) return (false);
+  abouttxt=((*it).second);
+  return (true);
 }
 
-text_t *z3950_server::connect_getAbout() {
-  // Assume we have not yet connected, so that must be done here.
-  text_t *returned;
-  returned=new text_t;
+
+// now functions that actually talk over the tcp connection.
+
+// create a tcp connection to the associated target. Currently, this will
+// re-initialise if we are already connected.
+bool z3950_server::connect() {
+  text_t server_and_port;
+  char *zserverinfo;
+
+  server_and_port=info->host+":"+info->port;
+  // remember that info.name is the database name
 
   z_initialize();
-  text_t server_and_port;
-  server_and_port=info->host+":"+info->port;
-  // remember that info.name is the database name
-  if (z_cmd_open(server_and_port.getcstr(),info->name.getcstr())==1) {
+
+  if (z_cmd_open(server_and_port.getcstr(),info->name.getcstr())==1)
     // we got a connection error
-    returned->setcstr("<H2>Server offline</H2>Error - could not connect to server <B>");
-    (*returned) += info->host.getcstr();
-    (*returned) += "</B> on port ";
-    (*returned) += info->port;
-    (*returned) += "\n";
-    return (returned);
+    return false;
+
+  // get initialisation response.
+  z_getnextAPDU();
+  zserverinfo=z_get_initResponse();
+  if (zserverinfo!=NULL) {
+    z_initstr.appendcstr(zserverinfo);
+  }
+  free(zserverinfo);
+
+  connected=true;
+  return true;
+}
+
+text_tarray *z3950_server::getbriefrecords(const text_t &query, 
+					   int first, int count,
+					   int *nummatches, comerror_t &err) {
+  char **c_str_titles;
+  int i;
+  int last;
+  if (titles!=NULL) delete (titles);
+  titles=new text_tarray;
+
+  if (connected==false) 
+    if (connect()==false) {
+      // we could not connect.
+      err=protocolError;
+      return (NULL);
+    }
+  
+
+  // 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;
+  */
+  // following functions defined in yaz_zclient.c
+  *nummatches=z_cmd_dosearch(query.getcstr()); // returns # found, -1 on err.
+  // could just check if (*nummatches)==0.
+
+  // 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);
+  if (c_str_titles==NULL) {
+    // an error occurred. we need a logout/err as an arg
+    return (NULL);
+  }
+  if (c_str_titles[0]==0) {
+    // no matches.
+    return (NULL);
+  }
+  last=(int)c_str_titles[0];
+  for (i=1;i<=last;i++) {
+    titles->push_back(c_str_titles[i]);
+    free(c_str_titles[i]);
+  }
+  free(c_str_titles);
+  return (titles);
+  
+}
+
+bool z3950_server::getfullrecord(const int ID, text_t &rettitle, 
+				text_t &rettext, comerror_t &err) {
+  if (connected==false) {
+    if (connect()==false) {
+      // error connecting...
+      err=protocolError;
+      return (false);
+    }
+    // since we have just re-connected, we need to do the
+    // query again.
   }
 
-  z_getnextAPDU(0);
-  (*returned)+="Internet server: <b>";
-  (*returned)+=info->host;
-  (*returned)+="</b> on port ";
-  (*returned)+=info->port;
-  (*returned)+=".<br>\n";
-  (*returned)+=z_get_initResponse();
-  if (returned==NULL) {
-    cerr << "Null was returned\n";
+  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) {
+      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";
   }
-  // should close
-  z_cmd_close(0);
-  return (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";
+  }
+  return (true);
 }
+
+text_t &z3950_server::getzAbout() {
+  text_t zserverresp;
+
+  // Assume we have not yet connected, so that must be done here.
+  if (connected==true)
+    return (z_initstr);
+  
+  // we need to create the tcp connection to the target (server)  
+  //  z_initstr=new text_t;
+  
+  if (connect()==false) {
+    z_initstr.setcstr("<H2>Server offline</H2>Error - could not connect to server <B>");
+    z_initstr += info->host.getcstr();
+    z_initstr += "</B> on port ";
+    z_initstr += info->port;
+    z_initstr += "\n";
+    return (z_initstr);
+  }
+  
+  // z_initstr currently contains the target's response. We want to
+  // PREPEND the following information.
+  zserverresp=z_initstr;
+  z_initstr="Internet server: <b>";
+  z_initstr+=info->host;
+  z_initstr+="</b> on port ";
+  z_initstr+=info->port;
+  z_initstr+=".<br>\n";
+  z_initstr+=zserverresp;
+
+  // should close /******* WHAT IF DOING A QUERY!??!?!? ********/
+  // z_cmd_close(0);
+  // connected=false;
+  return (z_initstr);
+}
+
