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