Changeset 13078 for trunk


Ignore:
Timestamp:
03/13/12 10:48:55 (13 years ago)
Author:
tbretz
Message:
Added th possibility to use a real regular expression.
Location:
trunk/Mars/mbase
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Mars/mbase/MDirIter.cc

    r9441 r13078  
    6363#include "MDirIter.h"
    6464
     65#include <fstream>
    6566#include <iostream>
    6667
     
    6869#include <TNamed.h>
    6970#include <TRegexp.h>
     71#include <TPRegexp.h>
    7072#include <TSystem.h>
     73
     74#include "MLog.h"
     75#include "MLogManip.h"
    7176
    7277ClassImp(MDirIter);
     
    171176}
    172177
     178Int_t MDirIter::ReadList(const char *name)
     179{
     180    TString fname(name);
     181    gSystem->ExpandPathName(fname);
     182
     183    ifstream fin(fname);
     184    if (!fin)
     185    {
     186        gLog << err << "Cannot open file " << fname << ": ";
     187        gLog << strerror(errno) << " [errno=" << errno << "]" <<endl;
     188        return -1;
     189    }
     190
     191    Int_t rc = 0;
     192
     193    while (1)
     194    {
     195        TString line;
     196        line.ReadLine(fin);
     197        if (!fin)
     198            break;
     199
     200        line = line.Strip(TString::kBoth);
     201        if (line[0]=='#' || line.IsNull())
     202            continue;
     203
     204        TObjArray &arr = *line.Tokenize(' ');
     205
     206        for (int i=1; i<arr.GetEntries(); i++)
     207            rc += AddDirectory(arr[0]->GetName(), arr[i]->GetName(), -1);
     208
     209        delete &arr;
     210    }
     211
     212    return true;
     213}
     214
    173215// --------------------------------------------------------------------------
    174216//
     
    211253
    212254// --------------------------------------------------------------------------
     255//
     256// Check whether the given name n matches the filter f.
     257//
     258// If the filter is encapsulated in ^ and $ it is assumed to be
     259// a valid regular expression and TPRegexp is used for filtering.
     260//
     261// In other cases TRegex is used:
    213262//
    214263// As the filter string may contain a + character, we have to replace
     
    220269//   +  by  \\+
    221270//   *  by  [^\\/:]*
    222 //   ?  by  .
    223271//
    224272// And surround the filter by ^ and $.
     
    227275//  TRegexp::MakeWildcard
    228276//
    229 const TRegexp MDirIter::MakeRegexp(TString n) const
    230 {
    231     n.Prepend("^");
    232     n.ReplaceAll(".", "\\.");
    233     n.ReplaceAll("+", "\\+");
    234     n.ReplaceAll("*", "[^\\/:]*");
    235     n.Append("$");
    236 
    237     return TRegexp(n, kFALSE);
    238 }
    239 
    240 // --------------------------------------------------------------------------
    241 //
    242 // Check whether the given name n matches the filter f.
    243 // Filters are of the form TRegexp(f, kTRUE)
    244 //
    245 Bool_t MDirIter::MatchFilter(const TString &n, const TString &f) const
    246 {
    247 
    248     return f.IsNull() || !n(MakeRegexp(f)).IsNull();
     277Bool_t MDirIter::MatchFilter(const TString &n, TString f) const
     278{
     279    if (f.IsNull())
     280        return kTRUE;
     281
     282    if (f[0]=='^' && f[f.Length()-1]=='$')
     283    {
     284        TPRegexp regex(f);
     285        return !n(regex).IsNull();
     286    }
     287
     288    f.Prepend("^");
     289    f.ReplaceAll(".", "\\.");
     290    f.ReplaceAll("+", "\\+");
     291    f.ReplaceAll("*", "[^\\/:]*");
     292    //f.ReplaceAll("?", "[^\\/:]?");
     293    f.Append("$");
     294
     295    return !n(TRegexp(f)).IsNull();
    249296}
    250297
  • trunk/Mars/mbase/MDirIter.h

    r9441 r13078  
    2020    Bool_t  CheckEntry(const TString n) const;
    2121    Int_t   IsDir(const char *dir) const;
    22     Bool_t  MatchFilter(const TString &name, const TString &filter) const;
     22    Bool_t  MatchFilter(const TString &name, TString filter) const;
    2323    TString ConcatFileName(const char *dir, const char *name) const;
    2424    void    PrintEntry(const TObject &o) const;
    25     const   TRegexp MakeRegexp(TString n) const;
    2625
    2726public:
     
    4948    }
    5049
     50    Int_t ReadList(const char *fname);
    5151    void Sort();
    5252
Note: See TracChangeset for help on using the changeset viewer.