annotate liboctave/util/oct-glob.cc @ 21726:f05a00e611ff

use namespace for glob and fnmatch wrapper functions * oct-glob.h, oct-glob.cc: Put fnmatch and glob in octave::sys namespace. (octave_fnmatch, octave_glob): Now deprecated aliases for functions in octave::sys namespace. * glob-match.cc: Use new names. * oct-conf-post.in.h, mk-octave-config-h.sh: Define OCTAVE_USE_DEPRECATED_FUNCTIONS.
author John W. Eaton <jwe@octave.org>
date Wed, 18 May 2016 15:17:00 -0400
parents aba2e6293dd8
children 11af9c03400c
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
19697
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 17769
diff changeset
3 Copyright (C) 2010-2015 John W. Eaton
10138
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
21724
aba2e6293dd8 use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents: 21301
diff changeset
23 #if defined (HAVE_CONFIG_H)
21301
40de9f8f23a6 Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents: 21202
diff changeset
24 # include "config.h"
10138
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
10345
33b012d39dce Convert between back and forward slashes before and after calling glob.
Michael Goffioul <michael.goffioul@gmail.com>
parents: 10314
diff changeset
27 #include <algorithm>
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
28 #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
29
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 <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
31 #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
32
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 "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
34 #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
35
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
36 // 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
37 // 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
38 // 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
39 // 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
40 // 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
41
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
42 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
43 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
44 {
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
45 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
46
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
47 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
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
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
50 namespace
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
51 octave
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
52 {
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
53 namespace
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
54 sys
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
55 {
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
56 bool
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
57 fnmatch (const string_vector& pat, const std::string& str, int fnm_flags)
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
58 {
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
59 int npat = pat.numel ();
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
60
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
61 const char *cstr = str.c_str ();
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
62
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
63 for (int i = 0; i < npat; i++)
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
64 if (::fnmatch (pat(i).c_str (), cstr, fnm_flags) != FNM_NOMATCH)
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
65 return true;
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
66
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
67 return false;
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
68 }
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
69
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
70 string_vector
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
71 glob (const string_vector& pat)
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
72 {
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
73 string_vector retval;
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
74
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
75 int npat = pat.numel ();
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
76
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
77 int k = 0;
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
78
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
79 for (int i = 0; i < npat; i++)
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
80 {
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
81 std::string xpat = pat(i);
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
82
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
83 if (! xpat.empty ())
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
84 {
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
85 glob_t glob_info;
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
86
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
87 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
88 && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
89 std::replace_if (xpat.begin (), xpat.end (),
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
90 std::bind2nd (std::equal_to<char> (), '\\'),
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
91 '/');
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
92 #endif
10345
33b012d39dce Convert between back and forward slashes before and after calling glob.
Michael Goffioul <michael.goffioul@gmail.com>
parents: 10314
diff changeset
93
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
94 int err = gnulib::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
95
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
96 if (! err)
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
97 {
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
98 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
99
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
100 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
101
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
102 // FIXME: we shouldn't have to check to see if
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
103 // a single match exists, but it seems that glob() won't
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
104 // check for us unless the pattern contains globbing
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
105 // 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
106
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
107 if (n > 1
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
108 || (n == 1
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
109 && single_match_exists (std::string (matches[0]))))
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
110 {
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
111 retval.resize (k+n);
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
112
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
113 for (int j = 0; j < n; j++)
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
114 {
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
115 std::string tmp = matches[j];
10345
33b012d39dce Convert between back and forward slashes before and after calling glob.
Michael Goffioul <michael.goffioul@gmail.com>
parents: 10314
diff changeset
116
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
117 #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
118 && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM)
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
119 ` std::replace_if (tmp.begin (), tmp.end (),
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
120 std::bind2nd (std::equal_to<char> (),
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
121 '/'),
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
122 '\\');
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
123 #endif
10345
33b012d39dce Convert between back and forward slashes before and after calling glob.
Michael Goffioul <michael.goffioul@gmail.com>
parents: 10314
diff changeset
124
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
125 retval[k++] = tmp;
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
126 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
127 }
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
128
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
129 gnulib::globfree (&glob_info);
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
130 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
131 }
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10138
diff changeset
132 }
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
133
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
134 return retval.sort ();
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
135 }
21726
f05a00e611ff use namespace for glob and fnmatch wrapper functions
John W. Eaton <jwe@octave.org>
parents: 21724
diff changeset
136 }
10138
805a83ecd3da avoid conflict between glob.h definition of glob and glob_match::glob function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
137 }