メインクラス

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

Form1.h


#pragma once

#include ".\reader.h"
#include ".\writer.h"
#include ".\convert.h"

namespace SourceToHtml
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

/// <summary>
/// Form1 の概要
///
/// 警告 : このクラスの名前を変更する場合、このクラスが依存するすべての .resx ファイルに関連付けられた
/// マネージ リソース コンパイラ ツールに対して 'Resource File Name' プロパティを
/// 変更する必要があります。この変更を行わないと、
/// デザイナと、このフォームに関連付けられたローカライズ済みリソースとが正しく相互に利用できなくなります。
/// </summary>
public __gc class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
}

protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private: System::Windows::Forms::ComboBox * cboLangType;
private: System::Windows::Forms::Label * label3;
private: System::Windows::Forms::Button * btnExit;
private: System::Windows::Forms::Button * btnOK;
private: System::Windows::Forms::TextBox * txtOutput;
private: System::Windows::Forms::TextBox * txtInput;
private: System::Windows::Forms::Label * label2;
private: System::Windows::Forms::Label * label1;

private:
/// <summary>
/// 必要なデザイナ変数です。
/// </summary>
System::ComponentModel::Container * components;

/// <summary>
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
/// </summary>
void InitializeComponent(void)
{
this->cboLangType = new System::Windows::Forms::ComboBox();
this->label3 = new System::Windows::Forms::Label();
this->btnExit = new System::Windows::Forms::Button();
this->btnOK = new System::Windows::Forms::Button();
this->txtOutput = new System::Windows::Forms::TextBox();
this->txtInput = new System::Windows::Forms::TextBox();
this->label2 = new System::Windows::Forms::Label();
this->label1 = new System::Windows::Forms::Label();
this->SuspendLayout();
//
// cboLangType
//
System::Object* __mcTemp__1[] = new System::Object*[13];
__mcTemp__1[0] = S"cs7";
__mcTemp__1[1] = S"vc6";
__mcTemp__1[2] = S"vc7";
__mcTemp__1[3] = S"vj6";
__mcTemp__1[4] = S"vj7";
__mcTemp__1[5] = S"java";
__mcTemp__1[6] = S"js";
__mcTemp__1[7] = S"bcb";
__mcTemp__1[8] = S"vb6";
__mcTemp__1[9] = S"vb7";
__mcTemp__1[10] = S"del";
__mcTemp__1[11] = S"psq";
__mcTemp__1[12] = S"tsq";
this->cboLangType->Items->AddRange(__mcTemp__1);
this->cboLangType->Location = System::Drawing::Point(112, 8);
this->cboLangType->Name = S"cboLangType";
this->cboLangType->Size = System::Drawing::Size(48, 20);
this->cboLangType->TabIndex = 9;
//
// label3
//
this->label3->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;
this->label3->Location = System::Drawing::Point(8, 8);
this->label3->Name = S"label3";
this->label3->Size = System::Drawing::Size(96, 19);
this->label3->TabIndex = 8;
this->label3->Text = S"言語タイプ";
this->label3->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;
//
// btnExit
//
this->btnExit->Location = System::Drawing::Point(312, 88);
this->btnExit->Name = S"btnExit";
this->btnExit->Size = System::Drawing::Size(72, 32);
this->btnExit->TabIndex = 15;
this->btnExit->Text = S"終了";
this->btnExit->Click += new System::EventHandler(this, btnExit_Click);
//
// btnOK
//
this->btnOK->Location = System::Drawing::Point(232, 88);
this->btnOK->Name = S"btnOK";
this->btnOK->Size = System::Drawing::Size(72, 32);
this->btnOK->TabIndex = 14;
this->btnOK->Text = S"実行";
this->btnOK->Click += new System::EventHandler(this, btnOK_Click);
//
// txtOutput
//
this->txtOutput->Location = System::Drawing::Point(112, 56);
this->txtOutput->Name = S"txtOutput";
this->txtOutput->Size = System::Drawing::Size(272, 19);
this->txtOutput->TabIndex = 13;
this->txtOutput->Text = S"";
//
// txtInput
//
this->txtInput->Location = System::Drawing::Point(112, 32);
this->txtInput->Name = S"txtInput";
this->txtInput->Size = System::Drawing::Size(272, 19);
this->txtInput->TabIndex = 11;
this->txtInput->Text = S"";
//
// label2
//
this->label2->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;
this->label2->Location = System::Drawing::Point(8, 56);
this->label2->Name = S"label2";
this->label2->Size = System::Drawing::Size(96, 19);
this->label2->TabIndex = 12;
this->label2->Text = S"出力ファイル名";
this->label2->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;
//
// label1
//
this->label1->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;
this->label1->Location = System::Drawing::Point(8, 32);
this->label1->Name = S"label1";
this->label1->Size = System::Drawing::Size(96, 19);
this->label1->TabIndex = 10;
this->label1->Text = S"入力ファイル名";
this->label1->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;
//
// Form1
//
this->AutoScaleBaseSize = System::Drawing::Size(5, 12);
this->ClientSize = System::Drawing::Size(394, 127);
this->Controls->Add(this->cboLangType);
this->Controls->Add(this->label3);
this->Controls->Add(this->btnExit);
this->Controls->Add(this->btnOK);
this->Controls->Add(this->txtOutput);
this->Controls->Add(this->txtInput);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
this->MaximizeBox = false;
this->MinimizeBox = false;
this->Name = S"Form1";
this->Text = S"SourceToHTML";
this->Load += new System::EventHandler(this, Form1_Load);
this->ResumeLayout(false);

}


//---------------------------------------------------------------------------------------------------
// 初期化
//---------------------------------------------------------------------------------------------------
System::Void Form1_Load(System::Object * sender, System::EventArgs * e)
{
cboLangType->Text = "";
txtInput->Text = "";
txtOutput->Text = "";

//言語タイプを実行時引数から取り込む
if (Environment::GetCommandLineArgs()->Length > 1)
cboLangType->Text = Environment::GetCommandLineArgs()[1];

//入力ファイル名を実行時引数から取り込む
if (Environment::GetCommandLineArgs()->Length > 2)
txtInput->Text = Environment::GetCommandLineArgs()[2];

//出力ファイル名を実行時引数から取り込む
if (Environment::GetCommandLineArgs()->Length > 3)
txtOutput->Text = Environment::GetCommandLineArgs()[3];

//実行時引数が全てセットされていたら、すぐに実行する
if (Environment::GetCommandLineArgs()->Length > 3)
btnOK->PerformClick();
}
//---------------------------------------------------------------------------------------------------
// 終了
//---------------------------------------------------------------------------------------------------
System::Void btnExit_Click(System::Object * sender, System::EventArgs * e)
{
this->Close();
}
//---------------------------------------------------------------------------------------------------
// 実行ボタン クリック
//---------------------------------------------------------------------------------------------------
System::Void btnOK_Click(System::Object * sender, System::EventArgs * e)
{
//実行時引数をチェック
if (!isCorrectArg()) return;

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

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

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

//テンプレートファイルを読んで、ヘッダ部を設定する
Reader* header = new Reader(String::Concat(System::Windows::Forms::Application::StartupPath, "\\Template\\header.txt"));
conv->charcopy(header, writer);
header = NULL;

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

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

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

//テンプレートファイルを読んで、フッタ部を設定する
Reader* footer = new Reader(String::Concat(System::Windows::Forms::Application::StartupPath, "\\Template\\footer.txt"));
conv->charcopy(footer, writer);
footer = NULL;

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

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

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

//入力ファイルチェック
System::IO::StreamReader* textReader;
try
{
textReader = new System::IO::StreamReader(txtInput->Text, System::Text::Encoding::GetEncoding("Shift_JIS"));
textReader->Close();
}
catch(Exception* ex)
{
MessageBox::Show(ex->Message,"エラー",MessageBoxButtons::OK,MessageBoxIcon::Exclamation);
return false;
}
__finally
{
textReader = NULL;
}

//出力ファイルチェック
System::IO::StreamWriter* textWriter;
try
{
textWriter = new System::IO::StreamWriter(txtOutput->Text, false, System::Text::Encoding::GetEncoding("Shift_JIS"));
textWriter->Close();
}
catch(Exception* ex)
{
MessageBox::Show(ex->Message,"エラー",MessageBoxButtons::OK,MessageBoxIcon::Exclamation);
return false;
}
__finally
{
textWriter = NULL;
}

return true;
}
//---------------------------------------------------------------------------------------------------
// 言語種別を取得
//---------------------------------------------------------------------------------------------------
String* getLangType(String* aLangType)
{
String* langType = aLangType->ToLower();

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

return langType;
}
};
}

Form1.cpp


#include "stdafx.h"
#include "Form1.h"
#include <windows.h>

using namespace SourceToHtml;

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState::STA;
Application::Run(new Form1());
return 0;
}