GNU Octave  4.2.1
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
file-ops.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2017 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_file_ops_h)
24 #define octave_file_ops_h 1
25 
26 #include "octave-config.h"
27 
28 #include <string>
29 
30 #include <sys/types.h>
31 
32 #include "str-vec.h"
33 
34 namespace octave
35 {
36  namespace sys
37  {
38  struct
39  OCTAVE_API
40  file_ops
41  {
42  protected:
43 
44  // Use a singleton class for dir_sep data members instead of just
45  // making them static members of the file_ops class so that we
46  // can ensure proper initialization.
47 
48  file_ops (char dev_sep_char_arg = 0, char dir_sep_char_arg = 0,
49  const std::string& dir_sep_str_arg = std::string ("/"),
50  const std::string& dir_sep_chars_arg = std::string ("/"))
51  : m_dev_sep_char (dev_sep_char_arg),
52  m_dir_sep_char (dir_sep_char_arg),
53  m_dir_sep_str (dir_sep_str_arg),
54  m_dir_sep_chars (dir_sep_chars_arg) { }
55 
56  public:
57 
58  typedef std::string (*tilde_expansion_hook) (const std::string&);
59 
60  static tilde_expansion_hook tilde_expansion_preexpansion_hook;
61 
62  static tilde_expansion_hook tilde_expansion_failure_hook;
63 
65 
67 
68  static char dev_sep_char (void)
69  {
70  return instance_ok () ? instance->m_dev_sep_char : 0;
71  }
72 
73  static bool is_dev_sep (char c);
74 
75  static char dir_sep_char (void)
76  {
77  return instance_ok () ? instance->m_dir_sep_char : 0;
78  }
79 
80  static std::string dir_sep_str (void)
81  {
82  return instance_ok () ? instance->m_dir_sep_str : "";
83  }
84 
85  static std::string dir_sep_chars (void)
86  {
87  return instance_ok () ? instance->m_dir_sep_chars : "";
88  }
89 
90  static bool is_dir_sep (char c)
91  {
92  std::string tmp = dir_sep_chars ();
93  return tmp.find (c) != std::string::npos;
94  }
95 
96  static std::string tilde_expand (const std::string&);
97 
98  static string_vector tilde_expand (const string_vector&);
99 
100  static std::string concat (const std::string&, const std::string&);
101 
102  // Return the directory part of a filename or an empty string if
103  // there is no directory component. Does not check to see
104  // whether the file exists or is a directory.
105  static std::string dirname (const std::string& path)
106  {
107  size_t ipos = path.find_last_of (dir_sep_chars ());
108 
109  return (ipos != std::string::npos) ? path.substr (0, ipos) : "";
110  }
111 
112  // Return the tail member of a filename.
113  static std::string tail (const std::string& path)
114  {
115  size_t ipos = path.find_last_of (dir_sep_chars ());
116 
117  if (ipos != std::string::npos)
118  ipos++;
119  else
120  ipos = 0;
121 
122  return path.substr (ipos);
123  }
124 
125  // convert path from UNIX type separators to whatever is the system separators
126  static std::string native_separator_path (const std::string& path);
127 
128  private:
129 
131 
132  static void cleanup_instance (void) { delete instance; instance = 0; }
133 
134  // No copying!
135 
136  file_ops (const file_ops&);
137 
138  file_ops& operator = (const file_ops&);
139 
140  static bool instance_ok (void);
141 
143 
147  };
148 
149  // We don't have these in the file_ops class with their simple names
150  // (i.e., mkdir instead of octave_mdir) because function names in
151  // standard headers may be #defined.
152 
153  extern OCTAVE_API int
154  mkdir (const std::string&, mode_t);
155 
156  extern OCTAVE_API int
157  mkdir (const std::string&, mode_t, std::string&);
158 
159  extern OCTAVE_API int
160  mkfifo (const std::string&, mode_t);
161 
162  extern OCTAVE_API int
163  mkfifo (const std::string&, mode_t, std::string&);
164 
165  extern OCTAVE_API int
166  link (const std::string&, const std::string&);
167 
168  extern OCTAVE_API int
169  link (const std::string&, const std::string&, std::string&);
170 
171  extern OCTAVE_API int
172  symlink (const std::string&, const std::string&);
173 
174  extern OCTAVE_API int
175  symlink (const std::string&, const std::string&, std::string&);
176 
177  extern OCTAVE_API int
178  readlink (const std::string&, std::string&);
179 
180  extern OCTAVE_API int
182 
183  extern OCTAVE_API int
184  rename (const std::string&, const std::string&);
185 
186  extern OCTAVE_API int
187  rename (const std::string&, const std::string&, std::string&);
188 
189  extern OCTAVE_API int
190  rmdir (const std::string&);
191 
192  extern OCTAVE_API int
193  rmdir (const std::string&, std::string&);
194 
195  extern OCTAVE_API int
196  recursive_rmdir (const std::string&);
197 
198  extern OCTAVE_API int
200 
201  extern OCTAVE_API int
202  umask (mode_t);
203 
204  extern OCTAVE_API int
205  unlink (const std::string&);
206 
207  extern OCTAVE_API int
208  unlink (const std::string&, std::string&);
209 
210  extern OCTAVE_API std::string
211  tempnam (const std::string&, const std::string&);
212 
213  extern OCTAVE_API std::string
214  tempnam (const std::string&, const std::string&, std::string&);
215 
216  extern OCTAVE_API std::string
218 
219  extern OCTAVE_API std::string
221  }
222 }
223 
224 #if defined (OCTAVE_USE_DEPRECATED_FUNCTIONS)
225 
226 OCTAVE_DEPRECATED ("use 'octave::sys::file_ops' instead")
227  typedef octave::sys::file_ops file_ops;
228 
229 OCTAVE_DEPRECATED ("use 'octave::sys::mkdir' instead")
230 inline int
231 octave_mkdir (const std::string& nm, mode_t md)
232 {
233  return octave::sys::mkdir (nm, md);
234 }
235 
236 OCTAVE_DEPRECATED ("use 'octave::sys::mkdir' instead")
237 inline int
238 octave_mkdir (const std::string& nm, mode_t md, std::string& msg)
239 {
240  return octave::sys::mkdir (nm, md, msg);
241 }
242 
243 OCTAVE_DEPRECATED ("use 'octave::sys::mkfifo' instead")
244 inline int
245 octave_mkfifo (const std::string& nm, mode_t md)
246 {
247  return octave::sys::mkfifo (nm, md);
248 }
249 
250 OCTAVE_DEPRECATED ("use 'octave::sys::mkfifo' instead")
251 inline int
252 octave_mkfifo (const std::string& nm, mode_t md, std::string& msg)
253 {
254  return octave::sys::mkfifo (nm, md, msg);
255 }
256 
257 OCTAVE_DEPRECATED ("use 'octave::sys::link' instead")
258 inline int
259 octave_link (const std::string& old_name, const std::string& new_name)
260 {
261  return octave::sys::link (old_name, new_name);
262 }
263 
264 OCTAVE_DEPRECATED ("use 'octave::sys::link' instead")
265 inline int
266 octave_link (const std::string& old_name, const std::string& new_name,
267  std::string& msg)
268 {
269  return octave::sys::link (old_name, new_name, msg);
270 }
271 
272 OCTAVE_DEPRECATED ("use 'octave::sys::symlink' instead")
273 inline int
274 octave_symlink (const std::string& old_name, const std::string& new_name)
275 {
276  return octave::sys::symlink (old_name, new_name);
277 }
278 
279 OCTAVE_DEPRECATED ("use 'octave::sys::symlink' instead")
280 inline int
281 octave_symlink (const std::string& old_name, const std::string& new_name,
282  std::string& msg)
283 {
284  return octave::sys::symlink (old_name, new_name, msg);
285 }
286 
287 OCTAVE_DEPRECATED ("use 'octave::sys::readlink' instead")
288 inline int
289 octave_readlink (const std::string& path, std::string& result)
290 {
291  return octave::sys::readlink (path, result);
292 }
293 
294 OCTAVE_DEPRECATED ("use 'octave::sys::readlink' instead")
295 inline int
296 octave_readlink (const std::string& path, std::string& result, std::string& msg)
297 {
298  return octave::sys::readlink (path, result, msg);
299 }
300 
301 OCTAVE_DEPRECATED ("use 'octave::sys::rename' instead")
302 inline int
303 octave_rename (const std::string& from, const std::string& to)
304 {
305  return octave::sys::rename (from, to);
306 }
307 
308 OCTAVE_DEPRECATED ("use 'octave::sys::rename' instead")
309 inline int
310 octave_rename (const std::string& from, const std::string& to, std::string& msg)
311 {
312  return octave::sys::rename (from, to, msg);
313 }
314 
315 OCTAVE_DEPRECATED ("use 'octave::sys::rmdir' instead")
316 inline int
317 octave_rmdir (const std::string& nm)
318 {
319  return octave::sys::rmdir (nm);
320 }
321 
322 OCTAVE_DEPRECATED ("use 'octave::sys::rmdir' instead")
323 inline int
324 octave_rmdir (const std::string& nm, std::string& msg)
325 {
326  return octave::sys::rmdir (nm, msg);
327 }
328 
329 OCTAVE_DEPRECATED ("use 'octave::sys::recursive_rmdir' instead")
330 inline int
331 octave_recursive_rmdir (const std::string& nm)
332 {
333  return octave::sys::recursive_rmdir (nm);
334 }
335 
336 OCTAVE_DEPRECATED ("use 'octave::sys::recursive_rmdir' instead")
337 inline int
338 octave_recursive_rmdir (const std::string& nm, std::string& msg)
339 {
340  return octave::sys::recursive_rmdir (nm, msg);
341 }
342 
343 OCTAVE_DEPRECATED ("use 'octave::sys::umask' instead")
344 inline int
345 octave_umask (mode_t md)
346 {
347  return octave::sys::umask (md);
348 }
349 
350 OCTAVE_DEPRECATED ("use 'octave::sys::unlink' instead")
351 inline int
352 octave_unlink (const std::string& nm)
353 {
354  return octave::sys::unlink (nm);
355 }
356 
357 OCTAVE_DEPRECATED ("use 'octave::sys::unlink' instead")
358 inline int
359 octave_unlink (const std::string& nm, std::string& msg)
360 {
361  return octave::sys::unlink (nm, msg);
362 }
363 
364 OCTAVE_DEPRECATED ("use 'octave::sys::tempnam' instead")
365 inline std::string
366 octave_tempnam (const std::string& dir, const std::string& pfx)
367 {
368  return octave::sys::tempnam (dir, pfx);
369 }
370 
371 OCTAVE_DEPRECATED ("use 'octave::sys::tempnam' instead")
372 inline std::string
373 octave_tempnam (const std::string& dir, const std::string& pfx,
374  std::string& msg)
375 {
376  return octave::sys::tempnam (dir, pfx, msg);
377 }
378 
379 OCTAVE_DEPRECATED ("use 'octave::sys::canonicalize_file_name' instead")
380 inline std::string
381 octave_canonicalize_file_name (const std::string& nm)
382 {
384 }
385 
386 OCTAVE_DEPRECATED ("use 'octave::sys::canonicalize_file_name' instead")
387 inline std::string
388 octave_canonicalize_file_name (const std::string& nm, std::string& msg)
389 {
390  return octave::sys::canonicalize_file_name (nm, msg);
391 }
392 
393 #endif
394 
395 #endif
static std::string dir_sep_str(void)
Definition: file-ops.h:80
Octave interface to the compression and uncompression libraries.
Definition: aepbalance.cc:47
static std::string dirname(const std::string &path)
Definition: file-ops.h:105
std::string canonicalize_file_name(const std::string &name)
Definition: file-ops.cc:719
static std::string tail(const std::string &path)
Definition: file-ops.h:113
file_ops(char dev_sep_char_arg=0, char dir_sep_char_arg=0, const std::string &dir_sep_str_arg=std::string("/"), const std::string &dir_sep_chars_arg=std::string("/"))
Definition: file-ops.h:48
int unlink(const std::string &name)
Definition: file-ops.cc:648
OCTAVE_EXPORT octave_value_list mkdir
Definition: dirfns.cc:158
static std::string dir_sep_chars(void)
Definition: file-ops.h:85
int symlink(const std::string &old_name, const std::string &new_name)
Definition: file-ops.cc:470
std::string tempnam(const std::string &dir, const std::string &pfx)
Definition: file-ops.cc:670
std::string m_dir_sep_str
Definition: file-ops.h:145
ComplexNDArray concat(NDArray &ra, ComplexNDArray &rb, const Array< octave_idx_type > &ra_idx)
Definition: CNDArray.cc:655
STL namespace.
static string_vector tilde_additional_prefixes
Definition: file-ops.h:64
static tilde_expansion_hook tilde_expansion_preexpansion_hook
Definition: file-ops.h:60
static void cleanup_instance(void)
Definition: file-ops.h:132
static file_ops * instance
Definition: file-ops.h:130
int recursive_rmdir(const std::string &name)
Definition: file-ops.cc:567
int rename(const std::string &from, const std::string &to)
Definition: file-ops.cc:521
OCTAVE_EXPORT octave_value_list isdir nd deftypefn *std::string nm
Definition: utils.cc:941
std::string m_dir_sep_chars
Definition: file-ops.h:146
int link(const std::string &old_name, const std::string &new_name)
Definition: file-ops.cc:447
static char dev_sep_char(void)
Definition: file-ops.h:68
static char dir_sep_char(void)
Definition: file-ops.h:75
double tmp
Definition: data.cc:6300
static tilde_expansion_hook tilde_expansion_failure_hook
Definition: file-ops.h:62
the sparsity preserving column transformation such that that defines the pivoting threshold can be given in which case it defines the c
Definition: lu.cc:138
may be zero for pure relative error test tem the relative tolerance must be greater than or equal to
Definition: Quad-opts.cc:233
With real return the complex result
Definition: data.cc:3375
int umask(mode_t mode)
Definition: file-ops.cc:642
int mkdir(const std::string &nm, mode_t md)
Definition: file-ops.cc:407
int rmdir(const std::string &name)
Definition: file-ops.cc:543
int readlink(const std::string &path, std::string &result)
Definition: file-ops.cc:493
static string_vector tilde_additional_suffixes
Definition: file-ops.h:66
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:854
static bool is_dir_sep(char c)
Definition: file-ops.h:90
int mkfifo(const std::string &nm, mode_t md)
Definition: file-ops.cc:427