免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3945 | 回复: 2
打印 上一主题 下一主题

CGI 模板操作库 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-06-27 20:50 |只看该作者 |倒序浏览
CGI 模板操作库  转致:慧佳生活资讯网
[url]http://www.hoojar.com/view-lore.php?lid=47179[/url]  CGI 模板操作库

CGI操作模板文件方便界面的变化与说明文档

filename is: index.htm
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>{TITLE}</title>
</head>
<body>
{CONTENT}老大
</body>
</html>
filename is: index1.htm
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>{TITLE}</title>
</head>
<body>
{CONTENT}一
</body>
</html>
filename is: index2.htm
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>{TITLE}</title>
</head>
<body>
{CONTENT}二
</body>
</html>
filename is: Makefile
GCC = gcc
EXE = $(GCC) -s -Wall -O2 -o test template.o test.o
exec: template.o test.o
    $(EXE)
template.o :
     $(GCC) -c template.c
test.o :
    $(GCC) -c test.c
clean:
    rm -f *.o
filename is: result.txt
FOLLOWING IS TEMPLATES head!
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>这里是标题</title>
</head>
<body>
这里是内容老大
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>这里是标题</title>
</head>
<body>
这里是内容一
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>这里是标题</title>
</head>
<body>
这里是内容二
</body>
</html>
filename is: replace.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
char *str_nreplace(char *src, const char *delim, const char *with, int n)
{
        unsigned int w_len, i, n_len, d_len, counter, c;
        char *buf, *tmpstr;
        w_len = strlen(with);
        d_len = strlen(delim);
        n_len = strlen(src);
        counter = 0;
        buf = (char *)malloc(n_len+1);
        if (buf == NULL)
        {
            exit(1);
        }
        c = 0;
        tmpstr = (char *)malloc(w_len + 1);
        if (tmpstr == NULL)
        {
            exit(1);
        }
        while (*src)
        {
                if (n-- > 0)
                {
                        /*分解字符串,得到一个长度与要查询值的字符串*/
                        for (i = 0; i < d_len; i++)
                        {
                            tmpstr[c++] = *src;
                            src++;
                        }
                        tmpstr[c++] = '\0';
                        c = 0;
                        /*返加原始字符串的指针位置*/
                        for (i = 0; i < d_len; i++)
                        {
                            src--;
                        }
                        /*分分大小的方式来比较字符串*/
                        if (strcmp(tmpstr, delim) == 0)
                        {
                            if (w_len > 1)
                            {
                                n_len += w_len;
                                buf = (char *)realloc(buf, n_len);
                                if (buf == NULL)
                                {
                                    exit(1);
                                }
                                for (i = 0; i < w_len; i++)
                                {
                                     buf[counter++] = with[i];
                                }
                            }
                            src = src + (d_len - 1);
                        }
                        else
                        {
                            buf[counter++] = *src;
                        }
                }
                else
                {
                        buf[counter++] = *src;
                }
                src++;
        }
        buf[counter] = '\0';
        return buf;
}
char *str_replace(char *str, const char *delim, const char *with)
{
        return str_nreplace(str, delim, with, strlen(str));
}
int main()
{
    char *str1 = "中华人民共和国woods中华人民共和国";
    char *str = str_replace(str1, "woods", "[中国人]我是一个兵");
    printf("%s\n", str);
}
   
filename is: template.c
/*************************************************************************************************
功能:CGI来操作模板文件方便界面的变化与说明文档的变更
原理:字符的替换以{variable}来替换新的内容
作者:张树林           Author:woods.zhang            日期:2005-06-16      Date:2005-06-16
版本:1.0.0            Version:1.0.0                 Email:hoojar@163.com   QQ:37894354
MSN:hoojar@hotmail.com 网站地址:[url]http://hoojar.com/[/url]  [url]http://www.hoojar.com[/url]  [url]www.hoojar.com/view/[/url]
************************************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
char *template_path; /*模板文件所存储的文件夹路径*/
char *template_str; /*模板文件中的内容*/
char *str_nreplace(char *src, const char *delim, const char *with, int n);
char *str_replace(char *str, const char *delim, const char *with);
/**************************************************************************************************/
/**
函数:unsigned short is_dir(const char *dname);
功能:判断是否为文件夹
参数:const char *dname 文件夹的地址
返回值:1为是文件夹0为无效文件夹
**/
unsigned short is_dir(const char *dname);
/**************************************************************************************************/
/**
函数:void template_halt(const char *str_err);
功能:模板文件执行中遇到严重错误,程序将停止执行
参数:const char *str_err为不可更改的出错说明描述
返回值:空
**/
void template_halt(const char *str_err);
/**************************************************************************************************/
/**
函数:unsigned short file_exist(const char *filename);
功能:判断文件是否存在
参数:const char *filename 为要判断的文件
返回值:1为设置成功0为失败
**/
unsigned short file_exist(const char *filename);
/**************************************************************************************************/
/**
函数:unsigned short template(const char *dir);
功能:模板文件的操作,设置要操作的文件夹
参数:const char *dir 为要操作的文件夹
返回值:1为设置成功0为失败
**/
unsigned short template(const char *dir);
/**************************************************************************************************/
/**
函数:int template_file(const char *filename);
功能:设置要操作的文件,并读取内容
参数:const char *filename 为要操作的文件
返回值:1为设置成功0为失败
**/
unsigned short template_file(const char *filename);
/**************************************************************************************************/
/**
函数:void template_setvar(const char *name, const char *value);
功能:替换内容,以NAME进行替换成VALUE值
参数:const char *name要查询替换的模板变量 const char *value要进行替换的值
返回值:空
**/
void template_setvar(const char *name, const char *value);
/**************************************************************************************************/
/**
函数:void template_print();
功能:输了模板中内容
参数:无
返回值:空
**/
void template_print();
/**************************************************************************************************/
char *str_nreplace(char *src, const char *delim, const char *with, int n)
{
        unsigned int w_len, i, n_len, d_len, counter, c;
        char *buf, *tmpstr;
        w_len = strlen(with);
        d_len = strlen(delim);
        n_len = strlen(src);
        counter = 0;
        buf = (char *)malloc(n_len+1);
        if (buf == NULL)
        {
            template_halt("程序无法申请较多的内存空间!");
        }
        c = 0;
        tmpstr = (char *)malloc(w_len + 1);
        if (tmpstr == NULL)
        {
            template_halt("程序无法申请较多的内存空间!");
        }
        while (*src)
        {
                if (n-- > 0)
                {
                        /*分解字符串,得到一个长度与要查询值的字符串*/
                        for (i = 0; i < d_len; i++)
                        {
                            tmpstr[c++] = *src;
                            src++;
                        }
                        tmpstr[c++] = '\0';
                        c = 0;
                        /*返加原始字符串的指针位置*/
                        for (i = 0; i < d_len; i++)
                        {
                            src--;
                        }
                        /*区分大小的方式来比较字符串*/
                        if (strcmp(tmpstr, delim) == 0)
                        {
                            if (w_len > 1)
                            {
                                n_len += w_len;
                                buf = (char *)realloc(buf, n_len);
                                if (buf == NULL)
                                {
                                    template_halt("程序无法申请较多的内存空间!");
                                }
                                for (i = 0; i < w_len; i++)
                                {
                                     buf[counter++] = with[i];
                                }
                            }
                            src = src + (d_len - 1);
                        }
                        else
                        {
                            buf[counter++] = *src;
                        }
                }
                else
                {
                        buf[counter++] = *src;
                }
                src++;
        }
        buf[counter] = '\0';
        return buf;
}
/**************************************************************************************************/
char *str_replace(char *str, const char *delim, const char *with)
{
        return str_nreplace(str, delim, with, strlen(str));
}
/**************************************************************************************************/
/**
函数:int is_dir(const char *dname);
功能:判断是否为文件夹
参数:const char *dname 文件夹的地址
返回值:1为是文件夹0为无效文件夹
**/
unsigned short is_dir(const char *dname)
{
        struct stat sbuf;
        if (lstat(dname, &sbuf) == -1)
        {
                return 0;
        }
        if (S_ISDIR(sbuf.st_mode))
        {
                return 1;
        }
        return 0;
}
/**************************************************************************************************/
/**
函数:template_halt(const char *str_err);
功能:模板文件执行中遇到严重错误,程序将停止执行
参数:const char *str_err为不可更改的出错说明描述
返回值:空
**/
void template_halt(const char *str_err)
{
     printf("%s\r\n", str_err);
     exit(1);
}
/**************************************************************************************************/
/**
函数:int template(const char *dir);
功能:模板文件的操作,设置要操作的文件夹
参数:const char *dir 为要操作的文件夹
返回值:1为设置成功0为失败
**/
unsigned short template(const char *dir)
{
    if (is_dir(dir) == 0)
    /*等于1是文件夹0不是文件夹*/
    {
       char str_err[1024];
       sprintf(str_err, "文件夹:[%s]无效!", dir);
       template_halt(str_err);
    }
    template_str = "";
    template_path = (char *)malloc((strlen(dir) + 1) * sizeof(char*));
    sprintf(template_path, "%s", dir);
    /*判断路径最后一个字符是否为符号/,如果是则删除*/
    if (template_path[strlen(template_path)-1] == '/')
    {
       template_path[strlen(template_path)-1] = '\0';
    }
    return 1;
}
/**************************************************************************************************/
/**
函数:unsigned short file_exist(const char *filename);
功能:判断文件是否存在
参数:const char *filename 为要判断的文件
返回值:1为设置成功0为失败
**/
unsigned short file_exist(const char *filename)
{
    if (access(filename, F_OK) == 0)
    /*__________表示文件锁存在__________*/
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
/**************************************************************************************************/
/**
函数:unsigned short template_file(const char *filename);
功能:设置要操作的文件,并读取内容
参数:const char *filename 为要操作的文件
返回值:1为设置成功0为失败
**/
unsigned short template_file(const char *filename)
{
    char file_str[1024];
    char str_err[1024];
    FILE *inc;
    char buffer[255];
    char *value;
    long i = 0;
    sprintf(file_str, "%s/%s", template_path, filename);
    if (file_exist(file_str) == 0)
    /*等于1是文件夹0不是文件夹*/
    {
       sprintf(str_err, "文件:[%s]不存在!", filename);
       template_halt(str_err);
    }
    /*_____________打开文件把内容读到全局字答指针中_______________begin__________*/
    if (!(inc = fopen(file_str, "r")))
    {
          sprintf(str_err, "文件:[%s]无法打开阅读!", filename);
          template_halt(str_err);
    }
    value = (char *)malloc(strlen(template_str) * sizeof(char*));
    sprintf(value, "%s", template_str);
    while (fgets(buffer, 255, inc))
    {
            i = strlen(value) + strlen(buffer) + 1;
            template_str = (char *)malloc(i * sizeof(char*));
            sprintf(template_str, "%s%s", value, buffer);
            free(value);
            value = (char *)malloc(strlen(template_str) * sizeof(char*));
            sprintf(value, "%s", template_str);
    }
    free(value);
    fclose(inc);
    /*_____________打开文件把内容读到全局字答指针中_______________end__________*/
    return 1;
}
/**************************************************************************************************/
/**
函数:void template_setvar(const char *name, const char *value);
功能:替换内容,以NAME进行替换成VALUE值
参数:const char *name要查询替换的模板变量 const char *value要进行替换的值
返回值:空
**/
void template_setvar(const char *name, const char *value)
{
     char v_name[255];
     char *str;
     sprintf(v_name, "{%s}", name);
     str = str_replace(template_str, v_name, value);
     template_str = (char *)malloc((strlen(str) + 1) * sizeof(char*));
     sprintf(template_str, "%s", str);
}
/**************************************************************************************************/
/**
函数:void template_print();
功能:输了模板中内容
参数:无
返回值:空
**/
void template_print()
{
     printf("%s", template_str);
     free(template_path);
     free(template_str);
     fflush(stdout);
     /*puts(template_str);*/
}
/**************************************************************************************************/
   
filename is: test.c
#include <stdio.h>
extern unsigned short template(const char *dir);
extern unsigned short template_file(const char *filename);
extern void template_setvar(const char *name, const char *value);
extern void template_print();
int main()
{
    printf("%s", "\t\rFOLLOWING IS TEMPLATES head!\n");
    template("./");
    template_file("index.htm");
    template_file("index1.htm");
    template_file("index2.htm");
    template_setvar("TITLE", "这里是标题");
    template_setvar("CONTENT", "这里是内容");
    template_print();
}

论坛徽章:
0
2 [报告]
发表于 2007-06-29 15:01 |只看该作者
C-CGI代码? 少见。

论坛徽章:
0
3 [报告]
发表于 2007-06-29 18:10 |只看该作者
SSI 技术, 不错的创意.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP