source: trunk/src/ini2struct.m @ 789

Last change on this file since 789 was 784, checked in by sommeria, 11 years ago

adpat uvmat to reading data from rdvision

File size: 4.1 KB
Line 
1function Result = ini2struct(FileName)
2%==========================================================================
3%  Author: Andriy Nych ( nych.andriy@gmail.com )
4% Version:        733341.4155741782200
5%==========================================================================
6%
7% INI = ini2struct(FileName)
8%
9% This function parses INI file FileName and returns it as a structure with
10% section names and keys as fields.
11%
12% Sections from INI file are returned as fields of INI structure.
13% Each fiels (section of INI file) in turn is structure.
14% It's fields are variables from the corresponding section of the INI file.
15%
16% If INI file contains "oprhan" variables at the beginning, they will be
17% added as fields to INI structure.
18%
19% Lines starting with ';' and '#' are ignored (comments).
20%
21% See example below for more information.
22%
23% Usually, INI files allow to put spaces and numbers in section names
24% without restrictions as long as section name is between '[' and ']'.
25% It makes people crazy to convert them to valid Matlab variables.
26% For this purpose Matlab provides GENVARNAME function, which does
27%  "Construct a valid MATLAB variable name from a given candidate".
28% See 'help genvarname' for more information.
29%
30% The INI2STRUCT function uses the GENVARNAME to convert strange INI
31% file string into valid Matlab field names.
32%
33% [ test.ini ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34%
35%     SectionlessVar1=Oops
36%     SectionlessVar2=I did it again ;o)
37%     [Application]
38%     Title = Cool program
39%     LastDir = c:\Far\Far\Away
40%     NumberOFSections = 2
41%     [1st section]
42%     param1 = val1
43%     Param 2 = Val 2
44%     [Section #2]
45%     param1 = val1
46%     Param 2 = Val 2
47%
48% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
49%
50% The function converts this INI file it to the following structure:
51%
52% [ MatLab session (R2006b) ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
53%  >> INI = ini2struct('test.ini');
54%  >> disp(INI)
55%         sectionlessvar1: 'Oops'
56%         sectionlessvar2: 'I did it again ;o)'
57%             application: [1x1 struct]
58%             x1stSection: [1x1 struct]
59%            section0x232: [1x1 struct]
60%
61%  >> disp(INI.application)
62%                    title: 'Cool program'
63%                  lastdir: 'c:\Far\Far\Away'
64%         numberofsections: '2'
65%
66%  >> disp(INI.x1stSection)
67%         param1: 'val1'
68%         param2: 'Val 2'
69%
70%  >> disp(INI.section0x232)
71%         param1: 'val1'
72%         param2: 'Val 2'
73%
74% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
75%
76% NOTE.
77% WhatToDoWithMyVeryCoolSectionAndVariableNamesInIniFileMyVeryCoolProgramWrites?
78% GENVARNAME also does the following:
79%   "Any string that exceeds NAMELENGTHMAX is truncated". (doc genvarname)
80% Period.
81%
82% =========================================================================
83Result = [];                            % we have to return something
84CurrMainField = '';                     % it will be used later
85f = fopen(FileName,'r');                % open file
86while ~feof(f)                          % and read until it ends
87    s = strtrim(fgetl(f));              % Remove any leading/trailing spaces
88    if isempty(s)
89        continue;
90    end;
91    if (s(1)==';')                      % ';' start comment lines
92        continue;
93    end;
94    if (s(1)=='#')                      % '#' start comment lines
95        continue;
96    end;
97    if ( s(1)=='[' ) && (s(end)==']' )
98        % We found section
99        CurrMainField = genvarname(lower(s(2:end-1)));
100        Result.(CurrMainField) = [];    % Create field in Result
101    else
102        % ??? This is not a section start
103        [par,val] = strtok(s, '=');
104        val = CleanValue(val);
105        if ~isempty(CurrMainField)
106            % But we found section before and have to fill it
107            Result.(CurrMainField).(lower(genvarname(par))) = val;
108        else
109            % No sections found before. Orphan value
110            Result.(lower(genvarname(par))) = val;
111        end
112    end
113end
114fclose(f);
115return;
116
117function res = CleanValue(s)
118res = strtrim(s);
119if strcmpi(res(1),'=')
120    res(1)=[];
121end
122res = strtrim(res);
123return;
Note: See TracBrowser for help on using the repository browser.