mylib.c
contents ::
  asgn3.c
  tree.c
  tree.h
  mylib.c
  mylib.h

/***************************************************************************
 *                                                                         *
 *     Tree Program in C                                                   *
 *                                                                         *
 *     COSC 242 Assignment 20/09/01                                        *
 *                                                                         *
 *     JAMES LITTLE                                                        *
 *                                                                         *
 ***************************************************************************/

#include <stdlib.h>
#include "mylib.h"


/**************************
 *                        *
 *   emalloc              *
 *                        *
 **************************

 Used to handle new memory allocation to a pointer and handle exceptions 
 which may arise if memory allocation fails, which happens all the time 
 when you try and enter negative values. 
 
 PARAMETERS: s       = calculated size of required memory. 

 RETURN VALUE: a pointer of any type.

*/

void *emalloc(size_t s){
  void *result = malloc(s);
  if(NULL == result){
    fprintf(stderr, "Memory allocation failed!\n");
    exit(EXIT_FAILURE);
  }
  return result;
}

/**************************
 *                        *
 *   erealloc             *
 *                        *
 **************************

 Handles the reallocation of an updated amount of memory to an existing
 with existing data attached.
 
 PARAMETERS: p       = the existing pointer we would like additional memory
                       allocated to.
             s       = calculated size of required memory. 

 RETURN VALUE: a pointer of any type.

*/

void *erealloc(void *p, size_t s){
  void *result = realloc(p,s);
  if(NULL == result){
    fprintf(stderr, "Memory allocation failed!\n");
    exit(EXIT_FAILURE);
  }
  return result;
}

/**************************
 *                        *
 *   getword              *
 *                        *
 **************************

 Is used to read input from the designated file stream (standard in for the 
 assignment). Removes white space with the first while loop. And only
 returns one word. Maintaining continuous input is therefore the responsibility
 of the calling function.
 
 PARAMETERS: s       = the pointer to the character array.
             limit   = the maximum size a word can be.
              stream  = where to read from.

 RETURN VALUE: the integer value of the character at s[0]. The string s does
               not need to be returned, since it is an array and is passed as 
               a memory address. If no chars were read into the string s, then
                s[0] would have the '\0' [NULL] value which equates to 0 if used
                in a boolean equation.

*/

int getword(char *s, int limit, FILE *stream){
  int c;

  while(!isalnum( c = getc(stream)) && c != EOF);

  if(c == EOF)
    return EOF;
  else
    *s++ = tolower(c);

  while(--limit > 0){
    if(isalnum(c = getc(stream)))
      *s++ = tolower(c);
    else if('\'' == c) continue;
    else break;
  }
  *s = '\0';
  return s[0];
}

/**************************
 *                        *
 *   stoi                 *
 *                        *
 **************************

 Not using this function now.
 
 PARAMETERS: s       = string representation of a number. 

 RETURN VALUE: the integer value.

*/

int stoi(char *s){
  int i,j,r;
  int sign = 1;
  i=1;
  r=0;

  for(j=my_strlen(s)-1; j>=0; j--){
    if(j == 0 && s[j] == '-')
      sign = -1;
    else {
      if(s[j]>='0' && s[j]<='9'){
         r+=((s[j]-'0')*i);
         i*=10;
      } else {
         fprintf(stderr, "Invalid input for String to int\n");
         exit(EXIT_FAILURE);
      }
    }
  }
  return r * sign;
}

/**************************
 *                        *
 *   my_strlen            *
 *                        *
 **************************

 I am using my own string length function, but I wrote it with the stoi
 function, so am using it here. Perhaps not as safe as the library 
 functions?
 
 PARAMETERS: s       = a string delimited by the NULL character. 

 RETURN VALUE: the number of characters in the string.

*/

int my_strlen(char *s){
  int i=0;
  while(s[i]!='\0')
   i++;
  return i;
}

/**************************
 *                        *
 *   factors              *
 *                        *
 **************************

 Another unrequired function used to calculate the possibility of 
 factorisation.
 
 PARAMETERS: x       = An integer to be factored towards. 

 RETURN VALUE: 0 if x has factors, 1 if x is a prime number.

*/


static int factors(int x){
  int f = 2;
  while(f*f < x){
    if(x % f == 0){
      return 0;
    } else { 
      f++;
    }
  }
  return 1;
}

/**************************
 *                        *
 *   prime_gt             *
 *                        *
 **************************

 Used in conjunction with factors to find factor-less integers. We increment
 bound until it is truly prime.

 Bound   - We start with bound, sending it to the factors function. If it is 
           a prime number, then stop searching. Otherwise loop until we find
            an prime integer larger than the input integer.
 
 PARAMETERS: s       = the input integer. 

 RETURN VALUE: Bound, for it is now a prime number.

*/

static int prime_gt(int s){
  int bound = s;
  while(bound > 0){
    if(factors(bound))
      break;
    else 
      bound++;
  }
  return bound;
}

/**************************
 *                        *
 *   relative_prime       *
 *                        *
 **************************

 Decides on a prime number to use to set the table size to.
 
 PARAMETERS: s       = the required size of the table. 

 RETURN VALUE: the newer better prime number size for the table.

*/

unsigned int relative_prime(int s){
  return prime_gt(s);
}

James Little