// File: util.C 

#include "util.h"

/////////////////////////////////////////////////////////////////////////
// WriteUrlToFile
//
// Writes out the url as a text string to a file, so anouther process
// can read the file to determine where I live.
// Returns 1 if successful else 0;
/////////////////////////////////////////////////////////////////////////
int WriteUrlToFile(char *file, char *host, unsigned short port) 
{
  fstream urlOut(file, ios::out);

  if (urlOut.fail())  
    return 0; // Failure

  urlOut << host << " " << port << endl;
  urlOut.close();
  return 1; // Success
}
  
/////////////////////////////////////////////////////////////////////////
// ReadUrlFile
//
// Reads the above written file containing a url. 
/////////////////////////////////////////////////////////////////////////
int ReadUrlFile(char *file, char *host, unsigned short *port) 
{
  //  fstream urlIn(file, ios::in);
  //
  //  if (urlIn.fail()) 
  //    return 0; // failure

  //  urlIn.close();

  FILE *fp = NULL;
  int   tPort = 0;

  if ((fp = fopen(file, "r")) == (FILE *) NULL)
    return 0;

  fscanf(fp,"%s %u\n",host,&tPort);
  fclose(fp);

  *port = (short)tPort;
  //  cout << host << ":" << *port << endl;
  
  return 1; // Success
} 
    
