2014/12/20


以前、Python の Flask からインスパイアされた C++ 製のとてもかっちょいい WAF「crow」を紹介しました。

Big Sky :: C++ 製 micro web framework「crow」を使って lingr の bot 書いてみた。

先日、github で crow という、python の flask からインスパイアされた C++ 製 micro web framework を見つけました。 ipkn/crow - GitHu...

http://mattn.kaoriya.net/software/lang/c/20140718122410.htm
ipkn/crow - GitHub
https://github.com/ipkn/crow

ひさびさ見てみたら、mustache テンプレートエンジンをサポートしていました。

{{ mustache }}

Logic-less templates

http://mustache.github.io/

さらに amalgamate というフォルダには1ファイルだけで使えるヘッダファイルも用意されています。

これはすごい。前回は lingr のボットを作ってみましたが、今回は MySQL を使った1行掲示板を書いてみました。

まず DDL は以下の通り。

CREATE TABLE bbs (
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    text VARCHAR(100),
    created TIMESTAMP DEFAULT NOW()
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

掲示板のコードは以下の通り。

#include <memory>
#include <mysql_connection.h>
#include <mysql_driver.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/resultset.h>
#include "crow_all.h"

int
main() {
  std::ifstream conf("config.json");
  if (!conf) {
    std::cerr << "config.json not found" << std::endl;
    return 1;
  }
  std::string json = {
    std::istreambuf_iterator<char>(conf),
    std::istreambuf_iterator<char>()};
  crow::json::rvalue config = crow::json::load(json);

  auto driver = sql::mysql::get_mysql_driver_instance();
  auto raw_con = driver->connect(
    (std::string) config["db_host"].s(),
    (std::string) config["db_user"].s(),
    (std::string) config["db_pass"].s());
  auto con = std::unique_ptr<sql::Connection>(raw_con);
  con->setSchema((std::string) config["db_name"].s());

  crow::SimpleApp app;
  crow::mustache::set_base(".");

  CROW_ROUTE(app, "/")
  ([&]{
    auto stmt = std::unique_ptr<sql::PreparedStatement>(
      con->prepareStatement("select * from bbs order by created"));
    auto res = std::unique_ptr<sql::ResultSet>(
      stmt->executeQuery());
    int n = 0;
    crow::mustache::context ctx;
    while (res->next()) {
      ctx["posts"][n]["id"] = res->getInt("id");
      ctx["posts"][n]["text"] = res->getString("text");
      n++;
    } 
    return crow::mustache::load("bbs.html").render(ctx);
  });

  CROW_ROUTE(app, "/post")
      .methods("POST"_method)
  ([&](const crow::request& req, crow::response& res){
    crow::query_string params(req.body);
    auto stmt = std::unique_ptr<sql::PreparedStatement>(
      con->prepareStatement("insert into bbs(text) values(?)"));
    stmt->setString(1, params.get("text"));
    stmt->executeUpdate();
    res = crow::response(302);
    res.set_header("Location""/");
    res.end();
  });

  app.port(40081)
    //.multithreaded()
    .run();
}

/ で一覧表示、/post で投稿します。MySQL の操作は MySQL Connector/C++ を使いました。

MySQL :: MySQL Connector/C++ Developer Guide

Table of Contents [ +/- ] Preface and Legal Notices 1 Introduction to MySQL Connector/C++ 2 How to G...

http://dev.mysql.com/doc/connector-cpp/en/

HTML (mustache) は以下の通り。

<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="UTF-8">
    <title>一行掲示板</title>
  </head>
  <body>
    <h2>一行掲示板</h2>
    <ul>
      {{# posts}}
      <li>{{id}}: {{text}}</li>
      {{/ posts}}
    </ul>
    <form action="/post" method="post">
      <input type="text" name="text"><input type="submit">
    </form>
  </body>
</html>

あとは MySQL への接続情報が書かれた config.json を用意すれば

{
    "db_host": "localhost",
    "db_user": "mysqluser",
    "db_pass": "mysqlpass",
    "db_name": "bbs"
}
一行掲示板

この様な1行掲示板が出来ました。もちろん入力チェック等は行っていませんので実際にはもう少しコードが長くなります。

crow、かっちょいいですね。

いつも通り、コードは github に置いておきます。

mattn/crow-bbs - GitHub
https://github.com/mattn/crow-bbs
Posted at by



2014/09/18


PDCurses はその名の通り Public Domain な curses 実装な訳ですが、Windows の実装こそありますが如何ともし難い問題がありました。

PDCurses には、ワイド文字列関数とバイト列で貰う実装があります。UNIX 系アプリケーションの多くは内部文字列がほぼ UTF-8 となり、C言語で受け渡される文字列は既成事実として UTF-8 になってしまいました。

しかしながら日本の Windows ではバイト列と言えば Shift_JIS な訳で、そのまま移植してしまうと文字化けが発生します。各アプリケーション側がワイド文字列でコードを書いてくれれば良いのですが、そんな事を気にする人はまずいません。ましてや各アプリケーションが UTF-8 と MBCS (Multi Byte Character Set) の変換を行うのは酷な話です。出来れば内部文字列は UTF-8 のままで Windows でも正しく表示されて欲しいのです。

mattn/pdcurses - GitHub
https://github.com/mattn/pdcurses

という訳で pdcurses に手を入れてみました。もし pdcurses.dll をリンクしていて表示が文字化けしているアプリケーションがあれば、dll を入れ替えるだけで文字化けが解消するかと思います。

今日はこれを使って、コマンドラインベースのプレゼンテーションツールである mdp を Windows で動かしてみました。

visit1985/mdp · GitHub

A command-line based markdown presentation tool.

https://github.com/visit1985/mdp

mdp 自身は大した事をやってないので、ncurses の代わりに pdcurses をリンクする様な修正を入れただけです。

diff --git a/Makefile b/Makefile
index 63a1feb..54f8327 100644
--- a/Makefile
+++ b/Makefile
@@ -20,7 +20,7 @@
 
 CFLAGS   = -O3 -Wall
 LDFLAGS  = -s
-LDLIBS   = -lncurses
+LDLIBS   = -lpdcurses
 OBJECTS  = cstring.o cstack.o markdown.o parser.o viewer.o mdp.o
 DESTDIR ?= /usr/bin
 
diff --git a/include/viewer.h b/include/viewer.h
index 68c7a13..f6e70fb 100644
--- a/include/viewer.h
+++ b/include/viewer.h
@@ -32,7 +32,7 @@
  *
  */
 
-#include <ncurses.h>
+#include <curses.h>
 
 #include "parser.h"
 #include "cstack.h"
diff --git a/viewer.c b/viewer.c
index ba76f2f..ed83196 100644
--- a/viewer.c
+++ b/viewer.c
@@ -22,7 +22,6 @@
  */
 
 #include <locale.h> // setlocale
-#include <ncurses.h>
 #include <stdlib.h>
 #include <string.h> // strchr
 #include <unistd.h>

sample.md も日本語にして正しく動作する事が確認出来ました。

mdp1
mdp2
mdp3
mdp4
Posted at by



2014/09/04


コマンドラインから「あ*」等とワイルドカードを使用してコマンドを起動した場合、UNIX とは異なり Windows ではプロセス自身が引数を展開します(内部的にではありますが)。

main 関数の argc/argv は MSVC コンパイラであれば setargv.obj というオブジェクトファイルをリンクする事で展開される様になりますが、MinGW ではデフォルトでワイルドカード展開が実行されてしまいます。しかしながら、このワイルドカード展開は各コンパイラベンダーの実装次第という所があり、時に異なる挙動となる場合があります。

そこでワイルドカードは自前でプログラムで展開するのが一番良い訳ですが、MinGW の場合は既述の通り、デフォルトが展開動作となっています。しかし以下のたった1行のおまじないを入れる事で、引数展開が無効となります。

#include <stdio.h>

#define WIN32_LEAN_AND_MEAN  1
#include <windows.h>

int _CRT_glob = 0// おまじない

int
main(int argc, char **argv) {
  int i;
  printf("command line: %s\n", GetCommandLine());
  for (i = 0; i < argc; i++)
    printf( "argv[%d]: %s\n", i, argv[i]);
  return 0;
}

おまじないの行をコメントアウトした場合

C:\dev>a あ*
command line: a  あ*
argv[0]: a
argv[1]: あ.txt

おまじないを実行した場合

C:\dev>a あ*
command line: a  あ*
argv[0]: a
argv[1]: あ*

アドホックすぎてかなり嫌ですね。どのコンパイラでも同じ動作をさせたいのならば、GetCommandLineW()CommandLineToArgvW() を使うべきだと思います。

Posted at by