メインページ   クラス階層   構成   ファイル一覧   構成メンバ   ファイルメンバ  

glob-match.cc

解説を見る。
00001 /*
00002 
00003 Copyright (C) 1996, 1997 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 2, or (at your option) any
00010 later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, write to the Free
00019 Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020 
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #include <fnmatch.h>
00028 #include <glob.h>
00029 
00030 #include <iostream>
00031 #include <string>
00032 
00033 #include "file-stat.h"
00034 #include "glob-match.h"
00035 #include "str-vec.h"
00036 
00037 bool
00038 glob_match::match (const std::string& s)
00039 {
00040   int npat = pat.length ();
00041 
00042   const char *str = s.c_str ();
00043 
00044   int fnmatch_flags = 0;
00045 
00046   if (flags & pathname)
00047     fnmatch_flags |= FNM_PATHNAME;
00048 
00049   if (flags & noescape)
00050     fnmatch_flags |= FNM_NOESCAPE;
00051 
00052   if (flags & period)
00053     fnmatch_flags |= FNM_PERIOD;
00054 
00055   for (int i = 0; i < npat; i++)
00056     if (fnmatch (pat(i).c_str (), str, fnmatch_flags) != FNM_NOMATCH)
00057       return true;
00058 
00059   return false;
00060 }
00061 
00062 Array<bool>
00063 glob_match::match (const string_vector& s)
00064 {
00065   int n = s.length ();
00066 
00067   Array<bool> retval (n);
00068 
00069   for (int i = 0; i < n; i++)
00070     retval(i) = match (s[i]);
00071 
00072   return retval;
00073 }
00074 
00075 static bool
00076 single_match_exists (const std::string& file)
00077 {
00078   file_stat s (file);
00079 
00080   return s.exists ();
00081 }
00082 
00083 string_vector
00084 glob_match::glob (void)
00085 {
00086   string_vector retval;
00087 
00088   int npat = pat.length ();
00089 
00090   int k = 0;
00091 
00092   for (int i = 0; i < npat; i++)
00093     {
00094       std::string xpat = pat(i);
00095 
00096       if (! xpat.empty ())
00097         {
00098           glob_t glob_info;
00099 
00100           int err = ::glob (xpat.c_str (), GLOB_NOSORT, 0, &glob_info);
00101 
00102           if (! err)
00103             {
00104               int n = glob_info.gl_pathc;
00105 
00106               const char * const *matches = glob_info.gl_pathv;
00107 
00108               // XXX FIXME XXX -- we shouldn't have to check to see if
00109               // a single match exists, but it seems that glob() won't
00110               // check for us unless the pattern contains globbing
00111               // characters.  Hmm.
00112 
00113               if (n > 1
00114                   || (n == 1
00115                       && single_match_exists (std::string (matches[0]))))
00116                 {
00117                   retval.resize (k+n);
00118 
00119                   for (int j = 0; j < n; j++)
00120                     retval[k++] = matches[j];
00121                 }
00122 
00123               globfree (&glob_info);
00124             }
00125         }
00126     }
00127 
00128   return retval.qsort ();
00129 }
00130 
00131 /*
00132 ;;; Local Variables: ***
00133 ;;; mode: C++ ***
00134 ;;; End: ***
00135 */
00136 

Wed Dec 29 11:51:36 2004に生成されました。 doxygen1.2.18
SEO [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送