forked from attofemto/SparrowCam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathini2struct.m
123 lines (122 loc) · 4.12 KB
/
ini2struct.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
function Result = ini2struct(FileName)
%==========================================================================
% Author: Andriy Nych ( [email protected] )
% Version: 733341.4155741782200
%==========================================================================
%
% INI = ini2struct(FileName)
%
% This function parses INI file FileName and returns it as a structure with
% section names and keys as fields.
%
% Sections from INI file are returned as fields of INI structure.
% Each fiels (section of INI file) in turn is structure.
% It's fields are variables from the corresponding section of the INI file.
%
% If INI file contains "oprhan" variables at the beginning, they will be
% added as fields to INI structure.
%
% Lines starting with ';' and '#' are ignored (comments).
%
% See example below for more information.
%
% Usually, INI files allow to put spaces and numbers in section names
% without restrictions as long as section name is between '[' and ']'.
% It makes people crazy to convert them to valid Matlab variables.
% For this purpose Matlab provides GENVARNAME function, which does
% "Construct a valid MATLAB variable name from a given candidate".
% See 'help genvarname' for more information.
%
% The INI2STRUCT function uses the GENVARNAME to convert strange INI
% file string into valid Matlab field names.
%
% [ test.ini ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%
% SectionlessVar1=Oops
% SectionlessVar2=I did it again ;o)
% [Application]
% Title = Cool program
% LastDir = c:\Far\Far\Away
% NumberOFSections = 2
% [1st section]
% param1 = val1
% Param 2 = Val 2
% [Section #2]
% param1 = val1
% Param 2 = Val 2
%
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%
% The function converts this INI file it to the following structure:
%
% [ MatLab session (R2006b) ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% >> INI = ini2struct('test.ini');
% >> disp(INI)
% sectionlessvar1: 'Oops'
% sectionlessvar2: 'I did it again ;o)'
% application: [1x1 struct]
% x1stSection: [1x1 struct]
% section0x232: [1x1 struct]
%
% >> disp(INI.application)
% title: 'Cool program'
% lastdir: 'c:\Far\Far\Away'
% numberofsections: '2'
%
% >> disp(INI.x1stSection)
% param1: 'val1'
% param2: 'Val 2'
%
% >> disp(INI.section0x232)
% param1: 'val1'
% param2: 'Val 2'
%
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%
% NOTE.
% WhatToDoWithMyVeryCoolSectionAndVariableNamesInIniFileMyVeryCoolProgramWrites?
% GENVARNAME also does the following:
% "Any string that exceeds NAMELENGTHMAX is truncated". (doc genvarname)
% Period.
%
% =========================================================================
Result = []; % we have to return something
CurrMainField = ''; % it will be used later
f = fopen(FileName,'r'); % open file
while ~feof(f) % and read until it ends
s = strtrim(fgetl(f)); % Remove any leading/trailing spaces
if isempty(s)
continue;
end;
if (s(1)==';') % ';' start comment lines
continue;
end;
if (s(1)=='#') % '#' start comment lines
continue;
end;
if ( s(1)=='[' ) && (s(end)==']' )
% We found section
CurrMainField = genvarname(lower(s(2:end-1)));
Result.(CurrMainField) = []; % Create field in Result
else
% ??? This is not a section start
[par,val] = strtok(s, '=');
val = CleanValue(val);
if ~isempty(CurrMainField)
% But we found section before and have to fill it
Result.(CurrMainField).(lower(genvarname(par))) = val;
else
% No sections found before. Orphan value
Result.(lower(genvarname(par))) = val;
end
end
end
fclose(f);
return;
function res = CleanValue(s)
res = strtrim(s);
if strcmpi(res(1),'=')
res(1)=[];
end
res = strtrim(res);
return;