summaryrefslogtreecommitdiff
path: root/tests/common/RecordsParam.cpp
blob: 74ad802a0dd4d2b40bfe8f9743a1dc28c71a7796 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "RecordsParam.h"

#include "../../src/dictionary/Field.h"
#include "../../src/dictionary/DicRecord.h"

const vector<string> RecordsParam::fieldNames {"English", "Russian", "Finnish"};

RecordsParam::RecordsParam(
    vector<int> packFields,
    vector<vector<string> > records,
    vector<string> questions,
    vector<vector<string> > answers):
        packFields(packFields),
        questions(questions),
        answers(answers)
{
    for(vector<string> fieldValues: records)
        {
        DicRecord* record = new DicRecord;
        for(unsigned i = 0; i < fieldValues.size(); i++)
            record->setField(fieldNames[i].c_str(), fieldValues[i].c_str());
        this->records << record;
        }
}

vector<string> RecordsParam::hashToStrVector(const QHash<QString, QString>& hash,
    const vector<string>& keys)
{
    vector<string> res;
    for(unsigned i = 0; i < keys.size(); i++)
        res.push_back( hash[keys[i].c_str()].toStdString() );
    return res;
}

vector<string> RecordsParam::recordsToStr() const
{
    vector<string> res;
    for(DicRecord* record: records)
    {
        vector<string> fieldValues = hashToStrVector(record->getFields(), fieldNames);
        res.push_back(string("(") + vectorToStr<string>(fieldValues) + ")");
    }
    return res;
}

ostream& operator<<(ostream& os, const RecordsParam& param)
{
    os << "Fields(" << param.vectorToStr<int>(param.packFields) << ") ";
    os << "{" << param.vectorToStr<string>( param.recordsToStr() ) << "}";
    return os;
}