61 lines
1.4 KiB
C
61 lines
1.4 KiB
C
#include "animals.h"
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
struct insect *insect_init(const char *species, size_t size, int can_fly, int can_swim){
|
|
|
|
if(species == NULL) return NULL;
|
|
|
|
struct insect *insect = malloc(sizeof(struct insect));
|
|
if (insect == NULL) return NULL;
|
|
|
|
int index = 0;
|
|
while (*(species + index) != '\0') index ++;
|
|
|
|
insect->species = malloc((index + 1)*sizeof(char*));
|
|
if (insect->species == NULL) {
|
|
free(insect);
|
|
return NULL;
|
|
}
|
|
|
|
strcpy(insect->species, species);
|
|
insect->size = size;
|
|
insect->can_fly = can_fly;
|
|
insect->can_swim = can_swim;
|
|
|
|
return insect;
|
|
}
|
|
|
|
struct animal *animal_from_insect(const char *color, struct insect *insect){
|
|
|
|
if (color == NULL || insect == NULL) return NULL;
|
|
|
|
struct animal *animal = malloc(sizeof(struct animal));
|
|
if(animal == NULL) return NULL;
|
|
|
|
int index = 0;
|
|
while(*(color + index) != '\0') index ++;
|
|
|
|
animal->color = malloc((index + 1) * sizeof(char*));
|
|
if(animal->color == NULL){
|
|
free(animal);
|
|
return NULL;
|
|
}
|
|
|
|
strcpy(animal->color, color);
|
|
animal->type = 0;
|
|
animal->animal.insect = insect;
|
|
return animal;
|
|
}
|
|
|
|
void free_insect(struct insect *insect){
|
|
|
|
if (insect == NULL) return;
|
|
|
|
//Normalement il ne devrait pas etre NULL
|
|
//if(insect->species != NULL) free(insect->species);
|
|
free(insect->species);
|
|
free(insect);
|
|
}
|