nis-util  1.0.D108
lib/space/netmasks/slurp.cc
Go to the documentation of this file.
00001 //
00002 // nis-util - NIS Administration Utilities
00003 // Copyright (C) 2002, 2008, 2009, 2011, 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
00016 // along with this program. If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 
00019 #include <lib/space/netmasks/slurp.h>
00020 #include <lib/net_utils.h>
00021 
00022 
00023 space_netmasks_slurp::~space_netmasks_slurp()
00024 {
00025 }
00026 
00027 
00028 space_netmasks_slurp::space_netmasks_slurp(const rcstring &a_filename) :
00029     space_netmasks(a_filename)
00030 {
00031 }
00032 
00033 
00034 space_netmasks_slurp::pointer
00035 space_netmasks_slurp::create(const rcstring &a_filename)
00036 {
00037     return pointer(new space_netmasks_slurp(a_filename));
00038 }
00039 
00040 
00041 void
00042 space_netmasks_slurp::process(const record::pointer &rp)
00043 {
00044     by_addr_t::iterator bat = by_addr.find(rp->get_number());
00045     if (bat != by_addr.end())
00046     {
00047         error
00048         (
00049             rp->get_source_location(),
00050             "duplicate %s network number...",
00051             rp->get_number().quote_c().c_str()
00052         );
00053         error
00054         (
00055             bat->second->get_source_location(),
00056             "... here is the first %s network number",
00057             bat->second->get_number().quote_c().c_str()
00058         );
00059     }
00060     else
00061     {
00062         assert(rp->get_number() == canonical_network_address(rp->get_number()));
00063         by_addr.insert(by_addr_t::value_type(rp->get_number(), rp));
00064     }
00065 }
00066 
00067 
00068 void
00069 space_netmasks_slurp::read_and_process(void)
00070 {
00071     for (;;)
00072     {
00073         record::pointer rp = get();
00074         if (!rp)
00075             break;
00076         process(rp);
00077     }
00078     close();
00079 }
00080 
00081 
00082 space_netmasks::record::pointer
00083 space_netmasks_slurp::query_by_address(const rcstring &addr)
00084     const
00085 {
00086     by_addr_t::const_iterator it = by_addr.find(addr);
00087     if (it != by_addr.end())
00088         return it->second;
00089 
00090     // let's assume they didn't use canonical form
00091     rcstring addr2 = canonical_network_address(addr);
00092     if (addr2 != addr)
00093     {
00094         it = by_addr.find(addr2);
00095         if (it != by_addr.end())
00096             return it->second;
00097     }
00098 
00099     // nada
00100     return record::pointer();
00101 }
00102 
00103 
00104 // vim: set ts=8 sw=4 et :