Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Keys containing "/" in INI files
Forum Updated to NodeBB v4.3 + New Features

Keys containing "/" in INI files

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 3.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Maaaks
    wrote on last edited by
    #1

    I'm looking for a way to work with a INI-like configuration file which should contain values like this:

    @[Group]
    Param1 = value1
    Param2 = value2
    Files/All/usr/bin/something = /opt/something/something@

    I mean, inside a Group there should be subgroup Files with subgroup All, and then there should be a key representing a filepath, e.g. /usr/bin/something must be parsed as a single key. If I would use JSON, this is what I would use:

    @"Group": {
    "Param1": "value1",
    "Param2": "value2",
    "Files": {
    "All": {
    "/usr/bin/something": "/opt/something/something"
    }
    }
    }@

    But JSON looks not very human-friendly to me since it requires too many quoteation marks, so I prefer INI-style configuration, but I need to stop parsing subgroups after the 3rd level (Group -> Files -> All).

    As far as I know, there is no way to do this with "QSettings":http://qt-project.org/doc/qt-5.0/qtcore/qsettings.html. Is there an external library that allows to do this? Or will it be easier to write my own INI-like parser?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      Why "should" the file contain ambiguous entries like the example?

      QSettings has a group mechanism that works in INI files but it uses / and \ as delimiters.
      @
      QSettings s("test.ini", QSettings::IniFormat);
      s.beginGroup("Group");
      s.beginGroup("Files");
      s.beginGroup("All");
      s.setValue("/usr/bin/something", "/opt/something/something");
      s.endGroup();
      s.endGroup();
      s.endGroup();

      s.beginGroup("Group");
      s.beginGroup("Files");
      s.beginGroup("All");
      qDebug() << s.value("/usr/bin/something").toString();
      s.endGroup();
      s.endGroup();
      s.endGroup();
      qDebug() << s.value("/Group/Files/All/usr/bin/something").toString();
      

      @

      The resulting file looks like:
      @
      [Group]
      Files\All\usr\bin\something=/opt/something/something
      @

      and the two qDebug() outputs are the same item. If all else fails you can encode the key that contains the '/' or use allKeys() and childKeys() to walk through the tree.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Maaaks
        wrote on last edited by
        #3

        And what about iterating through keys() inside All? I'll see only usr, but I want to see /usr/bin/something (or usr/bin/something) there.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          If you know the keys then you do not need to iterate over them. If you don't know the keys then you need to deal with the fact that your key is indistinguishable from another group. Encode the key any way that removes the / and the problem goes away.
          @
          QSettings s("test.ini", QSettings::IniFormat);
          s.beginGroup("Group");
          s.beginGroup("Files");
          s.beginGroup("All");
          s.setValue(QUrl::toPercentEncoding("/usr/bin/something"), "/opt/something/something");
          s.setValue(QUrl::toPercentEncoding("/usr/bin/other"), "/opt/something/other");
          s.endGroup();
          s.endGroup();
          s.endGroup();

          s.beginGroup("Group");
          s.beginGroup("Files");
          s.beginGroup("All");
          foreach(QString key, s.childKeys())
              qDebug() << QUrl::fromPercentEncoding(key.toUtf8());
          

          // "/usr/bin/other"
          // "/usr/bin/something"

          @

          The resulting INI file looks like this:
          @
          [Group]
          Files\All%252Fusr%252Fbin%252Fsomething=/opt/something/something
          Files\All%252Fusr%252Fbin%252Fother=/opt/something/other
          @

          You could even register your own QSettings format handler if you really need to read the exact format you started with. Writing would still be an issue though.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved