OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #ifndef CRASHPAD_UTIL_FILE_DELIMITED_FILE_READER_H_ |
| 16 #define CRASHPAD_UTIL_FILE_DELIMITED_FILE_READER_H_ |
| 17 |
| 18 #include <stdint.h> |
| 19 |
| 20 #include <string> |
| 21 |
| 22 #include "base/macros.h" |
| 23 #include "util/file/file_reader.h" |
| 24 |
| 25 namespace crashpad { |
| 26 |
| 27 //! \brief Reads a file one field or line at a time. |
| 28 //! |
| 29 //! The file is interpreted as a series of fields separated by delimiter |
| 30 //! characters. When the delimiter character is the newline character |
| 31 //! (<code>'\\n'</code>), the file is interpreted as a series of lines. |
| 32 //! |
| 33 //! It is safe to mix GetDelim() and GetLine() calls, if appropriate for the |
| 34 //! format being interpreted. |
| 35 //! |
| 36 //! This is a replacement for the standard library’s `getdelim()` and |
| 37 //! `getline()` functions, adapted to work with FileReaderInterface objects |
| 38 //! instead of `FILE*` streams. |
| 39 class DelimitedFileReader { |
| 40 public: |
| 41 //! \brief The result of a GetDelim() or GetLine() call. |
| 42 enum class Result { |
| 43 //! \brief An error occurred, and a message was logged. |
| 44 kError = -1, |
| 45 |
| 46 //! \brief A field or line was read from the file. |
| 47 kSuccess, |
| 48 |
| 49 //! \brief The end of the file was encountered. |
| 50 kEndOfFile, |
| 51 }; |
| 52 |
| 53 explicit DelimitedFileReader(FileReaderInterface* file_reader); |
| 54 ~DelimitedFileReader(); |
| 55 |
| 56 //! \brief Reads a single field from the file. |
| 57 //! |
| 58 //! \param[in] delimiter The delimiter character that terminates the field. |
| 59 //! It is safe to call this method multiple times while changing the value |
| 60 //! of this parameter, if appropriate for the format being interpreted. |
| 61 //! \param[out] field The field read from the file. This parameter will |
| 62 //! include the field’s terminating delimiter character unless the field |
| 63 //! was at the end of the file and was read without such a character. |
| 64 //! This parameter will not be empty. |
| 65 //! |
| 66 //! \return a #Result value. \a field is only valid when Result::kSuccess is |
| 67 //! returned. |
| 68 Result GetDelim(char delimiter, std::string* field); |
| 69 |
| 70 //! \brief Reads a single line from the file. |
| 71 //! |
| 72 //! \param[out] line The line read from the file. This parameter will include |
| 73 //! the line terminating delimiter character unless the line was at the |
| 74 //! end of the file and was read without such a character. This parameter |
| 75 //! will not be empty. |
| 76 //! |
| 77 //! \return a #Result value. \a line is only valid when Result::kSuccess is |
| 78 //! returned. |
| 79 Result GetLine(std::string* line); |
| 80 |
| 81 private: |
| 82 char buf_[4096]; |
| 83 FileReaderInterface* file_reader_; // weak |
| 84 uint16_t buf_pos_; // Index into buf_ of the start of the next field. |
| 85 uint16_t buf_len_; // The size of buf_ that’s been filled. |
| 86 bool eof_; // Caches the EOF signal when detected following a partial field. |
| 87 |
| 88 DISALLOW_COPY_AND_ASSIGN(DelimitedFileReader); |
| 89 }; |
| 90 |
| 91 } // namespace crashpad |
| 92 |
| 93 #endif // CRASHPAD_UTIL_FILE_DELIMITED_FILE_READER_H_ |
OLD | NEW |