src/hbcore/utils/hbiniparser.cpp
changeset 34 ed14f46c0e55
parent 31 7516d6d86cf5
equal deleted inserted replaced
31:7516d6d86cf5 34:ed14f46c0e55
     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 
       
    51     QByteArray line;
       
    52 
       
    53     HbIniGroup groupData;
       
    54     QString groupName;
       
    55 
       
    56     while (!file->atEnd()) {
       
    57         line = file->readLine().trimmed();
       
    58         if (line.isEmpty() || line.at(0) == '#') {
       
    59             continue;
       
    60         }
       
    61 
       
    62         if (line.at(0) == '[') { // found a group
       
    63             //add old group data
       
    64             if (!groupName.isEmpty()) {
       
    65                 mData.insert(groupName, groupData);
       
    66                 groupData.clear();
       
    67             }
       
    68 
       
    69             groupName = line.mid(1, line.indexOf(']') - 1);
       
    70             if (groupName.isEmpty()) {
       
    71                 return false; //error in file
       
    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 }