c++ builder / indy > MIMEタイプを取得する実装 > GetFileMIMEType()使用


動作確認
C++ Builder XE4

関連 http://qiita.com/7of9/items/6e88b5c8549fadd3b5f1

http://www.kyoto-su.ac.jp/ccinfo/use_web/mine_contenttype/
にあるMIMEのcontenttypeをファイル名から取得したい。

以下にあるDelphiコードのうち、Indy使用の実装が簡単だ。
http://mrxray.on.coocan.jp/Delphi/plSamples/728_File_MimeType.htm

実装

C++ Builderで実装した。

#include <IdGlobalProtocols.hpp>を追加してGetFileMIMEType()を使う。

Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include <IdGlobalProtocols.hpp>

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    String filepath = L"tmp.jpg";
    String res;

    TIdMimeTable *mimInst = new TIdMimeTable(/* AutoFill=*/false);
    res = mimInst->GetFileMIMEType(filepath);

    delete mimInst;
    mimInst = NULL;

    ShowMessage(res);
}
//---------------------------------------------------------------------------

クラスの静的関数ではなく、インスタンスを作成してそのメンバ関数を使うようだ。

上記の場合resには"image/pjpeg"となった。