0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C++ Builder > ファイル処理検討 > 複数のプロファイルの切替え > Setting.txtを経由する

Last updated at Posted at 2017-10-23
動作環境
C++ Builder XE4

関連

C++ Builder > UI検討 > 複数のプロファイルの切替え > TComboBox + Buttons (Add, Delete) + Button (Update)

処理

  • 複数のプロファイルを持つ
  • Setting.txtからプロファイルの詳細設定を取得する
  • 下記のファイルにそれぞれ保存する
    • Setting.prof1
    • Setting.prof2
    • Setting.prof3
Unit1.cpp
//---------------------------------------------------------------------------

# include <vcl.h>
# pragma hdrstop

# include <IOUtils.hpp>
# include <memory>
# include "Unit1.h"
//---------------------------------------------------------------------------
# pragma package(smart_init)
# pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------

static const String kFilename_withoutExt = L"Setting";
static const String kExtension_default = L"txt";


//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
	E_details->Text = L"";

	// ファイル選択用のデータ
	CMB_profile->Items->Clear();
	CMB_profile->Items->Add(L"prof1");
	CMB_profile->Items->Add(L"prof2");
	CMB_profile->Items->Add(L"prof3");
}

void __fastcall TForm1::FormShow(TObject *Sender)
{
	Setting_load();
}

//---------------------------------------------------------------------------
void __fastcall TForm1::Setting_load(void)
{
	// デフォルトファイルを読む

	String filename = kFilename_withoutExt + L"." + kExtension_default;

	if (FileExists(filename) == false) {
		return; // error
	}

	std::unique_ptr<TStringList> SLwrk(new TStringList);
	try {
		SLwrk->LoadFromFile(filename);
	} catch (Exception &exc) {
		ShowMessage(exc.Message);
		return; // error
	}

	if (SLwrk->Count == 0) {
		return; // error
	}

	E_details->Text = SLwrk->Strings[0];
}

bool __fastcall TForm1::Setting_overwriteDefault(String srcExtension)
{
	// デフォルトファイルを書換える

	String srcfile = kFilename_withoutExt + L"." + srcExtension;
	String dstfile = kFilename_withoutExt + L"." + kExtension_default;

	try {
		TFile::Copy(srcfile, dstfile, true); // true: overwrite
	} catch (Exception &exc) {
		ShowMessage(exc.Message);
		return false; // error
	}

	return true;
}

bool __fastcall TForm1::Setting_overwriteSpecified(String dstExtension)
{
	// 特定の設定ファイルを書換える

	String filename = kFilename_withoutExt + L"." + CMB_profile->Text;
	OutputDebugString(filename.c_str());

	std::unique_ptr<TStringList> SLwrk (new TStringList);
	SLwrk->Add(E_details->Text);

	try {
		SLwrk->SaveToFile(filename);
	} catch (Exception &exc) {
		ShowMessage(exc.Message);
	}
}

void __fastcall TForm1::CMB_profileSelect(TObject *Sender)
{
	// 特定の設定ファイルでデフォルトファイルを書換える

	String fileExt = CMB_profile->Text;
	if ( Setting_overwriteDefault(fileExt) == false ) {
		return; // error
	}
	Setting_load();
}

void __fastcall TForm1::B_saveClick(TObject *Sender)
{
	Setting_overwriteSpecified(CMB_profile->Text);
	Setting_overwriteDefault(CMB_profile->Text);
}

qiita.png

  • プロファイル選択
    • コンボボックスを選択
  • 詳細の書換え
    • detailを書換えて
    • B_saveを押す

設計理由

Setting.xxxというファイルに対してJSON形式での読書きを行うツールを二年前に作り、今まで使い続けてきた。
Setting.xxxというファイルで読書きをすることで、そのツールがそのまま使える。

ただし、仕組みが分かるドキュメントがないとソースリーディングはしにくいだろう。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?