home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 November / Chip_2002-11_cd1.bin / zkuste / delphi / unity / d56 / DW / DW10242.ZIP / IniWorks.pas < prev    next >
Pascal/Delphi Source File  |  2001-09-11  |  12KB  |  242 lines

  1. unit IniWorks;
  2.  
  3. interface
  4.  
  5. uses Classes, Forms, Graphics, SysUtils, IniFiles, StringWorks;
  6.  
  7. function IniValueList(Ini, Section: String): TStringList;
  8. function IniValueNameList(Ini, Section: String): TStringList;
  9. function IniReadInteger(Ini, Section, Value: String): Integer;
  10. function IniReadBool(Ini, Section, Value: String): Boolean;
  11. function IniWriteBool(Ini, Section, Item: String; Value: Boolean): Boolean;
  12. procedure IniSaveForm(Form: TForm; IniFile: String);
  13. procedure IniLoadForm(Form: TForm; IniFile: String);
  14.  
  15. function IsNotNIL(Ini: TIniFile; Section, Item: String): Boolean;
  16.  
  17. const NILSTRING = '!º>NLNONE<º!';
  18.  
  19. implementation
  20.  
  21. function IniValueList(Ini, Section: String): TStringList;
  22. var
  23.    I: Integer;
  24.    IniF: TIniFile;
  25.    TempList: TStringList;
  26.    TempStr: String;
  27. begin
  28.    result:= TStringList.Create;
  29.    TempList:= TStringList.Create;
  30.    if not FileExists(Ini) then exit;
  31.    IniF:= TIniFile.Create(Ini);
  32.    with IniF do begin
  33.       if not SectionExists(Section) then exit;
  34.       ReadSectionValues(Section, TempList);
  35.       if TempList.Count = 0 then exit else if TempList.Count = 1 then begin
  36.             TempStr:= TempList[0];
  37.             result.Add(StrPart(TempStr, Pos('=', TempStr) + 1,
  38.                                StringLen(TempStr)+1));
  39.          end else
  40.          if TempList.Count > 1 then
  41.             for I:= 0 to TempList.Count - 1 do begin
  42.                TempStr:= TempList[I];
  43.                result.Add(StrPart(TempStr, Pos('=', TempStr) + 1,
  44.                           StringLen(TempStr)+1));
  45.             end;
  46.    end;
  47.    TempList.Free;
  48.    IniF.Free;   
  49. end;
  50.  
  51. function IniValueNameList(Ini, Section: String): TStringList;
  52. var
  53.    I: Integer;
  54.    IniF: TIniFile;
  55.    TempList: TStringList;
  56.    TempStr: String;
  57. begin
  58.    result:= TStringList.Create;
  59.    TempList:= TStringList.Create;
  60.    if not FileExists(Ini) then exit;
  61.    IniF:= TIniFile.Create(Ini);
  62.    with IniF do begin
  63.       if not SectionExists(Section) then exit;
  64.       ReadSectionValues(Section, TempList);
  65.       if TempList.Count = 0 then exit else if TempList.Count = 1 then
  66.          result.Add(StrLeft(TempStr, Pos('=', TempStr) - 1)) else
  67.          if TempList.Count > 1 then
  68.             for I:= 0 to TempList.Count - 1 do begin
  69.                TempStr:= TempList[I];
  70.                result.Add(StrLeft(TempStr, Pos('=', TempStr) - 1));
  71.             end;
  72.    end;
  73.    TempList.Free;
  74.    IniF.Free;
  75. end;
  76.  
  77. function IniReadInteger(Ini, Section, Value: String): Integer;
  78. var IniF: TIniFile;
  79. begin
  80.    result:= 0;
  81.    if not FileExists(Ini) then exit;
  82.    IniF:= TIniFile.Create(Ini);
  83.    if not IniF.SectionExists(Section) or not IniF.ValueExists(Section, Value)
  84.       then exit;
  85.    result:= IniF.ReadInteger(Section, Value, 0);
  86.    IniF.Free;
  87. end;
  88.  
  89. function IniReadBool(Ini, Section, Value: String): Boolean;
  90. var IniF: TIniFile;
  91. begin
  92.    result:= FALSE;
  93.    if not FileExists(Ini) then exit;
  94.    IniF:= TIniFile.Create(Ini);
  95.    if not IniF.SectionExists(Section) or not IniF.ValueExists(Section, Value)
  96.       then exit;
  97.    result:= IniF.ReadBool(Section, Value, FALSE);
  98.    IniF.Free;
  99. end;
  100.  
  101. function IniWriteBool(Ini, Section, Item: String; Value: Boolean): Boolean;
  102. var IniF: TIniFile;
  103. begin
  104.    result:= FALSE;
  105.    if not FileExists(Ini) then exit;
  106.    IniF:= TIniFile.Create(Ini);
  107.    if not IniF.SectionExists(Section) or not IniF.ValueExists(Section, Item)
  108.       then exit;
  109.    IniF.WriteBool(Section, Item, Value);
  110.    result:= IniF.ReadBool(Section, Item, FALSE);
  111.    IniF.Free;
  112. end;
  113.  
  114. procedure IniSaveForm(Form: TForm; IniFile: String);
  115. var
  116.    Ini: TIniFile;
  117.    FN: String;
  118. begin
  119.    FN:= Form.Name;
  120.    Ini:= TIniFile.Create(IniFile);
  121.    with Ini do begin
  122.       if Assigned(Form.Action) then WriteString(FN, 'Action', Form.Action.Name) else WriteString(FN, 'Action', NILSTRING);
  123.       if Assigned(Form.ActiveControl) then WriteString(FN, 'ActiveControl', Form.ActiveControl.Name) else WriteString(FN, 'ActiveControl', NILSTRING);
  124.       WriteString(FN, 'Align', AlignToStr(Form.Align));
  125.       WriteString(FN, 'Anchors', AnchorsToStr(Form.Anchors));
  126.       WriteBool(FN, 'AutoScroll', Form.AutoScroll);
  127.       WriteBool(FN, 'AutoSize', Form.AutoSize);
  128.       WriteString(FN, 'BiDiMode', BiDiModeToStr(Form.BiDiMode));
  129.       WriteString(FN, 'BorderIcons', BorderIconsToStr(Form.BorderIcons));
  130.       WriteString(FN, 'BorderStyle', BorderStyleToStr(Form.BorderStyle));
  131.       WriteInteger(FN, 'BorderWidth', Form.BorderWidth);
  132.       WriteString(FN, 'Caption', Form.Caption);
  133.       WriteInteger(FN, 'ClientHeight', Form.ClientHeight);
  134.       WriteInteger(FN, 'ClientWidth', Form.ClientWidth);
  135.       WriteString(FN, 'Color', ColorToString(Form.Color));
  136.       WriteString(FN, 'Constraints', ConstraintsToStr(Form.Constraints));
  137.       WriteInteger(FN, 'Cursor', Int64(Form.Cursor));
  138.       WriteString(FN, 'DefaultMonitor', DefaultMonitorToStr(Form.DefaultMonitor));
  139.       WriteBool(FN, 'DockSite', Form.DockSite);
  140.       WriteString(FN, 'DragKind', DragKindToStr(Form.DragKind));
  141.       WriteString(FN, 'DragMode', DragModeToStr(Form.DragMode));
  142.       WriteBool(FN, 'Enabled', Form.Enabled);
  143.       WriteString(FN, 'Font', FontToStr(Form.Font));
  144.       WriteString(FN, 'FormStyle', FormStyleToStr(Form.FormStyle));
  145.       WriteInteger(FN, 'Height', Form.Height);
  146.       WriteInteger(FN, 'HelpContext', Form.HelpContext);
  147.       WriteString(FN, 'HelpFile', Form.HelpFile);
  148.       WriteString(FN, 'Hint', Form.Hint);
  149.       WriteString(FN, 'HorzScrollBar', ControlScrollBarToStr(Form.HorzScrollBar));
  150.       if Assigned(Form.Icon) then WriteString(FN, 'Icon', Form.Icon.ClassName) else WriteString(FN, 'Icon', NILSTRING);
  151.       WriteBool(FN, 'KeyPreview', Form.KeyPreview);
  152.       WriteInteger(FN, 'Left', Form.Left);
  153.       if Assigned(Form.Menu) then WriteString(FN, 'Menu', Form.Menu.Name) else WriteString(FN, 'Menu', NILSTRING);
  154.       WriteString(FN, 'Name', Form.Name);
  155.       if Assigned(Form.ObjectMenuItem) then WriteString(FN, 'ObjectMenuItem', Form.ObjectMenuItem.Name) else WriteString(FN, 'ObjectMenuItem', NILSTRING);
  156.       WriteBool(FN, 'ParentBiDiMode', Form.ParentBiDiMode);
  157.       WriteBool(FN, 'ParentFont', Form.ParentFont);
  158.       WriteInteger(FN, 'PixelsPerInch', Form.PixelsPerInch);
  159.       if Assigned(Form.PopupMenu) then WriteString(FN, 'PopupMenu', Form.PopupMenu.Name) else WriteString(FN, 'PopupMenu', NILSTRING);
  160.       WriteString(FN, 'Position', PositionToStr(Form.Position));
  161.       WriteString(FN, 'PrintScale', PrintScaleToStr(Form.PrintScale));
  162.       WriteBool(FN, 'Scaled', Form.Scaled);
  163.       WriteBool(FN, 'ShowHint', Form.ShowHint);
  164.       WriteInteger(FN, 'Tag', Form.Tag);
  165.       WriteInteger(FN, 'Top', Form.Top);
  166.       WriteBool(FN, 'UseDockManager', Form.UseDockManager);
  167.       WriteString(FN, 'VertScrollBar', ControlScrollBarToStr(Form.VertScrollBar));
  168.       WriteBool(FN, 'Visible', Form.Visible);
  169.       WriteInteger(FN, 'Width', Form.Width);
  170.       if Assigned(Form.WindowMenu) then WriteString(FN, 'WindowMenu', Form.WindowMenu.Name) else WriteString(FN, 'WindowMenu', NILSTRING);
  171.       WriteString(FN, 'WindowState', WindowStateToStr(Form.WindowState));
  172.    end;
  173. end;
  174.  
  175. procedure IniLoadForm(Form: TForm; IniFile: String);
  176. var
  177.    Ini: TIniFile;
  178.    FN: String;
  179. begin
  180.    Ini:= TIniFile.Create(IniFile);
  181.    FN:= Form.Name;
  182.    with Ini do begin
  183.       if IsNotNIL(Ini, FN, 'Action') then Form.Action.Name:= ReadString(FN, 'Action', NILSTRING);
  184.       if IsNotNIL(Ini, FN, 'ActiveControl') then Form.ActiveControl.Name:= ReadString(FN, 'ActiveControl', NILSTRING);
  185.       Form.Align:= StrToAlign(ReadString(FN, 'Align', NILSTRING));
  186.       Form.Anchors:= StrToAnchors(ReadString(FN, 'Anchors', NILSTRING));
  187.       Form.AutoScroll:= ReadBool(FN, 'AutoScroll', TRUE);
  188.       Form.AutoSize:= ReadBool(FN, 'AutoSize', TRUE);
  189.       Form.BiDiMode:= StrToBiDiMode(ReadString(FN, 'BiDiMode', NILSTRING));
  190.       Form.BorderIcons:= StrToBorderIcons(ReadString(FN, 'BorderIcons', NILSTRING));
  191.       Form.BorderStyle:= StrToBorderStyle(ReadString(FN, 'BorderStyle', NILSTRING));
  192.       Form.BorderWidth:= ReadInteger(FN, 'BorderWidth', 0);
  193.       Form.Caption:= ReadString(FN, 'Caption', Form.Name);
  194.       Form.ClientHeight:= ReadInteger(FN, 'ClientHeight', Form.ClientHeight);
  195.       Form.ClientWidth:= ReadInteger(FN, 'ClientWidth', Form.ClientWidth);
  196.       Form.Color:= StringToColor(ReadString(FN, 'Color', ColorToString(Form.Color)));
  197.       Form.Constraints:= StrToConstraints(ReadString(FN, 'Constraints', ConstraintsToStr(Form.Constraints)));
  198.       Form.Cursor:= ReadInteger(FN, 'Cursor', Int64(Form.Cursor));
  199.       Form.DefaultMonitor:= StrToDefaultMonitor(ReadString(FN, 'DefaultMonitor', DefaultMonitorToStr(Form.DefaultMonitor)));
  200.       Form.DockSite:= ReadBool(FN, 'DockSite', Form.DockSite);
  201.       Form.DragKind:= StrToDragKind(ReadString(FN, 'DragKind', DragKindToStr(Form.DragKind)));
  202.       Form.DragMode:= StrToDragMode(ReadString(FN, 'DragMode', DragModeToStr(Form.DragMode)));
  203.       Form.Enabled:= ReadBool(FN, 'Enabled', Form.Enabled);
  204.       Form.Font:= StrToFont(ReadString(FN, 'Font', FontToStr(Form.Font)));
  205.       Form.FormStyle:= StrToFormStyle(ReadString(FN, 'FormStyle', FormStyleToStr(Form.FormStyle)));
  206.       Form.Height:= ReadInteger(FN, 'Height', Form.Height);
  207.       Form.HelpContext:= ReadInteger(FN, 'HelpContext', Form.HelpContext);
  208.       Form.HelpFile:= ReadString(FN, 'HelpFile', Form.HelpFile);
  209.       Form.Hint:= ReadString(FN, 'Hint', Form.Hint);
  210.       Form.HorzScrollBar:= StrToControlScrollBar(ReadString(FN, 'HorzScrollBar', ''{ControlScrollBarToStr(Form.HorzScrollBar)}));
  211. //      if IsNotNIL(Ini, FN, 'Icon') then Form.Icon.ClassName:= ReadString(FN, 'Icon', Form.Icon.ClassName));
  212.       Form.KeyPreview:= ReadBool(FN, 'KeyPreview', Form.KeyPreview);
  213.       Form.Left:= ReadInteger(FN, 'Left', Form.Left);
  214.       if IsNotNIL(Ini, FN, 'Menu') then Form.Menu.Name:= ReadString(FN, 'Menu', Form.Menu.Name);
  215.       Form.Name:= ReadString(FN, 'Name', Form.Name);
  216.       if IsNotNIL(Ini, FN, 'ObjectMenuItem') then Form.ObjectMenuItem.Name:= ReadString(FN, 'ObjectMenuItem', Form.ObjectMenuItem.Name);
  217.       Form.ParentBiDiMode:= ReadBool(FN, 'ParentBiDiMode', Form.ParentBiDiMode);
  218.       Form.ParentFont:= ReadBool(FN, 'ParentFont', Form.ParentFont);
  219.       Form.PixelsPerInch:= ReadInteger(FN, 'PixelsPerInch', Form.PixelsPerInch);
  220.       if IsNotNIL(Ini, FN, 'PopupMenu') then Form.PopupMenu.Name:= ReadString(FN, 'PopupMenu', Form.PopupMenu.Name);
  221.       Form.Position:= StrToPosition(ReadString(FN, 'Position', PositionToStr(Form.Position)));
  222.       Form.PrintScale:= StrToPrintScale(ReadString(FN, 'PrintScale', PrintScaleToStr(Form.PrintScale)));
  223.       Form.Scaled:= ReadBool(FN, 'Scaled', Form.Scaled);
  224.       Form.ShowHint:= ReadBool(FN, 'ShowHint', Form.ShowHint);
  225.       Form.Tag:= ReadInteger(FN, 'Tag', Form.Tag);
  226.       Form.Top:= ReadInteger(FN, 'Top', Form.Top);
  227.       Form.UseDockManager:= ReadBool(FN, 'UseDockManager', Form.UseDockManager);
  228. //      Form.VertScrollBar:= StrToControlScrollBar(ReadString(FN, 'VertScrollBar', ControlScrollBarToStr(Form.VertScrollBar)));
  229.       Form.Visible:= ReadBool(FN, 'Visible', Form.Visible);
  230.       Form.Width:= ReadInteger(FN, 'Width', Form.Width);
  231.       if IsNotNIL(Ini, FN, 'WindowMenu') then Form.WindowMenu.Name:= ReadString(FN, 'WindowMenu', Form.WindowMenu.Name);
  232.       Form.WindowState:= StrToWindowState(ReadString(FN, 'WindowState', WindowStateToStr(Form.WindowState)));
  233.    end;
  234. end;
  235.  
  236. function IsNotNIL(Ini: TIniFile; Section, Item: String): Boolean;
  237. begin
  238.    result:= not (Ini.ReadString(Section, Item, NILSTRING) = NILSTRING);
  239. end;
  240.  
  241. end.
  242.