如何在yaml cpp中保留插入顺序

时间:2014-05-08 11:22:03

标签: c++ sorting qt4 yaml-cpp snakeyaml

我创建了一个yamlmap,其中包含作为字符串,我已将其保存为文本文件。在加载相同的文本文件时,包含字符串的键是由yaml自动排序。现在我想加载字符串名称的顺序与我保存的顺序相同。要解决我在函数saveSettings()和loadSettings.Thanks

中必须做的事情
std::vector<SettingsPair*> settingsvector;std::list<std::string> settings_list;
class SettingsPair
{
public:
    /**
     * @brief standard constructor
     */
    SettingsPair();
    SettingsPair(QLineEdit *le, QLabel *la);
    ~SettingsPair();
    QLineEdit* line_edit;
    QLabel* label;

};


   void task1::saveSettings()
{
    QString filename = QFileDialog::getSaveFileName(this,
                                                    tr("save YAML File "),"C:/Users/marthala/Desktop/ws/settingstask","ALL files(*.*);;YAML File(*.yaml)");
    std::ofstream ofstr;
    std::string file= filename.toStdString();
    YAML::Emitter out;
    ofstr.open(file.c_str());
    out << YAML::BeginMap;
    for(std::vector<SettingsPair*>::iterator list_iter = settingsvector.begin();
        list_iter != settingsvector.end(); list_iter++)
    {
        QString key_lineedit = (*list_iter)->line_edit->text();
        QString key_label = (*list_iter)->label->text();
        out << YAML::Key << key_label.toStdString();
        out << YAML::Value <<key_lineedit.toStdString();

    }
    ofstr<<out.c_str();
    out << YAML::EndMap;

    std::cout<<"here is the output while saving the file:\n"<<out.c_str() ;
    ofstr.close();

}

   void task1::loadSettings()
{
    QString filename = QFileDialog::getOpenFileName(this,
                                                    tr("Open File"),"C:/Users/marthala/Desktop/ws/settingstask","ALL files(*.*);;YAML File(*.yaml)");
    //getOpenFileName will retuns the file path of the file selected

    if(filename.isEmpty())
    {
        return;
    }
    else
    {
        try
        {

            std::ifstream fin(filename.toStdString().c_str());
            YAML::Parser parser(fin);
            YAML::Node doc;
            while(parser.GetNextDocument(doc))
            {
                std::vector<SettingsPair*>::iterator list_iter = settingsvector.begin();
                if(list_iter != settingsvector.end())
                {
                    std::string key_, value_;
                    for(YAML::Iterator it=doc.begin();it!=doc.end();++it)
                    {

                        it.first() >> key_;
                        it.second() >> value_;

                        QString str1 = QString::fromUtf8(key_.c_str());
                        QString str2 = QString::fromUtf8(value_.c_str());
                        (*list_iter)->label->setText(str1);
                        (*list_iter)->line_edit->setText(str2);
                        list_iter++;

                        std::cout<<"here is the output while loading the file:\n"<< key_<<":\n"<<value_;
                    }
                }
            }

        }
        catch(YAML::ParserException &e)
        {
            std::cout<<"YAML Exception caught"<<e.what()<<std::endl;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

目前正在考虑此功能。有关详细信息,请参阅项目页面上的issue report

答案 1 :(得分:0)