sbuild
1.4.26
|
00001 /* Copyright © 2005-2007 Roger Leigh <rleigh@debian.org> 00002 * 00003 * schroot is free software: you can redistribute it and/or modify it 00004 * under the terms of the GNU General Public License as published by 00005 * the Free Software Foundation, either version 3 of the License, or 00006 * (at your option) any later version. 00007 * 00008 * schroot is distributed in the hope that it will be useful, but 00009 * WITHOUT ANY WARRANTY; without even the implied warranty of 00010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 * General Public License for more details. 00012 * 00013 * You should have received a copy of the GNU General Public License 00014 * along with this program. If not, see 00015 * <http://www.gnu.org/licenses/>. 00016 * 00017 *********************************************************************/ 00018 00019 #ifndef SBUILD_KEYFILE_H 00020 #define SBUILD_KEYFILE_H 00021 00022 #include <sbuild/sbuild-basic-keyfile.h> 00023 00024 namespace sbuild 00025 { 00026 00031 struct keyfile_traits 00032 { 00034 typedef std::string group_name_type; 00035 00037 typedef std::string key_type; 00038 00040 typedef std::string value_type; 00041 00043 typedef std::string comment_type; 00044 00046 typedef unsigned int size_type; 00047 }; 00048 00052 template <typename K> 00053 class keyfile_parser : public basic_keyfile_parser<K> 00054 { 00055 public: 00056 // Workaround for GCC bug. 00057 typedef keyfile_base::error error; 00058 // This is the correct form, but is not currently supported by 00059 // GCC. http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14258 00060 // using typename basic_keyfile_parser<K>::error; 00061 00062 using basic_keyfile_parser<K>::group; 00063 using basic_keyfile_parser<K>::group_set; 00064 using basic_keyfile_parser<K>::key; 00065 using basic_keyfile_parser<K>::key_set; 00066 using basic_keyfile_parser<K>::value; 00067 using basic_keyfile_parser<K>::value_set; 00068 using basic_keyfile_parser<K>::comment; 00069 using basic_keyfile_parser<K>::comment_set; 00070 using basic_keyfile_parser<K>::line_number; 00071 00072 keyfile_parser(): 00073 basic_keyfile_parser<K>() 00074 {} 00075 00076 virtual ~keyfile_parser() 00077 {} 00078 00079 virtual void 00080 parse_line (std::string const& line) 00081 { 00082 if (comment_set == true) 00083 { 00084 comment.clear(); 00085 comment_set = false; 00086 } 00087 if (group_set == true) 00088 { 00089 // The group isn't cleared 00090 group_set = false; 00091 } 00092 if (key_set == true) 00093 { 00094 key.clear(); 00095 key_set = false; 00096 } 00097 if (value_set == true) 00098 { 00099 value.clear(); 00100 value_set = false; 00101 } 00102 00103 if (line.length() == 0) 00104 { 00105 // Empty line; do nothing. 00106 } 00107 else if (line[0] == '#') // Comment line 00108 { 00109 if (!comment.empty()) 00110 comment += '\n'; 00111 comment += line.substr(1); 00112 } 00113 else if (line[0] == '[') // Group 00114 { 00115 std::string::size_type fpos = line.find_first_of(']'); 00116 std::string::size_type lpos = line.find_last_of(']'); 00117 if (fpos == std::string::npos || lpos == std::string::npos || 00118 fpos != lpos) 00119 throw error(line_number, keyfile_base::INVALID_GROUP, line); 00120 group = line.substr(1, fpos - 1); 00121 00122 if (group.length() == 0) 00123 throw error(line_number, keyfile_base::INVALID_GROUP, line); 00124 00125 comment_set = true; 00126 group_set = true; 00127 } 00128 else // Item 00129 { 00130 std::string::size_type pos = line.find_first_of('='); 00131 if (pos == std::string::npos) 00132 throw error(line_number, keyfile_base::INVALID_LINE, line); 00133 if (pos == 0) 00134 throw error(line_number, keyfile_base::NO_KEY, line); 00135 key = line.substr(0, pos); 00136 if (pos == line.length() - 1) 00137 value = ""; 00138 else 00139 value = line.substr(pos + 1); 00140 00141 // No group specified 00142 if (group.empty()) 00143 throw error(line_number, keyfile_base::NO_GROUP, line); 00144 00145 comment_set = true; 00146 key_set = true; 00147 value_set = true; 00148 } 00149 00150 basic_keyfile_parser<K>::parse_line(line); 00151 } 00152 }; 00153 00159 typedef basic_keyfile<keyfile_traits, keyfile_parser<keyfile_traits> > keyfile; 00160 00161 } 00162 00163 #endif /* SBUILD_KEYFILE_H */ 00164 00165 /* 00166 * Local Variables: 00167 * mode:C++ 00168 * End: 00169 */