http.c 3.65 KB
#include "http.h"
#include <stdlib.h>

void freePage(struct PageWeb** page)
{
	if(*page!=NULL)
	{
		if((*page)->html_contents!=NULL) free((*page)->html_contents);
		free(*page);
	}
}



void getWeb(char* pageName, struct PageWeb** page)
{
	*page=malloc(sizeof(struct PageWeb));

	FILE* pageFile=fopen(pageName, "r+");

	if(pageFile!=NULL)
	{
		fseek(pageFile, 0L, SEEK_END);
		long nbr_char=ftell(pageFile);
		fseek(pageFile, 0L, SEEK_SET);


		(*page)->html_contents=malloc(nbr_char*sizeof(char));
		fread(((*page)->html_contents),1,nbr_char,pageFile);

		fclose(pageFile);
	}
	else
	{
		char erreur[]="<h1> 404 PAGE NOT FOUND </h1>\r\n\r\n La page que vous cherchez n'a pas été trouvée, sans doute parce que vous êtes nul.";
		(*page)->html_contents=malloc(strlen(erreur)*sizeof(char));
		strcpy((*page)->html_contents, erreur);
	}
	
	(*page)->size=strlen(((*page)->html_contents));


}

void createPage(struct PageWeb** page, struct interface_info* interfaces[20])
{
	*page=malloc(sizeof(struct PageWeb));

	(*page)->html_contents=malloc(BUFF_SIZE*sizeof(char));

	strcpy((*page)->html_contents,"");

	//Header de la page
	strcat((*page)->html_contents,"<!doctype html>\r\n<html>\r\n<head>\r\n<meta charset =\"utf-8\">\r\n<title> Interfaces Tangibles </title>\r\n</meta>");

	// Intégration de la fiche de style
	FILE* css_file=fopen("../Sioux/Tableau.css", "r+");

	if(css_file!=NULL)
	{
		char style_str[BUFF_SIZE];

		strcat((*page)->html_contents,"<style>");

		fseek(css_file, 0L, SEEK_END);
		long nbr_char = ftell(css_file);
		fseek(css_file, 0L, SEEK_SET);

		fread(style_str,1,nbr_char,css_file);
	
		strcat((*page)->html_contents,style_str);
	
		strcat((*page)->html_contents,"</style>");
		fclose(css_file);

	}

	strcat((*page)->html_contents,"</head><body><div class=\"container\"><h2>Interfaces Tangibles</h2><ul class=\"tableau\">");
	strcat((*page)->html_contents,"<li class=\"table-entete\"><div class=\"col nom\">Interfaces</div><div class=\"col etat\">Etat</div><div class=\"col commande\">Commande</div></li>");	
	
	int nbr_interfaces=0;

	while(interfaces[nbr_interfaces]!=NULL) nbr_interfaces++;

	if(nbr_interfaces==0)
		strcat((*page)->html_contents,"<li class=\"table-ligne\">\n<div class=\"no_inter\" data-label=\"NoInter\"> Aucune interface </div>");
	else
	{
		for(int i=0; i<nbr_interfaces; i++)
		{
			if(interfaces[i]->status >= 0)
			{
			  // Si on a le status de l'interface on l'affiche
				strcat((*page)->html_contents,"<li class=\"table-ligne\">\n<div class=\"col nom\" data-label=\"Nom\">");
				strcat((*page)->html_contents,interfaces[i]->adresse);
				switch(interfaces[i]->status)
				{
					case(0):
						strcat((*page)->html_contents,"</div>\n<div class=\"col etat\" style=\"color: red\" data-label=\"Etat\">Mode Sommeil</div>\n");
						break;
					case(1):
						strcat((*page)->html_contents,"</div>\n<div class=\"col etat\" style=\"color: green\" data-label=\"Etat\">Mode Eveillé</div>\n");
						break;	
				}	
				if(interfaces[i]->commande>=0)
				{
				  // Si on a la valeur de la commande de l'interface on l'affiche
					char tmp[10];
					sprintf(tmp, "%d", interfaces[i]->commande);
					strcat((*page)->html_contents,"<div class=\"col commande\" data-label=\"Etat\">");
					strcat((*page)->html_contents,tmp);
					strcat((*page)->html_contents,"%</div>\n");
				}
				else
				{
				  // Sinon inconnu
					strcat((*page)->html_contents,"<div class=\"col commande\" style=\"color: grey\" data-label=\"Etat\">Inconnu</div>\n");
				}
				strcat((*page)->html_contents,"</li>");
			}

		}
	}
				                        					
       	strcat((*page)->html_contents,"</ul>\n</div>\n</body>\n</html>");        

	(*page)->size=strlen(((*page)->html_contents));

}