TinyTinni / ValveFileVDF

C++ Parser and Writer for Valve Data Format (e.g. .vdf files used in steam)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is there any plan to add possibility to remove childs?

mozcelikors opened this issue · comments

Hi Matthias.
Great work on this parser!

I intend to use this for my latest project (https://github.com/mozcelikors/BetterGallery) and remove screenshots from screenshots.vdf file. However, I could not see any function to remove childs.

I have something like this at the moment:

    std::ifstream file("/home/deck/.local/share/Steam/userdata/"+qgetenv("STEAMUSERID")+"/760/screenshots.vdf");
    auto root = tyti::vdf::read(file);

    assert(root.name == "screenshots");
    const char * gameId_Text = gameId.toStdString().c_str();

    const std::shared_ptr<tyti::vdf::object> child = root.childs[gameId_Text];
    const std::shared_ptr<tyti::vdf::object> child2 = child->childs["0"];

    const std::string& k = child2->attribs["filename"];
    cout << "Filename: " << k;

For example I would like to remove child2 from the child, reconstruct and write into the file again.

File looks as such:

"screenshots"
{
    "760"
    {
        "0"
         {
              someattrs
         }
         "1"
         {
              someattrs
         }
         "2"
         {
              someattrs
         }
         ....
    }
    "1234"
    {
          ....
    }
}

I would like to access and delete the block for "1" here for example.

Is there a way of doing this?
Thanks a bunch.

Hi,
the childs member are per default just std::unordered_map<std::string>,std::shared_ptr<tyti::vdf::object>>.

When you want to erase an element, you can just call the unorderd_map::erase method.

Just keep in mind, that in your example, the variable child is deeclared as const and therefore, you cannot delete anything there.

But something like this should work

std::shared_ptr<tyti::vdf::object> child = root.childs[gameId_Text];
const std::shared_ptr<tyti::vdf::object> child2 = child->childs["0"];
child->childs.erase("0");

// child2 is still valid
// child2 is not part of child->childs anymore

When you write child, it should not have the whole tree under 0 anymore.

I hope this helps.

I guess this helped.

Feel free to reopen it.