/*
 * Cours Langage C - AIR1
 * C. Recanati 27/08/2019 12:43
 * TP4 exo2
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#define MAX_SIZE_LINE 240
#define MAXLINE	240

struct s_mot {
	char * mot; 
	struct s_mot *suivant;
} ;	
typedef struct s_mot element; 
typedef  element*  liste_mots;

void afficheListe(liste_mots list);
liste_mots creer(char ligne[], int n);
element * newElem(char * mot);
liste_mots add(liste_mots ls, element* nouv);

int main() {	
	char nom_fichier[40];
	char ligne[MAX_SIZE_LINE];
 	char *s;	
 	FILE *fichier;
 	
	liste_mots  liste = NULL;
	int nb_line=0; 
	liste_mots tab[MAXLINE];	

	printf("Entrez un nom de fichier texte: ");
	scanf("%s", nom_fichier);
 	fichier = fopen(nom_fichier, "r");
 	if (fichier == NULL) {
 		printf("impossible d'ouvrir %s ! erreur %d: %s\n", 
 					nom_fichier, errno, strerror(errno));
 		return errno;
 	}
 	
 	/* lire une ligne et la mettre dans ligne[]*/
	s = (char *) malloc(sizeof(char) * MAX_SIZE_LINE);
	ligne[MAX_SIZE_LINE-1]='\0';
	while ((s = fgets(ligne, MAX_SIZE_LINE, fichier)) != 0) {
		/* pour chaque ligne du fichier */
		ligne[strlen(ligne)-1] = '\0';
		liste = creer(ligne, strlen(ligne));
		tab[nb_line++] = liste;
	}
	tab[nb_line] = NULL; /* inutile, mais dernier element */
	for(int i=0; i < nb_line; i++)
		afficheListe(tab[i]);
	
	if (fclose(fichier) == EOF)
		printf("erreur de fermeture du fichier %s\n", nom_fichier);
	return EXIT_SUCCESS;
}

liste_mots creer(char ligne[], int n) {
		char *p,*q, *deb;
		char motlu[25];
		int i;
		liste_mots list = NULL;
		
		if (n ==0)
			return NULL;
		for (p= ligne; *p != '\0' ; p++) {	/* p parcourt la ligne */
			/* on recherche un mot */		
			for (q = p; *q == ' '; q++)
				; 		/* on "mange" les blancs (s'il y en a au debut) */		
			deb = q; /* le debut du mot eventuel suivant */
			i = 0; /* nombre de lettres du mot qu'on va mettre dans motlu */
			while ((*q != ' ') && (*q != '\0') ) {
				motlu[i++] =*q;
				q++; 	/* on avance q jusqu'a trouver un blanc (ou la fin) */
			}
			motlu[i] = '\0';
			if (i!= 0)
				list = add(list, newElem(motlu)); /* ajout en tete */
			if (q-deb >0) { /* equivalent sans doute a n != 0 */
			/* alors on vient de franchir un mot d'au moins 1 caractere */
				p = q - 1; /* on avance p sur le dernier caractere du mot */			
			/* au prochain tour de boucle for, p sera incremente */
			/* et pointera finalement sur le meme char que q (celui après le mot) */
			}
		};
		return list;
}

element * newElem(char * motlu) {
	element * nouvElem = (element *) malloc(sizeof(element));
	char* mot = (char*) malloc (strlen(motlu)*sizeof(char));
	nouvElem->mot = strcpy(mot, motlu); /* mot alloue statiquement */
    nouvElem->suivant = NULL;
    return nouvElem;
}

liste_mots add(liste_mots ls, element* nouv) {
	nouv -> suivant = ls;
	return nouv;
}

void afficheListe(liste_mots list) {
	element * p  = list;
	
	if (p==NULL)
		return;	
	printf("(%s", p->mot);
	while ( (p = p->suivant) != NULL) 
      	printf(",%s",  p->mot); // les suivants...
  	printf(")\n");
  	return;
}    
