Upstream tarball 20080911
[amule.git] / src / utils / cas / html.c
blob72627f20b7e2333d6a2cbfaa0d580287693a4a06
1 /*
2 * Name: HTML creation functions
4 * Purpose: Create a nice HTML page with all the statistics
6 * Author: Pedro de Oliveira <falso@rdk.homeip.net>
8 * Copyright (c) 2004-2008 Pedro de Oliveira ( falso@rdk.homeip-net )
9 *
10 * This file is part of aMule.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the
24 * Free Software Foundation, Inc.,
25 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <fcntl.h>
35 #include <string.h>
37 #include "html.h"
38 #include "functions.h"
39 #include "version.h"
41 int create_html(char *stats[20], char *lines[6], char template[120], char *path_for_html)
43 /* Strings */
44 char *path = NULL;
45 char version[25], upload[25], download[25];
46 char *search[] = {"#VERSION#", "#CLIENT#", "#NICK#", "#UPLOADRATE#" ,
47 "#DOWNLOADRATE#" , "#QUEUE#" , "#NUMSHARE#" , "#SESSIONUP#" ,
48 "#SESSIONDOWN#" , "#TOTALUP#", "#TOTALDOWN#" , "#SERVER#" , "#IP#",
49 "#PORT#" };
51 snprintf(version, 25, "cas %s", CAS_VERSION);
52 snprintf(upload, 25, "%s kB/s", stats[7]);
53 snprintf(download, 25, "%s kB/s", stats[6]);
55 char *repl[] = { version , lines[0] , stats[10] , upload , download ,
56 stats[8] , stats[9] , stats[15] , stats[14] , stats[12] , stats[11] ,
57 stats[1] , stats[2] , stats[3] };
59 /* get some memory to read the template into */
60 int fdTmpl;
61 if ((fdTmpl = open(template, O_RDONLY)) < 0)
63 printf("\n\n%s\n",template);
64 perror("Could not open file");
65 exit (43);
68 struct stat sb;
69 if (fstat(fdTmpl, &sb) < 0)
71 perror("Could not stat file");
72 exit(43);
74 close(fdTmpl);
76 /* 2 times the size of the template should be enough */
77 /* st_size is defined as off_t, but size_t seems more reasonable */
78 size_t size = sb.st_size*2;
79 char *mem = calloc(size, 1);
80 if (NULL == mem)
82 perror("Could not calloc\n");
83 exit(44);
86 /* read the template into the memory */
87 size_t len = 0;
88 int ler;
89 FILE *fTmpl = fopen(template,"r");
90 while ((ler=fgetc(fTmpl)) != EOF && len+1 < size)
92 mem[len++] = ler;
94 fclose(fTmpl);
96 /* printf ("HTML: %s\n", mem); */
98 int t;
99 for (t=0; t<=13; t++)
101 /* replace the special tags */
102 replace(mem, search[t], repl[t]);
105 /* printf("FINAL: %s\n",mem); */
107 path = get_amule_path("aMule-online-sign.html", 0, path_for_html);
109 if (NULL == path)
111 perror("could not get the HTML path\n");
112 free(mem);
113 return 0;
116 FILE *fHTML = NULL;
117 if ((fHTML = fopen(path, "w")) == NULL)
119 perror("Unable to create file\n");
120 free(path);
121 free(mem);
122 exit(44);
124 free(path);
126 fprintf(fHTML, "%s", mem);
127 fclose(fHTML);
128 free(mem);
130 printf("HTML file created.\n");
132 return 1;