nis-util  1.0.D108
lib/configuration/item/integer.cc
Go to the documentation of this file.
00001 //
00002 // nis-util - NIS Administration Utilities
00003 // Copyright (C) 2012 Peter Miller
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 2 of the License, or (at
00008 // your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License along
00016 // with this program. If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 
00019 #include <libexplain/output.h>
00020 
00021 #include <lib/configuration/item/integer.h>
00022 
00023 
00024 configuration_item_integer::~configuration_item_integer()
00025 {
00026 }
00027 
00028 
00029 configuration_item_integer::configuration_item_integer(
00030     const source_location &a_locn,
00031     level_t a_level,
00032     const rcstring &a_name,
00033     long a_value
00034 ) :
00035     configuration_item(a_locn, a_level, a_name),
00036     value(a_value)
00037 {
00038 }
00039 
00040 
00041 configuration_item_integer::pointer
00042 configuration_item_integer::create(const source_location &a_locn,
00043     level_t a_level, const rcstring &a_name, long a_value)
00044 {
00045     return
00046         pointer
00047         (
00048             new configuration_item_integer(a_locn, a_level, a_name, a_value)
00049         );
00050 }
00051 
00052 
00053 bool
00054 configuration_item_integer::candidate(const rcstring &ptext, long &pvalue)
00055 {
00056     const char *s = ptext.c_str();
00057     char *ep = 0;
00058     pvalue = strtol(s, &ep, 0);
00059     return (ep != s && ep && !*ep);
00060 }
00061 
00062 
00063 configuration_item_integer::pointer
00064 configuration_item_integer::create_if_candidate(const source_location &a_locn,
00065     level_t a_level, const rcstring &a_name, const rcstring &pvalue)
00066 {
00067     long a_value = false;
00068     if (!candidate(pvalue, a_value))
00069         return pointer();
00070     return create(a_locn, a_level, a_name, a_value);
00071 }
00072 
00073 
00074 rcstring
00075 configuration_item_integer::get_string_value(void)
00076     const
00077 {
00078     // Don't complain, needed for the configuration_item::print method.
00079     return rcstring::format("%ld", value);
00080 }
00081 
00082 
00083 long
00084 configuration_item_integer::get_long_value(void)
00085     const
00086 {
00087     return value;
00088 }
00089 
00090 
00091 bool
00092 configuration_item_integer::get_bool_value(void)
00093     const
00094 {
00095     explain_output_error_and_die
00096     (
00097         "%s: item %s: integer value \"%ld\" supplied, boolean value required",
00098         get_source_location().get_both().c_str(),
00099         get_name().quote_c().c_str(),
00100         value
00101     );
00102     return (value != 0);
00103 }
00104 
00105 
00106 // vim: set ts=8 sw=4 et :