| Colin Cross | ca7648d | 2010-04-13 19:29:51 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2010 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 |  | 
| Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 17 | #ifndef _INIT_PARSER_H_ | 
 | 18 | #define _INIT_PARSER_H_ | 
| Colin Cross | ca7648d | 2010-04-13 19:29:51 -0700 | [diff] [blame] | 19 |  | 
| Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 20 | #include <map> | 
 | 21 | #include <memory> | 
 | 22 | #include <string> | 
 | 23 | #include <vector> | 
 | 24 |  | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 25 | #include "result.h" | 
 | 26 |  | 
| Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 27 | //  SectionParser is an interface that can parse a given 'section' in init. | 
 | 28 | // | 
 | 29 | //  You can implement up to 4 functions below, with ParseSection() being mandatory. | 
 | 30 | //  The first two function return bool with false indicating a failure and has a std::string* err | 
 | 31 | //  parameter into which an error string can be written.  It will be reported along with the | 
 | 32 | //  filename and line number of where the error occurred. | 
 | 33 | // | 
 | 34 | //  1) bool ParseSection(std::vector<std::string>&& args, const std::string& filename, | 
 | 35 | //                       int line, std::string* err) | 
 | 36 | //    This function is called when a section is first encountered. | 
 | 37 | // | 
 | 38 | //  2) bool ParseLineSection(std::vector<std::string>&& args, int line, std::string* err) | 
 | 39 | //    This function is called on each subsequent line until the next section is encountered. | 
 | 40 | // | 
 | 41 | //  3) bool EndSection() | 
 | 42 | //    This function is called either when a new section is found or at the end of the file. | 
 | 43 | //    It indicates that parsing of the current section is complete and any relevant objects should | 
 | 44 | //    be committed. | 
 | 45 | // | 
 | 46 | //  4) bool EndFile() | 
 | 47 | //    This function is called at the end of the file. | 
 | 48 | //    It indicates that the parsing has completed and any relevant objects should be committed. | 
| Colin Cross | ca7648d | 2010-04-13 19:29:51 -0700 | [diff] [blame] | 49 |  | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 50 | namespace android { | 
 | 51 | namespace init { | 
 | 52 |  | 
| Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 53 | class SectionParser { | 
 | 54 |   public: | 
 | 55 |     virtual ~SectionParser() {} | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 56 |     virtual Result<Success> ParseSection(std::vector<std::string>&& args, | 
 | 57 |                                          const std::string& filename, int line) = 0; | 
 | 58 |     virtual Result<Success> ParseLineSection(std::vector<std::string>&&, int) { return Success(); }; | 
| Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 59 |     virtual void EndSection(){}; | 
 | 60 |     virtual void EndFile(){}; | 
| Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 61 | }; | 
 | 62 |  | 
| Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 63 | class Parser { | 
 | 64 |   public: | 
 | 65 |     //  LineCallback is the type for callbacks that can parse a line starting with a given prefix. | 
 | 66 |     // | 
 | 67 |     //  They take the form of bool Callback(std::vector<std::string>&& args, std::string* err) | 
 | 68 |     // | 
 | 69 |     //  Similar to ParseSection() and ParseLineSection(), this function returns bool with false | 
 | 70 |     //  indicating a failure and has an std::string* err parameter into which an error string can | 
 | 71 |     //  be written. | 
| Tom Cherry | 89bcc85 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 72 |     using LineCallback = std::function<Result<Success>(std::vector<std::string>&&)>; | 
| Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 73 |  | 
 | 74 |     Parser(); | 
 | 75 |  | 
 | 76 |     bool ParseConfig(const std::string& path); | 
 | 77 |     void AddSectionParser(const std::string& name, std::unique_ptr<SectionParser> parser); | 
 | 78 |     void AddSingleLineParser(const std::string& prefix, LineCallback callback); | 
 | 79 |  | 
 | 80 |   private: | 
 | 81 |     void ParseData(const std::string& filename, const std::string& data); | 
 | 82 |     bool ParseConfigFile(const std::string& path); | 
 | 83 |     bool ParseConfigDir(const std::string& path); | 
 | 84 |  | 
 | 85 |     std::map<std::string, std::unique_ptr<SectionParser>> section_parsers_; | 
 | 86 |     std::vector<std::pair<std::string, LineCallback>> line_callbacks_; | 
 | 87 | }; | 
| Colin Cross | ca7648d | 2010-04-13 19:29:51 -0700 | [diff] [blame] | 88 |  | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 89 | }  // namespace init | 
 | 90 | }  // namespace android | 
 | 91 |  | 
| Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 92 | #endif |