Initialize Tizen 2.3
[framework/web/wrt-commons.git] / modules_mobile / test / include / dpl / test / value_separated_parser.h
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        value_separated_parser.h
18  * @author      Tomasz Iwanek (t.iwanek@samsung.com)
19  * @brief       Parser for some value seperated files/data
20  */
21
22 #ifndef VALUE_SEPARATED_PARSER_H
23 #define VALUE_SEPARATED_PARSER_H
24
25 #include<string>
26 #include<vector>
27 #include<memory>
28
29 #include<dpl/test/value_separated_tokens.h>
30 #include<dpl/test/abstract_input_parser.h>
31
32 namespace DPL {
33
34 typedef std::vector<std::string> VSLine;
35 typedef std::vector<VSLine> VSResult;
36 typedef std::shared_ptr<VSResult> VSResultPtr;
37
38 /**
39  * Value Seperated parser
40  *
41  * Requires following policy class:
42  *
43  * template<VSResultPtr>
44  * struct CSVParserPolicy
45  * {
46  *     static bool SkipLine(VSLine & );
47  *     static bool Validate(VSResultPtr& result);
48  * };
49  */
50 template<class ParserPolicy>
51 class VSParser : public AbstractInputParser<VSResultPtr, VSToken>
52 {
53 public:
54     VSParser() : m_switchLine(true), m_result(new VSResult()) {}
55
56     void ConsumeToken(std::unique_ptr<VSToken> && token)
57     {
58         if(m_switchLine)
59         {
60             m_result->push_back(VSLine());
61             m_switchLine = false;
62         }
63         if(token->isNewLine())
64         {
65             if(ParserPolicy::SkipLine(*m_result->rbegin()))
66             {
67                 m_result->pop_back();
68             }
69             m_switchLine = true;
70         }
71         else
72         {
73             m_result->rbegin()->push_back(token->cell());
74         }
75     }
76
77     bool IsStateValid()
78     {
79         return ParserPolicy::Validate(m_result);
80     }
81
82     VSResultPtr GetResult() const
83     {
84         return m_result;
85     }
86
87 private:
88     bool m_switchLine;
89     VSResultPtr m_result;
90 };
91
92 }
93
94 #endif