sbuild-keyfile.h

Go to the documentation of this file.
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 
00049   template <typename K>
00050   struct keyfile_parser : public basic_keyfile_parser<K>
00051   {
00052     // Workaround for GCC bug.
00053     typedef keyfile_base::error error;
00054     // This is the correct form, but is not currently supported by
00055     // GCC.  http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14258
00056     // using typename basic_keyfile_parser<K>::error;
00057 
00058     using basic_keyfile_parser<K>::group;
00059     using basic_keyfile_parser<K>::group_set;
00060     using basic_keyfile_parser<K>::key;
00061     using basic_keyfile_parser<K>::key_set;
00062     using basic_keyfile_parser<K>::value;
00063     using basic_keyfile_parser<K>::value_set;
00064     using basic_keyfile_parser<K>::comment;
00065     using basic_keyfile_parser<K>::comment_set;
00066     using basic_keyfile_parser<K>::line_number;
00067 
00068     virtual void
00069     parse_line (std::string const& line)
00070     {
00071       if (comment_set == true)
00072         {
00073           comment.clear();
00074           comment_set = false;
00075         }
00076       if (group_set == true)
00077         {
00078           // The group isn't cleared
00079           group_set = false;
00080         }
00081       if (key_set == true)
00082         {
00083           key.clear();
00084           key_set = false;
00085         }
00086       if (value_set == true)
00087         {
00088           value.clear();
00089           value_set = false;
00090         }
00091 
00092       if (line.length() == 0)
00093         {
00094           // Empty line; do nothing.
00095         }
00096       else if (line[0] == '#') // Comment line
00097         {
00098           if (!comment.empty())
00099             comment += '\n';
00100           comment += line.substr(1);
00101         }
00102       else if (line[0] == '[') // Group
00103         {
00104           std::string::size_type fpos = line.find_first_of(']');
00105           std::string::size_type lpos = line.find_last_of(']');
00106           if (fpos == std::string::npos || lpos == std::string::npos ||
00107               fpos != lpos)
00108             throw error(line_number, keyfile_base::INVALID_GROUP, line);
00109           group = line.substr(1, fpos - 1);
00110 
00111           if (group.length() == 0)
00112             throw error(line_number, keyfile_base::INVALID_GROUP, line);
00113 
00114           comment_set = true;
00115           group_set = true;
00116         }
00117       else // Item
00118         {
00119           std::string::size_type pos = line.find_first_of('=');
00120           if (pos == std::string::npos)
00121             throw error(line_number, keyfile_base::INVALID_LINE, line);
00122           if (pos == 0)
00123             throw error(line_number, keyfile_base::NO_KEY, line);
00124           key = line.substr(0, pos);
00125           if (pos == line.length() - 1)
00126             value = "";
00127           else
00128             value = line.substr(pos + 1);
00129 
00130           // No group specified
00131           if (group.empty())
00132             throw error(line_number, keyfile_base::NO_GROUP, line);
00133 
00134           comment_set = true;
00135           key_set = true;
00136           value_set = true;
00137         }
00138 
00139       basic_keyfile_parser<K>::parse_line(line);
00140     }
00141   };
00142 
00148   typedef basic_keyfile<keyfile_traits, keyfile_parser<keyfile_traits> > keyfile;
00149 
00150 }
00151 
00152 #endif /* SBUILD_KEYFILE_H */
00153 
00154 /*
00155  * Local Variables:
00156  * mode:C++
00157  * End:
00158  */

Generated on Sun May 17 18:38:55 2009 for sbuild by  doxygen 1.5.9