1 /* 2 * Copyright (c) 2024 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 META_SRC_LOADERS_CSV_PARSER_H 17 #define META_SRC_LOADERS_CSV_PARSER_H 18 19 #include <base/containers/string.h> 20 #include <base/containers/string_view.h> 21 #include <base/containers/vector.h> 22 23 #include <meta/base/namespace.h> 24 25 META_BEGIN_NAMESPACE() 26 27 /** 28 * @brief The CsvParser class implements a simple CSV parser, which 29 * parses a given CSV format string. 30 * The parser supports non-quoted and quoted items, separated 31 * by a configurable delimiter. Also multiline quotes are 32 * supported. 33 */ 34 class CsvParser { 35 public: 36 CsvParser() = delete; 37 explicit CsvParser(BASE_NS::string_view csv, const char delimiter = ','); 38 39 using CsvRow = BASE_NS::vector<BASE_NS::string>; 40 41 /** 42 * @brief GetRow returns the next row from the CSV file. 43 * Regularly the user should call GetRow in a loop until 44 * the function returns false (either parse error or 45 * end of the CSV file). 46 * @param row Row items will be placed here. 47 * @return True if parsing was successful, false otherwide. 48 */ 49 bool GetRow(CsvRow& row); 50 /** 51 * @brief Resets to the beginning of the CSV string. 52 */ 53 void Reset(); 54 55 private: 56 enum State { 57 NO_QUOTE, 58 IN_QUOTE, 59 QUOTED, 60 }; 61 CsvRow ParseRow(); 62 BASE_NS::string_view Trimmed(BASE_NS::string_view sv, State state); 63 64 char delimiter_ {}; 65 BASE_NS::string csv_; 66 size_t pos_ {}; 67 }; 68 69 META_END_NAMESPACE() 70 71 #endif 72