static const char* version_id =
  "$Id: string-transfer.cc,v 1.9 1999/09/21 18:52:29 v2kea318 Exp $";

// $Log: string-transfer.cc,v $
// Revision 1.9  1999/09/21  18:52:29  v2kea318
// Added static versions of the read_int and write_int functions.  We should
// only send 4 byte data across the network, but the public versions of these
// functions use size_t, which is 8 bytes on many/most machines.  So these
// size_t values are internally cast to int's and the new versions are called.
//
// Revision 1.8  1999/08/06  15:27:32  v2kea318
// Added the read and write functions for int's (actually size_t's).
//
// Revision 1.7  1999/07/30  20:24:09  v2kea318
// Finished converting to G++ compliant code.
//
// Revision 1.6  1999/07/09  14:53:26  v2kea318
// Eliminated the bool flag on read_string.  The routine now always uses the
// read_force function.
//
// Revision 1.5  1999/07/07  18:59:44  v2kea318
// Use the read_force function (from the Socket.h header file) to read the
// number of bytes and the string (the latter if retry is true).  This results
// in much simpler code.
//
// Revision 1.4  1999/07/07  15:42:39  v2kea318
// Instead of creating a vector of the appropriate size, reading the data
// into that vector, and then copying the vector to the string object, simply
// reconstruct the string object to the appropriate size (with the placement
// new operator) and read the string directly.
//
// Revision 1.3  1999/07/07  14:46:07  v2kea318
// Fixed the read_string routine to work correctly.  The exception must be
// caught inside the while loop!
//
// Revision 1.2  1999/07/06  19:51:01  v2kea318
// Changed read_string() to have a boolean flag retry.  If this flag is true
// and we take a SocketIOBadNBytesException exception on the read of the string,
// we will try again from where we left off.  Otherwise, we will just rethrow
// the exception.
//
// Revision 1.1  1999/05/07  16:16:03  v2kea318
// Initial revision
//
//***************************************************************************

#include "string-transfer.h"
#include <vector>

//***************************************************************************

void write_string(const Socket& sock, const string& s)
{
  // send the number of bytes.
  write_int(sock, s.size());

  // write the string.
  sock.write(s.c_str(), s.size());
}

//***************************************************************************

void read_string(const Socket& sock, string& s)
{
  // read the string size.
  size_t nbytes;
  read_int(sock, nbytes);

  // reconstruct the string object with sufficient memory.
  new (&s) string(nbytes, '\0');

  // read the string.
  read_force(sock, &s[0], nbytes);
}

//***************************************************************************

static void write_int(const Socket& sock, const int i)
{
  write(sock, htonl(i));
}

//***************************************************************************

void write_int(const Socket& sock, const size_t i)
{
  /* We should only send 4 bytes, but size_t is 8 bytes.  So, internally
     convert the number to an int type and write it. */
  write_int(sock, static_cast<const int>(i));
}

//***************************************************************************

static void read_int(const Socket& sock, int& i)
{
  int r;
  read_force(sock, r); i = ntohl(r);
}

//***************************************************************************

void read_int(const Socket& sock, size_t& i)
{
  int r;
  read_int(sock, r);
  i = static_cast<size_t>(r);
}
