GNU Octave  4.0.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
file-ops.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2015 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 <string>
27 
28 #include <sys/types.h>
29 
30 #include "str-vec.h"
31 
32 struct
33 OCTAVE_API
35 {
36 public:
37 
38  // Use a singleton class for dir_sep data members instead of just
39  // making them static members of the dir_path class so that we can
40  // ensure proper initialization.
41 
42  file_ops (char dir_sep_char_arg = 0,
43  const std::string& dir_sep_str_arg = std::string ("/"),
44  const std::string& dir_sep_chars_arg = std::string ("/"))
45  : xdir_sep_char (dir_sep_char_arg), xdir_sep_str (dir_sep_str_arg),
46  xdir_sep_chars (dir_sep_chars_arg) { }
47 
48  typedef std::string (*tilde_expansion_hook) (const std::string&);
49 
50  static tilde_expansion_hook tilde_expansion_preexpansion_hook;
51 
52  static tilde_expansion_hook tilde_expansion_failure_hook;
53 
55 
57 
58  static char dir_sep_char (void)
59  {
60  return instance_ok () ? instance->xdir_sep_char : 0;
61  }
62 
63  static std::string dir_sep_str (void)
64  {
65  return instance_ok () ? instance->xdir_sep_str : std::string ();
66  }
67 
68  static std::string dir_sep_chars (void)
69  {
70  return instance_ok () ? instance->xdir_sep_chars : std::string ();
71  }
72 
73  static bool is_dir_sep (char c)
74  {
75  std::string tmp = dir_sep_chars ();
76  return tmp.find (c) != std::string::npos;
77  }
78 
79  static std::string tilde_expand (const std::string&);
80 
81  static string_vector tilde_expand (const string_vector&);
82 
83  static std::string concat (const std::string&, const std::string&);
84 
85  // Return the tail member of a file name.
86  static std::string tail (const std::string& path)
87  {
88  size_t ipos = path.find_last_of (dir_sep_chars ());
89 
90  if (ipos != std::string::npos)
91  ipos++;
92  else
93  ipos = 0;
94 
95  return path.substr (ipos);
96  }
97 
98  // convert path from UNIX type separators to whatever is the system separators
99  static std::string native_separator_path (const std::string& path);
100 
101 private:
102 
104 
105  static void cleanup_instance (void) { delete instance; instance = 0; }
106 
107  // No copying!
108 
109  file_ops (const file_ops&);
110 
111  file_ops& operator = (const file_ops&);
112 
113  static bool instance_ok (void);
114 
116  std::string xdir_sep_str;
117  std::string xdir_sep_chars;
118 };
119 
120 // We don't have these in the file_ops class with their simple names
121 // (i.e., mkdir instead of octave_mdir) because function names in
122 // standard headers may be #defined.
123 
124 extern OCTAVE_API int
125 octave_mkdir (const std::string& nm, mode_t md);
126 
127 extern OCTAVE_API int
128 octave_mkdir (const std::string& nm, mode_t md, std::string& msg);
129 
130 extern OCTAVE_API int
131 octave_mkfifo (const std::string& nm, mode_t md);
132 
133 extern OCTAVE_API int
134 octave_mkfifo (const std::string& nm, mode_t md, std::string& msg);
135 
136 extern OCTAVE_API int
137 octave_link (const std::string&, const std::string&);
138 
139 extern OCTAVE_API int
140 octave_link (const std::string&, const std::string&, std::string&);
141 
142 extern OCTAVE_API int
143 octave_symlink (const std::string&, const std::string&);
144 
145 extern OCTAVE_API int
146 octave_symlink (const std::string&, const std::string&, std::string&);
147 
148 extern OCTAVE_API int
149 octave_readlink (const std::string&, std::string&);
150 
151 extern OCTAVE_API int
152 octave_readlink (const std::string&, std::string&, std::string&);
153 
154 extern OCTAVE_API int
155 octave_rename (const std::string&, const std::string&);
156 
157 extern OCTAVE_API int
158 octave_rename (const std::string&, const std::string&, std::string&);
159 
160 extern OCTAVE_API int
161 octave_rmdir (const std::string&);
162 
163 extern OCTAVE_API int
164 octave_rmdir (const std::string&, std::string&);
165 
166 extern OCTAVE_API int
167 octave_recursive_rmdir (const std::string&);
168 
169 extern OCTAVE_API int
170 octave_recursive_rmdir (const std::string&, std::string&);
171 
172 extern OCTAVE_API int
174 
175 extern OCTAVE_API int
176 octave_unlink (const std::string&);
177 
178 extern OCTAVE_API int
179 octave_unlink (const std::string&, std::string&);
180 
181 extern OCTAVE_API std::string
182 octave_tempnam (const std::string&, const std::string&);
183 
184 extern OCTAVE_API std::string
185 octave_tempnam (const std::string&, const std::string&, std::string&);
186 
187 extern OCTAVE_API std::string
188 octave_canonicalize_file_name (const std::string&);
189 
190 extern OCTAVE_API std::string
191 octave_canonicalize_file_name (const std::string&, std::string&);
192 
193 #endif
OCTAVE_API int octave_rename(const std::string &, const std::string &)
Definition: file-ops.cc:517
static tilde_expansion_hook tilde_expansion_preexpansion_hook
Definition: file-ops.h:50
OCTAVE_API int octave_umask(mode_t)
Definition: file-ops.cc:639
ComplexNDArray concat(NDArray &ra, ComplexNDArray &rb, const Array< octave_idx_type > &ra_idx)
Definition: CNDArray.cc:664
OCTAVE_API std::string octave_canonicalize_file_name(const std::string &)
Definition: file-ops.cc:720
OCTAVE_API int octave_mkfifo(const std::string &nm, mode_t md)
Definition: file-ops.cc:414
static tilde_expansion_hook tilde_expansion_failure_hook
Definition: file-ops.h:52
#define mode_t
Definition: statdefs.h:34
OCTAVE_API int octave_link(const std::string &, const std::string &)
Definition: file-ops.cc:440
std::string xdir_sep_chars
Definition: file-ops.h:117
static void cleanup_instance(void)
Definition: file-ops.h:105
static file_ops * instance
Definition: file-ops.h:103
OCTAVE_API int octave_unlink(const std::string &)
Definition: file-ops.cc:649
std::string xdir_sep_str
Definition: file-ops.h:116
OCTAVE_API int octave_symlink(const std::string &, const std::string &)
Definition: file-ops.cc:463
static string_vector tilde_additional_suffixes
Definition: file-ops.h:56
static std::string tail(const std::string &path)
Definition: file-ops.h:86
static std::string dir_sep_chars(void)
Definition: file-ops.h:68
OCTAVE_API int octave_mkdir(const std::string &nm, mode_t md)
Definition: file-ops.cc:392
static string_vector tilde_additional_prefixes
Definition: file-ops.h:54
static bool is_dir_sep(char c)
Definition: file-ops.h:73
OCTAVE_API std::string octave_tempnam(const std::string &, const std::string &)
Definition: file-ops.cc:671
char xdir_sep_char
Definition: file-ops.h:115
OCTAVE_API int octave_readlink(const std::string &, std::string &)
Definition: file-ops.cc:486
static std::string dir_sep_str(void)
Definition: file-ops.h:63
OCTAVE_API int octave_rmdir(const std::string &)
Definition: file-ops.cc:540
OCTAVE_API int octave_recursive_rmdir(const std::string &)
Definition: file-ops.cc:564
file_ops(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:42
static char dir_sep_char(void)
Definition: file-ops.h:58