nis-util  1.0.D108
nis-util-netgroup/map.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 <algorithm>
00020 
00021 #include <lib/output/file.h>
00022 #include <lib/space/netgroup/slurp.h>
00023 
00024 #include <nis-util-netgroup/map.h>
00025 
00026 
00027 void
00028 map_by_name(const rcstring &ifn, const rcstring &ofn)
00029 {
00030     output::pointer ofp = output_file::create(ofn);
00031     space_netgroup::pointer ifp = space_netgroup::create(ifn);
00032     for (;;)
00033     {
00034         space_netgroup::record::pointer rp = ifp->get();
00035         if (!rp)
00036             break;
00037         ofp->put(rp->get_name());
00038         ofp->put(' ');
00039         ofp->put(rp->representation());
00040         ofp->put('\n');
00041     }
00042 }
00043 
00044 
00045 void
00046 map_by_host(const rcstring &ifn, const rcstring &ofn)
00047 {
00048     //
00049     // Read the input file.
00050     //
00051     space_netgroup_slurp::pointer ifp = space_netgroup_slurp::create(ifn);
00052     ifp->read_and_process();
00053 
00054     //
00055     // Open the output file.
00056     //
00057     output::pointer ofp = output_file::create(ofn);
00058 
00059     //
00060     // Spew out the inverted index.
00061     // (Because we use std::map, the keys are already sorted.
00062     // sorting is important for stable automated test results.)
00063     //
00064     space_netgroup_slurp::name_list_t keys = ifp->keys_by_host();
00065     // std::sort(keys.begin(), keys.end());
00066     for
00067     (
00068         space_netgroup_slurp::name_list_t::const_iterator kit = keys.begin();
00069         kit != keys.end();
00070         ++kit
00071     )
00072     {
00073         rcstring key = *kit;
00074         space_netgroup_slurp::name_list_t nlp = ifp->query_by_host(key);
00075         if (nlp.empty())
00076             continue;
00077         // std::sort(nlp->begin(), nlp->end());
00078         ofp->put(key);
00079         ofp->put(".* ");
00080         for
00081         (
00082             space_netgroup_slurp::name_list_t::const_iterator nit = nlp.begin();
00083             nit != nlp.end();
00084             ++nit
00085         )
00086         {
00087             if (nit != nlp.begin())
00088                 ofp->put(',');
00089             ofp->put(*nit);
00090         }
00091         // FIXME: check NIS row size?
00092         ofp->put('\n');
00093     }
00094 }
00095 
00096 
00097 void
00098 map_by_user(const rcstring &ifn, const rcstring &ofn)
00099 {
00100     //
00101     // Read the input file.
00102     //
00103     space_netgroup_slurp::pointer ifp = space_netgroup_slurp::create(ifn);
00104     ifp->read_and_process();
00105 
00106     //
00107     // Open the output file.
00108     //
00109     output::pointer ofp = output_file::create(ofn);
00110 
00111     //
00112     // Spew out the inverted index.
00113     // (The sort is to give more stable results, for testing purposes,
00114     // but by using std::map no sort is required.)
00115     //
00116     space_netgroup_slurp::name_list_t keys = ifp->keys_by_user();
00117     // std::sort(keys.begin(), keys.end());
00118     for
00119     (
00120         space_netgroup_slurp::name_list_t::const_iterator kit = keys.begin();
00121         kit != keys.end();
00122         ++kit
00123     )
00124     {
00125         rcstring key = *kit;
00126         space_netgroup_slurp::name_list_t nlp = ifp->query_by_user(key);
00127         if (nlp.empty())
00128             continue;
00129         // std::sort(nlp.begin(), nlp.end());
00130         ofp->put(key);
00131         ofp->put(".* ");
00132         for
00133         (
00134             space_netgroup_slurp::name_list_t::const_iterator nit = nlp.begin();
00135             nit != nlp.end();
00136             ++nit
00137         )
00138         {
00139             if (nit != nlp.begin())
00140                 ofp->put(',');
00141             ofp->put(*nit);
00142         }
00143         // FIXME: check NIS row size?
00144         ofp->put('\n');
00145     }
00146 }
00147 
00148 
00149 // vim: set ts=8 sw=4 et :