00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00109
00110
00111
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
00133
00134
00135
00136