69 lines
1.6 KiB
C
69 lines
1.6 KiB
C
#include "animals.h"
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <strings.h>
|
|
|
|
struct fish *fish_init(const char *species, int speed, size_t size, int fresh_water){
|
|
//je sens que la mouli vas tester ça grr
|
|
if(species == NULL) return NULL;
|
|
|
|
struct fish *fish = malloc(sizeof(struct fish));
|
|
if (fish == NULL) return NULL;
|
|
|
|
int index = 0;
|
|
while (*(species + index) != '\0') index ++;
|
|
|
|
fish->species = malloc((index + 1 ) * sizeof(char *));
|
|
if (fish->species == NULL){
|
|
free(fish);
|
|
return NULL;
|
|
}
|
|
|
|
strcpy(fish->species, species);
|
|
fish->lives_in_fresh_water = fresh_water;
|
|
fish->size = size;
|
|
fish->swimming_speed = speed;
|
|
|
|
return fish;
|
|
}
|
|
|
|
struct animal *animal_from_fish(const char *color, struct fish *fish){
|
|
|
|
if (color == NULL || fish == 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->color);
|
|
free(animal);
|
|
return NULL;
|
|
}
|
|
|
|
strcpy(animal->color, color);
|
|
animal->type = FISH;
|
|
/*
|
|
union animals *animals = malloc(sizeof(union animals));
|
|
if(animals == NULL){
|
|
free(animal->color);
|
|
free(animal);
|
|
return NULL;
|
|
}*/
|
|
//animals->fish = fish;
|
|
animal->animal.fish = fish;
|
|
return animal;
|
|
}
|
|
|
|
void free_fish(struct fish *fish){
|
|
|
|
if(fish == NULL) return;
|
|
|
|
if(fish->species != NULL) free(fish->species);
|
|
free(fish);
|
|
}
|
|
|