// -*- C++ -*- util.h #ifndef UTIL_H #define UTIL_H #include #include #include #include using namespace std; typedef unsigned char byte; /** 文字列型に変換 */ string to_str(int x); #if 0 template string to_str(T x, int len=0, char fillchar=0) { ostringstream os; if (len != 0) os.width(len); if (fillchar != 0) os.fill(fillchar); os << x; return string(os.str()); } #endif /** int 型に変換 */ inline int to_int(const string& s) { return atoi(s.c_str()); } /** 何かを何かに変換 (例: stream_cast("123") == 123) */ #ifdef __FAKE_SSTREAM__ template inline T stream_cast(S x) { ostringstream os; os << x; istringstream is(os.str()); T y = T(); is >> y; return y; } #else // devised by Kevlin Henney template inline T stream_cast(S x) { stringstream ss; ss << x; T y = T(); ss >> y; return y; } #endif /** 現在日を "2000-01-23" の形の文字列として返す */ string datestr(); /** 現在時刻を "2000-01-23 12:34:56" の形の文字列として返す */ string timestr(); /** 環境変数を取得。例: environment("REMOTE_ADDR") */ string environment(const char* env); /** 全環境変数を取得 */ map environment(); /** メッセージを HTML 形式で出力 */ void put_html(const string& title, const string& msg, const bool nocache = false); /** エラーメッセージを表示して終了 */ void error_html(const string& msg); /** & < > を & < > に直す */ string to_html(const string& s); /** 上と同じだが ... だけ許す */ string to_html_a(string& s); /** 正しいEUC文字列でない場合は修正する (全角文字の数を返す) */ int check_euc(string& s); /** ファイルにアペンド */ bool append(const string& file, const string& s); /** ロックファイルクラス */ class LockFile { int fd; int count; public: // explicit LockFile(const char* filename); explicit LockFile(const string& filename); ~LockFile(); int getcount() const { return count; } void setcount(int n); }; /** GET や PUT で得た文字列を通常の文字列に変換する */ void decode_query(string& s); /** 上の逆。危ない文字を % で始まる16進に変換する */ string encode_query(const string& s); /** POST された内容をパースする */ void parse_post(map& post, istream& in = cin); /** GET した内容をパースする */ void parse_get(map& post); /** start から始まる文字列の頭の部分を target と比較 */ inline bool isprefix(const char* start, const char* target); /** 上と同じだが tolower してから比較 */ inline bool isprefix_lower(const char* start, const char* target); /** BASE64 を復号 */ string demime(const string& s); /** BASE64 にする */ string enmime(const string& s); /** 文字コード判別 */ inline bool iseuc(int c) // EUC で許される文字か { return c >= 0xa1 && c <= 0xfe; } inline bool issjis1(int c) // Shift JIS の1バイト目か { return (c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xef); } inline bool issjis2(int c) // Shift JIS の2バイト目か { return c >= 0x40 && c <= 0xfc && c != 0x7f; } inline bool ishankana(int c) // 半角カナか { return c >= 0xa0 && c <= 0xdf; } /** シフト JIS の1文字を JIS に変換する */ void sjis_to_jis(int& ph, int& pl); /** iso-2022-jp の文字列を EUC に変換する */ string jis2euc(const string& s); /** EUC の文字列を iso-2022-jp に変換する */ string euc2jis(const string& s); #endif