//
//  gestions_fichiers.c
//  labyrinthe
//
//  Created by léo LELLOUCHE on 11/04/2016.
//  Copyright © 2016 léo LELLOUCHE. All rights reserved.
//

#include "header.h"

void    save_path(t_list path, int fd)
{
    
    dprintf(fd, "%d\n%d\n", path.line, path.col);
    if (path.next != NULL)
        save_path(*path.next, fd);
}

int     *ouverture_fichiers(int type)
{
    int *fd;
    
    fd = malloc(sizeof(int) * 5);
    
    if (type == 1)
    {
        fd[0] = open("map.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU | S_IRGRP | S_IROTH);
        fd[1] = open("chemin.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU | S_IRGRP | S_IROTH);
        fd[2] = open("position.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU | S_IRGRP | S_IROTH);
        fd[3] = open("sizes.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU | S_IRGRP | S_IROTH);
        fd[4] = open("current_affichage.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU | S_IRGRP | S_IROTH);
        fd[5] = open("vie.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU | S_IRGRP | S_IROTH);
    }
    if (type == 2)
    {
        fd[0] = open("map.txt", O_RDWR);
        fd[1] = open("chemin.txt", O_RDWR);
        fd[2] = open("position.txt", O_RDWR);
        fd[3] = open("sizes.txt", O_RDWR);
        fd[4] = open("current_affichage.txt", O_RDWR);
        fd[5] = open("vie.txt", O_RDWR);
    }
    
    if ((fd[0] == -1) || (fd[1] == -1) || (fd[2] == -1) || (fd[4] == -1))
    {
        printf("Erreur Ouverture Fichiers");
        return (NULL);
    }
    return (fd);
}
// On ecrira le fichier size de la manière suivant
// SIZE_MAP
// SIZE_PATH


void    lecture_fichier(int fd, char **tab)
{
    
    int ret;
    static int position = 0;
    
    ret = get_next_line(fd, &tab[position]);
    
    if (ret == 1)
    {
        position++;
        lecture_fichier(fd, tab);
    }
    else
        position = 0;
}

