nis-util  1.0.D108
lib/space/auto_master/functor.cc
Go to the documentation of this file.
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/ac/string.h>
00020 #include <lib/ac/unistd.h>
00021 
00022 #include <lib/path_join.h>
00023 #include <lib/space/auto_master/functor.h>
00024 
00025 space_auto_master_functor::string_list_t
00026     space_auto_master_functor::include_search_path;
00027 space_auto_master_functor::string_list_t space_auto_master_functor::suffixes;
00028 
00029 
00030 space_auto_master_functor::~space_auto_master_functor()
00031 {
00032 }
00033 
00034 
00035 space_auto_master_functor::space_auto_master_functor()
00036 {
00037 }
00038 
00039 
00040 void
00041 space_auto_master_functor::add_include_search_path(const rcstring &dir)
00042 {
00043     include_search_path.push_back(dir);
00044 }
00045 
00046 
00047 void
00048 space_auto_master_functor::add_suffix(const rcstring &sfx)
00049 {
00050     const char *base = strrchr(sfx.c_str(), '/');
00051     if (base)
00052         ++base;
00053     else
00054         base = sfx.c_str();
00055     const char *cp = strrchr(base, '.');
00056     if (cp)
00057         suffixes.push_back(rcstring(cp + 1));
00058     else
00059         suffixes.push_back(base);
00060 }
00061 
00062 
00063 rcstring
00064 space_auto_master_functor::find_file(const rcstring &fn)
00065 {
00066     if (include_search_path.empty())
00067         include_search_path.push_back(".");
00068     for
00069     (
00070         string_list_t::const_iterator it = include_search_path.begin();
00071         it != include_search_path.end();
00072         ++it
00073     )
00074     {
00075         rcstring incl = *it;
00076         for
00077         (
00078             string_list_t::const_iterator sit = suffixes.begin();
00079             sit != suffixes.end();
00080             ++sit
00081         )
00082         {
00083             rcstring suf = *sit;
00084             rcstring s = path_join(incl, fn + "." + suf);
00085             if (access(s.c_str(), F_OK) == 0)
00086                 return s;
00087         }
00088 
00089         //
00090         // try no suffix last
00091         //
00092         rcstring s = path_join(incl, fn);
00093         if (access(s.c_str(), F_OK) == 0)
00094             return s;
00095     }
00096 
00097     //
00098     // File not found.
00099     // Use the first include location and the first suffix.
00100     //
00101     rcstring fn2 = fn;
00102     if (!suffixes.empty())
00103         fn2 += "." + suffixes.front();
00104     return path_join(include_search_path.front(), fn2);
00105 }
00106 
00107 
00108 // vim: set ts=8 sw=4 et :