/* 
 * University of Leipzig: utils.cc, v0.1b  21.9.97 22:58 
 * 
 * 
 * Copyright 1997 by University of Leipzig
 * 
 * Permission to use, copy, modify, and distribute this software for noncommercial 
 * use and without fee is hereby granted, provided that the above copyright notice 
 * appear in all copies and that both that copyright notice and this permission 
 * notice appear in supporting documentation, and that the name of the U.L. not be 
 * used in advertising or publicity pertaining to distribution of the software 
 * without specific, written prior permission. The U.L. makes no representations about
 * the suitability of this software for any purpose.  It is provided "as is" 
 * without expressed or implied warranty. 
 * 
 * 
 * pantec@aix520.informatik.uni-leipzig.de   (Thomas Pantzer) 
 * 
 */




#include "utils.h"

#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

// Kommentar eigentlich total überflüssig
int __max(int a, int b)		// could be a macro
{
  if (a>b)
     return a;
   else 
     return b;
}       

int __min(int a, int b)
{
  if (a<b)
     return a;
   else 
     return b;
}       


char unit_prefix[8] = " kMGTE";

void print_kM(double d)	// print a number in %3.1f format with proper unit prefixes to stdout
{
  int i=0;
  double f = d;
  
  while (f > 1000.0)
    {
     i++;
     f /= 1000.0;
    } 
    
  printf("%3.1f%c",f,unit_prefix[i]);  
}



long power(int base, int exp)	// returns base^exp
{
   long s = 1;

   for (int i=0; i<exp; i++)
     s *= base;
  
   return s;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ist wahr, wenn es aus der Datei des Filedescriptors 'was zu lesen gibt  
// gewartet wird ca. 10ms
//______________________________________________________________________
int FD_Ready(int fd)		// return true if input data available
{
	return FD_Ready(fd,10000);
}


int FD_Ready(int fd, int to)	// same with user timeout in µsec
{
  struct fd_set Read_Set;
  struct timeval TimeOut;

	if (fd < 0)
	   return false;	// not a valid file descriptor

//  FD_ZERO(&Read_Set);
  FD_SET(fd,&Read_Set);
  TimeOut.tv_sec=0;
  TimeOut.tv_usec=to;

//#ifdef hpux
//  select(fd+1,(int *)&Read_Set,0,0,&TimeOut);
//#else
  select(fd+1,&Read_Set,0,0,&TimeOut);
//#endif    
  if FD_ISSET(fd,&Read_Set)
       return 1;
    else
       return 0;   
}

int FD_Writeable(int fd)	// return true if fd is _IMMEDEATLY_ writeable
{
  struct fd_set Write_Set;
  struct timeval TimeOut;

	if (fd < 0)
	   return false;	// not a valid file descriptor

  FD_ZERO(&Write_Set);
  FD_SET(fd,&Write_Set);
  TimeOut.tv_sec=0;
  TimeOut.tv_usec=10000;

//#ifdef hpux
//  select(fd+1,(int *)&Read_Set,0,0,&TimeOut);
//#else
  select(fd+1,0,&Write_Set,0,&TimeOut);
//#endif    
  if FD_ISSET(fd,&Write_Set)
       return 1;
    else
       return 0;   
}


char fexist(char *s)		// return true if file "s" exists
{
	if (s == NULL)
	   return false;

	struct stat buf;

	if (stat(s,&buf) == -1)
	   return false;

	return true;		
}



char strnrcmp(char *s1, char *s2, unsigned int len)
{
	if ((s1 == NULL) || (s2 == NULL))
	   return false;

	if ((strlen(s1) < len) || (strlen(s2) < len) )
	   return false; 

//	char st[256];
//	strcpy(st,s2);
	char *s3 = s2;
	s3 += strlen(s2);
	s3 -= len;

#ifdef DEBUG
	fprintf(stderr,">%s< >%s<\n",s1,s3);
#endif
	if (strncmp(s1,s3,len) == 0)
	   return true;

	return false;
}

