Delphi Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
델파이 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
FreePascal/Lazarus
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
델마당
볼랜드포럼 광고 모집

델파이 팁&트릭
Delphi Programming Tip&Tricks
[176] 어플리케이션 INI를 대신해보자
civilian,안영제 [civilian] 5753 읽음    2005-07-11 18:29
어플리케이션에서 사용하는 특정한 내용들은 보통 .INI 파일이나 레지스트리에 저장하는데
하다보면 무진장 귀찮습니다(귀차니즘 모드).

특히, 정수, 문자 등이 아닌 열거형이나 집합형을 읽고 쓰려면 더욱 귀찮지요.
그래서 델파이가 컴포넌트의 정보를 폼(DFM)에 저장되는 방식을 빌어서 좀 간편하게 사용할 수 있도록
클래스를 한번 만들어 봤습니다.

//
// Unit        : dxConfigManager.pas
// Description : INI를 대신하는 유닛
//               누구든 자유롭게 사용하고 고쳐쓸 수 있습니다.
// Author      : 안영제(civilian@korea.com)
// History
//   1.0   2005.07.11 처음 만듦
//

unit dxConfigManager;

interface

uses
  Windows, SysUtils, Classes;

type
  TCustomConfigManager = class(TComponent)
  private
    FFileName: String;
  public
    constructor Create(AOwner: TComponent); override;

    procedure LoadFromFile;
    procedure SaveToFile;

    property FileName: String read FFileName write FFileName;
  end;

implementation

{ TCustomConfigManager }

constructor TCustomConfigManager.Create(AOwner: TComponent);
begin
  inherited;

  FFileName := '';
end;

//
// 파일에서 읽어오기
//
procedure TCustomConfigManager.LoadFromFile;
var
  Stream: TFileStream;
  BinStream: TMemoryStream;
begin
  if FileExists(FFileName) then
  begin
    Stream := TFileStream.Create(FFileName, fmOpenRead);
    BinStream := TMemoryStream.Create;
    try
      ObjectTextToBinary(Stream, BinStream);
      BinStream.Seek(0, soFromBeginning);
      BinStream.ReadComponent(Self);
    finally
      Stream.Free;
      BinStream.Free;
    end;
  end;
end;

//
// 파일에 저장
//
procedure TCustomConfigManager.SaveToFile;
var
  Stream: TFileStream;
  BinStream: TMemoryStream;
begin
  Stream := TFileStream.Create(FFileName, fmCreate);
  BinStream := TMemoryStream.Create;

  try
    BinStream.WriteComponent(Self);
    BinStream.Seek(0, soFromBeginning);
    ObjectBinaryToText(BinStream, Stream);
  finally
    Stream.Free;
    BinStream.Free;
  end;
end;

end.


::사용방법::

위 클래스를 어플리케이션에서 사용하려면 우선 저녀석으로 부터 상속받은 클래스를 하나 만들어야 합니다.
type
  TMyConfig = class(TCustomConfigManager)
  private
    FServerIP: String;
    FServerPort: String;
  published
    //
    // 파일에 기록되는 것은 published 절에 있는 프로퍼티만 저장됩니다.
    //
    property ServerIP: String read FServerIP write FServerIP;
    property ServerPort: String read FServerPort write FServerPort;
  end;


자 이제 클래스를 만들었으니 사용하면 되겠지요.

::저장할 때::
MyConfig := TMyConfig.Create;
MyConfig.FileName := 'cL\temp\MyConfig.dat';
MyConfig.ServerIP := '10.0.0.1';
MyConfig.ServerPort := '8080';

MyConfig.SaveToFile;
MyConfig.Free;

::읽어올 때::
MyConfig := TMyConfig.Create;
MyConfig.FileName := 'cL\temp\MyConfig.dat';
MyConfig.LoadFromFile;
MyConfig.Free;


보통 유닛(Config.pas)에 넣어놓고 쓰는 것이 편리할 겁니다.

::저장된 파일의 구조::
델파이의 DFM이 TEXT 포맷으로 저장된 것과 동일합니다.
아래 샘플은 제가 만드는 어플리케이션의 설정값을 저장한 것입니다.
object TMyConfig
  AudioOptions.Auto = True
  AudioOptions.Device = -1
  AudioOptions.Format = -1
  FixedHeight = 0
  FixedWidth = 0
  CursorOptions.ActualCursor = False
  CursorOptions.HighliteColor = clYellow
  CursorOptions.HighliteCursor = False
  CursorOptions.HighliteShape = hsCircle
  CursorOptions.HighliteSize = 0
  CursorOptions.LeftClickUse = False
  CursorOptions.LeftClickShape = csRing
  CursorOptions.LeftClickSize = 0
  CursorOptions.RightClickUse = False
  CursorOptions.RightClickShape = csRing
  CursorOptions.RightClickSize = 0
  NameOption = foAsk
  ProgramOptions = []
  RecordOptions = [ro4Times]
  SoundOptions.MouseSoundUse = False
  SoundOptions.MouseVolume = 0
  SoundOptions.KeySoundUse = False
  SoundOptions.KeyVolume = 0
  VideoOptions.Auto = True
  VideoOptions.Frame = 10
  VideoOptions.KeyFrame = 1
end


도움이 되시길....

+ -

관련 글 리스트
176 어플리케이션 INI를 대신해보자 civilian,안영제 5753 2005/07/11
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.