存储设置的最佳做法 [英] Best practises for storing settings

查看:92
本文介绍了存储设置的最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当大的C ++应用程序(在Windows上,没有其他计划的平台),它目前在Windows注册表中存储所有设置(甚至某种地址)。

I have a fairly large C++ application (on Windows, no other platforms planned), which currently stores all settings (even some kind of addresses) in the Windows registry.

有时这是不方便的,因为用户在注册表中更改条目有困难。我想要设置版本,所以设置总是匹配当前的代码。目前我们版本注册文件,但你永远不能确定,如果所有注册文件已添加到目标机器上。使用C#,您可以在app.config中定义默认值,但不要覆盖现有设置。我不知道,如果这样的机制或库存在的C + +。

Sometimes this is inconvenient, because the users have difficulties changing entries in the registry. I would like to have settings versioned, so settings always match the current code. At the moment we version reg-files, but you are never sure, if all reg-files have been added on the target machines. With C# you can define default values in app.config, but don't overwrite existing settings. I don't know, if such a mechanism or library exists for C++.

我想有以下功能:


  • 设置可以进行版本控制

  • 在目标机器上进行简单更新(可以由用户执行)

  • 确保在更新时只添加新设置,并且不使用默认值覆盖现有设置

  • 用户简单更改设置

  • 在Win XP和Win 7下

  • Settings can be versioned
  • Simple update on target machines (can be done by user)
  • Ensure that on update only new settings are added and no existing settings are overwritten with default values
  • Simple change of settings for user
  • Same workflow under Win XP and Win 7

根据我的看法,在Windows上存储设置有3种可能:

As far as I see it, there are 3 possibilities to store settings on Windows:


  • 注册表

  • Ini文件

  • XML文件

我们套件中只有一个应用程序使用Qt,但Boost将可用。

Only one application of our suite uses Qt at the moment, but Boost would be available.

对于地址,我们将它们放在某种XML地址簿中,但对于其他设置我们不确定,最好的做法是什么。

For addresses, we will put them in some kind of XML address book, but for the other settings we are not sure, what's the best practise.

推荐答案

正如注释中提到的,基于树的键/值结构是一个常见的解决方案,库很容易找到。

As comments mention, tree-based key/value structures are a common solution and libraries are easy to find.

Boost的property_tree 是一个不错的选择,因为它经过良好测试,可以轻松导出作为XML或JSON

Boost's property_tree is an excellent choice, as it is well-tested and can easily be exported as XML or JSON

关于您的要求:



  • 设置可以进行版本控制

使version成为顶级键。

Yes! Make "version" a top-level key. Make it easily comparable with other versions.

您还可以将设置分类到各种树节点,并为每个节点分配一个版本。

You can also categorize your settings into various tree nodes and give each node a version.



  • 目标机器上的简单更新(可由用户完成)

让你的应用程序运行时。

Have your application do that when it runs. See below.



  • 确保在更新时仅添加新设置,并且不使用默认值覆盖现有设置

  • 简单更改用户设置

  • Win XP和Win 7下相同的工作流程

随着设置从一个版本更改为另一个版本,通常这些更改分为三类。需要新属性,放弃旧设置,并且某些设置更改其预期格式。例如。 32 Fahrenheit变为0 Celsius

As settings change from one version to another, usually these changes fall into three categories. New properties are needed, old settings are abandoned, and some settings change their expected format. E.g. "32 Fahrenheit" becomes "0 Celsius"

当您的应用程序初始化时:

When your application initializes:



  • 如果版本与应用程序的当前版本不匹配:

    • 创建新配置的新的空白属性树

    • 对于树中的每个节点,都有一组预期的属性名称,以及一个函数指针或类似函数,老文件的树。如果设置更改了其格式,请为其设置一个新名称。

    • 搜索旧树中的每个设置,如果找到则复制它,如果不是,则使用提供的函数的结果

    • Load the existing configuration file, regardless of its version.
    • If the version does not match what's current for the application:
      • Create a new blank property tree for the new configuration
      • For each node in the tree, have a set of expected property names, and a function pointer or similar to get this setting if it's absent in the old file's tree. If a setting changes its format, give it a new name.
      • Search for each setting in the old tree, copying it if it's found, and using the result of the supplied function if it's not.
      • Save your new settings file.

      您的缺少设置功能可以:

      Your "missing setting" functions can:


      • 返回常数默认值。


      • 询问用户

      这篇关于存储设置的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆