cgic: 为C语言编写CGI的C函数库
生活随笔
收集整理的這篇文章主要介紹了
cgic: 为C语言编写CGI的C函数库
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
http://www.qqread.com/cgi-perl/v223877.html
cgic: 為c語(yǔ)言編寫(xiě)CGI的C函數(shù)庫(kù) 由Thomas Boutell開(kāi)發(fā) 目錄 CGIC介紹 怎樣寫(xiě)CGIC應(yīng)用程序 怎樣產(chǎn)生圖片在CGIC中? CGI調(diào)試特征: 利用捕獲 cgic函數(shù)參考 cgic變量參考 cgic結(jié)果編碼參考 cgic快速索引 一般的UNIX系統(tǒng)都支持ANSIC。
作者:siyu?
cgic: 為C語(yǔ)言編寫(xiě)CGI的C函數(shù)庫(kù)?
由Thomas Boutell開(kāi)發(fā)?
目錄?
CGIC介紹?
怎樣寫(xiě)CGIC應(yīng)用程序?
怎樣產(chǎn)生圖片在CGIC中??
CGI調(diào)試特征: 利用捕獲?
cgic函數(shù)參考?
cgic變量參考?
cgic結(jié)果編碼參考?
cgic快速索引?
一般的UNIX系統(tǒng)都支持ANSIC,增加相應(yīng)的庫(kù)函數(shù)(和相應(yīng)的h文件)就可以實(shí)現(xiàn)CGI。在此我向大家推薦一個(gè)用于CGI編程的ANSIC庫(kù):cgic。?
cgic是用來(lái)生成基于CGI的WWW應(yīng)用程序的C語(yǔ)言函數(shù)庫(kù),它有以下功能:?
*對(duì)數(shù)據(jù)進(jìn)行語(yǔ)法分析?
*接收以GET和PSOT兩種方式發(fā)送的數(shù)據(jù)?
*把FORM中的不同域連接成連續(xù)的串?
*為檢索FORM數(shù)據(jù)而提供字符串,整數(shù),浮點(diǎn)以及單項(xiàng)和多項(xiàng)選擇功能?
*為數(shù)字字段提供邊界檢測(cè)?
*把CGI環(huán)境變量加載到非空的C串中?
*為調(diào)試而捕捉CGI狀態(tài)?
*提供相對(duì)安全的系統(tǒng)調(diào)用功能?
用一般ANSI C或C++編譯器就可以編譯cgic程序,不過(guò)與通常C程序不同的是,用cgic寫(xiě)的源碼其主函數(shù)是cgiMain(),而不是通常的main()。cgic的函數(shù)庫(kù)會(huì)自動(dòng)把cgiMain連接到相應(yīng)的main()上去。?
--------------------------------------------------------------------------------?
寫(xiě)CGIC程序?
Note: 所有的cgic應(yīng)用程序必須連接cgic.c.?
用cgimain()替代main() 必須包含: #include"cgic.h."?
基本結(jié)構(gòu)cgictest.c:?
int cgiMain() {?
#if DEBUG?
/* Load a saved CGI scenario if we're debugging */?
cgiReadEnvironment("/path/to/capcgi.dat");?
#endif?
/* Important: we must indicate the type of document */?
cgiHeaderContentType("text/html");?
/* Now invoke other functions to handle each part of the form */?
fprintf(cgiOut, "<HTML><HEAD>\n");?
fprintf(cgiOut, "<T99vLE>cgic test</T99vLE></HEAD>\n"):?
fprintf(cgiOut, "<BODY><H1>cgic test</H1>\n");?
Name();?
Address();?
Hungry();?
Temperature();?
Frogs();?
Color();?
Flavors();?
NonExButtons();?
RadioButtons();?
fprintf(cgiOut, "</BODY></HTML>\n");?
/* This value will be the exit code of the program; 0?
generally indicates success among Unix and DOS programs */?
return 0;?
}?
capture?
輸出標(biāo)頭?
cgiHeaderContentType()在輸出文擋之前簡(jiǎn)要說(shuō)明MIME內(nèi)型,如 "text/html"。?
cgiHeaderStatus()代替輸出錯(cuò)誤代碼 cgiHeaderLocation()代替重新引導(dǎo)至其他頁(yè)面。在一個(gè)獨(dú)立的應(yīng)用程序中只能有一個(gè)cgiHeader函數(shù)。?
重點(diǎn):在cgiHeader函數(shù)組中, cgiHeaderContentType(), 在任何向?yàn)g覽器輸出之前被調(diào)用. 否則將出錯(cuò)或?yàn)g覽器不能識(shí)別。 cgiOut?
接著, cgiMain() 調(diào)用不同的函數(shù).當(dāng)函數(shù)結(jié)束后,將返回0?
處理輸入文本?
void Name() {?
char name[81];?
cgiFormStringNoNewlines("name", name, 81);?
fprintf(cgiOut, "Name: %s<BR>\n", name);?
}?
這個(gè)函數(shù)的功能就是取的并顯示由用戶(hù)輸入的name .?
處理輸出?
Important: cgiOut通常相當(dāng)于stdout?
cgiFormString 確保斷航?
處理單一Checkboxes輸入?
這個(gè)Hungry() function確定用戶(hù)是否選擇"hungry"這個(gè) checkbox:?
void Hungry() {?
if (cgiFormCheckboxSingle("hungry") == cgiFormSuccess) {?
fprintf(cgiOut, "I'm Hungry!<BR>\n");?
} else {?
fprintf(cgiOut, "I'm Not Hungry!<BR>\n");?
}?
}?
這個(gè)函數(shù)依靠 cgiFormCheckboxSingle() 確定單一的checkbox 被選擇。 cgiFormCheckboxSingle() 接受checkbox名字的屬性值,如果存在就返回 cgiFormSuccess,否則返回cgiFormNotFound 如果是多項(xiàng)checkboxes,就用 cgiFormCheckboxMultiple()和cgiFormStringMultiple() 函數(shù).?
處理數(shù)字輸入?
Temperature() 返回浮點(diǎn)書(shū)的值確保在特定的返回內(nèi)。?
void Temperature() {?
double temperature;?
cgiFormDoubleBounded("temperature", &temperature, 80.0, 120.0, 98.6);?
fprintf(cgiOut, "My temperature is %f.<BR>\n", temperature);?
}?
依靠cgiFormDoubleBounded()得到數(shù)據(jù).第一個(gè)數(shù)據(jù)是返回?cái)?shù)據(jù)中輸入域的名字。最后一個(gè)值是用戶(hù)沒(méi)有提交時(shí)的默認(rèn)值。?
這個(gè)函數(shù)總是找回在特定返回內(nèi)合適的值; cgiFormDoubleBounded返回的值被檢查確信用戶(hù)輸入的資料在規(guī)定范圍內(nèi), 而不是其他無(wú)效的數(shù)據(jù)。查看 cgiFormDoubleBounded() 更多的資料. 如果限度檢查不理想,可以用 cgiFormDouble() 替代.?
在整數(shù)輸入,cgiFormInteger 和 cgiFormIntegerBounded 可以利用. 這些函數(shù)的功能類(lèi)似.?
處理單一選擇輸入?
<SELECT> HTML標(biāo)簽被用于向用戶(hù)提供幾個(gè)選擇. Radio buttons 和checkboxes 椰油這樣的用途,大門(mén)、能夠選擇的數(shù)量很小時(shí). Color()?
char *colors[] = {?
"Red",?
"Green",?
"Blue"?
};?
void Color() {?
int colorChoice;?
cgiFormSelectSingle("colors", colors, 3, &colorChoice, 0);?
fprintf(cgiOut, "I am: %s<BR>\n", colors[colorChoice]);?
}?
這個(gè)函數(shù)確定用戶(hù)選擇了幾個(gè)選項(xiàng)從<SELECT> 在表但的列表. cgiFormSelectSingle()?
cgiFormSelectSingle() 總是顯示合理的選項(xiàng)值.?
radio button也可以用這個(gè)函數(shù).另外還有 cgiFormRadio(), 也是一樣的?
處理多項(xiàng)選擇的輸入?
NonExButtons()?
char *votes[] = {?
"A",?
"B",?
"C",?
"D"?
};?
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, "Votes (method 1):<BR>\n");?
result = cgiFormCheckboxMultiple("vote", votes, 4,?
voteChoices, &invalid);?
if (result == cgiFormNotFound) {?
fprintf(cgiOut, "I hate them all!<p>\n");?
} else {?
fprintf(cgiOut, "My preferred candidates are:\n");?
fprintf(cgiOut, "<ul>\n");?
for (i=0; (i < 4); i++) {?
if (voteChoices[i]) {?
fprintf(cgiOut, "<li>%s\n", votes[i]);?
}?
}?
fprintf(cgiOut, "</ul>\n");?
}?
參考cgiFormCheckboxMultiple(), cgiFormSelectMultiple().?
cgiFormCheckboxMultiple() cgiFormCheckboxMultiple?
NonExButtons() 函數(shù)在 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, "Votes (method 2):<BR>\n");?
result = cgiFormStringMultiple("vote", &responses);?
if (result == cgiFormNotFound) {?
fprintf(cgiOut, "I hate them all!<p>\n");?
} else {?
int i = 0;?
fprintf(cgiOut, "My preferred candidates are:\n");?
fprintf(cgiOut, "<ul>\n");?
while (responses[i]) {?
fprintf(cgiOut, "<li>%s\n", responses[i]);?
i++;?
}?
fprintf(cgiOut, "</ul>\n");?
}?
/* 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);?
}
回顧文章:用 c 寫(xiě) CGI 程序簡(jiǎn)要指南
CGI程序可以用任何程序設(shè)計(jì)語(yǔ)言編寫(xiě),如Shell腳本語(yǔ)言、Perl、Fortran、Pascal、c語(yǔ)言等。但是用C語(yǔ)言編寫(xiě)的CGI程序具有執(zhí)行速度快、安全性高等特點(diǎn)。Web服務(wù)器在調(diào)用使用POST方法的CGI程序時(shí)設(shè)置此環(huán)境變量,它的文本值表示W(wǎng)eb服務(wù)器傳送給CGI程序的輸入中的字符數(shù)目,因此我們使用函數(shù)a。
總結(jié)
以上是生活随笔為你收集整理的cgic: 为C语言编写CGI的C函数库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CGIC简明教程
- 下一篇: sqlite3命令详解