2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file value_separated_tokenizer.h
18 * @author Tomasz Iwanek (t.iwanek@samsung.com)
19 * @brief Tokenizer for some value seperated files/data
22 #ifndef VALUE_SEPARATED_TOKENIZER_H
23 #define VALUE_SEPARATED_TOKENIZER_H
25 #include<dpl/test/abstract_input_tokenizer.h>
26 #include<dpl/test/value_separated_tokens.h>
27 #include<dpl/binary_queue.h>
33 * Value Sperated tokenizer
35 * Requires following policy class:
37 * struct TokenizerPolicy
39 * static std::string GetSeperators();
40 * static bool SkipEmpty();
41 * static void PrepareValue(std::string & value);
44 template<class TokenizerPolicy>
45 class VSTokenizer : public AbstractInputTokenizer<VSToken>
50 void Reset(std::shared_ptr<AbstractInput> ia)
52 AbstractInputTokenizer<VSToken>::Reset(ia);
58 std::unique_ptr<VSToken> GetNextToken()
60 std::unique_ptr<VSToken> token;
67 //check if newline was approched
70 token.reset(new VSToken());
84 auto baptr = m_input->Read(4096);
87 ThrowMsg(Exception::TokenizerError, "Input read failed");
91 if(TokenizerPolicy::TryAgainAtEnd(tryNumber))
99 m_queue.AppendMoveFrom(*baptr);
104 m_queue.FlattenConsume(&byte, 1); //queue uses pointer to consume bytes, this do not causes reallocations
108 if(!data.empty() || !TokenizerPolicy::SkipEmpty())
110 ProduceString(token, data);
114 else if(TokenizerPolicy::GetSeperators().find(byte) != std::string::npos)
116 if(!data.empty() || !TokenizerPolicy::SkipEmpty())
118 ProduceString(token, data);
131 if(!m_queue.Empty() && m_finished) return false;
136 void ProduceString(std::unique_ptr<VSToken> & token, std::string & data)
138 TokenizerPolicy::PrepareValue(data);
139 token.reset(new VSToken(data));