1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 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 16 #ifndef BUFFER_SPLITTER_H 17 #define BUFFER_SPLITTER_H 18 19 #include <string> 20 21 /** 22 * Description: Buffer Splitter Class 23 * The BufferSplitter class object splits the buffer content by moving the pointer 24 * Except for the last character, other buffer contents are not modified during the segmentation 25 */ 26 27 class BufferSplitter { 28 public: 29 /* During construction, buf[size-1] will be forcibly modified to '\ 0' 30 The constructor will automatically call the NextLine to initialize the first line of data */ 31 BufferSplitter(const char* buf, int size); 32 ~BufferSplitter()33 ~BufferSplitter() {} 34 /* Find and update the next line header pointer, and line length. Length does not include '\ n' and '\ 0' */ 35 bool NextLine(); 36 37 /* Find and update the NextWord's head pointer and Word length according to the delimiter within the 38 current line range. The length calculation does not include the 'delimiter' */ 39 /* When the current line cannot find a specific 'delimiter', it can 40 be split from the current position according to the new 'delimiter' */ 41 bool NextWord(char delimiter); 42 CurWord()43 const char* CurWord() 44 { 45 return curWord_; 46 } CurWordSize()47 size_t CurWordSize() const 48 { 49 return curWordSize_; 50 } CurLine()51 char* CurLine() 52 { 53 return curLine_; 54 } CurLineSize()55 size_t CurLineSize() const 56 { 57 return curLineSize_; 58 } 59 60 private: 61 char* curWord_ = nullptr; 62 size_t curWordSize_ = 0; 63 char* next_; 64 char* curLine_ = nullptr; 65 size_t curLineSize_ = 0; 66 char* nextLine_; 67 char* end_; 68 }; 69 #endif 70