blob: 5598eed8d824315c7cfac7fe192941b3e0c6ab5e [file] [log] [blame]
Yi Jinb44f7d42017-07-21 12:12:59 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
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#ifndef INCIDENT_HELPER_UTIL_H
18#define INCIDENT_HELPER_UTIL_H
19
20#include <string>
21#include <vector>
22#include <sstream>
23
24typedef std::vector<std::string> header_t;
25typedef std::vector<std::string> record_t;
Yi Jin4ef28b72017-08-14 14:45:28 -070026typedef std::string (*trans_func) (const std::string&);
Yi Jinb44f7d42017-07-21 12:12:59 -070027
28const char DEFAULT_NEWLINE = '\n';
29const std::string DEFAULT_WHITESPACE = " \t";
30
Yi Jin4ef28b72017-08-14 14:45:28 -070031header_t parseHeader(const std::string& line, const std::string& delimiters = DEFAULT_WHITESPACE);
32record_t parseRecord(const std::string& line, const std::string& delimiters = DEFAULT_WHITESPACE);
Yi Jinb44f7d42017-07-21 12:12:59 -070033
34/**
35 * Reader class reads data from given fd in streaming fashion.
36 * The buffer size is controlled by capacity parameter.
37 */
38class Reader
39{
40public:
41 Reader(const int fd);
42 Reader(const int fd, const size_t capacity);
43 ~Reader();
44
45 bool readLine(std::string& line, const char newline = DEFAULT_NEWLINE);
46 bool ok(std::string& error);
47
48private:
49 int mFd; // set mFd to -1 when read EOF()
50 const size_t mMaxSize;
51 size_t mBufSize;
52 char* mBuf; // implements a circular buffer
53
54 int mRead;
55 int mFlushed;
56 std::string mStatus;
57 // end of read
58 inline bool EOR() { return mFd == -1 && mBufSize == 0; };
59};
60
61#endif // INCIDENT_HELPER_UTIL_H