1 //////////////////////////////////////////////////////////////////////// 2 // 3 // Copyright (C) 1996-2021 The Octave Project Developers 4 // 5 // See the file COPYRIGHT.md in the top-level directory of this 6 // distribution or <https://octave.org/copyright/>. 7 // 8 // This file is part of Octave. 9 // 10 // Octave is free software: you can redistribute it and/or modify it 11 // under the terms of the GNU General Public License as published by 12 // the Free Software Foundation, either version 3 of the License, or 13 // (at your option) any later version. 14 // 15 // Octave is distributed in the hope that it will be useful, but 16 // WITHOUT ANY WARRANTY; without even the implied warranty of 17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 // GNU General Public License for more details. 19 // 20 // You should have received a copy of the GNU General Public License 21 // along with Octave; see the file COPYING. If not, see 22 // <https://www.gnu.org/licenses/>. 23 // 24 //////////////////////////////////////////////////////////////////////// 25 26 #if ! defined (octave_dir_ops_h) 27 #define octave_dir_ops_h 1 28 29 #include "octave-config.h" 30 31 #include <string> 32 33 #include "str-vec.h" 34 35 namespace octave 36 { 37 namespace sys 38 { 39 class 40 OCTAVE_API 41 dir_entry 42 { 43 44 // NOTE: This class cannot be used safely cross-platform (Windows) with 45 // non-ASCII characters in paths. 46 // Consider replacing the implementation using std::filesystem (C++ 17). 47 // In the meantime, consider using octave::sys::get_dirlist instead. 48 49 public: 50 51 dir_entry (const std::string& n = "") name(n)52 : name (n), dir (nullptr), fail (false), errmsg () 53 { 54 if (! name.empty ()) 55 open (); 56 } 57 dir_entry(const dir_entry & d)58 dir_entry (const dir_entry& d) 59 : name (d.name), dir (d.dir), fail (d.fail), errmsg (d.errmsg) { } 60 61 dir_entry& operator = (const dir_entry& d) 62 { 63 if (this != &d) 64 { 65 name = d.name; 66 dir = d.dir; 67 fail = d.fail; 68 errmsg = d.errmsg; 69 } 70 71 return *this; 72 } 73 ~dir_entry(void)74 ~dir_entry (void) { close (); } 75 76 bool open (const std::string& = ""); 77 78 string_vector read (void); 79 80 bool close (void); 81 ok(void)82 bool ok (void) const { return dir && ! fail; } 83 84 operator bool () const { return ok (); } 85 error(void)86 std::string error (void) const { return ok () ? "" : errmsg; } 87 88 static unsigned int max_name_length (void); 89 90 private: 91 92 // Name of the directory. 93 std::string name; 94 95 // A pointer to the contents of the directory. We use void here to 96 // avoid possible conflicts with the way some systems declare the 97 // type DIR. 98 void *dir; 99 100 // TRUE means the open for this directory failed. 101 bool fail; 102 103 // If a failure occurs, this contains the system error text. 104 std::string errmsg; 105 }; 106 } 107 } 108 109 #endif 110