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 2 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, write to the Free Software
00015  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00016  * MA  02111-1307  USA
00017  *
00018  *********************************************************************/
00019 
00020 #ifndef SBUILD_KEYFILE_H
00021 #define SBUILD_KEYFILE_H
00022 
00023 #include <sbuild/sbuild-basic-keyfile.h>
00024 
00025 namespace sbuild
00026 {
00027 
00032   struct keyfile_traits
00033   {
00035     typedef std::string group_name_type;
00036 
00038     typedef std::string key_type;
00039 
00041     typedef std::string value_type;
00042 
00044     typedef std::string comment_type;
00045 
00047     typedef unsigned int size_type;
00048   };
00049 
00050   template <typename K>
00051   struct keyfile_parser : public basic_keyfile_parser<K>
00052   {
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     virtual void
00070     parse_line (std::string const& line)
00071     {
00072       if (comment_set == true)
00073         {
00074           comment.clear();
00075           comment_set = false;
00076         }
00077       if (group_set == true)
00078         {
00079           // The group isn't cleared
00080           group_set = false;
00081         }
00082       if (key_set == true)
00083         {
00084           key.clear();
00085           key_set = false;
00086         }
00087       if (value_set == true)
00088         {
00089           value.clear();
00090           value_set = false;
00091         }
00092 
00093       if (line.length() == 0)
00094         {
00095           // Empty line; do nothing.
00096         }
00097       else if (line[0] == '#') // Comment line
00098         {
00099           if (!comment.empty())
00100             comment += '\n';
00101           comment += line.substr(1);
00102         }
00103       else if (line[0] == '[') // Group
00104         {
00105           std::string::size_type fpos = line.find_first_of(']');
00106           std::string::size_type lpos = line.find_last_of(']');
00107           if (fpos == std::string::npos || lpos == std::string::npos ||
00108               fpos != lpos)
00109             throw error(line_number, keyfile_base::INVALID_GROUP, line);
00110           group = line.substr(1, fpos - 1);
00111 
00112           if (group.length() == 0)
00113             throw error(line_number, keyfile_base::INVALID_GROUP, line);
00114 
00115           comment_set = true;
00116           group_set = true;
00117         }
00118       else // Item
00119         {
00120           std::string::size_type pos = line.find_first_of('=');
00121           if (pos == std::string::npos)
00122             throw error(line_number, keyfile_base::INVALID_LINE, line);
00123           if (pos == 0)
00124             throw error(line_number, keyfile_base::NO_KEY, line);
00125           key = line.substr(0, pos);
00126           if (pos == line.length() - 1)
00127             value = "";
00128           else
00129             value = line.substr(pos + 1);
00130 
00131           // No group specified
00132           if (group.empty())
00133             throw error(line_number, keyfile_base::NO_GROUP, line);
00134 
00135           comment_set = true;
00136           key_set = true;
00137           value_set = true;
00138         }
00139 
00140       basic_keyfile_parser<K>::parse_line(line);
00141     }
00142   };
00143 
00149   typedef basic_keyfile<keyfile_traits, keyfile_parser<keyfile_traits> > keyfile;
00150 
00151 }
00152 
00153 #endif /* SBUILD_KEYFILE_H */
00154 
00155 /*
00156  * Local Variables:
00157  * mode:C++
00158  * End:
00159  */

Generated on Sun Jul 8 21:23:22 2007 for sbuild by  doxygen 1.5.2