存储文件中的键值数据

时间:2022-10-29 16:58:16

Alright, so I've encounted a problem that my limited programming skills simply cannot solve, at least in an elegant fashion. I want to store the data from Valve KeyValues files, but I can't think of a good way of going about it. I'll illustrate the basic structure of the file format in this block of code.

好吧,所以我解决了一个问题,即我有限的编程技巧根本无法解决,至少是以优雅的方式。我想存储来自Valve KeyValues文件的数据,但我想不出一个很好的方法来实现它。我将在这段代码中说明文件格式的基本结构。

"Key"
{
    "Key"
    {
        "Key" "Value"
        "Key" "Value"
    }
    "Key"
    {
        "Key" "Value"
        "Key" "Value"
    }
    "Key" "Value"
    "Key" "Value"
}

Each value can be one of a few types. Each key has to have a value with a certain type assigned to it. Wherever the key itself is located by change the type, but I doubt this. I can actually tokenize the file already, by keys, values, and brackets, so I don't need any help with that unless it's necessary. I'd like to create a system that isn't quick and dirty and relies on heavy amounts of repetitive code. If any of you have any questions, feel free to ask.

每个值可以是几种类型中的一种。每个键必须具有分配给它的特定类型的值。通过更改类型来定位密钥本身的位置,但我对此表示怀疑。我实际上可以通过键,值和括号对文件进行标记,因此除非有必要,否则我不需要任何帮助。我想创建一个不快速和脏的系统,并依赖于大量重复的代码。如果您有任何疑问,请随时提出。

2 个解决方案

#1


0  

A straightforward solution would be to use an std::map<std::string, boost::any> container. You'd need to use boost::any for this. Another solution is to store the key-value pairs as straightforward string in the sense of an std::map<std::string, std::string> and then use boost::lexical_cast to convert.

一个简单的解决方案是使用std :: map 容器。你需要使用boost :: any。另一种解决方案是将键值对存储为std :: map 意义上的直接字符串,然后使用boost :: lexical_cast进行转换。

#2


0  

Here is a solution which I think can be applied to this problem:

这是一个我认为可以应用于此问题的解决方案:

I'm assuming that you may be given many blocks like this-

我假设你可能会得到很多这样的街区 -

"Key"

{

...

}

"Key"

{

...

}

Now, construct a forest with trees rooted at the initial "Key" of a block. The children of the key will be the keys which are in sub-block and so-on. The leaves would contain the "Values".

现在,构建一个森林,其树木以块的初始“Key”为根。密钥的子项将是子块中的密钥等等。叶子将包含“值”。

The trees will be n-ary, not binary. The "Keys" in a level could be stored in a sorted order from left to right which would enhance the retrieval time of the value corresponding the key queried.

树木将是n-ary,而不是二元树。级别中的“键”可以从左到右以排序顺序存储,这将增强与所查询的键相对应的值的检索时间。

Such a storage would be one of the most elegant way that I could think of. Further thoughts and suggestions appreciated.

这样的存储将是我能想到的最优雅的方式之一。进一步的想法和建议赞赏。

#1


0  

A straightforward solution would be to use an std::map<std::string, boost::any> container. You'd need to use boost::any for this. Another solution is to store the key-value pairs as straightforward string in the sense of an std::map<std::string, std::string> and then use boost::lexical_cast to convert.

一个简单的解决方案是使用std :: map 容器。你需要使用boost :: any。另一种解决方案是将键值对存储为std :: map 意义上的直接字符串,然后使用boost :: lexical_cast进行转换。

#2


0  

Here is a solution which I think can be applied to this problem:

这是一个我认为可以应用于此问题的解决方案:

I'm assuming that you may be given many blocks like this-

我假设你可能会得到很多这样的街区 -

"Key"

{

...

}

"Key"

{

...

}

Now, construct a forest with trees rooted at the initial "Key" of a block. The children of the key will be the keys which are in sub-block and so-on. The leaves would contain the "Values".

现在,构建一个森林,其树木以块的初始“Key”为根。密钥的子项将是子块中的密钥等等。叶子将包含“值”。

The trees will be n-ary, not binary. The "Keys" in a level could be stored in a sorted order from left to right which would enhance the retrieval time of the value corresponding the key queried.

树木将是n-ary,而不是二元树。级别中的“键”可以从左到右以排序顺序存储,这将增强与所查询的键相对应的值的检索时间。

Such a storage would be one of the most elegant way that I could think of. Further thoughts and suggestions appreciated.

这样的存储将是我能想到的最优雅的方式之一。进一步的想法和建议赞赏。