dir-ops.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1996-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #if !defined (octave_dir_ops_h)
00024 #define octave_dir_ops_h 1
00025 
00026 #include <string>
00027 
00028 #include "str-vec.h"
00029 
00030 class
00031 OCTAVE_API
00032 dir_entry
00033 {
00034 public:
00035 
00036   dir_entry (const std::string& n = std::string ())
00037     : name (n), dir (0), fail (false), errmsg ()
00038     {
00039       if (! name.empty ())
00040         open ();
00041     }
00042 
00043   dir_entry (const dir_entry& d)
00044     : name (d.name), dir (d.dir), fail (d.fail), errmsg (d.errmsg) { }
00045 
00046   dir_entry& operator = (const dir_entry& d)
00047     {
00048       if (this != &d)
00049         {
00050           name = d.name;
00051           dir = d.dir;
00052           fail = d.fail;
00053           errmsg = d.errmsg;
00054         }
00055 
00056       return *this;
00057     }
00058 
00059   ~dir_entry (void) { close (); }
00060 
00061   bool open (const std::string& = std::string ());
00062 
00063   string_vector read (void);
00064 
00065   void close (void);
00066 
00067   bool ok (void) const { return dir && ! fail; }
00068 
00069   operator bool () const { return ok (); }
00070 
00071   std::string error (void) const { return ok () ? std::string () : errmsg; }
00072 
00073 private:
00074 
00075   // Name of the directory.
00076   std::string name;
00077 
00078   // A pointer to the contents of the directory.  We use void here to
00079   // avoid possible conflicts with the way some systems declare the
00080   // type DIR.
00081   void *dir;
00082 
00083   // TRUE means the open for this directory failed.
00084   bool fail;
00085 
00086   // If a failure occurs, this contains the system error text.
00087   std::string errmsg;
00088 };
00089 
00090 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines