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
file-ops.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_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 private:
99 
101 
102  static void cleanup_instance (void) { delete instance; instance = 0; }
103 
104  // No copying!
105 
106  file_ops (const file_ops&);
107 
108  file_ops& operator = (const file_ops&);
109 
110  static bool instance_ok (void);
111 
113  std::string xdir_sep_str;
114  std::string xdir_sep_chars;
115 };
116 
117 // We don't have these in the file_ops class with their simple names
118 // (i.e., mkdir instead of octave_mdir) because function names in
119 // standard headers may be #defined.
120 
121 extern OCTAVE_API int
122 octave_mkdir (const std::string& nm, mode_t md);
123 
124 extern OCTAVE_API int
125 octave_mkdir (const std::string& nm, mode_t md, std::string& msg);
126 
127 extern OCTAVE_API int
128 octave_mkfifo (const std::string& nm, mode_t md);
129 
130 extern OCTAVE_API int
131 octave_mkfifo (const std::string& nm, mode_t md, std::string& msg);
132 
133 extern OCTAVE_API int
134 octave_link (const std::string&, const std::string&);
135 
136 extern OCTAVE_API int
137 octave_link (const std::string&, const std::string&, std::string&);
138 
139 extern OCTAVE_API int
140 octave_symlink (const std::string&, const std::string&);
141 
142 extern OCTAVE_API int
143 octave_symlink (const std::string&, const std::string&, std::string&);
144 
145 extern OCTAVE_API int
146 octave_readlink (const std::string&, std::string&);
147 
148 extern OCTAVE_API int
149 octave_readlink (const std::string&, std::string&, std::string&);
150 
151 extern OCTAVE_API int
152 octave_rename (const std::string&, const std::string&);
153 
154 extern OCTAVE_API int
155 octave_rename (const std::string&, const std::string&, std::string&);
156 
157 extern OCTAVE_API int
158 octave_rmdir (const std::string&);
159 
160 extern OCTAVE_API int
161 octave_rmdir (const std::string&, std::string&);
162 
163 extern OCTAVE_API int
164 octave_recursive_rmdir (const std::string&);
165 
166 extern OCTAVE_API int
167 octave_recursive_rmdir (const std::string&, std::string&);
168 
169 extern OCTAVE_API int
171 
172 extern OCTAVE_API int
173 octave_unlink (const std::string&);
174 
175 extern OCTAVE_API int
176 octave_unlink (const std::string&, std::string&);
177 
178 extern OCTAVE_API std::string
179 octave_tempnam (const std::string&, const std::string&);
180 
181 extern OCTAVE_API std::string
182 octave_tempnam (const std::string&, const std::string&, std::string&);
183 
184 extern OCTAVE_API std::string
185 octave_canonicalize_file_name (const std::string&);
186 
187 extern OCTAVE_API std::string
188 octave_canonicalize_file_name (const std::string&, std::string&);
189 
190 #endif