nis-util  1.0.D108
lib/space/hosts/slurp.cc
Go to the documentation of this file.
00001 //
00002 // nis-util - NIS Administration Utilities
00003 // Copyright (C) 2001, 2003, 2008, 2009, 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/hosts/slurp.h>
00020 #include <lib/symtab.h>
00021 
00022 
00023 space_hosts_slurp::~space_hosts_slurp()
00024 {
00025 }
00026 
00027 
00028 space_hosts_slurp::space_hosts_slurp(const rcstring &a_filename) :
00029     space_hosts(a_filename)
00030 {
00031 }
00032 
00033 
00034 space_hosts_slurp::pointer
00035 space_hosts_slurp::create(const rcstring &a_filename)
00036 {
00037     return pointer(new space_hosts_slurp(a_filename));
00038 }
00039 
00040 
00041 void
00042 space_hosts_slurp::read_and_process(void)
00043 {
00044     for (;;)
00045     {
00046         record::pointer rp = get();
00047         if (!rp)
00048             break;
00049 
00050         // index (and check) by IP address
00051         {
00052             by_addr_t::iterator it = by_addr.find(rp->get_ip_address());
00053             if (it != by_addr.end())
00054             {
00055                 error
00056                 (
00057                     rp->get_source_location(),
00058                     "duplicate %s IP address...",
00059                     rp->get_ip_address().quote_c().c_str()
00060                 );
00061                 error
00062                 (
00063                     it->second->get_source_location(),
00064                     "... here is the first use of the %s IP address",
00065                     it->second->get_ip_address().quote_c().c_str()
00066                 );
00067             }
00068             else
00069             {
00070                 by_addr.insert(by_addr_t::value_type(rp->get_ip_address(), rp));
00071             }
00072         }
00073 
00074         // index (and check) by name
00075         const record::names_t &names = rp->get_names();
00076         for
00077         (
00078             record::names_t::const_iterator nit = names.begin();
00079             nit != names.end();
00080             ++nit
00081         )
00082         {
00083             rcstring name = *nit;
00084             rcstring name_lc = name.downcase();
00085             by_name_t::iterator bnit = by_name.find(name_lc);
00086             if (bnit != by_name.end())
00087             {
00088                 error
00089                 (
00090                     rp->get_source_location(),
00091                     "duplicate %s host name...",
00092                     name.quote_c().c_str()
00093                 );
00094                 error
00095                 (
00096                     bnit->second->get_source_location(),
00097                     "... here is the first use of the %s host name",
00098                     name_lc.quote_c().c_str()
00099                 );
00100             }
00101             else
00102             {
00103                 by_name.insert(by_name_t::value_type(name_lc, rp));
00104             }
00105         }
00106 
00107         //
00108         // Check the line length.
00109         //
00110         if (rp->representation().size() >= 512)
00111         {
00112             error
00113             (
00114                 rp->get_source_location(),
00115                 "line too long (NIS limits records to 511 bytes, and NIS+ "
00116                 "limits records to 1024 bytes)"
00117             );
00118         }
00119     }
00120 }
00121 
00122 
00123 space_hosts::record::pointer
00124 space_hosts_slurp::query_by_name(const rcstring &name)
00125     const
00126 {
00127     by_name_t::const_iterator it = by_name.find(name.downcase());
00128     if (it == by_name.end())
00129         return record::pointer();
00130     return it->second;
00131 }
00132 
00133 
00134 space_hosts::record::pointer
00135 space_hosts_slurp::query_by_address(const rcstring &addr)
00136     const
00137 {
00138     by_addr_t::const_iterator it = by_addr.find(addr);
00139     if (it == by_addr.end())
00140         return record::pointer();
00141     return it->second;
00142 }
00143 
00144 
00145 // vim: set ts=8 sw=4 et :