メインクラス

これまでC# で作成してきた「SourceToHTML」を、C++Builder に焼きなおしてみます。

Unit1.h


//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE 管理のコンポーネント
TShape *Shape1;
TShape *Shape2;
TShape *Shape3;
TLabel *Label1;
TLabel *Label2;
TLabel *Label3;
TButton *btnOK;
TButton *btnExit;
TComboBox *cboLangType;
TEdit *txtInput;
TEdit *txtOutput;
void __fastcall FormCreate(TObject *Sender);
void __fastcall btnOKClick(TObject *Sender);
void __fastcall btnExitClick(TObject *Sender);
private: // ユーザー宣言
bool __fastcall isCorrectArg();
AnsiString __fastcall getLangType(AnsiString aLangType);
public: // ユーザー宣言
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Unit1.cpp


//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdio.h>
#pragma hdrstop

#include "Unit1.h"
#include "TextWriter.h"
#include "TextReader.h"
#include "Convert.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
}
//---------------------------------------------------------------------------
// 初期化
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
cboLangType->Text = "";
txtInput->Text = "";
txtOutput->Text = "";

//実行時引数の数を取得
int iParamCount = ParamCount();

//言語タイプを実行時引数から取り込む
if (iParamCount > 0)
cboLangType->Text = ParamStr(1);

//入力ファイル名を実行時引数から取り込む
if (iParamCount > 1)
txtInput->Text = ParamStr(2);

//出力ファイル名を実行時引数から取り込む
if (iParamCount > 2)
txtOutput->Text = ParamStr(3);

//実行時引数が全てセットされていたら、すぐに実行する
if (iParamCount > 2)
btnOK->Click();
}
//---------------------------------------------------------------------------
// 終了
//---------------------------------------------------------------------------
void __fastcall TForm1::btnExitClick(TObject *Sender)
{
this->Close();
}
//---------------------------------------------------------------------------
// 実行ボタン クリック
//---------------------------------------------------------------------------
void __fastcall TForm1::btnOKClick(TObject *Sender)
{
//実行時引数をチェック
if (!isCorrectArg()) return;

//言語種別を取得
AnsiString langType = getLangType(cboLangType->Text);

//出力用クラスを初期化
TTextWriter* writer = new TTextWriter(txtOutput->Text);

//変換用クラスを初期化
TConvert* conv = new TConvert();

//テンプレートファイルを読んで、ヘッダ部を設定する
TTextReader* header = new TTextReader(ExtractFilePath(Application->ExeName) + "\\Template\\header.txt");
conv->charcopy(header, writer);
delete header;
header = NULL;

//入力用クラスを初期化
TTextReader* reader = new TTextReader(txtInput->Text);

//HTML に変換する
conv->tohtml(reader, writer, langType);

//入力用クラスを解放
delete reader;
reader = NULL;

//テンプレートファイルを読んで、フッタ部を設定する
TTextReader* footer = new TTextReader(ExtractFilePath(Application->ExeName) + "\\Template\\footer.txt");
conv->charcopy(footer, writer);
delete footer;
footer = NULL;

//変換用クラスを解放
delete conv;
conv = NULL;

//出力用クラスを解放
writer->Close();
delete writer;
writer = NULL;

//終了
this->Close();
}
//---------------------------------------------------------------------------
// 実行時引数をチェック
//---------------------------------------------------------------------------
bool __fastcall TForm1::isCorrectArg()
{
//必須チェック
if (cboLangType->Text == "") return false;
if (txtInput->Text == "") return false;
if (txtOutput->Text == "") return false;

FILE *F;

//入力ファイルチェック
try
{
F = fopen(txtInput->Text.c_str(),"rt");
if (F==NULL)
{
Application->MessageBox("ファイルが見つかりません","エラー", MB_ICONEXCLAMATION + MB_OK);
return false;
}
fclose(F);
}
catch(Exception &ex)
{
Application->MessageBox(ex.Message.c_str(),"エラー", MB_ICONEXCLAMATION + MB_OK);
return false;
}

//出力ファイルチェック
try
{
F = fopen(txtOutput->Text.c_str(),"wt");
if (F==NULL)
{
Application->MessageBox("不正なファイル名です","エラー", MB_ICONEXCLAMATION + MB_OK);
return false;
}
fclose(F);
}
catch(Exception &ex)
{
Application->MessageBox(ex.Message.c_str(),"エラー", MB_ICONEXCLAMATION + MB_OK);
return false;
}

return true;
}
//---------------------------------------------------------------------------
// 言語種別を取得
//---------------------------------------------------------------------------
AnsiString __fastcall TForm1::getLangType(AnsiString aLangType)
{
AnsiString langType = LowerCase(aLangType);

if (langType == "cs7") langType = "cs7"; // VC#.NET
else if (langType == "vc6") langType = "vc6"; // VC++
else if (langType == "vc7") langType = "vc7"; // VC++.NET
else if (langType == "vj6") langType = "java"; // VJ++
else if (langType == "vj7") langType = "java"; // VJ#.NET
else if (langType == "java") langType = "java"; // Java
else if (langType == "js") langType = "js"; // JavaScript
else if (langType == "bcb") langType = "bcb"; // C++Builder
else if (langType == "vb6") langType = "vb6"; // VB
else if (langType == "vb7") langType = "vb7"; // VB.NET
else if (langType == "del") langType = "del"; // Delphi
else if (langType == "psq") langType = "psq"; // PL/SQL
else if (langType == "tsq") langType = "tsq"; // T-SQL
else langType = ""; // other

return langType;
}