#include "io.h"


int tailleG()
	{
	int n;
	printf("\nOrdre n de G:\t");
	scanf("%d",&n);
	return(n);
	}
	
void display (G g)
{
	int i,gf; /*gf comme garde fou */
	struct cellHorizontal *pt;
	
	
	printf("\n --------------");
	printf("\n Display Graph:(adr=%ld) :",g);
	printf("\tn=%d\tm=%d\tnbCouleur=%d\n",g.n,g.m,g.nbcol);
	printf(" --------------\n");
	for(i=1;i<=g.n;i++)
		{
		printf("\ni=%d : adr=%d,dg=%d,color=%d voisins ADR:[%d] :\t",i,&(g.content[i]),g.content[i].aux,g.content[i].color,g.content[i].next);
		pt=g.content[i].next;gf=1;
		while ((gf<=g.n) && (pt!=NULL))
			{
			printf(" %d,[%d],next=%d ",pt,(*pt).sommet,(*pt).next);
			pt=(*pt).next;gf++;
			}
		}
}

/*-------------------------------------------------
					File access 
convention: description d'un graphe dans graph.txt
	ordreDuGraphe
	numeroDuSommet Degré voisin1 voisin2 ... voisinDegré
	etc.
	-1
	
.-1 est le terminateur de description de graphe
-------------------------------------------------*/

void DisplayFileContent()
	{
	FILE *fp;
	int i,n,som,dg,ng;/*som=sommet, dg=degre, ng=neighbour*/
	
	fp=fopen("graph.txt","r");
	if (fp==NULL)
		{
		fprintf(stderr,"\nErreur d'ouverture de fichier 'graph.txt' in fct 'getGraphInFile'\n");
		exit(1);
		}
	
	fscanf(fp,"%d",&n);
	printf("\n ------------------ ");		
	printf("\n DisplayFileContent:  Ordre de G:%d\n",n);	
	printf(" ------------------ \n");
	
	fscanf(fp,"%d",&som);
	while (som != -1)
		{
		printf(" \nSommet %d :",som);
		fscanf(fp,"%d",&dg);
		printf(" dg=(%d) :",dg);
		for(i=1;i<=dg;i++)
			{
			fscanf(fp,"%d",&ng);
			printf("%d, ",ng);
			}
		fscanf(fp,"%d",&som);
		}
	fclose(fp);
	}

/* getGraphInFileNoSymetric
le fichier graph.txt contient un graphe codé de maniere non symetrique.
ie si
1 3 4 5 6
alors la liste des voisins des sommets 4 5 et 6 ne contiennent pas le sommet 1*/


G getGraphInFileNoSymetric(G g)
	{/* on place 2 aretes som->ng et ng->som */
	FILE *fp;
	int i,n,som,dg,ng;/*som=sommet, dg=degre, ng=neighbour*/
	
	fp=fopen("graph.txt","r");
	if (fp==NULL)
		{
		fprintf(stderr,"\nErreur d'ouverture de fichier 'graph.txt' in fct 'getGraphInFile'\n");
		exit(1);
		}
	printf("\n\n ------------------------ ");
	printf("\n getGraphInFileNoSymetric \n");
	printf(" ------------------------ \n");
	fscanf(fp,"%d",&n);
	g=AllocInitG(g,n);
	fscanf(fp,"%d",&som);
	while (som != -1)
		{
		fscanf(fp,"%d",&dg);
		for(i=1;i<=dg;i++)
			{
			fscanf(fp,"%d",&ng);
			g=TwoNewEdge(g,som,ng);
			}
		fscanf(fp,"%d",&som);
		}
	fclose(fp);
	return(g);
	}
	
	
/* getGraphInFileNoSymetric
le fichier graph.txt contient un graphe codé de maniere symetrique.
ie si
1 3 4 5 6
alors la liste des voisins des sommets 4 5 et 6 contiennent le sommet 1*/

G getGraphInFileSymetric(G g)
	{
	FILE *fp;
	int n,i,som,dg,ng;/*som=sommet, dg=degre, ng=neighbour*/
	
	fp=fopen("graph.txt","r");
	if (fp==NULL)
		{
		fprintf(stderr,"\nErreur d'ouverture de fichier 'graph.txt' in fct 'getGraphInFile'\n");
		exit(1);
		}
	printf("\n\n ------------------------ ");
	printf("\n getGraphInFileSymetric ");
	printf("\n ------------------------ \n");
	fscanf(fp,"%d",&n);
	g=AllocInitG(g,n);
	fscanf(fp,"%d",&som);
	while (som != -1)
		{
		fscanf(fp,"%d",&dg);
		for(i=1;i<=dg;i++)
			{
			fscanf(fp,"%d",&ng);
			g=OneNewEdge(g,som,ng); /* on place une seule arete som->ng */
			}
		fscanf(fp,"%d",&som);
		}
	fclose(fp);
	return(g);
	}
	
	
