src/hbcore/utils/hbiniparser.cpp
changeset 0 16d8024aca5e
child 5 627c4a0fd0e7
equal deleted inserted replaced
-1:000000000000 0:16d8024aca5e
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     6 **
       
     7 ** This file is part of the HbCore module of the UI Extensions for Mobile.
       
     8 **
       
     9 ** GNU Lesser General Public License Usage
       
    10 ** This file may be used under the terms of the GNU Lesser General Public
       
    11 ** License version 2.1 as published by the Free Software Foundation and
       
    12 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    13 ** Please review the following information to ensure the GNU Lesser General
       
    14 ** Public License version 2.1 requirements will be met:
       
    15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    16 **
       
    17 ** In addition, as a special exception, Nokia gives you certain additional
       
    18 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    20 **
       
    21 ** If you have questions regarding the use of this file, please contact
       
    22 ** Nokia at developer.feedback@nokia.com.
       
    23 **
       
    24 ****************************************************************************/
       
    25 
       
    26 #include "hbiniparser_p.h"
       
    27 #include <QByteArray>
       
    28 #include <QIODevice>
       
    29 #include <QtDebug>
       
    30 #include <QString>
       
    31 #include <QStringList>
       
    32 
       
    33 HbIniParser::HbIniParser()
       
    34 {
       
    35 }
       
    36 
       
    37 HbIniParser::~HbIniParser()
       
    38 {
       
    39 }
       
    40     
       
    41 /*
       
    42     The main function that populates the map data.
       
    43     currently the data is case sensitive and all api behave that way.
       
    44 */
       
    45 bool HbIniParser::read(QIODevice *file)
       
    46 {
       
    47     if(!file->isOpen())
       
    48         return false;
       
    49     
       
    50     QByteArray line;
       
    51     
       
    52     HbIniGroup groupData;
       
    53     QString groupName;
       
    54     
       
    55     while(!file->atEnd()) {        
       
    56         line=file->readLine().trimmed();
       
    57          if (line.isEmpty() || line.at(0) == '#') {
       
    58             continue; 
       
    59         }
       
    60         
       
    61         if (line.at(0) == '[') { // found a group
       
    62             //add old group data
       
    63             if(!groupName.isEmpty()) {
       
    64                 mData.insert(groupName,groupData);
       
    65                 groupData.clear();
       
    66             }
       
    67                 
       
    68             groupName = line.mid (1, line.indexOf(']') - 1);
       
    69             if (groupName.isEmpty()) {
       
    70                 return false; //error in file
       
    71             }         
       
    72         }
       
    73         else {
       
    74             QByteArray key,value;
       
    75             int equalPosition = line.indexOf('=');
       
    76             if (equalPosition > 0) {
       
    77                 key = line.left (equalPosition).trimmed();
       
    78                 line.remove(0,equalPosition+1);
       
    79                 value = line.trimmed();
       
    80                 groupData.insert(key,value);
       
    81             }
       
    82         }                
       
    83     }
       
    84     if(!groupName.isEmpty()) {
       
    85         mData.insert(groupName,groupData);
       
    86         groupData.clear();
       
    87     }
       
    88     return true;
       
    89 }
       
    90     
       
    91 bool HbIniParser::setCurrentGroup(const QString &name)
       
    92 {
       
    93     if (mData.contains(name)) {
       
    94         mCurrentGroup = name;
       
    95         return true;
       
    96     }
       
    97     return false;
       
    98 }
       
    99 
       
   100 QString HbIniParser::currentGroup()
       
   101 {
       
   102     return mCurrentGroup;
       
   103 }
       
   104     
       
   105 const QString HbIniParser::value(const QString &groupName,const QString &key) const
       
   106 {
       
   107     if(!mData.contains(groupName)){
       
   108         return QString();
       
   109     }
       
   110     return mData.value(groupName).value(key);
       
   111 }
       
   112 
       
   113 const QString HbIniParser::value(const QString &key) const
       
   114 {
       
   115     if(mCurrentGroup.isEmpty()) {
       
   116         return QString();
       
   117     }
       
   118     return mData.value(mCurrentGroup).value(key);
       
   119 }   
       
   120 
       
   121 QStringList HbIniParser::groups() const
       
   122 {
       
   123     return mData.keys();
       
   124 }