cigcc里面很多地方用到了STL的vector.STDNS就就是std::,因?yàn)檎麄€(gè)文件的定義是在namespace cgicc里面,所以此處要聲明std.。網(wǎng)頁(yè)文件的元素很適合用vector來(lái)標(biāo)示,迭代器也是直接使用vector的。//! A vector of FormEntry objects
typedef STDNS vector<FormEntry>::iterator form_iterator;
//! A vector of \c const FormEntry objects
typedef STDNS vector<FormEntry>::const_iterator const_form_iterator;
//! A vector of FormFile objects
typedef STDNS vector<FormFile>::iterator file_iterator;
//! A vector of \c const FormFile objects
typedef STDNS vector<FormFile>::const_iterator const_file_iterator;
int
main(int argc, char **argv) {
try {
cgicc::Cgicc cgi;
// do something with cgi
}
catch(const exception& e) {
//handle the error
}
}
private:
CgiEnvironment fEnvironment;
STDNS vector<FormEntry> fFormData;
STDNS vector<FormFile> fFormFiles;
// Convert query string into a list of FormEntries
void parseFormInput(const STDNS string& data);
// Parse a multipart/form-data header
MultipartHeader parseHeader(const STDNS string& data);
// Parse a (name=value) form entry
void parsePair(const STDNS string& data);
// Parse a MIME entry for ENCTYPE=""
void parseMIME(const STDNS string& data);
// Find elements in the list of entries
bool findEntries(const STDNS string& param,
bool byName,STDNS vector<FormEntry>& result) const;
};
如果使用的是FastCGI(CGI的一個(gè)擴(kuò)展版本)的話,會(huì)需要一個(gè)reader_function_t的參數(shù),用來(lái)讀取輸入。如果忽略或者是NULL的話,Cgicc(reader_function_t stream_reader = NULL);
是一種函數(shù)指針類(lèi)型。size_t 是Linux上常用的一種數(shù)據(jù)類(lèi)型。一般為long unsigned int.這種函數(shù)類(lèi)型接受兩個(gè)參數(shù),返回一個(gè)size_t.typedef size_t (* reader_function_t)(void *, size_t);
CGICCNS Cgicc::Cgicc(reader_function_t stream_reader) //CGICCNS--->cgicc::
: fEnvironment(stream_reader) //fEnvironment是一個(gè)CgiEnvironment 實(shí)例。
{
#if DEBUG
#if HAVE_STRFTIME //是否有strftime這個(gè)函數(shù),有的話就會(huì)有下面的調(diào)用。LINUX上一般都有。
time_t now;
tm *date;
char s[80];
now = time(0); //獲取UNIX時(shí)間(秒數(shù))。
date = localtime(&now); //轉(zhuǎn)換成通常用的時(shí)間格式
strftime(s, 80, "%A, %B %d, %Y %I:%M:%S %p", date); //寫(xiě)入字符數(shù)組s中
LOG("Cgicc debugging log started on ") //LOG和下面的stringsAreEqual都是Cgiutils.h中定義的一些實(shí)用函數(shù),見(jiàn)本文
LOGLN(s) //附錄。
#else
LOGLN("Cgicc debugging log started.")
#endif /* HAVE_STRFTIME */
#endif /* DEBUG */
// this can be tweaked for performance
fFormData.reserve(40); //請(qǐng)求存儲(chǔ)空間,最少能容納40個(gè)元素,STL中的接口
fFormFiles.reserve(5); //同上。Cgicc提供了整個(gè)庫(kù)最主要的也是最基本的接口,其中就包括存貯取得的信息。
if(stringsAreEqual(getEnvironment().getRequestMethod(), "post")) //具體函數(shù)見(jiàn)附錄2.
parseFormInput(getEnvironment().getPostData());
else
parseFormInput(getEnvironment().getQueryString());
}
析構(gòu)函數(shù):1 CGICCNS Cgicc::~Cgicc()
2 {
3 LOGLN("Cleaning up")
4 LOGLN("Cgicc debugging log closed.")
5 }
附錄:
所以此時(shí)上面的構(gòu)造函數(shù)的輸出為(標(biāo)準(zhǔn)輸出):Cgicc debugging log started on\n+一個(gè)時(shí)間extern STDNS ofstream gLogFile;
#define LOGLN(s) gLogFile << s << STDNS endl;
#define LOG(s) gLogFile << s;
bool
CGICCNS stringsAreEqual(const STDNS string& s1, //CGICCNS------>cgicc:: STDNS----->std::
const STDNS string& s2)
{
STDNS string::const_iterator p1 = s1.begin();
STDNS string::const_iterator p2 = s2.begin();
STDNS string::const_iterator l1 = s1.end();
STDNS string::const_iterator l2 = s2.end();
while(p1 != l1 && p2 != l2) {
if(toupper(*(p1++)) != toupper(*(p2++))) //忽略大小寫(xiě)
return false;
}
return (s2.size() == s1.size()) ? true : false;
}


")