-
Notifications
You must be signed in to change notification settings - Fork 9
/
IniConfig.m
1564 lines (1255 loc) · 57.7 KB
/
IniConfig.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% Copyright (c) 2009-2010, Evgeny Prilepin aka Iroln
% All rights reserved.
%
%From MathWorks File Exchange
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
% * Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
% * Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in
% the documentation and/or other materials provided with the distribution
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.
classdef IniConfig < handle
%IniConfig - The class for working with configurations of settings and INI-files.
% This class allows you to create configurations of settings, and to manage them.
% The structure of the storage settings is similar to the structure of
% the storage the settings in the INI-file format.
% The class allows you to import settings from the INI-file and to export
% the settings in INI-file.
% Can be used for reading/writing data in the INI-file and managing
% settings of application.
%
%
% Using:
% ini = IniConfig()
%
% Public Properties:
% Enter this command to get the properties:
% >> properties IniConfig
%
% Public Methods:
% Enter this command to get the methods:
% >> methods IniConfig
%
% Enter this command to get more info of method:
% >> help IniConfig/methodname
%
%
% Config Syntax:
%
% ; some comments
%
% [Section1] ; allowed the comment to section
% ; comment on the section
% key1 = value_1 ; allowed a comment to an individual parameter
% key2 = value_2
%
% [Section2]
% key1 = value_1.1, value_1.2 ; array data
% key2 = value_2
% ...
%
% Note:
% * There may be spaces in the names of sections and keys
% * Keys should not be repeated in the section (will read the last)
% * Sections should not be repeated in the config (will read the last)
%
% Supported data types:
% * numeric scalars and vectors
% * strings
%
%
% Example:
% ini = IniConfig();
% ini.ReadFile('example.ini')
% ini.ToString()
%
% Example:
% ini = IniConfig();
% ini.ReadFile('example.ini')
% sections = ini.GetSections()
% [keys, count_keys] = ini.GetKeys(sections{1})
% values = ini.GetValues(sections{1}, keys)
% new_values(:) = {rand()};
% ini.SetValues(sections{1}, keys, new_values, '%.3f')
% ini.WriteFile('example1.ini')
%
% Example:
% ini = IniConfig();
% ini.AddSections({'Some Section 1', 'Some Section 2'})
% ini.AddKeys('Some Section 1', {'some_key1', 'some_key2'}, {'hello!', [10, 20]})
% ini.AddKeys('Some Section 2', 'some_key3', true)
% ini.AddKeys('Some Section 2', 'some_key1')
% ini.WriteFile('example2.ini')
%
% Example:
% ini = IniConfig();
% ini.AddSections('Some Section 1')
% ini.AddKeys('Some Section 1', 'some_key1', 'hello!')
% ini.AddKeys('Some Section 1', {'some_key2', 'some_key3'}, {[10, 20], [false, true]})
% ini.WriteFile('example31.ini')
% ini.RemoveKeys('Some Section 1', {'some_key1', 'some_key3'})
% ini.RenameKeys('Some Section 1', 'some_key2', 'renamed_some_key2')
% ini.RenameSections('Some Section 1', 'Renamed Section 1')
% ini.WriteFile('example32.ini')
%
%
% See also:
% textscan, containers.Map
%
%
% Author: Iroln <[email protected]>
% Version: 1.2
% First release: 25.07.09
% Last revision: 21.03.10
% Copyright: (c) 2009-2010 Evgeny Prilepin aka Iroln
%
% Bug reports, questions, etc. can be sent to the e-mail given above.
%
properties (GetAccess = 'public', SetAccess = 'private')
comment_style = ';' % style of comments
count_sections = 0 % number of sections
count_all_keys = 0 % number of all keys
end
properties (GetAccess = 'private', SetAccess = 'private')
config_data_array = {}
indicies_of_sections
indicies_of_empty_strings
count_strings = 0
count_empty_strings = 0
is_created_configuration = false
end
%======================================================================
methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Public Methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%------------------------------------------------------------------
function obj = IniConfig()
%IniConfig - constructor
% To Create new object with empty default configuration.
%
% Using:
% obj = IniConfig()
%
% Input:
% none
%
% Output:
% obj - an instance of class IniConfig
% -------------------------------------------------------------
obj.CreateIni();
end
%------------------------------------------------------------------
function CreateIni(obj)
%CreateIni - create new empty configuration
%
% Using:
% CreateIni()
%
% Input:
% none
%
% Output:
% none
% -------------------------------------------------------------
obj.config_data_array = cell(2,3);
obj.config_data_array(:,:) = {''};
obj.updateCountStrings();
obj.updateSectionsInfo();
obj.updateEmptyStringsInfo();
obj.is_created_configuration = true;
end
%------------------------------------------------------------------
function status = ReadFile(obj, file_name, comment_style)
%ReadFile - to read in the object the config data from a INI file
%
% Using:
% status = ReadFile(file_name, comment_style)
%
% Input:
% file_name - INI file name
% comment_style - style of comments in INI file
%
% Output:
% status - 1 (true) - success, 0 (false) - failed
% -------------------------------------------------------------
narginchk(2, 3);
CheckIsString(file_name);
if (nargin == 3)
obj.comment_style = ValidateCommentStyle(comment_style);
end
% Get data from file
[file_data, status] = GetDataFromFile(file_name);
if (status)
obj.count_strings = size(file_data, 1);
obj.config_data_array = ...
ParseConfigData(file_data, obj.comment_style);
obj.updateSectionsInfo();
obj.updateEmptyStringsInfo();
obj.updateCountKeysInfo();
obj.is_created_configuration = true;
end
end
%------------------------------------------------------------------
function status = IsSections(obj, section_names)
%IsSections - determine whether there is a sections
%
% Using:
% status = IsSections(section_names)
%
% Input:
% section_names - name of section(s)
%
% Output:
% status - 1 (true) - yes, 0 (false) - no
% -------------------------------------------------------------
error(nargchk(2, 2, nargin));
section_names = DataToCell(section_names);
section_names = cellfun(@(x) obj.validateSectionName(x), ...
section_names, 'UniformOutput', false);
status = cellfun(@(x) obj.isSection(x), ...
section_names, 'UniformOutput', true);
end
%------------------------------------------------------------------
function [section_names, count_sect] = GetSections(obj)
%GetSections - get names of all sections
%
% Using:
% section_names = GetSections()
% [names_sect, count_sect] = GetSections()
%
% Input:
% none
%
% Output:
% section_names - cell array with the names of sections
% count_sect - number of sections in configuration
% -------------------------------------------------------------
narginchk(1, 1);
section_names = obj.config_data_array(obj.indicies_of_sections, 1);
% section_names = strrep(section_names, '[', '');
% section_names = strrep(section_names, ']', '');
count_sect = obj.count_sections;
end
%------------------------------------------------------------------
function status = AddSections(obj, section_names)
%AddSections - add sections to end configuration
%
% Using:
% status = AddSections(section_names)
%
% Input:
% section_names - name of section
%
% Output:
% status - 1 (true) - success, 0 (false) - failed
% -------------------------------------------------------------
narginchk(2, 2);
section_names = DataToCell(section_names);
section_names = cellfun(@(x) obj.validateSectionName(x), ...
section_names, 'UniformOutput', false);
status = cellfun(@(x) obj.addSection(x), ...
section_names, 'UniformOutput', true);
end
%------------------------------------------------------------------
function status = InsertSections(obj, positions, section_names)
%InsertSections - insert sections to given positions
%
% Using:
% status = InsertSections(positions, section_names)
%
% Input
% positions - positions of sections
% section_names - names of sections
%
% Output:
% status - 1 (true) - success, 0 (false) - failed
% -------------------------------------------------------------
narginchk(3, 3);
positions = DataToCell(positions);
section_names = DataToCell(section_names);
CheckEqualNumberElems(positions, section_names);
section_names = cellfun(@(x) obj.validateSectionName(x), ...
section_names, 'UniformOutput', false);
status = cellfun(@(x, y) obj.insertSection(x, y), ...
positions, section_names, 'UniformOutput', true);
end
%------------------------------------------------------------------
function status = RemoveSections(obj, section_names)
%RemoveSections - remove given section
%
% Using:
% status = RemoveSections(section_names)
%
% Input:
% section_names - names of sections
%
% Output:
% status - 1 (true) - success, 0 (false) - failed
% -------------------------------------------------------------
narginchk(2, 2);
section_names = DataToCell(section_names);
status = cellfun(@(x) obj.removeSection(x), ...
section_names, 'UniformOutput', true);
end
%------------------------------------------------------------------
function status = RenameSections(obj, old_section_names, new_section_names)
%RenameSections - rename given sections
%
% Using:
% status = RenameSections(old_section_names, new_section_names)
%
% Input:
% old_section_names - old names of sections
% new_section_names - new names of sections
%
% Output:
% status - 1 (true) - success, 0 (false) - failed
% -------------------------------------------------------------
narginchk(3, 3);
old_section_names = DataToCell(old_section_names);
new_section_names = DataToCell(new_section_names);
CheckEqualNumberElems(old_section_names, new_section_names);
status = cellfun(@(x, y) obj.renameSection(x, y), ...
old_section_names, new_section_names, 'UniformOutput', true);
end
%------------------------------------------------------------------
function status = IsKeys(obj, section_name, key_names)
%IsKeys - determine whether there is a keys in a given section
%
% Using:
% status = IsKeys(section_name, key_names)
%
% Input:
% key_names - name of keys
%
% Output:
% status - 1 (true) - yes, 0 (false) - no
% -------------------------------------------------------------
narginchk(3, 3);
key_names = DataToCell(key_names);
section_name = obj.validateSectionName(section_name);
section_names = PadDataToCell(section_name, numel(key_names));
status = cellfun(@(x, y) obj.isKey(x, y), ...
section_names, key_names, 'UniformOutput', 1);
end
%------------------------------------------------------------------
function [key_names, count_keys] = GetKeys(obj, section_name)
%GetKeys - get names of all keys from given section
%
% Using:
% key_names = GetKeys(section_name)
% [key_names, count_keys] = GetKeys(section_name)
%
% Input:
% section_name - name of section
%
% Output:
% key_names - cell array with the names of keys
% count_keys - number of keys in given section
% -------------------------------------------------------------
narginchk(2, 2);
section_name = obj.validateSectionName(section_name);
[key_names, count_keys] = obj.getKeys(section_name);
end
%------------------------------------------------------------------
function [status, tf_set_values] = ...
AddKeys(obj, section_name, key_names, key_values, value_formats)
%AddKeys - add keys in a end given section
%
% Using:
% status = AddKeys(section_name, key_names)
% status = AddKeys(section_name, key_names, key_values)
% status = AddKeys(section_name, key_names, key_values, value_formats)
% [status, tf_set_values] = AddKeys(...)
%
% Input:
% section_name -- name of section
% key_names -- names of keys
% key_values -- values of keys (optional)
% value_formats --
%
% Output:
% status -- 1 (true): Success, status - 0 (false): Failed
% tf_set_values -- 1 (true): Success, status - 0 (false): Failed
% -------------------------------------------------------------
narginchk(3, 5);
key_names = DataToCell(key_names);
num_of_keys = numel(key_names);
if (nargin < 5)
value_formats = PadDataToCell('', num_of_keys);
end
if (nargin < 4)
key_values = PadDataToCell('', num_of_keys);
end
key_values = DataToCell(key_values);
value_formats = DataToCell(value_formats);
CheckEqualNumberElems(key_names, key_values);
CheckEqualNumberElems(key_values, value_formats);
key_values = ValidateValues(key_values);
section_name = obj.validateSectionName(section_name);
section_names = PadDataToCell(section_name, num_of_keys);
[status, tf_set_values] = cellfun(@(a, b, c, d) obj.addKey(a, b, c, d), ...
section_names, key_names, key_values, value_formats, 'UniformOutput', 1);
end
%------------------------------------------------------------------
function [status, tf_set_values] = InsertKeys(obj, ...
section_name, key_positions, key_names, key_values, value_formats)
%InsertKeys - insert keys into the specified positions in a given section
%
% Using:
% status = InsertKeys(section_name, key_positions, key_names)
% status = InsertKeys(section_name, key_positions, key_names, key_values)
% status = InsertKeys(section_name, key_positions, key_names, key_values, value_formats)
% [status, tf_set_values] = InsertKeys(...)
%
% Input:
% section_name -- name of section
% key_positions -- positions of keys in section
% key_names -- names of keys
% key_values -- values of keys (optional)
% value_formats --
%
% Output:
% status - 1 (true): Success, status - 0 (false): Failed
% tf_set_values - 1 (true): Success, status - 0 (false): Failed
% -------------------------------------------------------------
narginchk(4, 6);
key_positions = DataToCell(key_positions);
key_names = DataToCell(key_names);
num_of_keys = numel(key_names);
CheckEqualNumberElems(key_positions, key_names);
if (nargin < 6)
value_formats = PadDataToCell('', num_of_keys);
end
if (nargin < 5)
key_values = PadDataToCell('', num_of_keys);
end
key_values = DataToCell(key_values);
value_formats = DataToCell(value_formats);
CheckEqualNumberElems(key_names, key_values);
CheckEqualNumberElems(key_values, value_formats);
key_values = ValidateValues(key_values);
section_name = obj.validateSectionName(section_name);
section_names = PadDataToCell(section_name, num_of_keys);
[status, tf_set_values] = ...
cellfun(@(a, b, c, d, e) obj.insertKey(a, b, c, d, e), ...
section_names, key_positions, key_names, ...
key_values, value_formats, 'UniformOutput', true);
end
%------------------------------------------------------------------
function status = RemoveKeys(obj, section_name, key_names)
%RemoveKeys - remove the keys from a given section
%
% Using:
% status = RemoveKeys(section_name, key_names)
%
% Input:
% section_name - name of section
% key_names - names of keys
%
% Output:
% status - 1 (true) - success, 0 (false) - failed
% -------------------------------------------------------------
narginchk(3, 3);
key_names = DataToCell(key_names);
section_name = obj.validateSectionName(section_name);
section_names = PadDataToCell(section_name, numel(key_names));
status = cellfun(@(a, b) obj.removeKey(a, b), ...
section_names, key_names, 'UniformOutput', true);
end
%------------------------------------------------------------------
function status = RenameKeys(obj, section_name, old_key_names, new_key_names)
%RenameKeys - rename the keys in a given section
%
% Using:
% status = RenameKeys(section_name, old_key_names, new_key_names)
%
% Input:
% section_name - name of section
% old_key_names - old names of keys
% new_key_names - new names of keys
%
% Output:
% status - 1 (true) - success, 0 (false) - failed
% -------------------------------------------------------------
narginchk(4, 4);
old_key_names = DataToCell(old_key_names);
new_key_names = DataToCell(new_key_names);
CheckEqualNumberElems(old_key_names, new_key_names);
section_name = obj.validateSectionName(section_name);
section_names = PadDataToCell(section_name, numel(old_key_names));
status = cellfun(@(a, b, c) obj.renameKey(a, b, c), ...
section_names, old_key_names, ...
new_key_names, 'UniformOutput', true);
end
%------------------------------------------------------------------
function [values, status] = GetValues(obj, section_name, key_names, default_values)
%GetValues - get values of keys from given section
%
% Using:
% values = GetValues(section_name, key_names)
% values = GetValues(section_name, key_names, default_values)
%
% Input:
% section_name -- name of given section
% key_names -- names of given keys
% default_values -- values of keys that are returned by default
%
% Output:
% values -- cell array with the values of keys
% status -- 1 (true) - success, 0 (false) - failed
% -------------------------------------------------------------
narginchk(2, 4);
% if (nargin < 2)
% % get all values
% sections = obj.GetSections();
%
% values = {};
% status = [];
% for i = 1:numel(sections)
% [vals, tf] = obj.GetValues(sections{i});
% values = cat(1, values, vals);
% status = cat(1, status, tf);
% end
% return;
% end
section_name = obj.validateSectionName(section_name);
if (nargin < 3)
% get all values from given section
key_names = obj.getKeys(section_name);
if isempty(key_names)
values = {};
status = false;
return;
end
end
if iscell(key_names)
is_cell = true;
else
is_cell = false;
end
key_names = DataToCell(key_names);
if (nargin < 4)
default_values = PadDataToCell([], numel(key_names));
end
default_values = DataToCell(default_values);
default_values = ValidateValues(default_values);
CheckEqualNumberElems(key_names, default_values);
section_names = PadDataToCell(section_name, numel(key_names));
[values, status] = cellfun(@(x, y, z) obj.getValue(x, y, z), ...
section_names, key_names, default_values, 'UniformOutput', false);
if (~is_cell)
values = values{1};
end
status = cell2mat(status);
end
%------------------------------------------------------------------
function status = SetValues(obj, section_name, key_names, key_values, value_formats)
%SetValues - set values for given keys from given section
%
% Using:
% status = SetValues(section_name, key_names, key_values)
% status = SetValues(section_name, key_names, key_values, value_formats)
%
% Input:
% section_name -- name of given section (must be string)
% key_names -- names of given keys (must be cell array of strings or string)
% key_values -- values of keys (must be cell array or one value)
% value_formats --
%
% Output:
% status -- 1 (true) - success, 0 (false) - failed
% -------------------------------------------------------------
narginchk(4, 5);
key_names = DataToCell(key_names);
num_of_keys = numel(key_names);
if (nargin < 5)
value_formats = PadDataToCell('', num_of_keys);
end
key_values = DataToCell(key_values);
value_formats = DataToCell(value_formats);
CheckEqualNumberElems(key_names, key_values);
CheckEqualNumberElems(key_values, value_formats);
key_values = ValidateValues(key_values);
section_name = obj.validateSectionName(section_name);
section_names = PadDataToCell(section_name, num_of_keys);
status = cellfun(@(a, b, c, d) obj.setValue(a, b, c, d), ...
section_names, key_names, key_values, value_formats, 'UniformOutput', true);
end
%------------------------------------------------------------------
function varargout = ToString(obj, section_name)
%ToString - export configuration to string or display
%
% Using:
% ToString()
% ToString(section_name)
% str = ToString(...)
%
% Input:
% section_name - name of sections for export (optional)
%
% Output:
% str - string with full or section configuration (optional)
% -------------------------------------------------------------
%FIXME: âîçìîæíî, íóæíî ðàçáèòü ýòîò ìåòîä íà íåñêîëüêî, îí ñëèøêîì äëèííûé
narginchk(1, 2);
if (nargin < 2)
is_full_export = true;
else
section_name = obj.validateSectionName(section_name);
is_full_export = false;
end
if is_full_export
count_str = obj.count_strings;
indicies = 1:count_str;
else
first_index = getSectionIndex(obj, section_name);
key_indicies = obj.getKeysIndexes(section_name);
if isempty(key_indicies)
last_index = first_index;
else
last_index = key_indicies(end);
end
indicies = first_index:last_index;
end
indicies_of_sect = obj.indicies_of_sections;
config_data = obj.config_data_array;
str = '';
conf_str = sprintf('\n');
for k = indicies
if isempty(config_data{k,1})
if isempty(config_data{k,3})
str = sprintf('\n');
else
comment_str = config_data{k,3};
str = sprintf('%s\n', comment_str);
end
elseif ~isempty(indicies_of_sect(indicies_of_sect == k))
if is_full_export
if isempty(config_data{k,3})
section_str = config_data{k,1};
str = sprintf('%s\n', section_str);
else
section_str = config_data{k,1};
comment_str = config_data{k,3};
str = sprintf('%s %s\n', ...
section_str, comment_str);
end
end
elseif ~isempty(config_data{k,1}) && ...
isempty(indicies_of_sect(indicies_of_sect == k))
if isempty(config_data{k,3})
key_str = config_data{k,1};
val_str = config_data{k,2};
str = sprintf('%s=%s\n', key_str, val_str);
else
key_str = config_data{k,1};
val_str = config_data{k,2};
comment_str = config_data{k,3};
str = sprintf('%s=%s %s\n', ...
key_str, val_str, comment_str);
end
end
conf_str = sprintf('%s%s', conf_str, str);
end
if (nargout == 0)
fprintf(1, '%s\n', conf_str);
elseif (nargout == 1)
varargout{1} = conf_str(2:end);
else
error('Too many output arguments.')
end
end
%------------------------------------------------------------------
function status = WriteFile(obj, file_name)
%WriteFile - write to the configuration INI file on disk
%
% Using:
% status = WriteFile(file_name)
%
% Input:
% file_name - name of output INI file
%
% Output:
% status - 1 (true) - success, 0 (false) - failed
% -------------------------------------------------------------
error(nargchk(2, 2, nargin));
CheckIsString(file_name);
fid = fopen(file_name, 'w');
if (fid ~= -1)
str = obj.ToString();
fprintf(fid, '%s', str);
fclose(fid);
status = true;
else
status = false;
return;
end
end
end % public methods
%----------------------------------------------------------------------
%======================================================================
methods (Access = 'private')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Private Methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%------------------------------------------------------------------
function num = nameToNumSection(obj, section_name)
%nameToNumSection - get number of section
section_names = obj.GetSections();
int_ind = find(strcmp(section_names, section_name));
if ~isempty(int_ind)
% If the section is not unique, then choose the latest
num = int_ind(end);
else
num = [];
end
end
%------------------------------------------------------------------
function sect_index = getSectionIndex(obj, section_name)
%getSectionIndex - get index of section in config data
num = obj.nameToNumSection(section_name);
sect_index = obj.indicies_of_sections(num);
end
%------------------------------------------------------------------
function [key_indicies, count_keys] = getKeysIndexes(obj, section_name)
%getKeysIndexes - get keys indices from given section
sect_num = obj.nameToNumSection(section_name);
if isempty(sect_num)
key_indicies = [];
count_keys = 0;
return;
elseif (sect_num == obj.count_sections)
key_indicies = ...
obj.indicies_of_sections(sect_num)+1:obj.count_strings;
else
key_indicies = ...
obj.indicies_of_sections(sect_num)+1:obj.indicies_of_sections(sect_num+1)-1;
end
indicies_of_empty = obj.indicies_of_empty_strings;
empty_indicies = ismember(key_indicies, indicies_of_empty);
% empty_indicies = cellfun('isempty', ...
% obj.config_data_array(key_indicies, 1));
key_indicies(empty_indicies) = [];
key_indicies = key_indicies(:);
count_keys = length(key_indicies);
end
%------------------------------------------------------------------
function ind = getKeyIndex(obj, section_name, key_name)
%getKeyIndex - get key index
key_names = obj.getKeys(section_name);
key_indicies = obj.getKeysIndexes(section_name);
int_ind = strcmp(key_names, key_name);
ind = key_indicies(int_ind);
end
%------------------------------------------------------------------
function updateSectionsInfo(obj)
%updateSectionsInfo - update info about sections
keys_data = obj.config_data_array(:,1);
sect_indicies_cell = regexp(keys_data, '^\[.*\]$');
obj.indicies_of_sections = ...
find(~cellfun('isempty', sect_indicies_cell));
obj.count_sections = length(obj.indicies_of_sections);
end
%------------------------------------------------------------------
function updateCountKeysInfo(obj)
%UpdateCountKeys - update full number of keys
obj.count_all_keys = ...
obj.count_strings - obj.count_sections - obj.count_empty_strings;
end
%------------------------------------------------------------------
function updateEmptyStringsInfo(obj)
%updateEmptyStringsInfo - update info about empty strings
keys_data = obj.config_data_array(:,1);
indicies_of_empty_cell = strcmp('', keys_data);
obj.indicies_of_empty_strings = find(indicies_of_empty_cell);
obj.count_empty_strings = length(obj.indicies_of_empty_strings);
end
%------------------------------------------------------------------
function updateCountStrings(obj)
%updateCountStrings - update full number of sections
obj.count_strings = size(obj.config_data_array, 1);
end
%------------------------------------------------------------------
function status = isUniqueKeyName(obj, section_name, key_name)
%isUniqueKeyName - check whether the name of the key unique
keys = obj.getKeys(section_name);
status = ~any(strcmp(key_name, keys));
end
%------------------------------------------------------------------
function status = isUniqueSectionName(obj, section_name)
%isUniqueKeyName - check whether the name of the section a unique
sections = obj.GetSections();
status = ~any(strcmp(section_name, sections));
end
%------------------------------------------------------------------
function status = isSection(obj, section_name)
%isSection - determine whether there is a section
% section_names = obj.GetSections();
data = obj.config_data_array(:, 1);
int_ind = find(strcmp(data, section_name), 1);
if ~isempty(int_ind)
status = true;
else
status = false;
end
end
%------------------------------------------------------------------
function section_name = validateSectionName(obj, section_name)
%validateSectionName - check the name of the section
CheckIsString(section_name);
section_name = section_name(:)';
section_name = strtrim(section_name);
if ~isempty(section_name)
sect_indicies_cell = ...
regexp(section_name, '^\[.*\]$', 'once');
indicies_cell_comment = ...
regexp(section_name, obj.comment_style, 'once');
if ~isempty(indicies_cell_comment)