source: trunk/src/ini2struct.m @ 933

Last change on this file since 933 was 924, checked in by g7moreau, 9 years ago
  • Update Copyright Copyright 2008-2016, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
File size: 5.0 KB
Line 
1%=======================================================================
2% Copyright 2008-2016, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
3%   http://www.legi.grenoble-inp.fr
4%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
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
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% WhatToDoW