OSDN Git Service

Edit documentation and comment.
[opengate/opengate.git] / opengate / opengatesrv / htmltemplate.c
1 /**************************************************
2 htmltemplate
3
4 Copyright (C) 2005 Opengate Project Team
5 Written by Katsuhiko Eguchi, 2005 
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
21 Email: eguchi@ai.is.saga-u.ac.jp
22 **************************************************/
23
24 #include "htmltemplate.h"
25 #include "opengatesrv.h"
26 #include <regex.h>
27
28 int htmltemplate(char* filename, struct html_key* key,int key_size)
29 {
30   FILE *fp;
31   char buff[BUFFMAXLN];
32   int i;
33   
34   if((fp=fopen(filename,"r"))==NULL){
35     printf("Cannot find html document.");
36     return 1;
37   }
38   while(fgets(buff,BUFFMAXLN,fp)!=NULL){
39     for(i=0;i<key_size;i++){
40       htmlReplace(buff,key[i].name,key[i].val);
41     }
42     printf("%s",buff);
43   }
44   fclose(fp);
45   return 0;
46 }
47
48 int htmlReplace(char* buff,char *before,char *after)
49 {
50   char* wbuff;
51   char* rep_point;
52   char *form , *latt;
53   char* temp_buff = strmirror(buff);
54   int n = 0;
55
56   
57   for(rep_point=buff;(rep_point=strstr(rep_point,before))!=NULL;){
58     size_t i;
59     n++;
60     for(i=0;i<strlen(before);i++)rep_point++;
61   }
62   
63   if(n==0)return 0;
64   
65   strcpy(buff,"");
66   for(form = temp_buff; (latt=strsplit(form,before)) != NULL; form = latt){
67     strcat(buff,form);
68     strcat(buff,after);
69   }
70   strcat(buff,form);
71   free(temp_buff);
72
73   return 0;
74 }
75
76 char* strsplit(char* str,const char* delimstr)
77 {
78     char* delim_point = strstr(str,delimstr);
79     const size_t delim_len = strlen(delimstr);
80     size_t i;
81
82     if(delim_point == NULL) return NULL;
83     else{
84         *delim_point = '\0';
85         for(i=0;i<delim_len;i++) delim_point++;
86     }
87     return delim_point;
88 }
89
90 char* strmirror(const char* str)
91 {
92     char* p = (char*)malloc(strlen(str)+1);
93     if(p == NULL) return NULL;
94
95     strcpy(p,str);
96     return p;
97 }