File: lpicustomdata.lpr

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (113 lines) | stat: -rw-r--r-- 3,418 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
{ Copyright (C) 2007 Mattias Gaertner

 *****************************************************************************
 *                                                                           *
 *  See the file COPYING.modifiedLGPL, included in this distribution,        *
 *  for details about the copyright.                                         *
 *                                                                           *
 *  This program is distributed in the hope that it will be useful,          *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     *
 *                                                                           *
 *****************************************************************************

  Abstract:
    This example shows how to read the custom data of an .lpi file and
    how to change it. The .lpi file is given as first command line parameter.
  
    For example:
      ./lpicustomdata lpicustomdata.lpi

      This will show the old custom data, change a value and save it.
      If there was no custom data in the .lpi file, there will be no output.
      
    Important:
      The IDE does not read the .lpi file if it changed on disk.
      If you add custom data, make sure the IDE has not this project open.
    
}
program LPICustomData;

{$mode objfpc}{$H+}

uses
  SysUtils,
  // LazUtils
  Laz2_XMLCfg, AvgLvlTree;

procedure LoadStringToStringTree(XMLConfig: TXMLConfig;
  Tree: TStringToStringTree; const Path: string);
var
  Cnt: LongInt;
  SubPath: String;
  CurName: String;
  CurValue: String;
  i: Integer;
begin
  Tree.Clear;
  Cnt:=XMLConfig.GetValue(Path+'Count',0);
  for i:=0 to Cnt-1 do begin
    SubPath:=Path+'Item'+IntToStr(i)+'/';
    CurName:=XMLConfig.GetValue(SubPath+'Name','');
    CurValue:=XMLConfig.GetValue(SubPath+'Value','');
    Tree.Values[CurName]:=CurValue;
  end;
end;

procedure SaveStringToStringTree(XMLConfig: TXMLConfig;
  Tree: TStringToStringTree; const Path: string);
var
  Node: TAvgLvlTreeNode;
  Item: PStringToStringItem;
  i: Integer;
  SubPath: String;
begin
  XMLConfig.SetDeleteValue(Path+'Count',Tree.Tree.Count,0);
  Node:=Tree.Tree.FindLowest;
  i:=0;
  while Node<>nil do begin
    Item:=PStringToStringItem(Node.Data);
    SubPath:=Path+'Item'+IntToStr(i)+'/';
    XMLConfig.SetDeleteValue(SubPath+'Name',Item^.Name,'');
    XMLConfig.SetDeleteValue(SubPath+'Value',Item^.Value,'');
    Node:=Tree.Tree.FindSuccessor(Node);
    inc(i);
  end;
end;

var
  XMLConfig: TXMLConfig;
  LPIFilename: String;
  CustomData: TStringToStringTree;
  Name, Value: string;
begin
  LPIFilename:=ParamStr(1);
  
  // load .lpi file as TXMLConfig
  XMLConfig:=TXMLConfig.Create(nil);
  XMLConfig.Filename:=LPIFilename;
  
  // read custom data
  CustomData:=TStringToStringTree.Create(true);
  LoadStringToStringTree(XMLConfig,CustomData,'ProjectOptions/CustomData/');
  
  // show all custom data
  if CustomData.GetFirst(Name,Value) then begin
    repeat
      writeln(Name,'=',Value);
    until not CustomData.GetNext(Name,Name,Value);
  end;
  
  // change a value
  CustomData.Values['CustomData1']:='LPICustomData example value';
  
  // save custom data
  SaveStringToStringTree(XMLConfig,CustomData,'ProjectOptions/CustomData/');
  
  // save TXMLConfig
  XMLConfig.Flush;

  CustomData.Free;
  XMLConfig.Free;
end.