nis-util
1.0.D108
|
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 <lib/space/automap/functor/save.h> 00020 #include <lib/space/automap/functor/tee.h> 00021 #include <lib/output/file.h> 00022 #include <lib/space/automap.h> 00023 #include <lib/space/hosts/slurp.h> 00024 00025 00026 space_automap::~space_automap() 00027 { 00028 } 00029 00030 00031 space_automap::space_automap(const rcstring &a_filename) : 00032 space(a_filename) 00033 { 00034 discard_blank_lines(); 00035 discard_comments(); 00036 discard_backslash_newline(); 00037 } 00038 00039 00040 space_automap::pointer 00041 space_automap::create(const rcstring &filename) 00042 { 00043 return pointer(new space_automap(filename)); 00044 } 00045 00046 00047 void 00048 space_automap::read_and_process(void) 00049 { 00050 space_automap_functor_save save(this); 00051 parse(save, NULL); 00052 } 00053 00054 00055 void 00056 space_automap::read_and_process(space_automap_functor &func, 00057 const space_hosts_slurp *hosts) 00058 { 00059 space_automap_functor_save save(this); 00060 space_automap_functor_tee chimera(save, func); 00061 parse(chimera, hosts); 00062 } 00063 00064 00065 void 00066 space_automap::walk(space_automap_functor &func) 00067 { 00068 for 00069 ( 00070 content_t::const_iterator it = content.begin(); 00071 it != content.end(); 00072 ++it 00073 ) 00074 { 00075 func(it->second); 00076 } 00077 } 00078 00079 00080 void 00081 space_automap::insert(const space_automap_row::pointer &rp) 00082 { 00083 rcstring key = rp->get_mount_point().get_mount_point(); 00084 space_automap_row::pointer qr = query(key); 00085 if (qr) 00086 { 00087 error 00088 ( 00089 rp->get_mount_point().get_source_location(), 00090 "duplicate %s definition...", 00091 key.quote_c().c_str() 00092 ); 00093 error 00094 ( 00095 qr->get_mount_point().get_source_location(), 00096 "...here is the first %s definition", 00097 key.quote_c().c_str() 00098 ); 00099 } 00100 else 00101 { 00102 content.insert(content_t::value_type(key, rp)); 00103 } 00104 } 00105 00106 00107 space_automap_row::pointer 00108 space_automap::query(const rcstring &key) 00109 const 00110 { 00111 content_t::const_iterator it = content.find(key); 00112 if (it == content.end()) 00113 return space_automap_row::pointer(); 00114 return it->second; 00115 } 00116 00117 00118 void 00119 space_automap::insert_sub(const space_automap_row::pointer &rp) 00120 { 00121 rcstring key = rp->get_mount_point().get_mount_point(); 00122 space_automap_row::pointer qr = query_sub(key); 00123 if (qr) 00124 { 00125 error 00126 ( 00127 rp->get_mount_point().get_source_location(), 00128 "duplicate %s definition...", 00129 key.quote_c().c_str() 00130 ); 00131 error 00132 ( 00133 qr->get_mount_point().get_source_location(), 00134 "...here is the first %s definition", 00135 key.quote_c().c_str() 00136 ); 00137 } 00138 else 00139 { 00140 content_sub.insert(content_t::value_type(key, rp)); 00141 } 00142 } 00143 00144 00145 space_automap_row::pointer 00146 space_automap::query_sub(const rcstring &key) 00147 const 00148 { 00149 content_t::const_iterator it = content_sub.find(key); 00150 if (it == content_sub.end()) 00151 return space_automap_row::pointer(); 00152 return it->second; 00153 } 00154 00155 00156 void 00157 space_automap::print(void) 00158 const 00159 { 00160 print(get_file_name()); 00161 } 00162 00163 00164 void 00165 space_automap::print(const rcstring &a_filename) 00166 const 00167 { 00168 output::pointer op = output_file::create(a_filename); 00169 // FIXME: use a functor 00170 for 00171 ( 00172 content_t::const_iterator it = content.begin(); 00173 it != content.end(); 00174 ++it 00175 ) 00176 { 00177 space_automap_row::pointer rp = it->second; 00178 rp->print(op); 00179 } 00180 } 00181 00182 00183 bool 00184 space_automap::empty(void) 00185 const 00186 { 00187 return content.empty(); 00188 } 00189 00190 00191 space_automap::keys_t 00192 space_automap::get_keys(void) 00193 const 00194 { 00195 keys_t result; 00196 for 00197 ( 00198 content_t::const_iterator it = content.begin(); 00199 it != content.end(); 00200 ++it 00201 ) 00202 { 00203 space_automap_row::pointer rp = it->second; 00204 result.push_back(rp->get_mount_point()); 00205 } 00206 return result; 00207 } 00208 00209 00210 // vim: set ts=8 sw=4 et :