Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

DbmNameFilter.cxx

Go to the documentation of this file.
00001 
00002 // DbmNameFilter                                                      //
00003 //                                                                    //
00004 // Package: Dbm (Database Maintenance).                               //
00005 //                                                                    //
00006 // N. West 09/2002                                                    //
00007 //                                                                    //
00008 // Concept: Utility to manage a list of names as a filter.            //
00009 //                                                                    //
00010 // Purpose: To filter table names for export commands.                //
00011 //                                                                    //
00013 
00014 #include <ctype.h>
00015 #include <fstream>
00016 
00017 #include "DatabaseMaintenance/DbmNameFilter.h"
00018 #include "LeakChecker/Lea.h"
00019 #include "MessageService/MsgService.h"
00020 #include "Util/UtilString.h"
00021 
00022 ClassImp(DbmNameFilter)
00023 
00024 
00025 //   Definition of static data members
00026 //   *********************************
00027 
00028 CVSID("$Id: DbmNameFilter.cxx,v 1.3 2004/03/24 12:57:28 west Exp $");
00029 
00030 // Definition of member functions (alphabetical order)
00031 // ***************************************************
00032 
00033 
00034 //.....................................................................
00035 
00036 DbmNameFilter::DbmNameFilter(const std::string& nameList)
00037 {
00038 //
00039 //
00040 //  Purpose:  Default constructor
00041 //
00042 //  Arguments
00043 //    nameList  in    Comma separated list of names (see Specification)
00044 //
00045 //  Contact:   N. West
00046 //
00047 //  Specification:-
00048 //  =============
00049 //
00050 //  o  Create a DbmNameFilter from a comma separated list of names.
00051 //     Each name is of the form:-
00052 //     
00053 //       [!][name-string][*]
00054 //     
00055 //     !  Makes the name a veto.
00056 //     *  Is a wildcard match.  Only permitted as last character.
00057 
00058 
00059   LEA_CTOR    //Leak Checker
00060 
00061   MSG("Dbm", Msg::kVerbose) << "Creating DbmNameFilter" << endl;
00062 
00063   bool ok = true;
00064 
00065   // Split list of names and process each.
00066   std::vector<std::string> names;
00067   UtilString::StringTok(names, nameList,",");
00068 
00069   std::vector<std::string>::iterator itr    = names.begin();
00070   std::vector<std::string>::iterator itrEnd = names.end();
00071   for (; itr != itrEnd; ++itr) {
00072     std::string& name = *itr;
00073     if ( name.size() == 0 ) {
00074       ok = false;
00075       MSG("Dbm", Msg::kError) << "List contains an empty element" 
00076                               << endl;
00077       continue;
00078     }
00079 
00080     // Check for veto name.
00081     bool type = true;
00082     if ( name[0] == '!' ) {
00083       type = false;
00084       name.erase(0,1);
00085     }
00086 
00087     // Check for illegal characters - must only be alphanumeric or _
00088     // with possible trailing *.
00089     int loc    = 0;
00090     int locMax =  name.size() -1;
00091     while (    loc <= locMax 
00092             && ( isalnum(name[loc]) ||  name[loc] == '_' )
00093            ) ++loc;
00094     if ( loc < locMax || ( loc == locMax & name[loc] != '*' ) ) {
00095       ok = false;
00096       MSG("Dbm", Msg::kError) << "List contains an illegal element: " 
00097                               << name << endl;
00098       continue;
00099     }
00100 
00101     // Record in list.
00102     fNameList[name] = type;
00103   }
00104 
00105   // Clear list if any error.
00106   if (! ok ) fNameList.clear();
00107 
00108 }
00109 
00110 //.....................................................................
00111 
00112 DbmNameFilter::~DbmNameFilter() {
00113 //
00114 //
00115 //  Purpose: Destructor
00116 //
00117 //  Contact:   N. West
00118 //
00119 //  Specification:-
00120 //  =============
00121 //
00122 //  o  Destroy DbmNameFilter.
00123 
00124 
00125   LEA_DTOR    //Leak Checker
00126 
00127   MSG("Dbm", Msg::kVerbose) << "Destroying DbmNameFilter" << endl;
00128 
00129 }
00130 
00131 //.....................................................................
00132 
00133 Int_t DbmNameFilter::BestMatch(const std::string& name,
00134                                 Bool_t type) const {
00135 //
00136 //
00137 //  Purpose:  Return the best (longest) match of type.
00138 //
00139 //  Arguments: 
00140 //    name      in    The name to be matched.
00141 //    type      in    The type of match (false for veto).
00142 //    
00143 //
00144 //  Return:    The length of the longest match.
00145 //
00146 //  Contact:   N. West
00147 //
00148 //  Specification:-
00149 //  =============
00150 //
00151 //  o Compare supplied name to the selected type in the namelist
00152 //    and return the length of the longest match.
00153 
00154 //  Program Notes:-
00155 //  =============
00156 
00157 //  Search the map backwards so that a longer name is found before
00158 //  a wildcard match.
00159 
00160   typedef std::map<const std::string,
00161                    Bool_t>::const_reverse_iterator itr_t;
00162   itr_t itr    = fNameList.rbegin(); 
00163   itr_t itrEnd = fNameList.rend();
00164 
00165   for (; itr != itrEnd; ++itr)
00166     if (     itr->second == type
00167          && UtilString::cmp_wildcard(name,itr->first) == 0
00168        ) return itr->first.size();
00169   return 0;
00170 
00171 }
00172 //.....................................................................
00173 
00174 Bool_t DbmNameFilter::Test(const std::string& name) const {
00175 //
00176 //
00177 //  Purpose:  Compare name to filter.
00178 //
00179 //  Arguments: 
00180 //    name    in    Name to be compared.
00181 //
00182 //  Return:    true if name passes filter.
00183 //
00184 //  Contact:   N. West
00185 
00186   //  See if the best match for the accept list is better 
00187   // (i.e. longer) than the veto list.  In the case of a tie,
00188   // the veto wins.
00189 
00190   return this->BestMatch(name,true) > this->BestMatch(name,false);
00191 
00192 }
00193 
00194 /*    Template for New Member Function
00195 
00196 //.....................................................................
00197 
00198 DbmNameFilter:: {
00199 //
00200 //
00201 //  Purpose:  
00202 //
00203 //  Arguments: 
00204 //    xxxxxxxxx    in    yyyyyy
00205 //
00206 //  Return:    
00207 //
00208 //  Contact:   N. West
00209 //
00210 //  Specification:-
00211 //  =============
00212 //
00213 //  o 
00214 
00215 //  Program Notes:-
00216 //  =============
00217 
00218 //  None.
00219 
00220 
00221 }
00222 
00223 */
00224 

Generated on Mon Feb 15 11:06:35 2010 for loon by  doxygen 1.3.9.1