- 论坛徽章:
- 0
|
这是我写的代码
/* Parse HTML and {$var} file. */
int parse_tpl(struct tpl *var_list, const char *filename, int count)
{
int i;
FILE *fp;
int maxtmp = 32;
//tpl_path = "./template";
char filepath[64];
char buf[1025];
char buf2[1024];
char tmp[maxtmp];
char *p_start;
char *p_end;
struct tpl *p;
sprintf(filepath, "%s/%s.tpl", tpl_path, filename);
if ((fp = fopen(filepath, "rb")) == NULL) {
fprintf(stderr, "Can not open file <%s>.", filepath);
return 0;
}
while (fgets(buf, 1024, fp) != NULL)
{
if (buf[0] == '\0' || buf[0] == '\r' || buf[0] == '\n')
continue;
for (i = 0; i < strlen(buf); i++)
{
if (buf[i] == '{')
p_start = &buf[i] +1;
if (buf[i] == '}') {
p_end = &buf[i];
memcpy(tmp, p_start, (p_end - p_start) / sizeof(char));
tmp[(p_end - p_start) / sizeof(char)] = '\0';
memset(p_start - 1, 32, (p_end - p_start) / sizeof(char) + 2);
for (p = var_list; p < var_list + count; p++)
{
if (strcasecmp(tmp, p->name) == 0) {
if (strlen(*p->value) > strlen(tmp) + 2) {
strcpy(buf2, p_end + 1);
memmove(p_start - 1, *p->value, strlen(*p->value));
strcpy(p_start + strlen(*p->value) - 1, buf2);
}
else
{
memmove(p_start - 1, *p->value, strlen(*p->value));
strcpy(p_start + strlen(*p->value) - 1, p_end + 1);
}
}
}
bzero(tmp, maxtmp);
}
}
fprintf(stdout, "%s",buf);
}
fclose(fp);
return 1;
} // End of parse_tpl
/* Parse get opt action */
char *parse_get_opt(char *optname)
{
char *p;
char *p1;
char *query_string;
if ((query_string = getenv("QUERY_STRING")) == NULL)
return NULL;
query_string = unescape(query_string);
if ((p = strstr(query_string, optname)) == NULL)
return NULL;
p1 = p;
for (p = p; p < p + strlen(p); p++)
{
if (*p == '&' || *p == '\0') {
*p = '\0';
break;
}
else {
continue;
}
}
for (p1 = p1; p1 < p1 + strlen(p1); p1++)
{
if (*p1 == '=')
break;
else
continue;
}
return ++p1;
} //End of parse_get_opt. |
|