Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / simulator / src / client / query_param_generator.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 #include "query_param_generator.h"
22
23 QPGenerator::QPGenerator(const std::map<std::string, std::vector<std::string>> &queryParams)
24 {
25     for (auto entry : queryParams)
26     {
27         if (entry.second.size())
28         {
29             QPDetail detail;
30             detail.key.assign(entry.first.c_str());
31             detail.values = entry.second;
32             detail.index = 0;
33             m_qpDetails.push_back(detail);
34         }
35     }
36 }
37
38 bool QPGenerator::hasNext()
39 {
40     if (m_qpDetails.size() &&
41         (m_qpDetails[0].index < m_qpDetails[0].values.size()))
42     {
43         return true;
44     }
45
46     return false;
47 }
48
49 std::map<std::string, std::string> QPGenerator::next()
50 {
51     std::map<std::string, std::string> queryParams;
52     if (!hasNext())
53         return queryParams;
54
55     for (auto ele : m_qpDetails)
56         queryParams[ele.key] = ele.values[ele.index];
57
58     for (int index = m_qpDetails.size() - 1; index >= 0; index--)
59     {
60         m_qpDetails[index].index++;
61         if (m_qpDetails[index].index >= m_qpDetails[index].values.size()) // Boundary check
62         {
63             if (index != 0)
64             {
65                 m_qpDetails[index].index = 0;
66                 continue;
67             }
68         }
69         break;
70     }
71
72     return queryParams;
73 }