Blame view

Sioux/http.c 3.65 KB
a69a94a7   skhinach   Ajout de gestion ...
1
  #include "http.h"
33777c91   skhinach   Test
2
  #include <stdlib.h>
a69a94a7   skhinach   Ajout de gestion ...
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  
  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);
8450f712   Antoine Moreau   fin
66
  		long nbr_char = ftell(css_file);
a69a94a7   skhinach   Ajout de gestion ...
67
68
69
70
71
72
73
74
75
76
77
78
  		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\">");
33777c91   skhinach   Test
79
  	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>");	
a69a94a7   skhinach   Ajout de gestion ...
80
81
82
  	
  	int nbr_interfaces=0;
  
8450f712   Antoine Moreau   fin
83
  	while(interfaces[nbr_interfaces]!=NULL) nbr_interfaces++;
a69a94a7   skhinach   Ajout de gestion ...
84
85
86
87
88
89
90
91
92
  
  	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)
  			{
8450f712   Antoine Moreau   fin
93
  			  // Si on a le status de l'interface on l'affiche
a69a94a7   skhinach   Ajout de gestion ...
94
95
96
97
98
99
100
101
102
103
  				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;	
33777c91   skhinach   Test
104
105
106
  				}	
  				if(interfaces[i]->commande>=0)
  				{
8450f712   Antoine Moreau   fin
107
  				  // Si on a la valeur de la commande de l'interface on l'affiche
33777c91   skhinach   Test
108
109
110
111
112
113
114
115
  					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
  				{
8450f712   Antoine Moreau   fin
116
  				  // Sinon inconnu
33777c91   skhinach   Test
117
  					strcat((*page)->html_contents,"<div class=\"col commande\" style=\"color: grey\" data-label=\"Etat\">Inconnu</div>\n");
a69a94a7   skhinach   Ajout de gestion ...
118
119
120
  				}
  				strcat((*page)->html_contents,"</li>");
  			}
33777c91   skhinach   Test
121
  
a69a94a7   skhinach   Ajout de gestion ...
122
123
124
125
126
127
128
129
  		}
  	}
  				                        					
         	strcat((*page)->html_contents,"</ul>\n</div>\n</body>\n</html>");        
  
  	(*page)->size=strlen(((*page)->html_contents));
  
  }