source: cprs/branches/tmg-cprs/CPRS-Chart/uTMGOptions.pas@ 745

Last change on this file since 745 was 735, checked in by Kevin Toppenberg, 15 years ago

Template formulas will calculate even if responses have characters, bug fixes

File size: 6.5 KB
Line 
1unit uTMGOptions;
2//kt Added entire unit. 2/10/10
3
4interface
5
6uses
7 IniFiles, SysUtils, Forms, uCore, ORFn, ORNet, Trpcb;
8
9var
10 USE_SERVER_INI : boolean;
11 Initialized : boolean;
12
13 Procedure WriteBool(const Key: String; Value: Boolean; AsDefault : boolean = false);
14 Procedure WriteInteger(const Key: String; Value: Integer; AsDefault : boolean = false);
15 Procedure WriteString(const Key: String; Value: String; AsDefault : boolean = false);
16 Function ReadBool(const Key: String; Default: Boolean): Boolean;
17 Function ReadInteger(const Key: String; Default: Integer): Integer;
18 Function ReadString(const Key: String; Default: String): String;
19
20implementation
21
22uses
23 Classes;
24
25const
26 DEFAULT_SECTION = 'DEFAULT';
27
28var
29 StrList : TStringList;
30 OptionsIniFile : TIniFile; //kt 8/09
31
32 procedure TMGOptionsInitialize;
33 begin
34 OptionsIniFile := TIniFile.Create(ChangeFileExt(Application.ExeName, '.INI')); //kt
35 StrList := TStringList.Create;
36
37 //if RPC exists, use in lieu of INI file
38 RPCBrokerV.remoteprocedure := 'XWB IS RPC AVAILABLE';
39 RPCBrokerV.Param[0].Value := 'TMG INIFILE GET';
40 RPCBrokerV.Param[0].ptype := literal;
41 RPCBrokerV.Param[1].Value := 'R';
42 RPCBrokerV.Param[1].ptype := literal;
43 //RPCResult := RPCBrokerV.StrCall; {returns 1 if available, 0 if not available}
44 USE_SERVER_INI := StrToBool(RPCBrokerV.StrCall);
45 Initialized := true;
46 end;
47
48 procedure WriteRPCStr(const Section, Key: String; Value: String);
49 begin
50 RPCBrokerV.remoteprocedure := 'TMG INIFILE SET';
51 RPCBrokerV.Param[0].Value := Section;
52 RPCBrokerV.Param[0].ptype := literal;
53 RPCBrokerV.Param[1].Value := Key;
54 RPCBrokerV.Param[1].ptype := literal;
55 RPCBrokerV.Param[2].Value := Value;
56 RPCBrokerV.Param[2].ptype := literal;
57 RPCBrokerV.Call;
58 end;
59
60 function ReadRPCStr(const Section, Key, Default: String) : string;
61 begin
62 RPCBrokerV.remoteprocedure := 'TMG INIFILE GET';
63 RPCBrokerV.param[0].ptype := literal;
64 RPCBrokerV.param[0].value := Section;
65 RPCBrokerV.Param[1].ptype := literal;
66 RPCBrokerV.param[1].value := Key;
67 RPCBrokerV.Param[2].ptype := literal;
68 RPCBrokerV.param[2].value := Default;
69 Result := Piece(RPCBrokerV.StrCall,'^',2);
70 end;
71
72
73 Procedure WriteBool(const Key: String; Value: Boolean; AsDefault : boolean = false);
74 begin
75 if Initialized = False then TMGOptionsInitialize;
76 if AsDefault then begin
77 if USE_SERVER_INI then begin
78 WriteRPCStr(DEFAULT_SECTION,Key,BoolToStr(Value));
79 end else begin
80 OptionsIniFile.WriteBool(DEFAULT_SECTION,Key,Value);
81 end;
82 end else begin
83 if USE_SERVER_INI then begin
84 WriteRPCStr(User.Name,Key,BoolToStr(Value));
85 end else begin
86 OptionsIniFile.ReadSection(DEFAULT_SECTION,StrList);
87 if (StrList.IndexOf(Key)=-1) or (OptionsIniFile.ReadBool(DEFAULT_SECTION,Key,false)<>Value) then begin
88 OptionsIniFile.WriteBool(User.Name,Key,Value); //Only write user value if differs from default value
89 end;
90 end;
91 end;
92 end;
93
94 Procedure WriteInteger(const Key: String; Value: Integer; AsDefault : boolean = false);
95 begin
96 if Initialized = False then TMGOptionsInitialize;
97 if AsDefault then begin
98 if USE_SERVER_INI then begin
99 WriteRPCStr(DEFAULT_SECTION,Key,IntToStr(Value));
100 end else begin
101 OptionsIniFile.WriteInteger(DEFAULT_SECTION,Key,Value);
102 end;
103 end else begin
104 if USE_SERVER_INI then begin
105 WriteRPCStr(User.Name,Key,IntToStr(Value));
106 end else begin
107 OptionsIniFile.ReadSection(DEFAULT_SECTION,StrList);
108 if (StrList.IndexOf(Key)=-1) or (OptionsIniFile.ReadInteger(DEFAULT_SECTION,Key,-999)<>Value) then begin
109 OptionsIniFile.WriteInteger(User.Name,Key,Value); //Only write user value if differs from default value
110 end;
111 end;
112 end;
113 end;
114
115 Procedure WriteString(const Key: String; Value: String; AsDefault : boolean = false);
116 begin
117 if Initialized = False then TMGOptionsInitialize;
118 if AsDefault then begin
119 if USE_SERVER_INI then begin
120 WriteRPCStr(DEFAULT_SECTION,Key,Value);
121 end else begin
122 OptionsIniFile.WriteString(DEFAULT_SECTION,Key,Value);
123 end;
124 end else begin
125 if USE_SERVER_INI then begin
126 WriteRPCStr(User.Name,Key,Value);
127 end else begin
128 OptionsIniFile.ReadSection(DEFAULT_SECTION,StrList);
129 if (StrList.IndexOf(Key)=-1) or (OptionsIniFile.ReadString(DEFAULT_SECTION,Key,'xxx')<>Value) then begin
130 OptionsIniFile.WriteString(User.Name,Key,Value); //Only write user value if differs from default value
131 end;
132 end;
133 end;
134 end;
135
136 Function ReadBool(const Key: String; Default: Boolean): Boolean;
137 begin
138 if Initialized = False then TMGOptionsInitialize;
139 if USE_SERVER_INI then begin
140 Result := StrToBool(ReadRPCStr(User.Name,Key,BoolToStr(Default)));
141 end else begin
142 OptionsIniFile.ReadSection(User.Name,StrList);
143 if StrList.IndexOf(Key) > -1 then begin
144 Result := OptionsIniFile.ReadBool(User.Name,Key,Default);
145 end else begin
146 Result := OptionsIniFile.ReadBool(DEFAULT_SECTION,Key,Default);
147 end;
148 end;
149 end;
150
151 Function ReadInteger(const Key: String; Default: Integer): Integer;
152 begin
153 if Initialized = False then TMGOptionsInitialize;
154 if USE_SERVER_INI then begin
155 Result := StrToInt(ReadRPCStr(User.Name,Key,IntToStr(Default)));
156 end else begin
157 OptionsIniFile.ReadSection(User.Name,StrList);
158 if StrList.IndexOf(Key) > -1 then begin
159 Result := OptionsIniFile.ReadInteger(User.Name,Key,Default);
160 end else begin
161 Result := OptionsIniFile.ReadInteger(DEFAULT_SECTION,Key,Default);
162 end;
163 end;
164 end;
165
166 Function ReadString(const Key: String; Default: String): String;
167 begin
168 if Initialized = False then TMGOptionsInitialize;
169 if USE_SERVER_INI then begin
170 Result := ReadRPCStr(User.Name,Key,Default);
171 end else begin
172 OptionsIniFile.ReadSection(User.Name,StrList);
173 if StrList.IndexOf(Key) > -1 then begin
174 Result := OptionsIniFile.ReadString(User.Name,Key,Default);
175 end else begin
176 Result := OptionsIniFile.ReadString(DEFAULT_SECTION,Key,Default);
177 end;
178 end;
179 end;
180
181
182initialization
183 Initialized := false;
184
185
186finalization
187 OptionsIniFile.Free; //kt 8/09
188 StrList.Free;
189
190end.
191
Note: See TracBrowser for help on using the repository browser.