-
Notifications
You must be signed in to change notification settings - Fork 2
/
IniFile.m
278 lines (262 loc) · 10.8 KB
/
IniFile.m
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
classdef IniFile < dynamicprops
properties
isValid
end
properties (Access = private)
FileName
isModified
Header
Variables
Values
Type
Type0
Comment
end
methods
function obj = IniFile(varargin)
obj.isValid = false;
if ~nargin
[fn p] = uigetfile('*','Please select a configuration file', [pwd '/']);
obj.FileName = fullfile(p,fn);
else
if ~isempty(fileparts(varargin{1})) && exist(varargin{1},'file')
obj.FileName = varargin{1};
else
if ~isempty(fileparts(varargin{1}))
fname = strrep(varargin{1},fileparts(varargin{1}),'');
fname = fname(2:end);
else
fname = varargin{1};
end
if exist(fullfile(pwd,fname),'file')
obj.FileName = fullfile(pwd,fname);
elseif ~isempty(which(fname))
obj.FileName = which(fname);
else
disp('ERROR! The specified configuration file not found. Aborting ...');
return;
end
end
end
header = 0;
ind = cell_index(varargin,'header');
if ind, header = varargin{ind+1}; end
[fid,message] = fopen(obj.FileName);
if ~isempty(message)
disp('ERROR! The specified configuration file could not be opened. Aborting ...');
return;
end
Lines = {};
while ~feof(fid)
Lines{end+1} = fgetl(fid);
end
fclose(fid);
obj.Header = {Lines{1:header}};
ns = 0; obj.Variables = {}; obj.Type = {}; obj.Comment = {}; C = '';
for i = header+1:numel(Lines)
if ~isempty(strfind(Lines{i},'%'))
if Lines{i}(1) == '%', C = Lines{i}; end
end
sep1 = strfind(Lines{i},'[');
sep2 = strfind(Lines{i},']');
if isempty(sep1)
continue;
end
if (sep1 == 1) % subset
ns = ns + 1;
obj.Variables{ns,1} = Lines{i}(sep1+1:sep2-1);
end
nv = 0;
for j = 1:size(obj.Variables,2)
nv = nv + ~isempty(obj.Variables{ns,j});
end
nv = nv + 1;
obj.Variables{ns,nv} = Lines{i}(1:sep1-1);
obj.Type{ns,nv} = Lines{i}(sep1+1:sep2-1);
switch obj.Type{ns,nv}
case 'n' % number
obj.Values{ns,nv} = str2double(Lines{i}(sep2+2:end));
case 'd' % date
obj.Values{ns,nv} = datestr(datenum(Lines{i}(sep2+2:end),31));
otherwise %'s' % string
obj.Values{ns,nv} = Lines{i}(sep2+2:end);
end
if ~isempty(C)
obj.Comment{ns,nv} = C;
C = '';
end
end
for i = 1:size(obj.Variables,1)
varname = lower(obj.Variables{i,1});
val = struct;
for j = 2:size(obj.Variables,2)
if ~isempty(obj.Variables{i,j})
val.(obj.Variables{i,j}) = obj.Values{i,j};
end
end
obj.addprop(varname);
obj.(varname) = val;
end
obj.isModified = false;
obj.isValid = true;
end
function e = Close(obj,fn)
obj = obj.Check(true);
istow = false;
if (nargin > 1) && ~strcmp(obj.FileName,fn)
obj.FileName = fn;
istow = true;
end
if obj.isModified || istow
fprintf('Modification is to be writen...');
Lines = obj.Header;
for i = 1:size(obj.Variables,1)
Lines{end+1} = sprintf('[%s]',upper(obj.Variables{i,1}));
for j = 2:size(obj.Variables,2)
if ~isempty(obj.Variables{i,j})
if (size(obj.Comment,1) >= i) && (size(obj.Comment,2) >= j) && ~isempty(obj.Comment{i,j})
Lines{end+1} = sprintf('%s',obj.Comment{i,j});
end
Lines{end+1} = sprintf('%s[%s]=%s',obj.Variables{i,j},obj.Type{i,j},num2str(obj.Values{i,j}));
end
end
Lines{end+1} = '';
end
[fid,message] = fopen(obj.FileName, 'w');
if ~isempty(message)
error('The specified configuration file could not be opened. Aborting ...');
end
for i = 1:numel(Lines)-2
fprintf(fid, '%s\n', Lines{i});
end
fprintf(fid, '%s', Lines{end-1});
fclose(fid);
fprintf('Done\n');
e = 1;
obj.isModified = false;
end
end
function str = getComment(obj,vStruct,vName)
ns = cell_index({obj.Variables{:,1}},upper(vStruct));
if ns % existing Struct
nv = cell_index({obj.Variables{ns,:}},vName);
if nv
str = obj.Comment{ns,nv};
if ~isempty(str), str = strrep(str,'% ',''); end
end
end
if ~ns || ~nv
fprintf('ERROR! No variable %s within struct %s exists in configuration %s!\n',...
vName, vStruct, obj.FileName);
end
end
function obj = AddVariable(obj,vStruct,nv,vName,vType,vVal,vComm)
out = 0;
ns = cell_index({obj.Variables{:,1}},upper(vStruct));
if ns % existing Struct
if ~nv
for j = 1:size(obj.Variables,2)
nv = nv + ~isempty(obj.Variables{ns,j});
end
end
nv = nv + 1;
obj.Variables = cell_insert(obj.Variables,vName,[ns nv]);
obj.Type = cell_insert(obj.Type,vType,[ns nv]);
obj.Values = cell_insert(obj.Values,vVal,[ns nv]);
if nargin < 7 || isempty(vComm)
vComm = '';
else
vComm = sprintf('%% %s',vComm);
end
obj.Comment = cell_insert(obj.Comment,vComm,[ns nv]);
obj.(vStruct).(vName) = vVal;
else
fprintf('ERROR! No struct %s exists in configuration %s!\n',vStruct, obj.FileName);
out = 1;
end
obj.isModified = ~logical(out);
end
function obj = RemoveVariable(obj,vStruct,vName)
out = 0;
ns = cell_index({obj.Variables{:,1}},upper(vStruct));
if ns % existing Struct
nv = cell_index({obj.Variables{ns,:}},vName);
if nv
obj.Variables = cell_remove(obj.Variables,[ns nv]);
obj.Type = cell_remove(obj.Type,[ns nv]);
obj.Values = cell_remove(obj.Values,[ns nv]);
obj.Comment = cell_remove(obj.Comment,[ns nv]);
obj.(vStruct) = rmfield(obj.(vStruct),vName);
end
end
if ~ns || ~nv
fprintf('ERROR! No variable %s within struct %s exists in configuration %s!\n',...
vName, vStruct, obj.FileName);
out = 1;
end
obj.isModified = ~logical(out);
end
function obj = ExcludeVariable(obj,vStruct,vName)
ns = cell_index({obj.Variables{:,1}},upper(vStruct));
if ns % existing Struct
nv = cell_index({obj.Variables{ns,:}},vName);
if nv
obj.Type0{ns, nv} = obj.Type{ns, nv};
obj.Type{ns, nv} = '0';
end
end
if ~ns || ~nv
fprintf('ERROR! No variable %s within struct %s exists in configuration %s!',...
vName, vStruct, obj.FileName);
end
end
function obj = IncludeVariable(obj,vStruct,vName)
ns = cell_index({obj.Variables{:,1}},upper(vStruct));
if ns % existing Struct
nv = cell_index({obj.Variables{ns,:}},vName);
if nv, obj.Type{ns, nv} = obj.Type0{ns, nv}; end
end
if ~ns || ~nv
fprintf('ERROR! No variable %s within struct %s exists in configuration %s!',...
vName, vStruct, obj.FileName);
end
end
function out = getFields(obj)
snames = fieldnames(obj);
for i = 1:numel(snames)
isf(i) = isstruct(obj.(snames{i}));
end
out = snames(isf);
end
end
methods (Access = private)
function obj = Check(obj,corr)
snames = obj.getFields;
for ns = 1:numel(snames)
vnames = fieldnames(obj.(snames{ns}));
for nv = 1:numel(vnames)
nc = cell_index({obj.Variables{:,1}},upper(snames{ns}));
old = obj.Values{nc,nv+1};
new = obj.(snames{ns}).(obj.Variables{nc,nv+1});
switch obj.Type{nc,nv+1}
case 'n' % number
if old ~= new
obj.isModified = true;
end
case 'd' % date
if datenum(old) ~= datenum(new)
obj.isModified = true;
end
case 's' % string
if ~strcmp(old,new)
obj.isModified = true;
end
otherwise % '0' no check
continue;
end
if corr, obj.Values{nc,nv+1} = new; end
end
end
end
end
end