/*
** Species.c
*/

# include <stdlib.h>
# include <stdio.h>
# include <string.h>
# include "Species.h"

/*@only@*/ Species Species_new (/*@only@*/ char *name,
				/*@only@*/ char *genome) {
  Species s = (Species) malloc (sizeof (*s));
  if (s == NULL) { 
    fprintf (stderr, "Out of memory in Species_new.\n");
    exit(EXIT_FAILURE); 
  }
  
  s->name = name;
  s->genome = genome;
  return s;
}

void Species_markOwned (/*@owned@*/ /*@unused@*/ Species s) {
  ; /* Should record this species in a table and free it later.  This is a memory leak. */
  /*@-mustfreeonly@*/
} /*@=mustfreeonly@*/

void Species_free (/*@only@*/ Species s) {
  free (s->name);
  free (s->genome);
  free (s);
}

const char *Species_getName (Species s) {
  return s->name;
}

const char *Species_getGenome (Species s) {
  return s->genome;
}

char *Species_toString (Species s) {
  size_t length = strlen (s->name) + strlen (s->genome) + 4;
  char *res = (char *) malloc (sizeof (*res) * length);

  if (res == NULL) {
      fprintf (stderr, "Out of memory in Species_toString.\n");
      exit(EXIT_FAILURE); 
    }

  (void) snprintf (res, length, "%s [%s]", s->name, s->genome);
  return res;
}