/***************************************************************
 * PrDataInput.h
 *
 * This class provides Data Input methods.
 *
 ****************************************************************/

/*
 * modification history
 *----------------------
 *
 * 02/14/98: K.C. Chang: created.
 */

#ifndef _PrDataInput_h
#define _PrDataInput_h

#include "PrGlobalDefn.h"
#include "PrNetworkDefn.h"

#define PR_DATAINPUT_OK    1
#define PR_DATAINPUT_ERROR 0

class PrSocket;

class PrDataInput
{
public:
  PrDataInput( PrSocket *socket, int bufferSize = PR_NETWORK_BUFFER_SIZE );
  ~PrDataInput();

  int flag() { return flag_; }

  int select( long msec );

  // number of data bytes
  int ready();

  // number of data bytes
  int bufferSize( int nbytes );

  int type() { return type_; }

  // returns nbytes

  int readChar( char *data, int num ) { return Unpack(data,num,1); }
  int readShort( short *data, int num ) { return Unpack((char *)data,num,2); }
  int readInt( int *data, int num ) { return Unpack((char *)data,num,4); }
  int readLong( long *data, int num ) { return Unpack((char *)data,num,4); }
  int readFloat( float *data, int num ) { return Unpack((char *)data,num,4); }
  int readDouble( double *data, int num ) { return Unpack((char *)data,num,8); }

  //  int readLine( char *ptr, int maxlen );

private:
  int flag_;
  int socketFD_;
  char *buffer_;
  int bufferSize_;
  int byteCounter_;
  short size_;
  short type_;
  int endian_;
  int readSize_;

  int Init( int nbytes );
  int InitByteCounter();

  int Peek( char *buff, int nbytes );
  int Peek();

  int Receive();

  int Endian();

  char   Unpack1B( char *text );
  short  Unpack2B( char *text );
  long   Unpack4B( char *text );
  double Unpack8B( char *text );

  int Unpack( char *data, int num, int size );

  // copy is not allowed
  PrDataInput( const PrDataInput & ) {}
};

#endif // _PrDataInput_h



