Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 24 | typedef std::vector<std::string> header_t; |
| 25 | typedef std::vector<std::string> record_t; |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 26 | typedef std::string (*trans_func) (const std::string&); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 27 | |
| 28 | const char DEFAULT_NEWLINE = '\n'; |
| 29 | const std::string DEFAULT_WHITESPACE = " \t"; |
| 30 | |
Yi Jin | 4ef28b7 | 2017-08-14 14:45:28 -0700 | [diff] [blame] | 31 | header_t parseHeader(const std::string& line, const std::string& delimiters = DEFAULT_WHITESPACE); |
| 32 | record_t parseRecord(const std::string& line, const std::string& delimiters = DEFAULT_WHITESPACE); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Reader class reads data from given fd in streaming fashion. |
| 36 | * The buffer size is controlled by capacity parameter. |
| 37 | */ |
| 38 | class Reader |
| 39 | { |
| 40 | public: |
| 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 | |
| 48 | private: |
| 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 |