source: trunk/src/ini2struct.m @ 1148

Last change on this file since 1148 was 1127, checked in by g7moreau, 12 months ago

Update Joel email

File size: 5.0 KB
RevLine 
[809]1%=======================================================================
[1126]2% Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
[809]3%   http://www.legi.grenoble-inp.fr
[1127]4%   Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr
[809]5%
6%     This file is part of the toolbox UVMAT.
7%
8%     UVMAT is free software; you can redistribute it and/or modify
9%     it under the terms of the GNU General Public License as published
10%     by the Free Software Foundation; either version 2 of the license,
11%     or (at your option) any later version.
12%
13%     UVMAT is distributed in the hope that it will be useful,
14%     but WITHOUT ANY WARRANTY; without even the implied warranty of
15%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16%     GNU General Public License (see LICENSE.txt) for more details.
17%=======================================================================
18
[784]19function Result = ini2struct(FileName)
20%==========================================================================
21%  Author: Andriy Nych ( nych.andriy@gmail.com )
22% Version:        733341.4155741782200
23%==========================================================================
24%
25% INI = ini2struct(FileName)
26%
27% This function parses INI file FileName and returns it as a structure with
28% section names and keys as fields.
29%
30% Sections from INI file are returned as fields of INI structure.
31% Each fiels (section of INI file) in turn is structure.
32% It's fields are variables from the corresponding section of the INI file.
33%
34% If INI file contains "oprhan" variables at the beginning, they will be
35% added as fields to INI structure.
36%
37% Lines starting with ';' and '#' are ignored (comments).
38%
39% See example below for more information.
40%
41% Usually, INI files allow to put spaces and numbers in section names
42% without restrictions as long as section name is between '[' and ']'.
43% It makes people crazy to convert them to valid Matlab variables.
44% For this purpose Matlab provides GENVARNAME function, which does
45%  "Construct a valid MATLAB variable name from a given candidate".
46% See 'help genvarname' for more information.
47%
48% The INI2STRUCT function uses the GENVARNAME to convert strange INI
49% file string into valid Matlab field names.
50%
51% [ test.ini ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
52%
53%     SectionlessVar1=Oops
54%     SectionlessVar2=I did it again ;o)
55%     [Application]
56%     Title = Cool program
57%     LastDir = c:\Far\Far\Away
58%     NumberOFSections = 2
59%     [1st section]
60%     param1 = val1
61%     Param 2 = Val 2
62%     [Section #2]
63%     param1 = val1
64%     Param 2 = Val 2
65%
66% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
67%
68% The function converts this INI file it to the following structure:
69%
70% [ MatLab session (R2006b) ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
71%  >> INI = ini2struct('test.ini');
72%  >> disp(INI)
73%         sectionlessvar1: 'Oops'
74%         sectionlessvar2: 'I did it again ;o)'
75%             application: [1x1 struct]
76%             x1stSection: [1x1 struct]
77%            section0x232: [1x1 struct]
78%
79%  >> disp(INI.application)
80%                    title: 'Cool program'
81%                  lastdir: 'c:\Far\Far\Away'
82%         numberofsections: '2'
83%
84%  >> disp(INI.x1stSection)
85%         param1: 'val1'
86%         param2: 'Val 2'
87%
88%  >> disp(INI.section0x232)
89%         param1: 'val1'
90%         param2: 'Val 2'
91%
92% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
93%
94% NOTE.
95% WhatToDoWithMyVeryCoolSectionAndVariableNamesInIniFileMyVeryCoolProgramWrites?
96% GENVARNAME also does the following:
97%   "Any string that exceeds NAMELENGTHMAX is truncated". (doc genvarname)
98% Period.
99%
100% =========================================================================
101Result = [];                            % we have to return something
102CurrMainField = '';                     % it will be used later
103f = fopen(FileName,'r');                % open file
104while ~feof(f)                          % and read until it ends
105    s = strtrim(fgetl(f));              % Remove any leading/trailing spaces
106    if isempty(s)
107        continue;
108    end;
109    if (s(1)==';')                      % ';' start comment lines
110        continue;
111    end;
112    if (s(1)=='#')                      % '#' start comment lines
113        continue;
114    end;
115    if ( s(1)=='[' ) && (s(end)==']' )
116        % We found section
117        CurrMainField = genvarname(lower(s(2:end-1)));
118        Result.(CurrMainField) = [];    % Create field in Result
119    else
120        % ??? This is not a section start
121        [par,val] = strtok(s, '=');
122        val = CleanValue(val);
123        if ~isempty(CurrMainField)
124            % But we found section before and have to fill it
125            Result.(CurrMainField).(lower(genvarname(par))) = val;
126        else
127            % No sections found before. Orphan value
128            Result.(lower(genvarname(par))) = val;
129        end
130    end
131end
132fclose(f);
133return;
134
135function res = CleanValue(s)
136res = strtrim(s);
137if strcmpi(res(1),'=')
138    res(1)=[];
139end
140res = strtrim(res);
141return;
Note: See TracBrowser for help on using the repository browser.