Go to the documentation of this file.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 <algorithm>
00028 #include <string>
00029
00030 #include <fnmatch.h>
00031 #include <glob.h>
00032
00033 #include "oct-glob.h"
00034 #include "file-stat.h"
00035
00036
00037
00038
00039
00040
00041
00042 static bool
00043 single_match_exists (const std::string& file)
00044 {
00045 file_stat s (file);
00046
00047 return s.exists ();
00048 }
00049
00050 bool
00051 octave_fnmatch (const string_vector& pat, const std::string& str,
00052 int fnmatch_flags)
00053 {
00054 int npat = pat.length ();
00055
00056 const char *cstr = str.c_str ();
00057
00058 for (int i = 0; i < npat; i++)
00059 if (fnmatch (pat(i).c_str (), cstr, fnmatch_flags) != FNM_NOMATCH)
00060 return true;
00061
00062 return false;
00063 }
00064
00065 string_vector
00066 octave_glob (const string_vector& pat)
00067 {
00068 string_vector retval;
00069
00070 int npat = pat.length ();
00071
00072 int k = 0;
00073
00074 for (int i = 0; i < npat; i++)
00075 {
00076 std::string xpat = pat(i);
00077
00078 if (! xpat.empty ())
00079 {
00080 glob_t glob_info;
00081
00082 #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
00083 && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM)
00084 std::replace_if (xpat.begin (), xpat.end (),
00085 std::bind2nd (std::equal_to<char> (), '\\'),
00086 '/');
00087 #endif
00088
00089 int err = gnulib::glob (xpat.c_str (), GLOB_NOSORT, 0, &glob_info);
00090
00091 if (! err)
00092 {
00093 int n = glob_info.gl_pathc;
00094
00095 const char * const *matches = glob_info.gl_pathv;
00096
00097
00098
00099
00100
00101
00102 if (n > 1
00103 || (n == 1
00104 && single_match_exists (std::string (matches[0]))))
00105 {
00106 retval.resize (k+n);
00107
00108 for (int j = 0; j < n; j++)
00109 {
00110 std::string tmp = matches[j];
00111
00112 #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
00113 && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM)
00114 std::replace_if (tmp.begin (), tmp.end (),
00115 std::bind2nd (std::equal_to<char> (),
00116 '/'),
00117 '\\');
00118 #endif
00119
00120 retval[k++] = tmp;
00121 }
00122 }
00123
00124 gnulib::globfree (&glob_info);
00125 }
00126 }
00127 }
00128
00129 return retval.sort ();
00130 }