メインクラス

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

Unit1.pas


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, TextWriter, TextReader, Convert;

type
TForm1 = class(TForm)
Shape1: TShape;
Shape2: TShape;
Shape3: TShape;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
btnOK: TButton;
btnExit: TButton;
cboLangType: TComboBox;
txtInput: TEdit;
txtOutput: TEdit;
procedure btnOKClick(Sender: TObject);
procedure btnExitClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
function isCorrectArg():Boolean;
function getLangType(aLangType:string):string;
private
{ Private 宣言 }
public
{ Public 宣言 }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

//---------------------------------------------------------------------------------------------------
// 初期化
//---------------------------------------------------------------------------------------------------
procedure TForm1.FormCreate(Sender: TObject);
var
iParamCount : Integer;
begin
cboLangType.Text := '';
txtInput.Text := '';
txtOutput.Text := '';

//実行時引数の取り込む
iParamCount := ParamCount;

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

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

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

//実行時引数が全てセットされていたら、すぐに実行する
if iParamCount > 2 then
btnOK.Click;
end;
//---------------------------------------------------------------------------------------------------
// 終了
//---------------------------------------------------------------------------------------------------
procedure TForm1.btnExitClick(Sender: TObject);
begin
self.Close;
end;
//---------------------------------------------------------------------------------------------------
// 実行ボタン クリック
//---------------------------------------------------------------------------------------------------
procedure TForm1.btnOKClick(Sender: TObject);
var
langType:string; //言語種別
writer:TTextWriter; //出力用クラス
reader:TTextReader; //ソース入力用クラス
header:TTextReader; //ヘッダ部入力用クラス
footer:TTextReader; //フッタ部入力用クラス
conv:TConvert; //変換用クラス
begin
//実行時引数をチェック
if (not isCorrectArg()) then exit;

//言語種別を取得
langType := getLangType(cboLangType.Text);

//出力用クラスを初期化
writer := TTextWriter.Create(txtOutput.Text);

//変換用クラスを初期化
conv := TConvert.Create();

//テンプレートファイルを読んで、ヘッダ部を設定する
header := TTextReader.Create(ExtractFilePath(Application.ExeName) + 'Template\header.txt');
conv.charcopy(header, writer);
header.Free;
header := nil;

//入力用クラスを初期化
reader := TTextReader.Create(txtInput.Text);

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

//入力用クラスを解放
reader.Free;
reader := nil;

//テンプレートファイルを読んで、フッタ部を設定する
footer := TTextReader.Create(ExtractFilePath(Application.ExeName) + 'Template\footer.txt');
conv.charcopy(footer, writer);
footer.Free;
footer := nil;

//変換用クラスを解放
conv.Free;
conv := nil;

//出力用クラスを解放
writer.Close();
writer.Free;
writer := nil;

//終了
self.Close;
end;
//---------------------------------------------------------------------------------------------------
// 実行時引数をチェック
//---------------------------------------------------------------------------------------------------
function TForm1.isCorrectArg():Boolean;
var
F:TextFile;
begin
result := false;

//必須チェック
if (cboLangType.Text = '') then exit;
if (txtInput.Text = '') then exit;
if (txtOutput.Text = '') then exit;

//入力ファイルチェック
try
AssignFile(F,txtInput.Text);
ReSet(F);
CloseFile(F);
except
on ex:Exception do
begin
Application.MessageBox(PChar(ex.message),'エラー', MB_ICONEXCLAMATION + MB_OK);
exit;
end;
end;

//出力ファイルチェック
try
AssignFile(F,txtOutput.Text);
ReWrite(F);
CloseFile(F);
except
on ex:Exception do
begin
Application.MessageBox(PChar(ex.message),'エラー', MB_ICONEXCLAMATION + MB_OK);
exit;
end;
end;

result := true;
end;
//---------------------------------------------------------------------------------------------------
// 言語種別を取得
//---------------------------------------------------------------------------------------------------
function TForm1.getLangType(aLangType:string):string;
begin
result := LowerCase(aLangType);

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

end.