備忘録

備忘録

C++でShift-JIS, UTF-8, UTF-16 BOM有無とエンディアンを考慮してファイルの読み込みをする

Ⅰ. はじめに

タイトルの通り「C++でShift-JIS, UTF-8, UTF-16 BOM有無を考慮してファイルの読み込みをする」方法です。
参考のURLには重要な内容が含まれているので目を通してください。

Ⅱ. 環境

Ⅲ. プログラム

ファイルの内容を全て読み込みむサンプルです。

Shift-JIS

#include <iostream>
#include <fstream>
#include <string>

ifstream ifs("shift-jis.txt");
string str((istreambuf_iterator<char>(ifs)), istreambuf_iterator<char>());

cout << str << endl;

UTF-8 (BOM なし)

#include <iostream>
#include <fstream>
#include <string>
#include <codecvt>

setlocale(LC_ALL, "");

wifstream ifs("UTF-8N.txt");
ifs.imbue(locale(locale::empty(), new codecvt_utf8<wchar_t>));
wstring str((istreambuf_iterator<wchar_t>(ifs)), istreambuf_iterator<wchar_t>());

wcout << str << endl;

UTF-8 (BOM あり)

#include <iostream>
#include <fstream>
#include <string>
#include <codecvt>

setlocale(LC_ALL, "");

wifstream ifs("UTF-8(BOM).txt");
ifs.imbue(locale(locale::empty(), new codecvt_utf8<wchar_t, 0x10ffff, consume_header>));
wstring str((istreambuf_iterator<wchar_t>(ifs)), istreambuf_iterator<wchar_t>());

wcout << str << endl

UTF-16トルエンディアン (BOM なし)

#include <iostream>
#include <fstream>
#include <string>
#include <codecvt>

setlocale(LC_ALL, "");

wifstream ifs("UTF-16LEN.txt");
ifs.imbue(locale(locale::empty(), new codecvt_utf16<wchar_t, 0x10ffff, little_endian>));
wstring str((istreambuf_iterator<wchar_t>(ifs)), istreambuf_iterator<wchar_t>());

wcout << str << endl

UTF-16トルエンディアン (BOM あり)

#include <iostream>
#include <fstream>
#include <string>
#include <codecvt>

setlocale(LC_ALL, "");

wifstream ifs("UTF-16LE(BOM).txt");
ifs.imbue(locale(locale::empty(), new codecvt_utf16<wchar_t, 0x10ffff, consume_header>));
wstring str((istreambuf_iterator<wchar_t>(ifs)), istreambuf_iterator<wchar_t>());

wcout << str << endl;

UTF-16 ビッグエンディアン (BOM なし)

#include <iostream>
#include <fstream>
#include <string>
#include <codecvt>

setlocale(LC_ALL, "");

wifstream ifs("UTF-16BEN.txt");
ifs.imbue(locale(locale::empty(), new codecvt_utf16<wchar_t>));
wstring str((istreambuf_iterator<wchar_t>(ifs)), istreambuf_iterator<wchar_t>());

wcout << str << endl;

UTF-16 ビッグエンディアン (BOM あり)

#include <iostream>
#include <fstream>
#include <string>
#include <codecvt>

setlocale(LC_ALL, "");

wifstream ifs("UTF-16BE(BOM).txt");
ifs.imbue(locale(locale::empty(), new codecvt_utf16<wchar_t, 0x10ffff, consume_header>));
wstring str((istreambuf_iterator<wchar_t>(ifs)), istreambuf_iterator<wchar_t>());

wcout << str << endl;

C++ ファイルを全て読み込む

Ⅰ. はじめに

STL を使ってファイルを全て読み込む(全行読み込む)方法です。

※追記
C++でShift-JIS, UTF-8, UTF-16 BOM有無とエンディアンを考慮してファイルの読み込みをする - 備忘録

Ⅱ. サンプル

test.txt

aiueo

Source.cpp

#include <iostream>
#include <fstream>
#include <string>

int main()
{
  std::ifstream ifs("test.txt");
  std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
  std::cout << str << std::endl;

  return 0;
}

Ⅲ. 実行結果

f:id:kagasu:20170501215610p:plain

C# で HtmlAgilityPack を使って HTML の要素値を XPath で取得する

Ⅰ. はじめに

タイトルの通り、「C# で HtmlAgilityPack を使って HTML の要素値を XPath で取得する」方法です。
Web スクレイピングが簡単になります。

Ⅱ. サンプル

f:id:kagasu:20170501191818p:plain
http://example.comの「タイトル」と「リンク先(href)の値」を取得するサンプルです。

1. NuGet で HtmlAgilityPack をインストールする

Install-Package HtmlAgilityPack

2. コードを書く

static void Main(string[] args)
{
  var str = new HttpClient().GetStringAsync("http://example.com/").Result;

  var html = new HtmlDocument();
  html.LoadHtml(str);

  var title = html.DocumentNode.SelectSingleNode("/html/body/div/h1");
  Console.WriteLine(title.InnerText);

  var link = html.DocumentNode.SelectSingleNode("/html/body/div/p[2]/a");
  Console.WriteLine(link.Attributes["href"].Value);
}

3. 実行結果

f:id:kagasu:20170501192039p:plain

※2017/05/24 追記
http://example.com の HTML が変わるとサンプルの意味が無くなるので、
念のため http://example.com のバックアップを残します。
https://gist.github.com/anonymous/e325a74047edab47a2cf6ccdef60af95

C#でSMTP, Mailgun, SendGridなどを利用してメールを送信する方法

Ⅰ. はじめに

メール配信サービスのAPIをラップした FluentEmail というライブラリの紹介です。
.NET Standard で作られているためクロスプラットフォームで動作します。

各サービスの API ドキュメントには HttpClient や RestClient 等を使って Web API を直接呼び出すサンプルがあります。
しかし、メールを送信する為に各サービスごとに Web API ドキュメントを読み、理解し、実装する手間がかかります。

FluentEmail はこれらの問題を解決してくれます。

2017/04/23 時点でSMTP, Mailgun, SendGrid に対応しています。

Ⅱ. FluentEmail のサンプル(SMTPの例)

1. NuGet から FluentEmail.Smtp をインストールする

f:id:kagasu:20210718020714p:plain

2. サンプルプログラム
var smtp = new SmtpSender(new SmtpClient("smtp.example.com", 465));
var email = Email.From("from@example.com", "MyName")
    .To("to@gmail.com")
    .Subject("Test subject")
    .Body("Test body");

smtp.Send(email);

Ⅲ. FluentEmail のサンプル(Mailgunの例)

1. NuGet から FluentEmail.Mailgun をインストールする

f:id:kagasu:20170423123802p:plain

2. サンプルプログラム
public async Task<bool> SendEmail()
{
  Email.DefaultSender = new MailgunSender(
      "mydomain.tld",
      "key-********************************");

  var email = Email
    .From("from@mydomain.tld")
    .To("to@gmail.com")
    .Subject("件名")
    .Body("本文");

  var response = await email.SendAsync();
  return response.Successful;
}

nginx で autoindexの文字化けを治す

Ⅰ. はじめに

nginx の autoindex を on にしただけだと以下のように文字化けします。
これを治す方法です。

Ⅱ. やり方

1. charset utf-8; を追加する
server {
  listen 80;
  root /var/www/html/hoge;
  index index.html;

  location / {
    autoindex on;
    charset utf-8;
  }
}
2. 設定をリロードする
# CentOS 7
systemctl reload nginx

# CentOS 6
service nginx reload