コンピュータや音楽の事書いてます

c++11(c++0x)でcgi

c++11でテキスト処理が大分楽に出来る様になってきたので、そろそろcgic++で良いのではないか?
使うライブラリ候補

という訳でcgiccを採用

#include <vector>
#include <iostream>
#include <algorithm>
#include <regex>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <cgicc/Cgicc.h>
#include <cgicc/HTTPHTMLHeader.h>
#include <cgicc/HTMLClasses.h>

using namespace std;
namespace fs = boost::filesystem;

bool regmatch(string source, string pattern){
    regex reg(pattern);
    smatch res;
    if(regex_match(source, res, reg))
        return true;
    else
        return false;
}

struct File { 
    int level; 
    string name; 
    bool operator<(const File& rhs) const { 
        return level < rhs.level || 
               level == rhs.level && name < rhs.name; 
    }
};

vector<File> getFiles(string dirname){
    vector<File> ret;
    fs::path dir(dirname);
    for(fs::recursive_directory_iterator f(dir), end; f != end; f++)    {
        string tmp = f->path().string();
        if(regmatch(tmp, ".*\\.txt")) {
            ret.push_back({ f.level(), tmp });
        }
    }
    
    sort(ret.begin(), ret.end());
    return ret;
}

using namespace cgicc;

int main(){
    cout << HTTPContentHeader("text/plain; charset=shift_jis;") << endl;
    for(auto f : getFiles("./up")){
        cout<<f.name<<endl;
    }
    return 0;
}
g++ files.cpp -std=c++0x -lcgicc -lboost_filesystem -lboost_system -static -o files.cgi 

ローカルでは動くけど、サーバーにあげたら動かない・・・はてなで質問して解決 http://q.hatena.ne.jp/1319473671

-staticオプションをつけるとなぜかレンタルサーバで動作しなかった。-lでリンクするのではなく、ソースと一緒に指定(どう違うんだ??)すると、問題なく動いた。

g++ files.cpp /usr/lib/libcgicc.a /usr/lib/libstdc++.a /usr/lib/libboost_system.a /usr/lib/libboost_filesystem.a -o files.cgi -std=c++0x

cgiccライブラリのタグを書いてくれる機能はいまいち。これだったら自分で書いたほうが分り易い。受信formの解析機能はまだ使ってないけど、良さげ。
c++11とcgiccが話の中心のはずだったのに、結局boost使うというオチw。

環境
gcc (GCC) 4.6.1 20110819 (prerelease)
Linux arch 2.6.33.7-co-0.7.9 #1 PREEMPT Sat Apr 9 20:30:51 UTC 2011 i686 Intel(R) Core(TM) i5 CPU 750 @ 2.67GH