nis-util  1.0.D108
lib/space/networks/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/networks/slurp.h>
00020 #include <lib/symtab.h>
00021 
00022 
00023 space_networks_slurp::~space_networks_slurp()
00024 {
00025 }
00026 
00027 
00028 space_networks_slurp::space_networks_slurp(const rcstring &a_filename) :
00029     space_networks(a_filename)
00030 {
00031 }
00032 
00033 
00034 space_networks_slurp::pointer
00035 space_networks_slurp::create(const rcstring &a_filename)
00036 {
00037     return pointer(new space_networks_slurp(a_filename));
00038 }
00039 
00040 
00041 void
00042 space_networks_slurp::process(const record::pointer &rp)
00043 {
00044     if (rp->representation().size() >= 512)
00045     {
00046         error
00047         (
00048             rp->get_source_location(),
00049             "record too long (NIS limits records to 511 bytes, "
00050             "and NIS+ limits records to 1024 bytes"
00051         );
00052     }
00053 
00054     //
00055     // index by name
00056     //
00057     {
00058         by_name_t::iterator it = by_name.find(rp->get_name());
00059         if (it != by_name.end())
00060         {
00061             error
00062             (
00063                 rp->get_source_location(),
00064                 "duplicate %s network name...",
00065                 rp->get_name().quote_c().c_str()
00066             );
00067             error
00068             (
00069                 it->second->get_source_location(),
00070                 "... here is the first %s network name",
00071                 rp->get_name().quote_c().c_str()
00072             );
00073         }
00074         else
00075         {
00076             by_name.insert(by_addr_t::value_type(rp->get_name(), rp));
00077         }
00078     }
00079 
00080     //
00081     // index by address
00082     //
00083     {
00084         by_addr_t::iterator it = by_addr.find(rp->get_number());
00085         if (it != by_addr.end())
00086         {
00087             error
00088             (
00089                 rp->get_source_location(),
00090                 "duplicate %s network number...",
00091                 rp->get_number().quote_c().c_str()
00092             );
00093             error
00094             (
00095                 it->second->get_source_location(),
00096                 "... here is the first %s network number",
00097                 it->second->get_number().quote_c().c_str()
00098             );
00099         }
00100         else
00101         {
00102             by_addr.insert(by_addr_t::value_type(rp->get_number(), rp));
00103         }
00104     }
00105 
00106     //
00107     // index the aliases by name, too
00108     //
00109     const record::aliases_t &aliases = rp->get_aliases();
00110     for
00111     (
00112         record::aliases_t::const_iterator ait = aliases.begin();
00113         ait != aliases.end();
00114         ++ait
00115     )
00116     {
00117         by_name_t::iterator it = by_name.find(*ait);
00118         if (it != by_name.end())
00119         {
00120             error
00121             (
00122                 rp->get_source_location(),
00123                 "duplicate %s network name...",
00124                 ait->quote_c().c_str()
00125             );
00126             error
00127             (
00128                 it->second->get_source_location(),
00129                 "... here is the first %s network name",
00130                 ait->quote_c().c_str()
00131             );
00132         }
00133         else
00134         {
00135             by_name.insert(by_addr_t::value_type(*ait, rp));
00136         }
00137     }
00138 }
00139 
00140 
00141 void
00142 space_networks_slurp::read_and_process(void)
00143 {
00144     for (;;)
00145     {
00146         record::pointer rp = get();
00147         if (!rp)
00148             break;
00149         process(rp);
00150     }
00151     close();
00152 }
00153 
00154 
00155 space_networks::record::pointer
00156 space_networks_slurp::query_by_name(const rcstring &name)
00157     const
00158 {
00159     by_name_t::const_iterator it = by_name.find(name);
00160     if (it == by_name.end())
00161         return record::pointer();
00162     return it->second;
00163 }
00164 
00165 
00166 space_networks::record::pointer
00167 space_networks_slurp::query_by_address(const rcstring &addr)
00168     const
00169 {
00170     by_addr_t::const_iterator it = by_addr.find(addr);
00171     if (it == by_addr.end())
00172         return record::pointer();
00173     return it->second;
00174 }
00175 
00176 
00177 // vim: set ts=8 sw=4 et :