#include <sys/types.h>
#include <ctype.h>
#include "danlib.h"

/* Some generally useful routines */
  
void initStr(s,size)
char **s;
int size;
{
  /* allocate space for a string */
  if ((*s = (char *)malloc((size_t) (size + 1) * sizeof(char))) == NULL)
    die("\n Memory Error: Couldn't allocate string\n","");

  **s = '\0';
}

int isAllNums(s)
char * s;
{
/* returns true if a string contains only digits, dashes and newlines */
  int n = 0, len = strlen(s);
  
  for(n=0;
      (n < len)  &&  ( isdigit(s[n]) || (s[n] == '-') || (s[n] == '\n'));
      n++);
  return (n == len);
}

char * numSuffix(i)
int i;
{
/* returns he proper ordinal suffix of a number */
  if ((i / 10) == 1) return "th";
  switch (i % 10) {
  case 1 : return "st";
  case 2 : return "nd";
  case 3 : return "rd";
  default: return "th";
  }
}

char * itoa (i) 
int i;
{
  static char ret[7];
  int c = 0;
  char tmp;

  if (i < 0) {			/* is i negative? */
    ret[c++] = '-';		/* then put a minus */
    i = abs(i);
  }
  
  while (i >0) {		/* compose the number */
    ret[c++] = (i % 10) + '0';
    i /= 10;
  }
  i = c-1;
  
  for (c =0; c < i; c++) {	/* reverse the number */
    tmp = ret[c];
    ret[c] = ret[i-c];
    ret[i-c] = tmp;
  } 

  return ret;
}
 

