GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
pathsearch.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2013 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if !defined (octave_pathsearch_h)
24 #define octave_pathsearch_h 1
25 
26 #include <string>
27 
28 #include "str-vec.h"
29 
30 class
31 OCTAVE_API
33 {
34 public:
35 
36  dir_path (const std::string& s = std::string (),
37  const std::string& d = std::string ())
38  : p_orig (s), p_default (d), initialized (false), p (), pv ()
39  {
40  if (! p_orig.empty ())
41  init ();
42  }
43 
44  dir_path (const dir_path& dp)
45  : p_orig (dp.p_orig), p_default (dp.p_default),
46  initialized (dp.initialized), p (dp.p), pv (dp.pv)
47  { }
48 
49  dir_path& operator = (const dir_path& dp)
50  {
51  p_orig = dp.p_orig;
52  p_default = dp.p_default;
54  p = dp.p;
55  pv = dp.pv;
56  return *this;
57  }
58 
59  ~dir_path (void) { }
60 
61  void set (const std::string& s)
62  {
63  initialized = false;
64  p_orig = s;
65  init ();
66  }
67 
68  string_vector elements (void);
69  string_vector all_directories (void);
70 
71  std::string find_first (const std::string&);
72  std::string find (const std::string& nm) { return find_first (nm); }
73 
74  string_vector find_all (const std::string&);
75 
76  std::string find_first_of (const string_vector& names);
77  string_vector find_all_first_of (const string_vector& names);
78 
79  void rehash (void)
80  {
81  initialized = false;
82  init ();
83  }
84 
85  static char path_sep_char (void)
86  {
87  return static_members::path_sep_char ();
88  }
89 
90  static void path_sep_char (char c)
91  {
92  static_members::path_sep_char (c);
93  }
94 
95  static std::string path_sep_str (void)
96  {
97  return static_members::path_sep_str ();
98  }
99 
100  static bool is_path_sep (char c) { return c == path_sep_char (); }
101 
102 private:
103 
104  // The colon separated list that we were given.
105  std::string p_orig;
106 
107  // The default path. If specified, replaces leading, trailing, or
108  // doubled colons in p_orig.
109  std::string p_default;
110 
111  // TRUE means we've unpacked p.
113 
114  // A version of the colon separate list on which we have performed
115  // tilde, variable, and possibly default path expansion.
116  std::string p;
117 
118  // The elements of the list.
120 
121  void init (void);
122 
123  // Use a singleton class for these data members instead of just
124  // making them static members of the dir_path class so that we can
125  // ensure proper initialization.
126 
127  class OCTAVE_API static_members
128  {
129  public:
130 
131  static_members (void);
132 
133  static char path_sep_char (void)
134  {
135  return instance_ok () ? instance->xpath_sep_char : 0;
136  }
137 
138  static void path_sep_char (char c)
139  {
140  if (instance_ok ())
141  {
142  instance->xpath_sep_char = c;
143  instance->xpath_sep_str = std::string (1, c);
144  }
145  }
146 
147  static std::string path_sep_str (void)
148  {
149  return instance_ok () ? instance->xpath_sep_str : std::string ();
150  }
151 
152  private:
153 
155 
156  static void cleanup_instance (void) { delete instance; instance = 0; }
157 
158  static bool instance_ok (void);
159 
160  // No copying!
161 
163 
164  static_members& operator = (const static_members&);
165 
167 
168  std::string xpath_sep_str;
169  };
170 };
171 
172 #endif