annotate liboctave/oct-glob.cc @ 10314:07ebe522dac2

untabify liboctave C++ sources
author John W. Eaton <jwe@octave.org>
date Thu, 11 Feb 2010 12:23:32 -0500
parents 805a83ecd3da
children 33b012d39dce
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
1 /*
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
2
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
3 Copyright (C) 2010 John W. Eaton
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
4
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
5 This file is part of Octave.
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
6
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
7 Octave is free software; you can redistribute it and/or modify it
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
9 Free Software Foundation; either version 3 of the License, or (at your
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
10 option) any later version.
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
11
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
12 Octave is distributed in the hope that it will be useful, but WITHOUT
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
15 for more details.
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
16
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
18 along with Octave; see the file COPYING. If not, see
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
19 <http://www.gnu.org/licenses/>.
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
20
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
21 */
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
22
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
23 #ifdef HAVE_CONFIG_H
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
24 #include <config.h>
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
25 #endif
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
26
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
27 #include <string>
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
28
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
29 #include <fnmatch.h>
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
30 #include <glob.h>
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
31
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
32 #include "oct-glob.h"
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
33 #include "file-stat.h"
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
34
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
35 // These functions are defined here and not in glob_match.cc so that we
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
36 // can include the glob.h file from gnulib, which defines glob to
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
37 // be rpl_glob. If we include glob.h in glob_match.cc, then it
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
38 // transforms the glob_match::glob function to be glob_match::rpl_glob,
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
39 // which is not what we want...
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
40
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
41 static bool
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
42 single_match_exists (const std::string& file)
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
43 {
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
44 file_stat s (file);
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
45
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
46 return s.exists ();
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
47 }
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
48
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
49 bool
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
50 octave_fnmatch (const string_vector& pat, const std::string& str,
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
51 int fnmatch_flags)
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
52 {
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
53 int npat = pat.length ();
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
54
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
55 const char *cstr = str.c_str ();
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
56
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
57 for (int i = 0; i < npat; i++)
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
58 if (fnmatch (pat(i).c_str (), cstr, fnmatch_flags) != FNM_NOMATCH)
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
59 return true;
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
60
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
61 return false;
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
62 }
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
63
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
64 string_vector
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
65 octave_glob (const string_vector& pat)
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
66 {
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
67 string_vector retval;
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
68
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
69 int npat = pat.length ();
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
70
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
71 int k = 0;
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
72
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
73 for (int i = 0; i < npat; i++)
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
74 {
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
75 std::string xpat = pat(i);
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
76
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
77 if (! xpat.empty ())
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
78 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
79 glob_t glob_info;
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
80
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
81 int err = ::glob (xpat.c_str (), GLOB_NOSORT, 0, &glob_info);
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
82
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
83 if (! err)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
84 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
85 int n = glob_info.gl_pathc;
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
86
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
87 const char * const *matches = glob_info.gl_pathv;
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
88
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
89 // FIXME -- we shouldn't have to check to see if
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
90 // a single match exists, but it seems that glob() won't
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
91 // check for us unless the pattern contains globbing
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
92 // characters. Hmm.
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
93
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
94 if (n > 1
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
95 || (n == 1
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
96 && single_match_exists (std::string (matches[0]))))
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
97 {
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
98 retval.resize (k+n);
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
99
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
100 for (int j = 0; j < n; j++)
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
101 retval[k++] = matches[j];
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
102 }
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
103
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
104 globfree (&glob_info);
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
105 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
106 }
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
107 }
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
108
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
109 return retval.sort ();
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
110 }