fold all codes

简单的,维持缩进的,以html格式显示代码的cgi程序

/cgi-bin/getfile.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

void htmlspecialchars(const string& s, string& ret)
{
    ret = "";
    for (string::const_iterator i = s.begin(); i != s.end(); i++)
    {
        switch (*i)
        {
        case '<':
            ret += "&lt;";
            break;
        case '>':
            ret += "&gt;";
            break;
        case '&':
            ret += "&amp;";
            break;
        case ' ':
            ret += "&nbsp;";
            break;
        default:
            ret += *i;
        }
    }
}

int main()
{
    cout << "Content-type: text/html; charset=UTF-8\n\n";

    char *f = getenv("QUERY_STRING");
    ifstream ifs(f);
    do
    {
        if (!ifs.is_open())
        {
            cout << "file cannot open: " << f << endl;
            break;
        }

        cout << "<h5>" << f << "</h5>\n";
        cout << "<pre>\n";

        for (string line; getline(ifs, line); )
        {
            string s;
            htmlspecialchars(line, s);
            cout << s << endl;
        }
        cout << "</pre>\n";
    }
    while (0);
    return 0;
}
posted @ 2011-11-03 11:38  hylent  阅读(403)  评论(0编辑  收藏  举报