]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/ini_file.cpp
* all: added a INI file parser in skins2. For instance if the file
[vlc] / modules / gui / skins2 / src / ini_file.cpp
1 /*****************************************************************************
2  * ini_file.cpp
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include "ini_file.hpp"
25 #include "var_manager.hpp"
26 #include <fstream>
27
28
29 IniFile::IniFile( intf_thread_t *pIntf, const string &rName,
30                   const string &rPath ):
31     SkinObject( pIntf ), m_name( rName ), m_path( rPath )
32 {
33 }
34
35
36 void IniFile::parseFile()
37 {
38     VarManager *pVarManager = VarManager::instance( getIntf() );
39
40     // Open the file
41     fstream fs( m_path.c_str(), fstream::in );
42     if( fs.is_open() )
43     {
44         string section;
45         string line;
46         while( !fs.eof() )
47         {
48             // Read the next line
49             fs >> line;
50
51             switch( line[0] )
52             {
53             // "[section]" line ?
54             case '[':
55                 section = line.substr( 1, line.size() - 2);
56                 break;
57
58             // Comment
59             case ';':
60             case '#':
61                 break;
62
63             // Variable declaration
64             default:
65                 size_t eqPos = line.find( '=' );
66                 string var = line.substr( 0, eqPos );
67                 string val = line.substr( eqPos + 1, line.size() - eqPos - 1);
68
69                 // register the value in the var manager
70                 pVarManager->registerConst( m_name + "." + section + "." + var,
71                                             val );
72             }
73         }
74         fs.close();
75     }
76     else
77     {
78         msg_Err( getIntf(), "Failed to open INI file %s", m_path.c_str() );
79     }
80 }
81