教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 互联网资料 >

cgic 为C语言编写CGI的C函数库

来源:网络收集 时间:2026-09-06
导读: cgic: 为C语言编写CGI的C函数库 由Thomas Boutell开发 目录 CGIC介绍 怎样写CGIC应用程序 怎样产生图片在CGIC中? CGI调试特征: 利用捕获 cgic函数参考 cgic变量参考 cgic结果编码参考 cgic快速索引 一般的UNIX系统都支持ANSIC,增加相应的库函数(和相应的h文

cgic: 为C语言编写CGI的C函数库 由Thomas Boutell开发 目录

CGIC介绍

怎样写CGIC应用程序 怎样产生图片在CGIC中? CGI调试特征: 利用捕获 cgic函数参考 cgic变量参考

cgic结果编码参考 cgic快速索引

一般的UNIX系统都支持ANSIC,增加相应的库函数(和相应的h文件)就可以实现CGI。在此我向大家推荐一个用于CGI编程的ANSIC库:cgic。

cgic是用来生成基于CGI的WWW应用程序的C语言函数库,它有以下功能:

*对数据进行语法分析

*接收以GET和PSOT两种方式发送的数据

*把FORM中的不同域连接成连续的串

*为检索FORM数据而提供字符串,整数,浮点以及单项和多项选择功能

*为数字字段提供边界检测

*把CGI环境变量加载到非空的C串中

*为调试而捕捉CGI状态

*提供相对安全的系统调用功能

用一般ANSI C或C++编译器就可以编译cgic程序,不过与通常C程序不同的是,用cgic写的源码其主函数是cgiMain(),而不是通常的main()。cgic的函数库会自动把cgiMain连接到相应的main()上去。

--------------------------------------------------------------------------------

写CGIC程序

Note: 所有的cgic应用程序必须连接cgic.c.

用cgimain()替代main() 必须包含: #include\

基本结构cgictest.c:

int cgiMain() { #if DEBUG

/* Load a saved CGI scenario if we're debugging */ cgiReadEnvironment(\ #endif

/* Important: we must indicate the type of document */ cgiHeaderContentType(\

/* Now invoke other functions to handle each part of the form */ fprintf(cgiOut, \

fprintf(cgiOut, \ fprintf(cgiOut, \ Name(); Address(); Hungry();

Temperature(); Frogs(); Color(); Flavors();

NonExButtons(); RadioButtons();

fprintf(cgiOut, \

/* This value will be the exit code of the program; 0

generally indicates success among Unix and DOS programs */ return 0; }

capture 输出标头

cgiHeaderContentType()在输出文挡之前简要说明MIME内型,如 \。 cgiHeaderStatus()代替输出错误代码 cgiHeaderLocation()代替重新引导至其他页面。在一个独立的应用程序中只能有一个cgiHeader函数。

重点:在cgiHeader函数组中, cgiHeaderContentType(), 在任何向浏览器输出之前被调用. 否则将出错或浏览器不能识别。 cgiOut

接着, cgiMain() 调用不同的函数.当函数结束后,将返回0

处理输入文本

void Name() { char name[81];

cgiFormStringNoNewlines(\ fprintf(cgiOut, \ }

这个函数的功能就是取的并显示由用户输入的name . 处理输出

Important: cgiOut通常相当于stdout

cgiFormString 确保断航

处理单一Checkboxes输入

这个Hungry() function确定用户是否选择\这个 checkbox: void Hungry() {

if (cgiFormCheckboxSingle(\ fprintf(cgiOut, \ } else {

fprintf(cgiOut, \ } }

这个函数依靠 cgiFormCheckboxSingle() 确定单一的checkbox 被选择。 cgiFormCheckboxSingle() 接受checkbox名字的属性值,如果存在就返回 cgiFormSuccess,否则返回cgiFormNotFound 如果是多项checkboxes,就用 cgiFormCheckboxMultiple()和cgiFormStringMultiple() 函数. 处理数字输入

Temperature() 返回浮点书的值确保在特定的返回内。 void Temperature() { double temperature;

cgiFormDoubleBounded(\ fprintf(cgiOut, \ }

依靠cgiFormDoubleBounded()得到数据.第一个数据是返回数据中输入域的名字。最后一个值是用户没有提交时的默认值。

这个函数总是找回在特定返回内合适的值; cgiFormDoubleBounded返回的值被检查确信用户输入的资料在规定范围内, 而不是其他无效的数据。查看 cgiFormDoubleBounded() 更多的资料. 如果限度检查不理想,可以用 cgiFormDouble() 替代.

在整数输入,cgiFormInteger 和 cgiFormIntegerBounded 可以利用. 这些函数的功能类似.

处理单一选择输入

在表但的列表. cgiFormSelectSingle()

cgiFormSelectSingle() 总是显示合理的选项值.

radio button也可以用这个函数.另外还有 cgiFormRadio(), 也是一样的

处理多项选择的输入 NonExButtons() char *votes[] = { \ \ \ \ };

void NonExButtons() { int voteChoices[4]; int i; int result; int invalid;

char **responses;

/* Method #1: check for valid votes. This is a good idea, since votes for nonexistent candidates should probably be discounted... */

fprintf(cgiOut, \otes (method 1):
\\n\

result = cgiFormCheckboxMultiple(\

voteChoices, &invalid);

if (result == cgiFormNotFound) {

fprintf(cgiOut, \ } else {

fprintf(cgiOut, \ fprintf(cgiOut, \ for (i=0; (i < 4); i++) { if (voteChoices[i]) {

fprintf(cgiOut, \ } }

fprintf(cgiOut, \ }

参考cgiFormCheckboxMultiple(), cgiFormSelectMultiple(). cgiFormCheckboxMultiple() cgiFormCheckboxMultiple

NonExButtons() 函数在 cgictest.c:

/* Method #2: get all the names voted for and trust them. This is good if the form will change more often

than the code and invented responses are not a danger or can be checked in some other way. */ fprintf(cgiOut, \otes (method 2):
\\n\

result = cgiFormStringMultiple(\ if (result == cgiFormNotFound) {

fprintf(cgiOut, \ } else { int i = 0;

fprintf(cgiOut, \ fprintf(cgiOut, \ while (responses[i]) {

fprintf(cgiOut, \ i++; }

fprintf(cgiOut, \ }

/* We must be sure to free the string array or a memory leak will occur. Simply calling free() would free the array but not the individual strings. The

function cgiStringArrayFree() does the job completely. */ cgiStringArrayFree(responses); }

…… 此处隐藏:2297字,全部文档内容请下载后查看。喜欢就下载吧 ……
cgic 为C语言编写CGI的C函数库.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/445008.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)